@@ -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 ] )
0 commit comments