Skip to content

Commit 4d5c6a7

Browse files
committed
Fix an issue where in some cases 'wrong' commits are shown in listed branches
This was because the merge base computed was wrong. Instead of relying on the default target.sha, which may have become stale, read the git reference of the default target
1 parent d03c9c1 commit 4d5c6a7

File tree

2 files changed

+27
-3
lines changed
  • crates

2 files changed

+27
-3
lines changed

crates/gitbutler-branch-actions/tests/extra/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ use gitbutler_branch_actions::{
1919
BranchManagerExt, Get,
2020
};
2121
use gitbutler_commit::{commit_ext::CommitExt, commit_headers::CommitHeadersV2};
22+
use gitbutler_oxidize::OidExt;
2223
use gitbutler_reference::{Refname, RemoteRefname};
2324
use gitbutler_repo::RepositoryExt;
2425
use gitbutler_stack::{BranchOwnershipClaims, Target, VirtualBranchesHandle};
2526
use gitbutler_testsupport::{commit_all, virtual_branches::set_test_target, Case, Suite};
27+
use gix::refs::transaction::PreviousValue;
2628
use pretty_assertions::assert_eq;
2729

2830
#[test]
@@ -829,6 +831,12 @@ fn merge_vbranch_upstream_clean_rebase() -> Result<()> {
829831
sha: target_oid,
830832
push_remote_name: None,
831833
})?;
834+
ctx.gix_repository()?.reference(
835+
"refs/remotes/origin/master".to_string(),
836+
target_oid.to_gix(),
837+
PreviousValue::Any,
838+
"",
839+
)?;
832840

833841
// add some uncommitted work
834842
let file_path2 = Path::new("test2.txt");
@@ -959,6 +967,12 @@ fn merge_vbranch_upstream_conflict() -> Result<()> {
959967
sha: target_oid,
960968
push_remote_name: None,
961969
})?;
970+
ctx.gix_repository()?.reference(
971+
"refs/remotes/origin/master".to_string(),
972+
target_oid.to_gix(),
973+
PreviousValue::Any,
974+
"",
975+
)?;
962976

963977
let remote_branch: RemoteRefname = "refs/remotes/origin/g-branch-1".parse().unwrap();
964978
let branch_manager = ctx.branch_manager();

crates/gitbutler-stack/src/stack.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,19 @@ impl Stack {
310310
/// - If a target is not set for the project
311311
/// - If the head commit of the stack is not found
312312
pub fn merge_base(&self, stack_context: &StackContext) -> Result<git2::Oid> {
313-
let target = stack_context.target();
314-
let repository = stack_context.repository();
315-
let merge_base = repository.merge_base(self.head(&repository.to_gix()?)?, target.sha)?;
313+
let gix_repo = stack_context.repository().to_gix()?;
314+
let git2_repository = stack_context.repository();
315+
let target_sha = gix_repo
316+
.find_reference(&stack_context.target().branch.to_string())?
317+
.peel_to_commit()?
318+
.id;
319+
let mut revwalk = git2_repository.revwalk()?;
320+
revwalk.hide(self.head(&gix_repo)?)?;
321+
revwalk.push(target_sha.to_git2())?;
322+
revwalk.simplify_first_parent()?;
323+
let last_commit = revwalk.last().ok_or_else(|| anyhow!("fucked"))??;
324+
let last_commit = git2_repository.find_commit(last_commit)?;
325+
let merge_base = last_commit.parent_id(0)?;
316326
Ok(merge_base)
317327
}
318328

0 commit comments

Comments
 (0)