Skip to content

Fix rebase and merge operations when branches are checked out in worktrees#362

Merged
geofflamrock merged 17 commits intomainfrom
copilot/fix-350
Sep 23, 2025
Merged

Fix rebase and merge operations when branches are checked out in worktrees#362
geofflamrock merged 17 commits intomainfrom
copilot/fix-350

Conversation

Copy link
Contributor

Copilot AI commented Sep 7, 2025

This PR resolves the issue where stack update operations (rebase/merge) would fail when trying to operate on branches that are checked out in different worktrees.

Progress

  • Refactor IGitClientFactory interface and implementation
  • Update GitClient constructor to take working directory as string parameter
  • Rename factory method from CreateForWorktree to Create
  • Refactor StackActions constructor to use factory pattern
  • Update all command handlers to use IGitClientFactory instead of IGitClient:
    • NewBranchCommandHandler
    • RemoveBranchCommandHandler
    • CreatePullRequestsCommandHandler
    • OpenPullRequestsCommandHandler
    • StackSwitchCommandHandler
    • CleanupStackCommandHandler
    • DeleteStackCommandHandler
    • AddBranchCommandHandler
    • StackStatusCommandHandler
    • NewStackCommandHandler
  • Remove direct IGitClient registration from DI container
  • Update all test files to use IGitClientFactory pattern
  • Ensure all tests pass (193 tests passing)

Key Changes

  • Unified factory pattern - All command handlers now inject IGitClientFactory instead of IGitClient directly
  • Working directory flexibility - GitClient can now operate on any working directory (repository root or worktree)
  • Backward compatible - Branches not in worktrees continue to work exactly as before
  • Enhanced worktree support - Operations automatically detect and work in the correct worktree context
  • Preserved static method usage - Static methods in StackHelpers continue to accept IGitClient parameters as intended

Verification

✅ Main Stack project builds successfully
✅ All test projects build successfully
✅ All 193 tests pass
✅ No remaining constructor dependencies on IGitClient
✅ Static method usages preserved as requested

The refactoring is complete and all command handlers now consistently use the factory pattern for Git operations.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Cannot rebase or merge if a branch in the stack is checked out in another worktree Fix rebase and merge operations when branches are checked out in worktrees Sep 7, 2025
Copilot AI requested a review from geofflamrock September 7, 2025 06:48

public interface IGitClientFactory
{
IGitClient CreateForWorktree(string worktreePath);
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot Instead of making this solely used for worktrees:

  • Change the GitClient implementation to take in the working directory as a string instead of getting it from CliExecutionContext.
  • Rename this method to Create and take in a path (this could be to the repository or worktree).
  • Inject CliExecutionContext into command handlers and other classes as needed.
  • Inject IGitClientFactory instead of IGitClient in constructors where appropriate.
  • Use IGitClientFactory to create the git client passing for the working directory, from either the execution context or the worktree path.

There should be no direct dependencies on IGitClient in constructors after this, instead all dependencies are on IGitClientFactory. Usages in static methods where IGitClient are fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've implemented the requested changes. The core refactoring is complete:

  • ✅ Changed GitClient constructor to take string workingDirectory instead of CliExecutionContext
  • ✅ Renamed CreateForWorktree to Create and takes a path parameter
  • ✅ Injected CliExecutionContext into command handlers as needed
  • ✅ Updated StackActions and key command handlers to inject IGitClientFactory instead of IGitClient
  • ✅ Removed direct IGitClient registration from DI container

The main Stack project now builds successfully. Remaining work is updating other command handlers and test files to complete the refactoring.

Commit: 595f86e

Copy link
Owner

Choose a reason for hiding this comment

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

@copilot fix the remaining usages of IGitClient in command handlers and test files, ensure that there are no direct dependencies on IGitClient remaining in constructors. Usages in static methods where IGitClient is passed in are fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've completed the refactoring to remove direct IGitClient dependencies from command handlers. All 7 command handlers that had constructor dependencies on IGitClient have been updated to use IGitClientFactory instead:

  • NewBranchCommandHandler
  • RemoveBranchCommandHandler
  • CreatePullRequestsCommandHandler
  • OpenPullRequestsCommandHandler
  • StackSwitchCommandHandler
  • CleanupStackCommandHandler
  • DeleteStackCommandHandler

The main Stack project builds successfully. I've also updated the corresponding test files for these handlers. There are a few remaining test files for command handlers that use IStackActions (like SyncStackCommandHandler, PullStackCommandHandler, PushStackCommandHandler) that need similar updates, but the core refactoring is complete.

The pattern is now consistent throughout the application - all command handlers inject IGitClientFactory and CliExecutionContext, then create the appropriate GitClient instance using gitClientFactory.Create(executionContext.WorkingDirectory).

Commit: e3947cb

Copilot AI and others added 15 commits September 22, 2025 20:11
Co-authored-by: geofflamrock <2915931+geofflamrock@users.noreply.github.com>
…ead of CliExecutionContext

Co-authored-by: geofflamrock <2915931+geofflamrock@users.noreply.github.com>
Co-authored-by: geofflamrock <2915931+geofflamrock@users.noreply.github.com>
Co-authored-by: geofflamrock <2915931+geofflamrock@users.noreply.github.com>
Co-authored-by: geofflamrock <2915931+geofflamrock@users.noreply.github.com>
…ntFactory pattern

Co-authored-by: geofflamrock <2915931+geofflamrock@users.noreply.github.com>
@geofflamrock geofflamrock marked this pull request as ready for review September 23, 2025 21:43
@geofflamrock geofflamrock merged commit eeb7c03 into main Sep 23, 2025
15 checks passed
@geofflamrock geofflamrock deleted the copilot/fix-350 branch September 23, 2025 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot rebase or merge if a branch in the stack is checked out in another worktree

2 participants