git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: mh@glandium.org, "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH] rev-parse: take prefix into account in --git-common-dir
Date: Fri, 12 Feb 2016 11:31:45 +0700	[thread overview]
Message-ID: <1455251505-20685-1-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <20160212034723.GA20739@glandium.org>

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>
---
 On Fri, Feb 12, 2016 at 10:47 AM, Mike Hommey <mh@glandium.org> wrote:
 > Hi,
 >
 > In a worktree, git rev-parse --git-common-dir returns the non
 > worktree-specific git directory. e.g. .git instead of
 > .git/worktrees/name. The problem is that while it returns the right
 > thing from a subdirectory of a worktree, it doesn't from a subdirectory
 > of the "main" work tree. In the latter case it returns ".git" instead
 > of the full path to it.
 
 Oops. Fixed.

 builtin/rev-parse.c      |  3 ++-
 t/t2027-worktree-list.sh | 10 ++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index bd16876..cf8487b 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -763,7 +763,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "--git-common-dir")) {
-				puts(get_git_common_dir());
+				const char *pfx = prefix ? prefix : "";
+				puts(prefix_filename(pfx, strlen(pfx), get_git_common_dir()));
 				continue;
 			}
 			if (!strcmp(arg, "--resolve-git-dir")) {
diff --git a/t/t2027-worktree-list.sh b/t/t2027-worktree-list.sh
index 75ebb1b..1b1b65a 100755
--- a/t/t2027-worktree-list.sh
+++ b/t/t2027-worktree-list.sh
@@ -8,6 +8,16 @@ test_expect_success 'setup' '
 	test_commit init
 '
 
+test_expect_success 'rev-parse --git-common-dir on main worktree' '
+	git rev-parse --git-common-dir >actual &&
+	echo .git >expected &&
+	test_cmp expected actual &&
+	mkdir sub &&
+	git -C sub rev-parse --git-common-dir >actual2 &&
+	echo sub/.git >expected2 &&
+	test_cmp expected2 actual2
+'
+
 test_expect_success '"list" all worktrees from main' '
 	echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
 	test_when_finished "rm -rf here && git worktree prune" &&
-- 
2.7.0.377.g4cd97dd

      reply	other threads:[~2016-02-12  4:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-12  3:47 `git rev-parse --git-common-dir` doesn't work in a subdirectory of the main work tree Mike Hommey
2016-02-12  4:31 ` Nguyễn Thái Ngọc Duy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1455251505-20685-1-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=mh@glandium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).