Skip to content

Commit 5cc4fa2

Browse files
committed
refactor: use --force-with-lease instead of -f for safer pushes
1 parent d230813 commit 5cc4fa2

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

src/main.rs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -366,25 +366,25 @@ fn inner_main() -> Result<()> {
366366
&& let Some(branch) = state.get_tree_branch(&repo, &current_branch)
367367
&& let Some(pr_number) = branch.pr_number
368368
&& let Ok(repo_id) = github::get_repo_identifier(&git_repo)
369-
&& let Ok(client) = github::GitHubClient::from_env(&repo_id)
370-
{
371-
match client.update_pr(
372-
&repo_id,
373-
pr_number,
374-
github::UpdatePrRequest {
375-
base: Some(&parent),
376-
title: None,
377-
body: None,
378-
},
379-
) {
380-
Ok(_) => {
381-
println!("Retargeted PR #{} base to '{}'.", pr_number, parent);
382-
}
383-
Err(e) => {
384-
tracing::warn!("Failed to retarget PR #{pr_number}: {e}");
385-
}
369+
&& let Ok(client) = github::GitHubClient::from_env(&repo_id)
370+
{
371+
match client.update_pr(
372+
&repo_id,
373+
pr_number,
374+
github::UpdatePrRequest {
375+
base: Some(&parent),
376+
title: None,
377+
body: None,
378+
},
379+
) {
380+
Ok(_) => {
381+
println!("Retargeted PR #{} base to '{}'.", pr_number, parent);
382+
}
383+
Err(e) => {
384+
tracing::warn!("Failed to retarget PR #{pr_number}: {e}");
386385
}
387386
}
387+
}
388388
Ok(())
389389
}
390390
Some(Command::Status { fetch }) => {
@@ -981,21 +981,18 @@ fn restack(
981981
if push
982982
&& !git_repo.shas_match(&format!("{DEFAULT_REMOTE}/{}", branch.name), &branch.name)
983983
{
984-
run_git(&[
985-
"push",
986-
match branch.stack_method {
987-
StackMethod::ApplyMerge => {
988-
tracing::debug!(
989-
"Force-pushing '{}' to {DEFAULT_REMOTE}...",
990-
branch.name
991-
);
992-
"-fu"
993-
}
994-
StackMethod::Merge => "-u",
995-
},
996-
DEFAULT_REMOTE,
997-
&format!("{branch_name}:{branch_name}", branch_name = branch.name),
998-
])?;
984+
let refspec = format!("{branch_name}:{branch_name}", branch_name = branch.name);
985+
let mut args = vec!["push", "-u"];
986+
if matches!(branch.stack_method, StackMethod::ApplyMerge) {
987+
tracing::debug!(
988+
"Force-pushing (with lease) '{}' to {DEFAULT_REMOTE}...",
989+
branch.name
990+
);
991+
args.push("--force-with-lease");
992+
}
993+
args.push(DEFAULT_REMOTE);
994+
args.push(&refspec);
995+
run_git(&args)?;
999996
pushed_branches.push(branch.name.clone());
1000997
status = "no changes, pushed".to_string();
1001998
}
@@ -1320,7 +1317,8 @@ fn git_push(git_repo: &GitRepo, branch: &str) -> Result<()> {
13201317
if !git_repo.shas_match(&format!("{DEFAULT_REMOTE}/{}", branch), branch) {
13211318
run_git(&[
13221319
"push",
1323-
"-fu",
1320+
"-u",
1321+
"--force-with-lease",
13241322
DEFAULT_REMOTE,
13251323
&format!("{}:{}", branch, branch),
13261324
])?;

src/sync.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,8 @@ fn apply_remote_change(
14361436
println!(" Pushing '{}' to remote", branch.yellow());
14371437
run_git(&[
14381438
"push",
1439-
"-fu",
1439+
"-u",
1440+
"--force-with-lease",
14401441
DEFAULT_REMOTE,
14411442
&format!("{}:{}", branch, branch),
14421443
])?;

0 commit comments

Comments
 (0)