Skip to content

Commit 93dba5a

Browse files
committed
git_path(): handle .lock files correctly
Ever since worktrees were introduced, the `git_path()` function _really_ needed to be called e.g. to get at the path to `logs/HEAD` (`HEAD` is specific to the worktree). However, the wrong path is returned for `logs/HEAD.lock`. This does not matter as long as the Git executable is doing the asking, as the path for that `index.lock` file is constructed from `git_path("index")` by appending the `.lock` suffix. However, Git GUI just learned to use `--git-path` instead of appending relative paths to what `git rev-parse --git-dir` returns (and as a consequence not only using the correct hooks directory, but also using the correct paths in worktrees other than the main one). While it does not seem as if Git GUI in particular is asking for `logs/HEAD.lock`, let's be safe rather than sorry. Side note: Git GUI _does_ ask for `index.lock`, but that is already resolved correctly. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent cf97c51 commit 93dba5a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

path.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ static int trie_find(struct trie *root, const char *key, match_fn fn,
268268
int result;
269269
struct trie *child;
270270

271-
if (!*key) {
271+
if (!*key || !strcmp(key, ".lock")) {
272272
/* we have reached the end of the key */
273273
if (root->value && !root->len)
274274
return fn(key, root->value, baton);
@@ -288,7 +288,7 @@ static int trie_find(struct trie *root, const char *key, match_fn fn,
288288

289289
/* Matched the entire compressed section */
290290
key += i;
291-
if (!*key)
291+
if (!*key || !strcmp(key, ".lock"))
292292
/* End of key */
293293
return fn(key, root->value, baton);
294294

t/t1500-rev-parse.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ test_expect_success 'git-path inside sub-dir' '
116116
test_cmp expect actual
117117
'
118118

119+
test_expect_success 'git-path in worktree' '
120+
test_tick &&
121+
git commit --allow-empty -m empty &&
122+
git worktree add --detach wt &&
123+
test_write_lines >expect \
124+
"$(pwd)/.git/worktrees/wt/logs/HEAD" \
125+
"$(pwd)/.git/worktrees/wt/logs/HEAD.lock" \
126+
"$(pwd)/.git/worktrees/wt/index" \
127+
"$(pwd)/.git/worktrees/wt/index.lock" &&
128+
git -C wt rev-parse >actual \
129+
--git-path logs/HEAD --git-path logs/HEAD.lock \
130+
--git-path index --git-path index.lock &&
131+
test_cmp expect actual
132+
'
133+
119134
test_expect_success 'rev-parse --is-shallow-repository in shallow repo' '
120135
test_commit test_commit &&
121136
echo true >expect &&

0 commit comments

Comments
 (0)