Skip to contentSkip to conversation

`git rev-parse --git-common-dir` doesn't work in a subdirectory of the main work tree

PATCH New Mike Hommey 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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.

Mike

#1 lore raw

PATCH 1/1 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.

---

Quoted 8 lines · Mike Hommey

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(-)
builtin/rev-parse.c +2 -1
index bd16876..cf8487b 100644
@@ -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")) {
t/t2027-worktree-list.sh +10 -0
index 75ebb1b..1b1b65a 100755
@@ -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" &&
Signature · 1 line
2.7.0.377.g4cd97dd