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 <mh@glandium.org> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Feb 12, 2016 at 11:31 UTC 17f1365dbce9aa00cab3f196e7fa7138d9081eef
2 files changed +12 -1
builtin/rev-parse.c
+2 -1
@@ -763,7 +763,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
763 continue;
764 }
765 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()));
768 continue;
769 }
770 if (!strcmp(arg, "--resolve-git-dir")) {
t/t2027-worktree-list.sh
+10
@@ -8,6 +8,16 @@ test_expect_success 'setup' '
8 test_commit init
9 '
10
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 +
21 test_expect_success '"list" all worktrees from main' '
22 echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
23 test_when_finished "rm -rf here && git worktree prune" &&