This allows unlocked repos to work correctly with multiple worktrees.
When doing git worktree add newtree2 in an "unlocked" repo, I get the following output and creating the new worktree fails.
Preparing worktree (new branch 'newtree2')
git-crypt: Error: Unable to open key file - have you unlocked/initialized this repository yet?
error: external filter '"/usr/bin/git-crypt" smudge' failed 1
error: external filter '"/usr/bin/git-crypt" smudge' failed
fatal: env/docker-all.secret-env: smudge filter git-crypt failed
By putting strace in front of the smudge command in .git/config, I see:
openat(AT_FDCWD, "/home/morgan/Development/dev1/.git/worktrees/newtree2/git-crypt/keys/default", O_RDONLY) = -1 ENOENT (No such file or directory)
Git is calling the smudge command when checking out the new working tree, which then fails to find the key file because git rev-parse uses a separate dir for each worktree. Hence the worktrees/newtree2 in the path. The command fails because there is no git-crypt dir in .git/worktrees/newtree2.
See gitrepository-layout[5] for more information. The rule of thumb is do not make any assumption about whether a path belongs to $GIT_DIR or $GIT_COMMON_DIR when you need to directly access something inside $GIT_DIR. Use git rev-parse --git-path to get the final path.
This allows unlocked repos to work correctly with multiple worktrees.
When doing
git worktree add newtree2
in an "unlocked" repo, I get the following output and creating the new worktree fails.By putting
strace
in front of the smudge command in.git/config
, I see:Git is calling the smudge command when checking out the new working tree, which then fails to find the key file because
git rev-parse
uses a separate dir for each worktree. Hence theworktrees/newtree2
in the path. The command fails because there is nogit-crypt
dir in.git/worktrees/newtree2
.git rev-parse --git-path
should be used instead ofgit rev-parse --git-dir
, based on https://git-scm.com/docs/git-worktree#_details :More importantly,
git-crypt
should be using thecommon
subdir of the git dir to store in-use keys, since those need to be shared across workdirs. https://git-scm.com/docs/gitrepository-layout#Documentation/gitrepository-layout.txt-common