Skip to content

Commit 17f1365

Browse files
pcloudsgitster
authored andcommitted
rev-parse: take prefix into account in --git-common-dir
Most of the time, get_git_common_dir() returns an absolute path so prefix is irrelevant. If it returns a relative path (e.g. from the main worktree) then prefixing is required. Noticed-by: Mike Hommey <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7548842 commit 17f1365

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

builtin/rev-parse.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
763763
continue;
764764
}
765765
if (!strcmp(arg, "--git-common-dir")) {
766-
puts(get_git_common_dir());
766+
const char *pfx = prefix ? prefix : "";
767+
puts(prefix_filename(pfx, strlen(pfx), get_git_common_dir()));
767768
continue;
768769
}
769770
if (!strcmp(arg, "--resolve-git-dir")) {

t/t2027-worktree-list.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ test_expect_success 'setup' '
88
test_commit init
99
'
1010

11+
test_expect_success 'rev-parse --git-common-dir on main worktree' '
12+
git rev-parse --git-common-dir >actual &&
13+
echo .git >expected &&
14+
test_cmp expected actual &&
15+
mkdir sub &&
16+
git -C sub rev-parse --git-common-dir >actual2 &&
17+
echo sub/.git >expected2 &&
18+
test_cmp expected2 actual2
19+
'
20+
1121
test_expect_success '"list" all worktrees from main' '
1222
echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
1323
test_when_finished "rm -rf here && git worktree prune" &&

0 commit comments

Comments
 (0)