Skip to content

Compute behind count from octopus merge-base and update upstream display#13689

Merged
mtsgrd merged 1 commit intomasterfrom
behind-count
May 7, 2026
Merged

Compute behind count from octopus merge-base and update upstream display#13689
mtsgrd merged 1 commit intomasterfrom
behind-count

Conversation

@mtsgrd
Copy link
Copy Markdown
Contributor

@mtsgrd mtsgrd commented May 7, 2026

Summary

Behind count now reflects the farthest-behind stack

behind and upstream_commits on BaseBranch are now computed using the
octopus merge-base across all workspace stack tips and the target tip, instead
of counting from the stored target.sha. This means the behind count shows
the worst case across all stacks in the workspace.

Updated CLI display

Before:

┊● 801d97d (upstream) ⏫ 10 new commits
├╯ 801d97d [origin/main] 2000-01-02 add upstream-10

After:

┊● 801d97d (merge base) ⏫ 10 commits
├╯ 0dc3733 (common base) 2000-01-02 add M
  • Top line: target tip labeled (merge base) with commit count between the two.
  • Bottom line: octopus merge-base labeled (common base) — the deepest shared
    ancestor across all stacks. Previously showed [origin/main], which was
    misleading since this commit isn't where the target ref points.
  • With --upstream, the individual commits are listed between the two lines.

Desktop app button

  • Shows N behind with a single stack, ≤N behind with multiple stacks.

Robustness improvements (from copilot review)

  • Use try_find_reference for the workspace ref with fallback to target.sha.
  • Avoid extra allocation and object lookup in merge_base_octopus call.
  • Simplify lowest_merge_base from Option<ObjectId> to plain ObjectId.

Test plan

  • cargo test --workspace — all tests pass
  • New test: behind_reflects_farthest_behind_stack asserts the behind count
    matches the farthest-behind stack (3 behind, not 1)
  • Manual: verify but status and but status --upstream display
  • Manual: verify desktop button shows N behind / ≤N behind

🤖 Generated with Claude Code

@github-actions github-actions Bot added the rust Pull requests that update Rust code label May 7, 2026
@mtsgrd mtsgrd requested a review from Caleb-T-Owens May 7, 2026 10:04
@mtsgrd mtsgrd marked this pull request as ready for review May 7, 2026 10:05
Copilot AI review requested due to automatic review settings May 7, 2026 10:05
Copy link
Copy Markdown
Contributor

@Caleb-T-Owens Caleb-T-Owens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm.

Kind of confusing that the target ref commit is called target_commit_id given that terms is now synonyms with the target sha, but it's fine I guess

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts how BaseBranch.behind / upstream_commits are computed so the UI “sync” state reflects the farthest-behind applied stack, using an octopus merge-base across workspace stack tips and the target tip.

Changes:

  • Updates target_to_base_branch() to compute an octopus merge-base across workspace parents + target tip, then counts first-parent commits from target tip to that base.
  • Updates existing set_base_branch tests to match the new behind semantics.
  • Adds a new fixture + regression test covering multiple stacks with different fork points.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
crates/gitbutler-branch-actions/src/base.rs Recomputes upstream_commits/behind using octopus merge-base of workspace stack tips and target tip.
crates/gitbutler-branch-actions/tests/branch-actions/virtual_branches/set_base_branch.rs Updates assertions for new behind behavior; adds regression test for “farthest-behind stack”.
crates/gitbutler-branch-actions/tests/fixtures/scenario/three-stacks-different-bases.sh New git fixture creating two stacks with different bases to validate behind-count logic.

Comment thread crates/gitbutler-branch-actions/src/base.rs Outdated
Comment thread crates/gitbutler-branch-actions/src/base.rs Outdated
@github-actions github-actions Bot added the CLI The command-line program `but` label May 7, 2026
Copilot AI review requested due to automatic review settings May 7, 2026 10:52
@mtsgrd mtsgrd enabled auto-merge May 7, 2026 10:52
@mtsgrd
Copy link
Copy Markdown
Contributor Author

mtsgrd commented May 7, 2026

Kind of confusing that the target ref commit is called target_commit_id given that terms is now synonyms with the target sha, but it's fine I guess

@Caleb-T-Owens I think you'll find about 150 mentions of this term in the code base, but I agree, I'd prefer target_sha.. maybe we could bulk replace tem?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 6 changed files in this pull request and generated 2 comments.

Comment on lines +4 to +6
# Two stacks that fork from different points on master, with origin/master
# advanced past both. This creates a scenario where each stack has
# a different number of "behind" commits relative to the target.
.map(|id| id.detach())
.collect();
if heads.is_empty() {
None
@mtsgrd mtsgrd disabled auto-merge May 7, 2026 11:00
Change how `upstream_commits` and `behind` are computed on `BaseBranch`:
instead of counting commits between the target tip and the stored
`target.sha`, compute the octopus merge-base across all workspace
stack tips (from the workspace ref) and the target tip, then walk
first-parent commits from the target tip down to that merge-base.

This makes the behind count reflect the farthest-behind stack in the
workspace, which is what the sync button needs to show.
Copilot AI review requested due to automatic review settings May 7, 2026 11:10
@mtsgrd mtsgrd enabled auto-merge May 7, 2026 11:10
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 6 changed files in this pull request and generated 3 comments.

Comment on lines +381 to +386
let heads: Vec<_> = repo
.find_reference(WORKSPACE_REF_NAME)?
.peel_to_commit()?
.parent_ids()
.map(|id| id.detach())
.collect();
Comment on lines +391 to +395
let base = repo
.merge_base_octopus([heads.as_slice(), &[target_commit_id]].concat())
.context("failed to compute octopus merge-base for workspace stacks")?;
Some(base.object()?.id().detach())
}
Comment on lines +389 to +403
Some(target.sha)
} else {
let base = repo
.merge_base_octopus([heads.as_slice(), &[target_commit_id]].concat())
.context("failed to compute octopus merge-base for workspace stacks")?;
Some(base.object()?.id().detach())
}
};

// Commits on the target branch between its tip and the lowest merge base.
let upstream_commit_ids = lowest_merge_base
.map(|lb| first_parent_commit_ids_until(repo, target_commit_id, lb))
.transpose()
.context("failed to get upstream commits")?
.unwrap_or_default();
@mtsgrd mtsgrd merged commit 07dbfbd into master May 7, 2026
40 checks passed
@mtsgrd mtsgrd deleted the behind-count branch May 7, 2026 11:18
@mtsgrd mtsgrd changed the title Count 'behind' using octopus merge-base of workspace stack tips Count 'behind' using octopus merge-base and update upstream display May 7, 2026
@mtsgrd mtsgrd changed the title Count 'behind' using octopus merge-base and update upstream display Update upstream display: "N behind" with common base label May 7, 2026
@mtsgrd mtsgrd changed the title Update upstream display: "N behind" with common base label Compute behind count from octopus merge-base and update upstream display May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLI The command-line program `but` rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants