Skip to content

feat: optimize detached HEAD merge base for jj users#470

Open
121watts wants to merge 1 commit intomainfrom
watts/dep-3974-detached-head-merge-base
Open

feat: optimize detached HEAD merge base for jj users#470
121watts wants to merge 1 commit intomainfrom
watts/dep-3974-detached-head-merge-base

Conversation

@121watts
Copy link
Copy Markdown
Contributor

@121watts 121watts commented Mar 26, 2026

Summary

For detached-HEAD workflows (standard jj/Jujutsu pattern), findMergeBase now finds the closest pushed ancestor commit instead of falling all the way back to origin/main. This produces smaller, more precise patches when running depot ci run with local changes.

What was happening

When HEAD is detached, findMergeBase skipped the remote tracking branch check entirely (since there's no named branch) and fell back to git merge-base HEAD origin/main. For jj users who always work in detached HEAD but have pushed branches, this meant the patch included the entire branch diff from main -- potentially thousands of lines -- instead of just the unpushed local changes.

What happens now

When HEAD is detached, findClosestRemoteAncestor enumerates all refs under refs/remotes/origin/, checks which ones are ancestors of HEAD, and picks the one with the shortest distance (fewest commits between it and HEAD). This finds the most recent pushed commit that's an ancestor of the current working state.

Falls back to the existing default-branch merge base behavior if no remote tracking ref is an ancestor of HEAD.

Anything else?

This is a pure optimization -- the worst case is identical to current behavior (falls back to origin/main). Edge cases with jj's ref model (bookmarks vs working copy) should be safe since we're only reading standard git refs. Tested with a simulated jj-style workflow: push branch, add local commits, detach HEAD.

Closes DEP-3974

Made with Cursor


Note

Medium Risk
Medium risk: changes how depot ci run computes the merge base for patch generation in detached-HEAD states, which could alter patch contents if the wrong remote ancestor is selected; fallback behavior remains unchanged when no ancestor is found.

Overview
Improves patch generation for detached-HEAD workflows by teaching findMergeBase to prefer the closest pushed origin/* ancestor commit instead of defaulting straight to the default branch merge base.

Adds findClosestRemoteAncestor, which scans refs/remotes/origin/*, filters to ancestors of HEAD, and selects the one with the fewest commits to HEAD, producing smaller patches for jj-style workflows. Includes a new unit test covering the detached-HEAD-with-pushed-ancestor scenario.

Written by Cursor Bugbot for commit 28b2da7. This will update automatically on new commits. Configure here.

When HEAD is detached (standard jj workflow), findMergeBase now walks
remote tracking refs under refs/remotes/origin/ to find the closest
ancestor of HEAD. This produces a minimal patch containing only truly
unpushed local changes, instead of falling back to origin/main and
including the entire branch diff.

The algorithm: enumerate all remote tracking refs, check which are
ancestors of HEAD, and pick the one with the fewest commits between it
and HEAD. Falls back to default branch merge base if no remote ancestor
is found.

Closes DEP-3974

Made-with: Cursor
@linear
Copy link
Copy Markdown

linear bot commented Mar 26, 2026

@121watts 121watts requested a review from lukevmorris March 26, 2026 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant