Skip to content

Commit fb9c2d2

Browse files
peffgitster
authored andcommitted
refs: use chdir_notify to update cached relative paths
Commit f57f37e (files-backend: remove the use of git_path(), 2017-03-26) introduced a regression when a relative $GIT_DIR is used in a working tree: - when we initialize the ref backend, we make a copy of get_git_dir(), which may be relative - later, we may call setup_work_tree() and chdir to the root of the working tree - further calls to the ref code will use the stored git directory, but relative paths will now point to the wrong place The new test in t1501 demonstrates one such instance (the bug causes us to write the ref update to the nonsense "relative/relative/.git"). Since setup_work_tree() now uses chdir_notify, we can just ask it update our relative paths when necessary. Reported-by: Rafael Ascensao <[email protected]> Helped-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8500e0d commit fb9c2d2

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

refs/files-backend.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../lockfile.h"
1010
#include "../object.h"
1111
#include "../dir.h"
12+
#include "../chdir-notify.h"
1213

1314
/*
1415
* This backend uses the following flags in `ref_update::flags` for
@@ -106,6 +107,11 @@ static struct ref_store *files_ref_store_create(const char *gitdir,
106107
refs->packed_ref_store = packed_ref_store_create(sb.buf, flags);
107108
strbuf_release(&sb);
108109

110+
chdir_notify_reparent("files-backend $GIT_DIR",
111+
&refs->gitdir);
112+
chdir_notify_reparent("files-backend $GIT_COMMONDIR",
113+
&refs->gitcommondir);
114+
109115
return ref_store;
110116
}
111117

refs/packed-backend.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "packed-backend.h"
66
#include "../iterator.h"
77
#include "../lockfile.h"
8+
#include "../chdir-notify.h"
89

910
enum mmap_strategy {
1011
/*
@@ -202,6 +203,8 @@ struct ref_store *packed_ref_store_create(const char *path,
202203
refs->store_flags = store_flags;
203204

204205
refs->path = xstrdup(path);
206+
chdir_notify_reparent("packed-refs", &refs->path);
207+
205208
return ref_store;
206209
}
207210

t/t1501-work-tree.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,4 +431,16 @@ test_expect_success 'error out gracefully on invalid $GIT_WORK_TREE' '
431431
)
432432
'
433433

434+
test_expect_success 'refs work with relative gitdir and work tree' '
435+
git init relative &&
436+
git -C relative commit --allow-empty -m one &&
437+
git -C relative commit --allow-empty -m two &&
438+
439+
GIT_DIR=relative/.git GIT_WORK_TREE=relative git reset HEAD^ &&
440+
441+
git -C relative log -1 --format=%s >actual &&
442+
echo one >expect &&
443+
test_cmp expect actual
444+
'
445+
434446
test_done

0 commit comments

Comments
 (0)