cli: inline branch_updates_for_push()

There are only two callers, and one of them knows that the branch exists.
This commit is contained in:
Yuya Nishihara 2023-07-03 17:17:40 +09:00
parent d564c94548
commit 78e8790e10

View file

@ -694,14 +694,17 @@ fn cmd_git_push(
if !seen_branches.insert(branch_name.clone()) {
continue;
}
if let Some(update) = branch_updates_for_push(repo.as_ref(), &remote, branch_name)? {
branch_updates.push((branch_name.clone(), update));
} else {
writeln!(
let branch_target = repo
.view()
.get_branch(branch_name)
.ok_or_else(|| user_error(format!("Branch {branch_name} doesn't exist")))?;
match classify_branch_update(branch_name, branch_target, &remote) {
Ok(Some(update)) => branch_updates.push((branch_name.clone(), update)),
Ok(None) => writeln!(
ui,
"Branch {}@{} already matches {}",
branch_name, &remote, branch_name
)?;
"Branch {branch_name}@{remote} already matches {branch_name}",
)?,
Err(message) => return Err(user_error(message)),
}
}
@ -742,14 +745,14 @@ fn cmd_git_push(
}
tx.mut_repo()
.set_local_branch(branch_name.clone(), RefTarget::Normal(commit.id().clone()));
if let Some(update) = branch_updates_for_push(tx.mut_repo(), &remote, &branch_name)? {
branch_updates.push((branch_name.clone(), update));
} else {
writeln!(
let branch_target = tx.repo().view().get_branch(&branch_name).unwrap();
match classify_branch_update(&branch_name, branch_target, &remote) {
Ok(Some(update)) => branch_updates.push((branch_name.clone(), update)),
Ok(None) => writeln!(
ui,
"Branch {}@{} already matches {}",
branch_name, &remote, branch_name
)?;
"Branch {branch_name}@{remote} already matches {branch_name}",
)?,
Err(message) => return Err(user_error(message)),
}
}
@ -969,17 +972,6 @@ fn get_default_push_remote(
}
}
fn branch_updates_for_push(
repo: &dyn Repo,
remote_name: &str,
branch_name: &str,
) -> Result<Option<BranchPushUpdate>, CommandError> {
let maybe_branch_target = repo.view().get_branch(branch_name);
let branch_target = maybe_branch_target
.ok_or_else(|| user_error(format!("Branch {branch_name} doesn't exist")))?;
classify_branch_update(branch_name, branch_target, remote_name).map_err(user_error)
}
fn classify_branch_update(
branch_name: &str,
branch_target: &BranchTarget,