git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Rappazzo <rappazzo@gmail.com>
To: git@vger.kernel.org
Cc: pclouds@gmail.com, gitster@pobox.com,
	Michael Rappazzo <rappazzo@gmail.com>
Subject: [PATCH] rev-parse: fix --git-common-dir when executed from subpath of main tree
Date: Sun,  3 Apr 2016 21:42:23 -0400	[thread overview]
Message-ID: <1459734143-95832-1-git-send-email-rappazzo@gmail.com> (raw)

Executing `git-rev-parse --git-common-dir` from the root of the main
worktree results in '.git', which is the relative path to the git dir.
When executed from a subpath of the main tree it returned somthing like:
'sub/path/.git'.  Change this to return the proper relative path to the
git directory (similar to `--show-cdup`).

Add as test to t1500-rev-parse.sh for this case and adjust another test
in t2027-worktree-list.sh to use this expectation.

Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
---
 builtin/rev-parse.c      | 14 ++++++++++++--
 t/t1500-rev-parse.sh     | 10 ++++++++++
 t/t2027-worktree-list.sh |  2 +-
 3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index c961b74..c2918e1 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -787,8 +787,18 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "--git-common-dir")) {
-				const char *pfx = prefix ? prefix : "";
-				puts(prefix_filename(pfx, strlen(pfx), get_git_common_dir()));
+				const char *git_common_dir = get_git_common_dir();
+				if (prefix && !is_absolute_path(git_common_dir)) {
+					const char *pfx = prefix;
+					while (pfx) {
+						pfx = strchr(pfx, '/');
+						if (pfx) {
+							pfx++;
+							printf("../");
+						}
+					}
+				}
+				printf("%s\n", git_common_dir);
 				continue;
 			}
 			if (!strcmp(arg, "--is-inside-git-dir")) {
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index 48ee077..2023208 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -3,6 +3,16 @@
 test_description='test git rev-parse'
 . ./test-lib.sh
 
+test_expect_success 'git-common-dir inside sub-dir' '
+   (
+		mkdir -p path/to/child &&
+		cd path/to/child &&
+		echo "$(git rev-parse --show-cdup).git" >expect &&
+		git rev-parse --git-common-dir >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_rev_parse() {
 	name=$1
 	shift
diff --git a/t/t2027-worktree-list.sh b/t/t2027-worktree-list.sh
index 1b1b65a..3780b14 100755
--- a/t/t2027-worktree-list.sh
+++ b/t/t2027-worktree-list.sh
@@ -14,7 +14,7 @@ test_expect_success 'rev-parse --git-common-dir on main worktree' '
 	test_cmp expected actual &&
 	mkdir sub &&
 	git -C sub rev-parse --git-common-dir >actual2 &&
-	echo sub/.git >expected2 &&
+	echo ../.git >expected2 &&
 	test_cmp expected2 actual2
 '
 
-- 
2.8.0

             reply	other threads:[~2016-04-04  1:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-04  1:42 Michael Rappazzo [this message]
2016-04-04  6:01 ` [PATCH] rev-parse: fix --git-common-dir when executed from subpath of main tree Eric Sunshine
2016-04-08 11:47 ` Duy Nguyen
2016-04-08 12:35   ` Mike Rappazzo
2016-05-19  7:49     ` Mike Hommey
2016-05-19 14:15       ` Mike Rappazzo
2016-05-19  8:50 ` Mike Hommey

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=1459734143-95832-1-git-send-email-rappazzo@gmail.com \
    --to=rappazzo@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    /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).