git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: BUG: git worktree add directory-path fails due to over-aggressive disallowing of worktrees with same suffix

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Feb 12, 2019, 22:44 UTC
Message-ID
<CAPig+cQh8hxeoVjLHDKhAcZVQPpPT5v0AUY8gsL9=qfJ7z-L2A@mail.gmail.com>
In-Reply-To
<0308570E-AAA3-43B8-A592-F4DA9760DBED@synopsys.com>

On Mon, Feb 11, 2019 at 6:50 PM Cameron Gunnin <cameron.gunnin@synopsys.com> wrote:

Show 17 quoted lines
> The bug:
>   cd unique-path-1
>   git --git-dir=../worktree-test-repo/.git worktree add subdir branch1
>   cd ../unique-path-2
>   git --git-dir=../worktree-test-repo/.git worktree add subdir branch2
>   # FAILS WITH: fatal: 'subdir' is a missing but already registered worktree; use 'add -f' to override, or 'prune' or 'remove' to clear
>
> This appears to have been introduced by the following commit:
>       worktree: disallow adding same path multiple times
>
> The fix, I think, should be applied to builtin/worktree.c to the
> validate_worktree_add method. After finding a worktree that matches
> the suffix (via find_worktree), it should check that the absolute
> path of the found worktree is the same as the absolute path of the
> worktree being added, and allow the add when they are different. Or,
> perhaps there should be a way to invoke 'find_worktree' such that it
> only finds absolute path matches.

When crafting cb56f55c16 (worktree: disallow adding same path multiple times, 2018-08-28), I flip-flopped between two implementations: (1) using find_worktree(), and (2) manually scanning the worktree list with absolute path comparison. The latter approach, would not have suffered from this problem. The one ugly bit of the manual scan was that it was a bit too cozy with the underlying worktree implementation, so I eventually went with the find_worktree() approach despite its obvious drawback of doing suffix matching (though, I didn't feel particularly comfortable with the decision).

A likely reasonable approach to fixing it would probably be to introduce new worktree API to find a worktree without magic suffix matching (i.e. literal absolute path matching) which would avoid having to imbue the higher-level "git worktree" command with that low-level knowledge, and then take advantage of that new API.

Previous: Cameron GunninNext: Eric Sunshine
Message 2 of 8 in “BUG: git worktree add directory-path fails due to over-aggressive disallowing of worktrees with same suffix”
  1. Cameron GunninFeb 11, 2019
  2. Eric SunshineFeb 12, 2019
  3. 0/3 git-worktree: fix "add" being fooled by suffix matchingEric Sunshine, Feb 24, 2020
  4. 2/3 worktree: add utility to find worktree by pathnameEric Sunshine, Feb 24, 2020
  5. 3/3 worktree: don't allow "add" validation to be fooled by suffix matchingEric Sunshine, Feb 24, 2020
  6. Junio C HamanoFeb 24, 2020
  7. 1/3 worktree: improve find_worktree() documentationEric Sunshine, Feb 24, 2020
  8. Junio C HamanoFeb 24, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.