Skip to content

Reject non-empty worktree targets before branch creation - #14244

Merged
babakks merged 3 commits into
trunkfrom
tidy-dev-worktree-checkout-feedback
Aug 26, 2026
Merged

Reject non-empty worktree targets before branch creation#14244
babakks merged 3 commits into
trunkfrom
tidy-dev-worktree-checkout-feedback

Conversation

@tidy-dev

Copy link
Copy Markdown
Contributor

Follow-up to #14136 (comment)

Description

gh issue develop --checkout --worktree created its linked branch before Git rejected an existing non-empty target directory. This could leave an orphaned linked branch without a local worktree.

Reject existing non-empty directories while resolving a fresh worktree target, before gh issue develop creates or reuses any linked branch. Existing linked worktrees remain reusable.

How did you test this change?

Given an existing directory containing a file
When I target it with gh issue develop --checkout --worktree
Then the command reports that the worktree path must be empty before creating a linked branch.

Given an empty or non-existent target directory
When I resolve it as a new worktree target
Then checkout proceeds as before.

Given an existing linked worktree
When I target its root
Then the worktree remains eligible for reuse even though it contains files.

Key points

The validation lives in the shared worktree resolver so gh issue develop and gh pr checkout retain consistent target semantics. The non-empty check runs only after determining that the target is not an existing linked worktree.

Notes for reviewers

Start with pkg/cmd/pr/shared/worktree.go. The regression test in pkg/cmd/issue/develop/develop_test.go deliberately registers no linked-branch mutation, proving validation happens before branch creation.

This follows up on the deferred edge case identified during review of #14136.

Authorship and follow-up

Who wrote this:

  • A human wrote it.
  • An agent wrote it under close human direction.
  • An agent wrote it independently, and no human has guided the implementation beyond the initial prompt.

Who answers review comments:

  • @tidy-dev will read and reply directly.
  • An agent will draft replies and @username will read them before they are posted.
  • Nobody has explicitly committed to replying.

Validate fresh worktree paths before issue develop creates a linked branch, preventing orphaned remote branches when checkout cannot proceed.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings August 24, 2026 00:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Rejects non-empty worktree targets before linked branch creation, preventing orphaned branches.

Changes:

  • Validates fresh worktree targets are empty.
  • Preserves reuse of linked worktrees.
  • Adds shared and command-level regression tests.
Show a summary per file
File Description
pkg/cmd/pr/shared/worktree.go Adds target emptiness validation.
pkg/cmd/pr/shared/worktree_test.go Tests non-empty target rejection.
pkg/cmd/issue/develop/develop_test.go Verifies rejection precedes branch creation.

Review details

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread pkg/cmd/pr/shared/worktree.go
@tidy-dev
tidy-dev marked this pull request as ready for review August 24, 2026 19:27
@tidy-dev
tidy-dev requested a review from a team as a code owner August 24, 2026 19:27
@tidy-dev
tidy-dev requested a review from sergiou87 August 24, 2026 19:27
Protect linked worktree reuse from the fresh-target emptiness guard.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 62a807cf-3d2d-42a5-a327-cc451efa7a59
@tidy-dev
tidy-dev requested a review from BagToad August 24, 2026 19:30

@babakks babakks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough follow-up here, @tidy-dev! 🙏

The fix looks solid. Putting the emptiness check in the shared ResolveWorktreeTarget keeps issue develop and pr checkout consistent, and gating it on !reuse correctly leaves existing linked worktrees reusable. Verifying that validation runs before CreateLinkedBranch (so no orphan linked branch is left behind) is exactly the right thing to prove, and the unit test that registers no branch mutation nails it.

The only thing I felt was missing is acceptance coverage for the non-empty target case. I'm going to push a couple of acceptance tests for that shortly (one under issue develop, one under pr checkout), so no action needed on your end there. Once those land I think this is good to go. 🚀

Add acceptance coverage for rejecting an existing non-empty directory as a
worktree target. The issue develop case also asserts no orphan linked branch
is left behind, and the pr checkout case asserts the early, clear error.

Co-authored-by: Copilot <[email protected]>
@babakks

babakks commented Aug 26, 2026

Copy link
Copy Markdown
Member

Enabling auto-merge as the updated acceptance tests pass.

@babakks
babakks enabled auto-merge August 26, 2026 11:19
@babakks
babakks merged commit 9e69e88 into trunk Aug 26, 2026
11 checks passed
@babakks
babakks deleted the tidy-dev-worktree-checkout-feedback branch August 26, 2026 11:29
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.

3 participants