Skip to content

Commit 5de5ed0

Browse files
authored
Merge pull request #7898 from gitbutlerapp/save_and_return_to_workspace-rebase-engine
save_and_return_to_workspace-rebase-engine
2 parents 558d498 + e34aa02 commit 5de5ed0

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/gitbutler-edit-mode/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ gitbutler-diff.workspace = true
2424
gitbutler-stack.workspace = true
2525
gitbutler-cherry-pick.workspace = true
2626
gitbutler-workspace.workspace = true
27+
but-workspace.workspace = true
28+
but-rebase.workspace = true
2729
serde.workspace = true
2830
tracing.workspace = true
2931

crates/gitbutler-edit-mode/src/lib.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::str::FromStr;
44

55
use anyhow::{bail, Context, Result};
66
use bstr::ByteSlice;
7+
use but_workspace::stack_ext::StackExt;
78
use git2::build::CheckoutBuilder;
89
use gitbutler_branch_actions::internal::list_virtual_branches;
910
use gitbutler_branch_actions::{update_workspace_commit, RemoteBranchFile};
@@ -19,12 +20,13 @@ use gitbutler_operating_modes::{
1920
OperatingMode, EDIT_BRANCH_REF, WORKSPACE_BRANCH_REF,
2021
};
2122
use 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
};
2425
use gitbutler_project::access::{WorktreeReadPermission, WorktreeWritePermission};
2526
use gitbutler_reference::{ReferenceName, Refname};
26-
use gitbutler_repo::{rebase::cherry_rebase, RepositoryExt};
27+
use gitbutler_repo::RepositoryExt;
2728
use gitbutler_repo::{signature, SignaturePurpose};
29+
use gitbutler_stack::stack_context::CommandContextExt;
2830
use gitbutler_stack::{Stack, VirtualBranchesHandle};
2931
use gitbutler_workspace::branch_trees::{update_uncommited_changes_with_tree, WorkspaceState};
3032
#[allow(deprecated)]
@@ -269,7 +271,7 @@ pub(crate) fn save_and_return_to_workspace(
269271
.find_commit(edit_mode_metadata.commit_oid)
270272
.context("Failed to find commit")?;
271273

272-
let Some(mut virtual_branch) =
274+
let Some(mut stack) =
273275
find_virtual_branch_by_reference(ctx, &edit_mode_metadata.branch_reference)?
274276
else {
275277
bail!("Failed to find virtual branch for this reference. Entering and leaving edit mode for non-virtual branches is unsupported")
@@ -301,26 +303,35 @@ 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(
306-
ctx,
307-
new_commit_oid,
308-
commit.id(),
309-
virtual_branch.head(&gix_repo)?,
310-
)
311-
.context("Failed to rebase commits onto new commit")?
312-
.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();
313323

314324
// Update virtual_branch
315325
let (new_branch_head, new_branch_tree) = if ctx.app_settings().feature_flags.v3 {
316326
(new_branch_head, None)
317327
} else {
318328
#[allow(deprecated)]
319-
let res = compute_updated_branch_head(ctx.repo(), &virtual_branch, new_branch_head)?;
329+
let res = compute_updated_branch_head(ctx.repo(), &stack, new_branch_head)?;
320330
(res.head, Some(res.tree))
321331
};
322332

323-
virtual_branch.set_stack_head(&vb_state, &gix_repo, new_branch_head, new_branch_tree)?;
333+
stack.set_stack_head(&vb_state, &gix_repo, new_branch_head, new_branch_tree)?;
334+
stack.set_heads_from_rebase_output(ctx, output.references)?;
324335

325336
// Switch branch to gitbutler/workspace
326337
repository

0 commit comments

Comments
 (0)