feat: optimize detached HEAD merge base for jj users#470
Open
feat: optimize detached HEAD merge base for jj users#470
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
For detached-HEAD workflows (standard jj/Jujutsu pattern),
findMergeBasenow finds the closest pushed ancestor commit instead of falling all the way back toorigin/main. This produces smaller, more precise patches when runningdepot ci runwith local changes.What was happening
When HEAD is detached,
findMergeBaseskipped the remote tracking branch check entirely (since there's no named branch) and fell back togit 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,
findClosestRemoteAncestorenumerates all refs underrefs/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 runcomputes 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
findMergeBaseto prefer the closest pushedorigin/*ancestor commit instead of defaulting straight to the default branch merge base.Adds
findClosestRemoteAncestor, which scansrefs/remotes/origin/*, filters to ancestors ofHEAD, and selects the one with the fewest commits toHEAD, 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.