@@ -4,6 +4,7 @@ use std::str::FromStr;
44
55use anyhow:: { bail, Context , Result } ;
66use bstr:: ByteSlice ;
7+ use but_workspace:: stack_ext:: StackExt ;
78use git2:: build:: CheckoutBuilder ;
89use gitbutler_branch_actions:: internal:: list_virtual_branches;
910use gitbutler_branch_actions:: { update_workspace_commit, RemoteBranchFile } ;
@@ -19,12 +20,13 @@ use gitbutler_operating_modes::{
1920 OperatingMode , EDIT_BRANCH_REF , WORKSPACE_BRANCH_REF ,
2021} ;
2122use gitbutler_oxidize:: {
22- git2_to_gix_object_id, gix_to_git2_index, GixRepositoryExt , OidExt , RepoExt ,
23+ git2_to_gix_object_id, gix_to_git2_index, GixRepositoryExt , ObjectIdExt , OidExt , RepoExt ,
2324} ;
2425use gitbutler_project:: access:: { WorktreeReadPermission , WorktreeWritePermission } ;
2526use gitbutler_reference:: { ReferenceName , Refname } ;
26- use gitbutler_repo:: { rebase :: cherry_rebase , RepositoryExt } ;
27+ use gitbutler_repo:: RepositoryExt ;
2728use gitbutler_repo:: { signature, SignaturePurpose } ;
29+ use gitbutler_stack:: stack_context:: CommandContextExt ;
2830use gitbutler_stack:: { Stack , VirtualBranchesHandle } ;
2931use gitbutler_workspace:: branch_trees:: { update_uncommited_changes_with_tree, WorkspaceState } ;
3032#[ allow( deprecated) ]
@@ -301,10 +303,23 @@ pub(crate) fn save_and_return_to_workspace(
301303 . context ( "Failed to commit new commit" ) ?;
302304
303305 let gix_repo = repository. to_gix ( ) ?;
304- // Rebase all all commits on top of the new commit and update reference
305- let new_branch_head = cherry_rebase ( ctx, new_commit_oid, commit. id ( ) , stack. head ( & gix_repo) ?)
306- . context ( "Failed to rebase commits onto new commit" ) ?
307- . unwrap_or ( new_commit_oid) ;
306+
307+ let mut steps = stack. as_rebase_steps ( ctx, & gix_repo) ?;
308+ // swap out the old commit with the new, updated one
309+ steps. iter_mut ( ) . for_each ( |step| {
310+ if let but_rebase:: RebaseStep :: Pick { commit_id, .. } = step {
311+ if commit. id ( ) == commit_id. to_git2 ( ) {
312+ * commit_id = new_commit_oid. to_gix ( ) ;
313+ }
314+ }
315+ } ) ;
316+ let stack_ctx = ctx. to_stack_context ( ) ?;
317+ let merge_base = stack. merge_base ( & stack_ctx) ?;
318+ let mut rebase = but_rebase:: Rebase :: new ( & gix_repo, Some ( merge_base. to_gix ( ) ) , None ) ?;
319+ rebase. rebase_noops ( false ) ;
320+ rebase. steps ( steps) ?;
321+ let output = rebase. rebase ( ) ?;
322+ let new_branch_head = output. top_commit . to_git2 ( ) ;
308323
309324 // Update virtual_branch
310325 let ( new_branch_head, new_branch_tree) = if ctx. app_settings ( ) . feature_flags . v3 {
@@ -316,6 +331,7 @@ pub(crate) fn save_and_return_to_workspace(
316331 } ;
317332
318333 stack. set_stack_head ( & vb_state, & gix_repo, new_branch_head, new_branch_tree) ?;
334+ stack. set_heads_from_rebase_output ( ctx, output. references ) ?;
319335
320336 // Switch branch to gitbutler/workspace
321337 repository
0 commit comments