Skip to content

Use target branch tip as base for new independent branches#13626

Merged
mtsgrd merged 1 commit into
masterfrom
newest-base-for-new-branches
May 7, 2026
Merged

Use target branch tip as base for new independent branches#13626
mtsgrd merged 1 commit into
masterfrom
newest-base-for-new-branches

Conversation

@mtsgrd
Copy link
Copy Markdown
Contributor

@mtsgrd mtsgrd commented May 5, 2026

Problem

When creating a new independent branch, we used the workspace's lower bound — the lowest common ancestor of all stacks. If stacks have different merge bases with the target, this places the new branch on an unnecessarily old commit:

                    target tip
                      ↓
M1 ← M2 ← M3 ← M4   (origin/main)
      ↑    ↑
  Stack A  Stack B

lower_bound = M2   ← new branch landed here (old!)

Solution

Use the stored target commit (target.sha) as the base for new independent branches. If unavailable, fall back to the target ref tip, then to the workspace lower bound (for local-only workflows without a target).

new branch base = M4   ← now uses target commit ✓

Key changes

  • but-graph/.../workspace/api.rs
    • resolved_target_commit_id() — returns the stored target_commit ID, falling back to the target_ref segment tip
    • target_segment_index() — extracted shared helper to deduplicate target resolution with merge_base_with_target_branch()
  • but-workspace/.../create_reference.rs — use resolved_target_commit_id().or(lower_bound) instead of just lower_bound

Test plan

Five tests in resolved_target_commit_id.rs:

  • Two stacks with different bases — returns the target tip
  • One stack above the target — still returns the target tip
  • target_commit preferred over target_ref when both are set
  • No target configured — returns None
  • Only extra_target set — returns None (not sufficient)

@github-actions github-actions Bot added the rust Pull requests that update Rust code label May 5, 2026
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch 3 times, most recently from dcef811 to 31a10a1 Compare May 5, 2026 00:34
@mtsgrd mtsgrd marked this pull request as ready for review May 5, 2026 00:53
@mtsgrd mtsgrd requested a review from krlvi as a code owner May 5, 2026 00:53
Copilot AI review requested due to automatic review settings May 5, 2026 00:53
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

This PR adjusts how “independent” branches are based in a workspace by preferring the newest (closest-to-target) merge base among all stacks, instead of always using the workspace lower bound. This better aligns new independent branches with the most recent common history when stacks diverge at different points from the target.

Changes:

  • Added Workspace::newest_base_among_stacks() to compute a newer merge base across stacks vs. the target.
  • Updated independent-branch creation to prefer newest_base_among_stacks() and fall back to lower_bound.
  • Added workspace tests covering expected “newest base” selection and target configuration edge cases.

Reviewed changes

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

File Description
crates/but-workspace/src/branch/create_reference.rs Uses the new workspace API to choose a more recent base for independent branches.
crates/but-graph/src/projection/workspace/api.rs Introduces newest_base_among_stacks() to compute a newer merge base among stacks and the target.
crates/but-graph/tests/graph/workspace/newest_base_among_stacks.rs Adds tests validating newest-base selection and behavior without a configured target.
crates/but-graph/tests/graph/workspace/mod.rs Registers the new test module.

Comment thread crates/but-graph/src/projection/workspace/api.rs Outdated
Comment thread crates/but-graph/tests/graph/workspace/newest_base_among_stacks.rs Outdated
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch from 31a10a1 to 0590302 Compare May 5, 2026 17:52
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.

@mtsgrd mtsgrd changed the title Use newest stack merge base when creating independent branches Use target branch tip as base for new independent branches May 5, 2026
Copilot AI review requested due to automatic review settings May 5, 2026 21:06
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch from 0590302 to 6e87b22 Compare May 5, 2026 21:06
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 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread crates/but-workspace/src/branch/create_reference.rs Outdated
Comment thread crates/but-graph/src/projection/workspace/api.rs Outdated
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch from 6e87b22 to b6dd81e Compare May 6, 2026 06:41
Copilot AI review requested due to automatic review settings May 6, 2026 07:08
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch from b6dd81e to cc3bb1f Compare May 6, 2026 07:08
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 5 out of 5 changed files in this pull request and generated 1 comment.

Comment thread crates/but-workspace/src/branch/create_reference.rs Outdated
Comment thread crates/but-graph/src/projection/workspace/api.rs
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch 2 times, most recently from 6cac6cd to 4718902 Compare May 6, 2026 12:21
Copilot AI review requested due to automatic review settings May 6, 2026 13:04
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch from 4718902 to 06366aa Compare May 6, 2026 13:04
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 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread crates/but-graph/src/projection/workspace/api.rs
Comment thread crates/but-graph/src/projection/workspace/api.rs
Comment thread crates/but-graph/tests/graph/workspace/resolved_target_commit_id.rs
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch 2 times, most recently from 620682c to b9be514 Compare May 6, 2026 13:54
@Byron
Copy link
Copy Markdown
Collaborator

Byron commented May 7, 2026

@mtsgrd This PR reads like it's trying to optimize the location of newly created refs, such that more recent locations are preferred. While this makes sense, I don't know where this is coming from:

Creating an independent branch without any target is now an error.

In theory, that independent branch can still be created on the lower bound of the workspace. Needing a target, always, goes against supporting all states that Git can legitimately be in, like local-only workflows.

Maybe it's just the description that is somewhat stale, I didn't look at the implementation in detail.

Copilot AI review requested due to automatic review settings May 7, 2026 09:42
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch from b9be514 to 12d229a Compare May 7, 2026 09:42
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 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread crates/but-graph/src/projection/workspace/api.rs Outdated
Comment thread crates/but-graph/tests/graph/workspace/resolved_target_commit_id.rs
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch from 12d229a to c3789dd Compare May 7, 2026 09:47
Previously, new independent branches were based on the workspace's
lower bound (the lowest common ancestor of all stacks). When stacks
have different merge bases with the target, this placed the new
branch on an unnecessarily old commit.

Now we prefer the stored target commit (target.sha) via a new
`Workspace::target_tip_commit_id()` helper, which checks
`target_commit` first, then `target_ref`. Falls back to the
workspace lower bound for local-only workflows without a target.

Also extracts `target_segment_index()` to deduplicate the target
resolution logic shared with `merge_base_with_target_branch()`.

Tested with four cases in `target_tip_commit_id.rs`:
- Two stacks with different bases — returns the target tip
- One stack above the target — still returns the target tip
- No target configured — returns None
- Only extra_target set — returns None (not sufficient)
Copilot AI review requested due to automatic review settings May 7, 2026 10:11
@mtsgrd mtsgrd force-pushed the newest-base-for-new-branches branch from c3789dd to 4eabdc1 Compare May 7, 2026 10:11
@mtsgrd
Copy link
Copy Markdown
Contributor Author

mtsgrd commented May 7, 2026

@Byron could you have another look? I'd like to get this merged asap so we can build a nightly. 🙏

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 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread crates/but-graph/src/projection/workspace/api.rs
Comment thread crates/but-workspace/src/branch/create_reference.rs
@mtsgrd mtsgrd merged commit b8de772 into master May 7, 2026
40 checks passed
@mtsgrd mtsgrd deleted the newest-base-for-new-branches branch May 7, 2026 10:44
@Byron
Copy link
Copy Markdown
Collaborator

Byron commented May 8, 2026

@Byron could you have another look? I'd like to get this merged asap so we can build a nightly. 🙏

I think we are past that, I am many PRs behind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants