Skip to content

Commit 98577ff

Browse files
committed
fix rebase in certain scenarios
1 parent 089d051 commit 98577ff

2 files changed

Lines changed: 41 additions & 29 deletions

File tree

src/main.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,15 @@ fn inner_main() -> Result<()> {
170170
}) => {
171171
let restack_branch = branch.clone().unwrap_or_else(|| current_branch.clone());
172172
state.try_auto_mount(&repo, &restack_branch)?;
173-
restack(state, &repo, run_version, branch, current_branch, fetch, push)
173+
restack(
174+
state,
175+
&repo,
176+
run_version,
177+
branch,
178+
current_branch,
179+
fetch,
180+
push,
181+
)
174182
}
175183
Some(Command::Mount { parent_branch }) => {
176184
state.mount(&repo, &current_branch, parent_branch)
@@ -466,9 +474,11 @@ fn restack(
466474

467475
match branch.stack_method {
468476
StackMethod::ApplyMerge => {
477+
// Check if we can use the fast format-patch/am approach:
478+
// requires an LKG parent that is still an ancestor of the branch
469479
if let Some(lkg_parent) = branch.lkg_parent.as_deref() {
470-
tracing::info!("LKG parent: {}", lkg_parent);
471480
if is_ancestor(lkg_parent, &source)? {
481+
tracing::info!("LKG parent: {}", lkg_parent);
472482
let patch_rev = format!("{}..{}", &lkg_parent, &branch.name);
473483
tracing::info!("Creating patch {}", &patch_rev);
474484
// The branch is still on top of the LKG parent. Let's create a format-patch of the
@@ -499,32 +509,27 @@ fn restack(
499509
git_push(&branch.name)?;
500510
}
501511
continue;
502-
} else {
503-
tracing::info!(
504-
"Branch '{}' is not on top of the LKG parent. Using `git rebase`...",
505-
branch.name
506-
);
507-
run_git(&["checkout", &branch.name])?;
508-
let rebased = run_git_status(&["rebase", &parent], None)?.success();
509-
510-
if !rebased {
511-
eprintln!(
512-
"{} did not complete automatically.",
513-
"Rebase".blue().bold()
514-
);
515-
eprintln!("Run `git mergetool` to resolve conflicts.");
516-
eprintln!(
517-
"Once you have finished the {}, re-run this script.",
518-
"rebase".blue().bold()
519-
);
520-
std::process::exit(1);
521-
}
522-
if push {
523-
git_push(&branch.name)?;
524-
}
525-
tracing::info!("Rebase completed successfully. Continuing...");
526512
}
527513
}
514+
515+
// Fall back to regular rebase (no LKG parent, or branch diverged from LKG)
516+
tracing::info!("Using `git rebase` for '{}'...", branch.name);
517+
run_git(&["checkout", &branch.name])?;
518+
let rebased = run_git_status(&["rebase", &parent], None)?.success();
519+
520+
if !rebased {
521+
eprintln!("{} did not complete automatically.", "Rebase".blue().bold());
522+
eprintln!("Run `git mergetool` to resolve conflicts.");
523+
eprintln!(
524+
"Once you have finished the {}, re-run this script.",
525+
"rebase".blue().bold()
526+
);
527+
std::process::exit(1);
528+
}
529+
if push {
530+
git_push(&branch.name)?;
531+
}
532+
tracing::info!("Rebase completed successfully. Continuing...");
528533
}
529534
StackMethod::Merge => {
530535
run_git(&["checkout", &branch.name])

src/state.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::{
22
cell::{Cell, Ref, RefCell},
33
collections::{BTreeMap, HashMap, VecDeque},
4-
default, fs,
4+
default,
5+
fs,
56
path::PathBuf,
67
process::Command,
78
rc::Rc,
@@ -13,8 +14,14 @@ use serde::{Deserialize, Serialize};
1314

1415
use crate::{
1516
git::{
16-
DEFAULT_REMOTE, GitTrunk, after_text, git_branch_exists, git_remote_main, git_sha,
17-
git_trunk, is_ancestor,
17+
DEFAULT_REMOTE,
18+
GitTrunk,
19+
after_text,
20+
git_branch_exists,
21+
git_remote_main,
22+
git_sha,
23+
git_trunk,
24+
is_ancestor,
1825
},
1926
run_git,
2027
};

0 commit comments

Comments
 (0)