{"thread":{"id":"41397","subject":"`git rev-parse --git-common-dir` doesn't work in a subdirectory of the main work tree","startedAt":"2016-02-12T03:47:23Z","lastAt":"2016-02-12T04:31:45Z","messageCount":2,"participants":["Mike Hommey","Nguyễn Thái Ngọc Duy"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"277996","messageId":"20160212034723.GA20739@glandium.org","threadId":"41397","inReplyTo":null,"subject":"`git rev-parse --git-common-dir` doesn't work in a subdirectory of the main work tree","fromName":"Mike Hommey","fromEmail":"mh@glandium.org","sentAt":"2016-02-12T03:47:23Z","receivedAt":"2016-02-12T03:47:23Z","isPatch":false,"sender":{"key":"mh@glandium.org","avatar":"https://avatars.githubusercontent.com/u/1038527?v=4"},"body":"Hi,\n\nIn a worktree, git rev-parse --git-common-dir returns the non\nworktree-specific git directory. e.g. .git instead of\n.git/worktrees/name. The problem is that while it returns the right\nthing from a subdirectory of a worktree, it doesn't from a subdirectory\nof the \"main\" work tree. In the latter case it returns \".git\" instead\nof the full path to it.\n\nMike\n"},{"id":"277997","messageId":"1455251505-20685-1-git-send-email-pclouds@gmail.com","threadId":"41397","inReplyTo":"20160212034723.GA20739@glandium.org","subject":"[PATCH] rev-parse: take prefix into account in --git-common-dir","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2016-02-12T04:31:45Z","receivedAt":"2016-02-12T04:31:45Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Most of the time, get_git_common_dir() returns an absolute path so\nprefix is irrelevant. If it returns a relative path (e.g. from the\nmain worktree) then prefixing is required.\n\nNoticed-by: Mike Hommey <mh@glandium.org>\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n On Fri, Feb 12, 2016 at 10:47 AM, Mike Hommey <mh@glandium.org> wrote:\n > Hi,\n >\n > In a worktree, git rev-parse --git-common-dir returns the non\n > worktree-specific git directory. e.g. .git instead of\n > .git/worktrees/name. The problem is that while it returns the right\n > thing from a subdirectory of a worktree, it doesn't from a subdirectory\n > of the \"main\" work tree. In the latter case it returns \".git\" instead\n > of the full path to it.\n \n Oops. Fixed.\n\n builtin/rev-parse.c      |  3 ++-\n t/t2027-worktree-list.sh | 10 ++++++++++\n 2 files changed, 12 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/rev-parse.c b/builtin/rev-parse.c\nindex bd16876..cf8487b 100644\n--- a/builtin/rev-parse.c\n+++ b/builtin/rev-parse.c\n@@ -763,7 +763,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t\tcontinue;\n \t\t\t}\n \t\t\tif (!strcmp(arg, \"--git-common-dir\")) {\n-\t\t\t\tputs(get_git_common_dir());\n+\t\t\t\tconst char *pfx = prefix ? prefix : \"\";\n+\t\t\t\tputs(prefix_filename(pfx, strlen(pfx), get_git_common_dir()));\n \t\t\t\tcontinue;\n \t\t\t}\n \t\t\tif (!strcmp(arg, \"--resolve-git-dir\")) {\ndiff --git a/t/t2027-worktree-list.sh b/t/t2027-worktree-list.sh\nindex 75ebb1b..1b1b65a 100755\n--- a/t/t2027-worktree-list.sh\n+++ b/t/t2027-worktree-list.sh\n@@ -8,6 +8,16 @@ test_expect_success 'setup' '\n \ttest_commit init\n '\n \n+test_expect_success 'rev-parse --git-common-dir on main worktree' '\n+\tgit rev-parse --git-common-dir >actual &&\n+\techo .git >expected &&\n+\ttest_cmp expected actual &&\n+\tmkdir sub &&\n+\tgit -C sub rev-parse --git-common-dir >actual2 &&\n+\techo sub/.git >expected2 &&\n+\ttest_cmp expected2 actual2\n+'\n+\n test_expect_success '\"list\" all worktrees from main' '\n \techo \"$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]\" >expect &&\n \ttest_when_finished \"rm -rf here && git worktree prune\" &&\n-- \n2.7.0.377.g4cd97dd\n"}]}