Skip to content

AddWorktree(path) with no mode silently creates a branch named after the folder (or checks out an existing one); remarks claim a branch is always named #152

Description

@matt-edmondson

What's wrong

In GitIntegration/Builders/GitWorktreeAddBuilder.cs, when no mode method and no commit-ish are set, AddMode.Plain emits only git worktree add --end-of-options <path> (L190-L206). Given that command, git does one of three things:

  • It creates a new branch named after the last path segment. Running git worktree add --end-of-options ../wt1 printed Preparing worktree (new branch 'wt1').
  • If a branch with that name already exists and isn't checked out elsewhere, it checks that branch out.
  • With the host setting worktree.guessRemote=true, it can instead base the new branch on a matching remote branch.

The class remarks (L72-L77) justify leaving out --guess-remote with "--guess-remote extends that to the case where no branch is named at all, which this builder always names." That is not true in the Plain/no-commit-ish case.

Failure scenario

A caller who wants "a worktree at ../feature-x" calls repo.AddWorktree(path).ExecuteAsync(). Depending on the folder name and host config, they get:

  • an unrequested branch feature-x left in the repository, or
  • someone else's existing feature-x branch checked out in the new worktree, which means later commits land on the wrong branch.

The library otherwise pins host config to make results deterministic, but here the result depends on worktree.guessRemote.

Suggested fix

  • When neither a mode nor a commit-ish is set, either emit --detach (a worktree at HEAD with no branch side effects), or throw InvalidOperationException asking the caller to choose Branch/NewBranch/Detach/a commit-ish.
  • Correct the --guess-remote remark to match.
  • Add a builder test for the argument list in the bare AddWorktree(path) case, and an integration test asserting that no new branch appears.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions