Skip to content

Extract worktree list parsing into git client - #13953

Closed
tidy-dev wants to merge 3 commits into
tidy-dev-pr-checkout-worktreefrom
tidy-dev-worktree-client-helper
Closed

Extract worktree list parsing into git client#13953
tidy-dev wants to merge 3 commits into
tidy-dev-pr-checkout-worktreefrom
tidy-dev-worktree-client-helper

Conversation

@tidy-dev

Copy link
Copy Markdown
Contributor

Summary

Small, behavior-preserving refactor stacked on top of #13946 (gh pr checkout --worktree). It promotes the inline git worktree list --porcelain parsing out of the pr/checkout command and into the shared git client so the remaining worktree subtasks can reuse it.

  • Adds a Worktree type in git/objects.go (Path, Head, Branch, Detached, Bare).
  • Adds Client.Worktrees(ctx) in git/client.go, backed by a pure parseWorktrees helper, following the existing Remotes/parseRemotes and ShowRefs conventions.
  • Rewires isWorktreeAtPath in pkg/cmd/pr/checkout/checkout.go to consume the new method (no behavior change).
  • Adds TestClientWorktrees and Test_parseWorktrees covering normal, multiple, detached, bare, and no-trailing-newline records.

Why

The two follow-up subtasks under epic github/gh-cli-and-desktop#258 (gh issue develop --worktree and gh pr merge --delete-branch worktree guards) both need to inspect existing worktrees. Extracting one tested parser avoids each command re-implementing porcelain parsing.

Stacking

This PR is based on tidy-dev-pr-checkout-worktree (#13946). Review/merge that PR first; the diff here is only the parser extraction.

Testing

  • go test ./git/... ./pkg/cmd/pr/checkout/... — new and existing worktree tests pass. (TestClientLastCommit fails in my local sandbox due to a safe.bareRepository git config, unrelated to this change and pre-existing on the base branch.)
  • go vet and gofmt clean.

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

Promote the inline `git worktree list --porcelain` parsing from the
pr/checkout command into a reusable `Client.Worktrees` method backed by a
new `Worktree` type and pure `parseWorktrees` helper. Rewire
`isWorktreeAtPath` to consume it so behavior is unchanged.

This gives the follow-up worktree subtasks (gh issue develop --worktree,
gh pr merge --delete-branch guards) a shared, tested parser instead of
each re-implementing porcelain parsing.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 23, 2026 17:12
@tidy-dev
tidy-dev requested a review from a team as a code owner July 23, 2026 17:12
@tidy-dev
tidy-dev requested a review from williammartin July 23, 2026 17:12
@tidy-dev
tidy-dev changed the base branch from trunk to tidy-dev-pr-checkout-worktree July 23, 2026 17:12

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

This PR performs a small, behavior-preserving refactor in the GitHub CLI by extracting git worktree list --porcelain parsing from gh pr checkout into the shared git.Client, enabling reuse by other commands that need to inspect worktrees.

Changes:

  • Introduces a git.Worktree object model for git worktree list --porcelain entries.
  • Adds Client.Worktrees(ctx) and a dedicated parseWorktrees helper in the shared git client.
  • Updates pr checkout worktree path detection to use the new shared API and adds unit tests for both the client method and parser.
Show a summary per file
File Description
pkg/cmd/pr/checkout/checkout.go Switches worktree-at-path detection to use the new shared Client.Worktrees method.
git/objects.go Adds a Worktree struct to represent parsed worktree entries.
git/client.go Adds Client.Worktrees(ctx) and parseWorktrees implementation.
git/client_test.go Adds coverage for Client.Worktrees and parseWorktrees across key scenarios.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Low

@tidy-dev
tidy-dev marked this pull request as draft July 23, 2026 17:18
Drop the Head, Detached, and Bare fields. None of the worktree subtasks
read the checked-out SHA, and detached/bare worktrees are handled by
simply having an empty Branch, so the extra fields were speculative. The
parser still tolerates HEAD/detached/bare lines; it just ignores them.

Co-authored-by: Copilot App <[email protected]>
@tidy-dev
tidy-dev marked this pull request as ready for review July 23, 2026 17:31
…nto tidy-dev-worktree-client-helper

# Conflicts:
#	pkg/cmd/pr/checkout/checkout.go
@tidy-dev

Copy link
Copy Markdown
Contributor Author

Closing - this content is now absorbed into #13955 which targets trunk directly.

@tidy-dev tidy-dev closed this Jul 29, 2026
@tidy-dev
tidy-dev deleted the tidy-dev-worktree-client-helper branch July 29, 2026 14:38
An error occurred while trying to automatically change base from tidy-dev-pr-checkout-worktree to trunk August 4, 2026 11: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.

2 participants