Web lists-archives.com

"add worktree" fails with "fatal: Invalid path" error


I'm running git v.2.20.1 on Ubuntu from a program which follows the pattern:

1. create a temporary directory /tmp/tmpabc
2. in a loop:
    2a. create a second level of temporary directory /tmp/tmpabc/tmpworktree123
    2b. use "git worktree add" on the second level directory
    2c. do something
3. cleanup
    3b. "git branch -D" on each basename(second level directory)
    3a. "git worktree prune"

The loop size is of the order of 8-20. In step 2b, I often get errors
like this (from a Bash reproducer):

$ git worktree add /tmp/tmpgtxug4y9/git_worktree.gBGqnfnU
Preparing worktree (new branch 'git_worktree.gBGqnfnU')
fatal: Invalid path '/tmp/tmp1q9ysvyl': No such file or directory

I can see that the problematic path exists in the "gitdir" file of
what must be an earlier worktree from an older run (the branch is
gone, but the tree is still there). The path appear to relate to the
older run's first level directory:

$ grep -r /tmp/tmp1q9ysvyl ../.git/worktrees/
$ git worktree list
/tmp/tmp1q9ysvyl/git_worktree.frcwtjt_  edde3f25 (detached HEAD)
$ git branch | grep frcwtjt_
<no matches>

NOTE: I've not yet had to try deleting the worktree, since "add
worktree" does appear to work some of the time, so I am able to limp

I have these questions:

1. There is no branch or first level directory, but "git prune" has
not deleted the worktree, is this expected?
2. Is there something wrong with the sequence of steps I am following?

Thanks, Shaheed

P.S. I have an strace of a failing worktree add if needed.