]> granicus.if.org Git - git/commitdiff
git_path(): handle `.lock` files correctly
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Mon, 21 Oct 2019 21:54:42 +0000 (21:54 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 24 Oct 2019 03:12:36 +0000 (12:12 +0900)
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, and therefore so is its reflog). 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 `logs/HEAD.lock` file is constructed from
`git_path("logs/HEAD")` 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, due to `update_common_dir()` preferring to leave
unknown paths in the (worktree-specific) git directory.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
path.c
t/t1500-rev-parse.sh

diff --git a/path.c b/path.c
index 25e97b8c3f76ce9246d8d985adba9777acd5f43c..aef103ee8f97e46c539d75cdcf71be70036ee0ab 100644 (file)
--- a/path.c
+++ b/path.c
@@ -349,10 +349,17 @@ static int check_common(const char *unmatched, void *value, void *baton)
 static void update_common_dir(struct strbuf *buf, int git_dir_len,
                              const char *common_dir)
 {
-       char *base = buf->buf + git_dir_len;
+       char *base = buf->buf + git_dir_len, *base2 = NULL;
+       size_t baselen;
+
+       if (strip_suffix(base, ".lock", &baselen))
+               base = base2 = xstrndup(base, baselen);
+
        init_common_trie();
        if (trie_find(&common_trie, base, check_common, NULL) > 0)
                replace_dir(buf, git_dir_len, common_dir);
+
+       free(base2);
 }
 
 void report_linked_checkout_garbage(void)
index 01abee533dedfd1e2d8bd347d06fc5c0c8b7833a..d318a1eeef3ea38caed22cb5032bf6d28a6b2471 100755 (executable)
@@ -116,6 +116,21 @@ test_expect_success 'git-path inside sub-dir' '
        test_cmp expect actual
 '
 
+test_expect_success 'git-path in worktree' '
+       test_tick &&
+       git commit --allow-empty -m empty &&
+       git worktree add --detach wt &&
+       test_write_lines >expect \
+               "$(pwd)/.git/worktrees/wt/logs/HEAD" \
+               "$(pwd)/.git/worktrees/wt/logs/HEAD.lock" \
+               "$(pwd)/.git/worktrees/wt/index" \
+               "$(pwd)/.git/worktrees/wt/index.lock" &&
+       git -C wt rev-parse >actual \
+               --git-path logs/HEAD --git-path logs/HEAD.lock \
+               --git-path index --git-path index.lock &&
+       test_cmp expect actual
+'
+
 test_expect_success 'rev-parse --is-shallow-repository in shallow repo' '
        test_commit test_commit &&
        echo true >expect &&