Skip to content

Add worktree-scoped Git execution - #526

Open
skarim wants to merge 2 commits into
mainfrom
skarim/worktrees-scoped-git-execution
Open

skarim wants to merge 2 commits into
mainfrom
skarim/worktrees-scoped-git-execution

Conversation

@skarim

@skarim skarim commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Git worktrees share branch refs and objects, but each has its own HEAD, index, working files, and paused Git operations. This PR adds explicit worktree-scoped Git execution: the foundation for independent stacks in separate checkouts and for one stack whose layers live across several worktrees.

Functionality and user impact

  • Discover registered worktrees and the branches they hold, including branches reserved by an interrupted rebase. Keep the shared Git directory distinct from each worktree's local Git directory.
  • Run Git in the selected checkout without changing the process directory or inheriting another checkout's index/environment. Refuse missing, replaced, or unrelated repository locations instead of executing somewhere else.
  • Keep conflict inspection, rebase continuation, and cherry-picks in the correct worktree. Rebases disable automatic stashing and automatic updates of other refs; date-preserving rebases retain their settings through conflicts.

Boundary: this PR does not move stack metadata, add navigation flags, or automatically run stack commands in other worktrees. Shared tracking follows in #528, cross-worktree rebase/sync in #529, and distributed modify in #521. Discovery requires Git 2.36+; this supports existing worktrees, not their creation or removal.

Key areas to review

  • internal/git/worktree.go: ForWorktree and gitClient bind execution to a repository/worktree identity. Focus on rejecting a changed Git directory rather than falling back to the caller.
  • internal/git/worktree.go: Worktrees, rebaseBranch, and resolveMainWorktree handle ownership during detached rebases and separate Git directories. Existing core.worktree backlinks are used; unknown checkout paths are not guessed.
  • internal/git/git.go: configureCommand, rebaseArgs, and runRebaseCommand isolate the environment, set rebase policy, and distinguish a failure to start from a recoverable conflict.
  • internal/git/gitops.go: follow RebaseOnto, RebaseContinue, and the conflict/cherry-pick helpers to confirm checkout-sensitive operations stay on the scoped receiver.

Related issues

*Part 1 of the 4-PR split of #520

@skarim
skarim added this pull request to stack #530 September 28, 2026 17:20
@skarim
skarim marked this pull request as ready for review September 29, 2026 16:27
Copilot AI balanced review requested due to automatic review settings September 29, 2026 16:27

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.

Copilot review overview

🔵 Needs a closer look

The broad, cross-platform changes to Git execution identity and recovery behavior warrant final human validation.

Review effort: Balanced
Findings: None

What changed in this PR

Adds worktree-scoped Git execution while preserving repository identity and isolating checkout-specific state.

Changes:

  • Adds worktree discovery, validation, and scoped Git clients.
  • Makes rebase, conflict, and cherry-pick operations receiver-scoped.
  • Adds extensive integration coverage for worktree behavior and recovery.
File Description
internal/​git/​worktree.go Implements worktree discovery and scoping.
internal/​git/​worktree_test.go Tests parsing, versions, wrappers, and environment isolation.
internal/​git/​git.go Adds scoped command execution and rebase policy.
internal/​git/​gitops.go Routes Git operations through scoped receivers.
internal/​git/​gitops_test.go Adds linked-worktree integration coverage.
internal/​git/​mock_ops.go Extends mocks for worktree APIs.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

Copilot review overview

🟡 Changes recommended

Scoped configuration can redirect operations, and invalid scopes can be silently converted into boolean state.

Review effort: Balanced
Findings: 1 High severity · 1 Medium severity

Open (2)

Comment thread internal/git/git.go
Comment thread internal/git/worktree.go Outdated

This branch has not been deployed

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