Skip to content

Commit 76a53d6

Browse files
dschogitster
authored andcommitted
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, 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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3ce4721 commit 76a53d6

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

path.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "path.h"
1212
#include "packfile.h"
1313
#include "object-store.h"
14+
#include "lockfile.h"
1415

1516
static int get_st_mode_bits(const char *path, int *mode)
1617
{
@@ -350,9 +351,14 @@ static void update_common_dir(struct strbuf *buf, int git_dir_len,
350351
const char *common_dir)
351352
{
352353
char *base = buf->buf + git_dir_len;
354+
int has_lock_suffix = strbuf_strip_suffix(buf, LOCK_SUFFIX);
355+
353356
init_common_trie();
354357
if (trie_find(&common_trie, base, check_common, NULL) > 0)
355358
replace_dir(buf, git_dir_len, common_dir);
359+
360+
if (has_lock_suffix)
361+
strbuf_addstr(buf, LOCK_SUFFIX);
356362
}
357363

358364
void report_linked_checkout_garbage(void)

t/t0060-path-utils.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,10 @@ test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
285285
test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
286286
test_expect_success 'setup common repository' 'git --git-dir=bar init'
287287
test_git_path GIT_COMMON_DIR=bar index .git/index
288+
test_git_path GIT_COMMON_DIR=bar index.lock .git/index.lock
288289
test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
289290
test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD
291+
test_git_path GIT_COMMON_DIR=bar logs/HEAD.lock .git/logs/HEAD.lock
290292
test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo .git/logs/refs/bisect/foo
291293
test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo bar/logs/refs/bisec/foo
292294
test_git_path GIT_COMMON_DIR=bar logs/refs/bisec bar/logs/refs/bisec

0 commit comments

Comments
 (0)