Skip to content

fix(repo/sync): refuse to advance ref when branch is checked out in another worktree - #13200

Closed
SAY-5 wants to merge 4 commits into
cli:trunkfrom
SAY-5:fix-repo-sync-worktree-corruption
Closed

SAY-5 wants to merge 4 commits into
cli:trunkfrom
SAY-5:fix-repo-sync-worktree-corruption

Conversation

@SAY-5

@SAY-5 SAY-5 commented Apr 17, 2026

Copy link
Copy Markdown

Fixes #12927.

Problem

gh repo sync currently uses git update-ref to fast-forward a branch that isn't the currently checked-out branch (in the current worktree). That works when no other worktree has the branch checked out, but when a worktree does, update-ref silently advances the ref without updating that worktree's index or working tree. The other worktree is left in an inconsistent state — git status shows every file changed between the old and new commit as "staged changes".

Reproducer from #12927:

git worktree add ./wt -b feature
cd ./wt
gh repo sync             # silently corrupts main repo's worktree
cd ..
git status               # shows bogus "staged" changes

Fix

Before UpdateBranch (which runs git update-ref), check whether the target branch is checked out in any worktree other than the current one. If it is, refuse to sync and tell the user what to do instead.

Implementation:

  • gitClient gains IsCheckedOutInOtherWorktree(branch) (bool, error).
  • gitExecuter implements it via git worktree list --porcelain, scanning for a branch refs/heads/<name> record whose worktree path is different from the current worktree (determined via git rev-parse --show-toplevel).
  • executeLocalRepoSync calls this before UpdateBranch and returns a clear error when the branch is in another worktree.

Only the update-ref path is guarded. The current-worktree path still uses git merge --ff-only / git reset --hard, which correctly update the index and working tree. The CreateBranch path is unaffected (new branches can't be checked out anywhere yet).

Error message

refusing to sync "trunk" because it is checked out in another worktree
tip: run sync from that worktree (or check it out here first) so the working tree is updated alongside the ref

Tests

  • Updated the existing "sync local repo with parent" test to stub IsCheckedOutInOtherWorktree → false so the happy path still exercises UpdateBranch.
  • Added a new test "sync local repo with parent - refuse when branch checked out in another worktree" that stubs IsCheckedOutInOtherWorktree → true and asserts the new error message.
  • Added the mock to mocks.go.

No new integration tests — the git worktree list --porcelain parsing is simple enough to unit-test via the mock, and the existing integration test matrix exercises the worktree-less happy paths.

Behavior change

Users who previously triggered this silent corruption now see an error instead. That's the point — they were already getting a broken worktree; now they get a clear message telling them how to proceed.

SAY-5 added 4 commits April 16, 2026 22:43
Adds IsCheckedOutInOtherWorktree helper to gitClient interface and
gitExecuter. Uses git worktree list --porcelain to parse worktrees
and check whether the target branch is checked out in any worktree
other than the current one.

Refs #12927.

Signed-off-by: SAY-5 <[email protected]>
Fixes #12927.

Previously, gh repo sync used git update-ref to fast-forward a branch
that wasnt the currently checked-out branch. This works when no other
worktree has the branch, but when a worktree does, update-ref silently
advances the ref without updating that worktrees index or working tree.
The other worktree is left with git thinking files are staged that
havent actually changed.

With this change, gh repo sync now uses IsCheckedOutInOtherWorktree
and refuses to advance the ref with a helpful error telling the user
to run sync from the other worktree (or check the branch out in the
current worktree first) so git also updates the index/working tree.

Signed-off-by: SAY-5 <[email protected]>
Add test case verifying that executeLocalRepoSync refuses to update
the ref when the branch is checked out in another worktree. Also
updates the existing non-current-branch UpdateBranch test to stub
IsCheckedOutInOtherWorktree returning false so the happy path
continues to work.

Signed-off-by: SAY-5 <[email protected]>
@SAY-5
SAY-5 requested a review from a team as a code owner April 17, 2026 05:44
@SAY-5
SAY-5 requested a review from williammartin April 17, 2026 05:44
@github-actions github-actions Bot added external pull request originating outside of the CLI core team needs-triage needs to be reviewed unmet-requirements and removed needs-triage needs to be reviewed labels Apr 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your pull request! Unfortunately, it doesn't meet the minimum requirements for review:

  • None of the referenced issues have the help wanted label

Please update your PR to address the above. Requirements:

  1. Include a detailed description of what this PR does
  2. Link to an issue with the help wanted label (use Fixes #123 or Closes #123 if it resolves the issue)

This PR will be automatically closed in 7 days if these requirements are not met.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution! Unfortunately, this pull request is not linked to a help wanted issue, so we are unable to accept it at this time.

We encourage community contributions on issues labeled help wanted. If you'd like to contribute, please find an open help wanted issue and link your PR to it.

If you believe this was done in error, please let us know.

@github-actions github-actions Bot closed this Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team no-help-wanted-issue unmet-requirements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gh repo sync silently corrupts worktree when synced branch is checked out in a different worktree

2 participants