]>
Commit | Line | Data |
---|---|---|
dace6e44 ML |
1 | #!/bin/sh |
2 | ||
3 | test_description='test git rev-parse' | |
06d53148 | 4 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
5 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
6 | ||
e77b3da6 | 7 | TEST_PASSES_SANITIZE_LEAK=true |
dace6e44 ML |
8 | . ./test-lib.sh |
9 | ||
fac60b89 | 10 | test_one () { |
11 | dir="$1" && | |
12 | expect="$2" && | |
13 | shift && | |
14 | shift && | |
15 | echo "$expect" >expect && | |
16 | git -C "$dir" rev-parse "$@" >actual && | |
17 | test_cmp expect actual | |
18 | } | |
19 | ||
a2f5a876 | 20 | # usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir |
12f7526c | 21 | test_rev_parse () { |
1e043cff | 22 | d= |
1dea0dc9 | 23 | bare= |
e6273f4d | 24 | gitdir= |
1e043cff ES |
25 | while : |
26 | do | |
27 | case "$1" in | |
28 | -C) d="$2"; shift; shift ;; | |
1dea0dc9 ES |
29 | -b) case "$2" in |
30 | [tfu]*) bare="$2"; shift; shift ;; | |
31 | *) error "test_rev_parse: bogus core.bare value '$2'" ;; | |
32 | esac ;; | |
e6273f4d | 33 | -g) gitdir="$2"; shift; shift ;; |
1e043cff ES |
34 | -*) error "test_rev_parse: unrecognized option '$1'" ;; |
35 | *) break ;; | |
36 | esac | |
37 | done | |
38 | ||
dace6e44 ML |
39 | name=$1 |
40 | shift | |
41 | ||
12f7526c ES |
42 | for o in --is-bare-repository \ |
43 | --is-inside-git-dir \ | |
44 | --is-inside-work-tree \ | |
45 | --show-prefix \ | |
a2f5a876 SG |
46 | --git-dir \ |
47 | --absolute-git-dir | |
12f7526c ES |
48 | do |
49 | test $# -eq 0 && break | |
50 | expect="$1" | |
51 | test_expect_success "$name: $o" ' | |
e6273f4d ES |
52 | if test -n "$gitdir" |
53 | then | |
54 | test_when_finished "unset GIT_DIR" && | |
55 | GIT_DIR="$gitdir" && | |
56 | export GIT_DIR | |
57 | fi && | |
58 | ||
1dea0dc9 ES |
59 | case "$bare" in |
60 | t*) test_config ${d:+-C} ${d:+"$d"} core.bare true ;; | |
61 | f*) test_config ${d:+-C} ${d:+"$d"} core.bare false ;; | |
62 | u*) test_unconfig ${d:+-C} ${d:+"$d"} core.bare ;; | |
63 | esac && | |
64 | ||
12f7526c | 65 | echo "$expect" >expect && |
1e043cff | 66 | git ${d:+-C} ${d:+"$d"} rev-parse $o >actual && |
12f7526c ES |
67 | test_cmp expect actual |
68 | ' | |
69 | shift | |
70 | done | |
dace6e44 ML |
71 | } |
72 | ||
8fb3c00d | 73 | ROOT=$(pwd) |
7efeb8f0 | 74 | |
d66f68ff ES |
75 | test_expect_success 'setup' ' |
76 | mkdir -p sub/dir work && | |
fac60b89 | 77 | cp -R .git repo.git && |
78 | git checkout -B main && | |
79 | test_commit abc && | |
80 | git checkout -b side && | |
81 | test_commit def && | |
82 | git checkout main && | |
83 | git worktree add worktree side | |
d66f68ff ES |
84 | ' |
85 | ||
a2f5a876 | 86 | test_rev_parse toplevel false false true '' .git "$ROOT/.git" |
dace6e44 | 87 | |
a2f5a876 SG |
88 | test_rev_parse -C .git .git/ false true false '' . "$ROOT/.git" |
89 | test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" "$ROOT/.git" | |
dace6e44 | 90 | |
a2f5a876 | 91 | test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" "$ROOT/.git" |
dace6e44 | 92 | |
1dea0dc9 | 93 | test_rev_parse -b t 'core.bare = true' true false false |
dace6e44 | 94 | |
1dea0dc9 | 95 | test_rev_parse -b u 'core.bare undefined' false false true |
dace6e44 | 96 | |
dace6e44 | 97 | |
a2f5a876 | 98 | test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true '' "../.git" "$ROOT/.git" |
dace6e44 | 99 | |
e6273f4d | 100 | test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false '' |
dace6e44 | 101 | |
e6273f4d | 102 | test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true '' |
dace6e44 | 103 | |
dace6e44 | 104 | |
a2f5a876 | 105 | test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true '' "../repo.git" "$ROOT/repo.git" |
dace6e44 | 106 | |
e6273f4d | 107 | test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false '' |
dace6e44 | 108 | |
e6273f4d | 109 | test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true '' |
dace6e44 | 110 | |
fac60b89 | 111 | test_expect_success 'rev-parse --path-format=absolute' ' |
112 | test_one "." "$ROOT/.git" --path-format=absolute --git-dir && | |
113 | test_one "." "$ROOT/.git" --path-format=absolute --git-common-dir && | |
114 | test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-dir && | |
115 | test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-common-dir && | |
116 | test_one "worktree" "$ROOT/.git/worktrees/worktree" --path-format=absolute --git-dir && | |
117 | test_one "worktree" "$ROOT/.git" --path-format=absolute --git-common-dir && | |
118 | test_one "." "$ROOT" --path-format=absolute --show-toplevel && | |
119 | test_one "." "$ROOT/.git/objects" --path-format=absolute --git-path objects && | |
120 | test_one "." "$ROOT/.git/objects/foo/bar/baz" --path-format=absolute --git-path objects/foo/bar/baz | |
121 | ' | |
122 | ||
123 | test_expect_success 'rev-parse --path-format=relative' ' | |
124 | test_one "." ".git" --path-format=relative --git-dir && | |
125 | test_one "." ".git" --path-format=relative --git-common-dir && | |
126 | test_one "sub/dir" "../../.git" --path-format=relative --git-dir && | |
127 | test_one "sub/dir" "../../.git" --path-format=relative --git-common-dir && | |
128 | test_one "worktree" "../.git/worktrees/worktree" --path-format=relative --git-dir && | |
129 | test_one "worktree" "../.git" --path-format=relative --git-common-dir && | |
130 | test_one "." "./" --path-format=relative --show-toplevel && | |
131 | test_one "." ".git/objects" --path-format=relative --git-path objects && | |
132 | test_one "." ".git/objects/foo/bar/baz" --path-format=relative --git-path objects/foo/bar/baz | |
133 | ' | |
134 | ||
135 | test_expect_success '--path-format=relative does not affect --absolute-git-dir' ' | |
136 | git rev-parse --path-format=relative --absolute-git-dir >actual && | |
137 | echo "$ROOT/.git" >expect && | |
138 | test_cmp expect actual | |
139 | ' | |
140 | ||
141 | test_expect_success '--path-format can change in the middle of the command line' ' | |
142 | git rev-parse --path-format=absolute --git-dir --path-format=relative --git-path objects/foo/bar >actual && | |
143 | cat >expect <<-EOF && | |
144 | $ROOT/.git | |
145 | .git/objects/foo/bar | |
146 | EOF | |
147 | test_cmp expect actual | |
148 | ' | |
149 | ||
99fc5551 WM |
150 | test_expect_success '--path-format does not segfault without an argument' ' |
151 | test_must_fail git rev-parse --path-format | |
152 | ' | |
153 | ||
5de8a549 MR |
154 | test_expect_success 'git-common-dir from worktree root' ' |
155 | echo .git >expect && | |
156 | git rev-parse --git-common-dir >actual && | |
157 | test_cmp expect actual | |
158 | ' | |
159 | ||
098aa867 | 160 | test_expect_success 'git-common-dir inside sub-dir' ' |
5de8a549 MR |
161 | mkdir -p path/to/child && |
162 | test_when_finished "rm -rf path" && | |
163 | echo "$(git -C path/to/child rev-parse --show-cdup).git" >expect && | |
164 | git -C path/to/child rev-parse --git-common-dir >actual && | |
165 | test_cmp expect actual | |
166 | ' | |
167 | ||
168 | test_expect_success 'git-path from worktree root' ' | |
169 | echo .git/objects >expect && | |
170 | git rev-parse --git-path objects >actual && | |
171 | test_cmp expect actual | |
172 | ' | |
173 | ||
098aa867 | 174 | test_expect_success 'git-path inside sub-dir' ' |
5de8a549 MR |
175 | mkdir -p path/to/child && |
176 | test_when_finished "rm -rf path" && | |
177 | echo "$(git -C path/to/child rev-parse --show-cdup).git/objects" >expect && | |
178 | git -C path/to/child rev-parse --git-path objects >actual && | |
179 | test_cmp expect actual | |
180 | ' | |
181 | ||
417abfde ØW |
182 | test_expect_success 'rev-parse --is-shallow-repository in shallow repo' ' |
183 | test_commit test_commit && | |
184 | echo true >expect && | |
185 | git clone --depth 1 --no-local . shallow && | |
186 | test_when_finished "rm -rf shallow" && | |
187 | git -C shallow rev-parse --is-shallow-repository >actual && | |
188 | test_cmp expect actual | |
189 | ' | |
190 | ||
191 | test_expect_success 'rev-parse --is-shallow-repository in non-shallow repo' ' | |
192 | echo false >expect && | |
193 | git rev-parse --is-shallow-repository >actual && | |
194 | test_cmp expect actual | |
195 | ' | |
196 | ||
2eabd383 | 197 | test_expect_success 'rev-parse --show-object-format in repo' ' |
a48a8801 | 198 | test_oid algo >expect && |
2eabd383 | 199 | git rev-parse --show-object-format >actual && |
200 | test_cmp expect actual && | |
201 | git rev-parse --show-object-format=storage >actual && | |
202 | test_cmp expect actual && | |
203 | git rev-parse --show-object-format=input >actual && | |
204 | test_cmp expect actual && | |
205 | git rev-parse --show-object-format=output >actual && | |
206 | test_cmp expect actual && | |
207 | test_must_fail git rev-parse --show-object-format=squeamish-ossifrage 2>err && | |
208 | grep "unknown mode for --show-object-format: squeamish-ossifrage" err | |
209 | ' | |
210 | ||
2d92ab32 JK |
211 | test_expect_success '--show-toplevel from subdir of working tree' ' |
212 | pwd >expect && | |
213 | git -C sub/dir rev-parse --show-toplevel >actual && | |
214 | test_cmp expect actual | |
215 | ' | |
216 | ||
217 | test_expect_success '--show-toplevel from inside .git' ' | |
218 | test_must_fail git -C .git rev-parse --show-toplevel | |
219 | ' | |
220 | ||
bf0231c6 SB |
221 | test_expect_success 'showing the superproject correctly' ' |
222 | git rev-parse --show-superproject-working-tree >out && | |
223 | test_must_be_empty out && | |
224 | ||
225 | test_create_repo super && | |
226 | test_commit -C super test_commit && | |
227 | test_create_repo sub && | |
228 | test_commit -C sub test_commit && | |
8a96dbcb TB |
229 | git -c protocol.file.allow=always \ |
230 | -C super submodule add ../sub dir/sub && | |
bf0231c6 | 231 | echo $(pwd)/super >expect && |
c5cbb27c SM |
232 | git -C super/dir/sub rev-parse --show-superproject-working-tree >out && |
233 | test_cmp expect out && | |
234 | ||
235 | test_commit -C super submodule_add && | |
236 | git -C super checkout -b branch1 && | |
237 | git -C super/dir/sub checkout -b branch1 && | |
238 | test_commit -C super/dir/sub branch1_commit && | |
239 | git -C super add dir/sub && | |
240 | test_commit -C super branch1_commit && | |
06d53148 JS |
241 | git -C super checkout -b branch2 main && |
242 | git -C super/dir/sub checkout -b branch2 main && | |
c5cbb27c SM |
243 | test_commit -C super/dir/sub branch2_commit && |
244 | git -C super add dir/sub && | |
245 | test_commit -C super branch2_commit && | |
246 | test_must_fail git -C super merge branch1 && | |
247 | ||
bf0231c6 SB |
248 | git -C super/dir/sub rev-parse --show-superproject-working-tree >out && |
249 | test_cmp expect out | |
250 | ' | |
251 | ||
a5cdca45 EW |
252 | # at least one external project depends on this behavior: |
253 | test_expect_success 'rev-parse --since= unsqueezed ordering' ' | |
254 | x1=--since=1970-01-01T00:00:01Z && | |
255 | x2=--since=1970-01-01T00:00:02Z && | |
256 | x3=--since=1970-01-01T00:00:03Z && | |
257 | git rev-parse $x1 $x1 $x3 $x2 >actual && | |
258 | cat >expect <<-EOF && | |
259 | --max-age=1 | |
260 | --max-age=1 | |
261 | --max-age=3 | |
262 | --max-age=2 | |
263 | EOF | |
264 | test_cmp expect actual | |
265 | ' | |
266 | ||
5305474e VD |
267 | test_expect_success 'rev-parse --bisect includes bad, excludes good' ' |
268 | test_commit_bulk 6 && | |
269 | ||
270 | git update-ref refs/bisect/bad-1 HEAD~1 && | |
271 | git update-ref refs/bisect/b HEAD~2 && | |
272 | git update-ref refs/bisect/bad-3 HEAD~3 && | |
273 | git update-ref refs/bisect/good-3 HEAD~3 && | |
274 | git update-ref refs/bisect/bad-4 HEAD~4 && | |
275 | git update-ref refs/bisect/go HEAD~4 && | |
276 | ||
277 | # Note: refs/bisect/b and refs/bisect/go should be ignored because they | |
278 | # do not match the refs/bisect/bad or refs/bisect/good prefixes. | |
279 | cat >expect <<-EOF && | |
280 | refs/bisect/bad-1 | |
281 | refs/bisect/bad-3 | |
282 | refs/bisect/bad-4 | |
283 | ^refs/bisect/good-3 | |
284 | EOF | |
285 | ||
286 | git rev-parse --symbolic-full-name --bisect >actual && | |
287 | test_cmp expect actual | |
288 | ' | |
289 | ||
dace6e44 | 290 | test_done |