From 265d957fb375797a91249db8f3bfe6c5d9381902 Mon Sep 17 00:00:00 2001 From: estib Date: Fri, 4 Oct 2024 18:28:27 +0200 Subject: [PATCH] Base branch: Use graph ahead behind --- crates/gitbutler-branch-actions/src/base.rs | 40 +++++++++------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/crates/gitbutler-branch-actions/src/base.rs b/crates/gitbutler-branch-actions/src/base.rs index 5e011163b..c742d6149 100644 --- a/crates/gitbutler-branch-actions/src/base.rs +++ b/crates/gitbutler-branch-actions/src/base.rs @@ -564,33 +564,25 @@ pub(crate) fn target_to_base_branch(ctx: &CommandContext, target: &Target) -> Re let commit = branch.get().peel_to_commit()?; let oid = commit.id(); - // determined if the base branch is behind it's upstream - let fork_point = repo.merge_base(target.sha, oid).context(format!( - "failed to find merge base between {} and {}", - target.sha, oid - ))?; + // determine if the base branch is behind it's upstream + let (number_commits_ahead, number_commits_behind) = repo.graph_ahead_behind(target.sha, oid)?; - let diverged = fork_point != target.sha; + let diverged_ahead = repo + .log(target.sha, LogUntil::Take(number_commits_ahead)) + .context("failed to get fork point")? + .iter() + .map(|commit| commit.id()) + .collect::>(); - let diverged_ahead = if diverged { - repo.log(target.sha, LogUntil::Commit(fork_point)) - .context("failed to get diverged ahead commits")? - .iter() - .map(|commit| commit.id()) - .collect::>() - } else { - Vec::new() - }; + let diverged_behind = repo + .log(oid, LogUntil::Take(number_commits_behind)) + .context("failed to get fork point")? + .iter() + .map(|commit| commit.id()) + .collect::>(); - let diverged_behind = if diverged { - repo.log(oid, LogUntil::Commit(fork_point)) - .context("failed to get diverged behind commits")? - .iter() - .map(|commit| commit.id()) - .collect::>() - } else { - Vec::new() - }; + // if there are commits ahead of the base branch consider it diverged + let diverged = !diverged_ahead.is_empty(); // gather a list of commits between oid and target.sha let upstream_commits = repo