Skip to content

Commit 39b8980

Browse files
committed
Merge branch 'js/git-path-in-subdir'
The "--git-path", "--git-common-dir", and "--shared-index-path" options of "git rev-parse" did not produce usable output. They are now updated to show the path to the correct file, relative to where the caller is. * js/git-path-in-subdir: rev-parse: fix several options when running in a subdirectory rev-parse tests: add tests executed from a subdirectory
2 parents b760481 + 098aa86 commit 39b8980

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

builtin/rev-parse.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
545545
unsigned int flags = 0;
546546
const char *name = NULL;
547547
struct object_context unused;
548+
struct strbuf buf = STRBUF_INIT;
548549

549550
if (argc > 1 && !strcmp("--parseopt", argv[1]))
550551
return cmd_parseopt(argc - 1, argv + 1, prefix);
@@ -599,7 +600,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
599600
if (!strcmp(arg, "--git-path")) {
600601
if (!argv[i + 1])
601602
die("--git-path requires an argument");
602-
puts(git_path("%s", argv[i + 1]));
603+
strbuf_reset(&buf);
604+
puts(relative_path(git_path("%s", argv[i + 1]),
605+
prefix, &buf));
603606
i++;
604607
continue;
605608
}
@@ -831,8 +834,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
831834
continue;
832835
}
833836
if (!strcmp(arg, "--git-common-dir")) {
834-
const char *pfx = prefix ? prefix : "";
835-
puts(prefix_filename(pfx, strlen(pfx), get_git_common_dir()));
837+
strbuf_reset(&buf);
838+
puts(relative_path(get_git_common_dir(),
839+
prefix, &buf));
836840
continue;
837841
}
838842
if (!strcmp(arg, "--is-inside-git-dir")) {
@@ -855,7 +859,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
855859
die(_("Could not read the index"));
856860
if (the_index.split_index) {
857861
const unsigned char *sha1 = the_index.split_index->base_sha1;
858-
puts(git_path("sharedindex.%s", sha1_to_hex(sha1)));
862+
const char *path = git_path("sharedindex.%s", sha1_to_hex(sha1));
863+
strbuf_reset(&buf);
864+
puts(relative_path(path, prefix, &buf));
859865
}
860866
continue;
861867
}
@@ -907,6 +913,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
907913
continue;
908914
verify_filename(prefix, arg, 1);
909915
}
916+
strbuf_release(&buf);
910917
if (verify) {
911918
if (revs_count == 1) {
912919
show_rev(type, sha1, name);

t/t1500-rev-parse.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,32 @@ test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = tru
8888

8989
test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
9090

91+
test_expect_success 'git-common-dir from worktree root' '
92+
echo .git >expect &&
93+
git rev-parse --git-common-dir >actual &&
94+
test_cmp expect actual
95+
'
96+
97+
test_expect_success 'git-common-dir inside sub-dir' '
98+
mkdir -p path/to/child &&
99+
test_when_finished "rm -rf path" &&
100+
echo "$(git -C path/to/child rev-parse --show-cdup).git" >expect &&
101+
git -C path/to/child rev-parse --git-common-dir >actual &&
102+
test_cmp expect actual
103+
'
104+
105+
test_expect_success 'git-path from worktree root' '
106+
echo .git/objects >expect &&
107+
git rev-parse --git-path objects >actual &&
108+
test_cmp expect actual
109+
'
110+
111+
test_expect_success 'git-path inside sub-dir' '
112+
mkdir -p path/to/child &&
113+
test_when_finished "rm -rf path" &&
114+
echo "$(git -C path/to/child rev-parse --show-cdup).git/objects" >expect &&
115+
git -C path/to/child rev-parse --git-path objects >actual &&
116+
test_cmp expect actual
117+
'
118+
91119
test_done

t/t1700-split-index.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,20 @@ EOF
200200
test_cmp expect actual
201201
'
202202

203+
test_expect_success 'rev-parse --shared-index-path' '
204+
test_create_repo split-index &&
205+
(
206+
cd split-index &&
207+
git update-index --split-index &&
208+
echo .git/sharedindex* >expect &&
209+
git rev-parse --shared-index-path >actual &&
210+
test_cmp expect actual &&
211+
mkdir subdirectory &&
212+
cd subdirectory &&
213+
echo ../.git/sharedindex* >expect &&
214+
git rev-parse --shared-index-path >actual &&
215+
test_cmp expect actual
216+
)
217+
'
218+
203219
test_done

t/t2027-worktree-list.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ test_expect_success 'rev-parse --git-common-dir on main worktree' '
1414
test_cmp expected actual &&
1515
mkdir sub &&
1616
git -C sub rev-parse --git-common-dir >actual2 &&
17-
echo sub/.git >expected2 &&
17+
echo ../.git >expected2 &&
1818
test_cmp expected2 actual2
1919
'
2020

21+
test_expect_success 'rev-parse --git-path objects linked worktree' '
22+
echo "$(git rev-parse --show-toplevel)/.git/objects" >expect &&
23+
test_when_finished "rm -rf linked-tree && git worktree prune" &&
24+
git worktree add --detach linked-tree master &&
25+
git -C linked-tree rev-parse --git-path objects >actual &&
26+
test_cmp expect actual
27+
'
28+
2129
test_expect_success '"list" all worktrees from main' '
2230
echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
2331
test_when_finished "rm -rf here && git worktree prune" &&

0 commit comments

Comments
 (0)