Skip to content

Prevent repo sync from corrupting worktrees - #13075

Closed
txhno wants to merge 1 commit into
cli:trunkfrom
txhno:fix/repo-sync-worktree-12927
Closed

txhno wants to merge 1 commit into
cli:trunkfrom
txhno:fix/repo-sync-worktree-12927

Conversation

@txhno

@txhno txhno commented Apr 1, 2026 •

Copy link
Copy Markdown

Summary

This changes gh repo sync so it does not update a branch ref behind another worktree's back.

Before this patch, syncing a local repository on a branch that existed locally but was not the current branch in the active worktree would call git update-ref directly. That is safe for an un-checked-out branch, but it is not safe when the branch is checked out in a different worktree: the ref moves forward while that other worktree's index and working tree remain stale.

This patch switches that non-current existing-branch path to git branch -f, which lets Git enforce the worktree safety check directly:

  • if the target branch is current in the active worktree, keep the existing fast-forward/reset behavior
  • if the target branch exists locally but is checked out in another worktree, Git rejects the forced branch update
  • if the target branch exists locally and is not checked out in any worktree, keep the existing branch update path
  • if the target branch does not exist locally, keep the existing branch-creation path

Files changed

  • pkg/cmd/repo/sync/git.go: use git branch -f instead of git update-ref for the non-current existing-branch path
  • pkg/cmd/repo/sync/sync_test.go: add regression coverage for the checked-out-in-another-worktree case

Fixes #12927.

Testing

  • git diff --check
  • go test ./pkg/cmd/repo/sync (could not run here: go is not installed in this environment)
  • gofmt -w pkg/cmd/repo/sync/git.go pkg/cmd/repo/sync/sync_test.go (could not run here: gofmt is not installed in this environment)

@txhno
txhno requested a review from a team as a code owner April 1, 2026 06:14
@txhno
txhno requested a review from BagToad April 1, 2026 06:14
@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 1, 2026
@github-actions

github-actions Bot commented Apr 1, 2026

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 github-actions Bot closed this Apr 9, 2026
@txhno

txhno commented Apr 9, 2026

Copy link
Copy Markdown
Author

Updated this to use \ for the non-current existing-branch path, so Git enforces the worktree constraint directly instead of us parsing worktrees ourselves. I couldn't rerun \ or \ here because Go isn't installed in this environment. The remaining blocker I can't clear from this side is the repo policy gate: #12927 still needs the \ label.

@txhno

txhno commented Apr 9, 2026

Copy link
Copy Markdown
Author

Updated this to use git branch -f for the non-current existing-branch path, so Git enforces the worktree constraint directly instead of us parsing worktrees ourselves. I couldn't rerun gofmt or go test ./pkg/cmd/repo/sync here because Go isn't installed in this environment. The remaining blocker I can't clear from this side is the repo policy gate: #12927 still needs the help wanted label.

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 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