Skip to content

Commit 9c17cca

Browse files
committed
Merge branch 'nd/git-common-dir-fix' into maint
"git rev-parse --git-common-dir" used in the worktree feature misbehaved when run from a subdirectory. * nd/git-common-dir-fix: rev-parse: take prefix into account in --git-common-dir
2 parents 8834ea3 + 17f1365 commit 9c17cca

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

builtin/rev-parse.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
763763
continue;
764764
}
765765
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()));
767768
continue;
768769
}
769770
if (!strcmp(arg, "--resolve-git-dir")) {

t/t2027-worktree-list.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ test_expect_success 'setup' '
88
test_commit init
99
'
1010

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+
1121
test_expect_success '"list" all worktrees from main' '
1222
echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
1323
test_when_finished "rm -rf here && git worktree prune" &&

0 commit comments

Comments
 (0)