]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1512-rev-parse-disambiguation.sh
Merge branch 'en/fetch-negotiation-default-fix'
[thirdparty/git.git] / t / t1512-rev-parse-disambiguation.sh
1 #!/bin/sh
2
3 test_description='object name disambiguation
4
5 Create blobs, trees, commits and a tag that all share the same
6 prefix, and make sure "git rev-parse" can take advantage of
7 type information to disambiguate short object names that are
8 not necessarily unique.
9
10 The final history used in the test has five commits, with the bottom
11 one tagged as v1.0.0. They all have one regular file each.
12
13 +-------------------------------------------+
14 | |
15 | .-------b3wettvi---- ad2uee |
16 | / / |
17 | a2onsxbvj---czy8f73t--ioiley5o |
18 | |
19 +-------------------------------------------+
20
21 '
22
23 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
24 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
25
26 . ./test-lib.sh
27
28 if ! test_have_prereq SHA1
29 then
30 skip_all='not using SHA-1 for objects'
31 test_done
32 fi
33
34 test_expect_success 'blob and tree' '
35 test_tick &&
36 (
37 test_write_lines 0 1 2 3 4 5 6 7 8 9 &&
38 echo &&
39 echo b1rwzyc3
40 ) >a0blgqsjc &&
41
42 # create one blob 0000000000b36
43 git add a0blgqsjc &&
44
45 # create one tree 0000000000cdc
46 git write-tree
47 '
48
49 test_expect_success 'warn ambiguity when no candidate matches type hint' '
50 test_must_fail git rev-parse --verify 000000000^{commit} 2>actual &&
51 test_i18ngrep "short object ID 000000000 is ambiguous" actual
52 '
53
54 test_expect_success 'disambiguate tree-ish' '
55 # feed tree-ish in an unambiguous way
56 git rev-parse --verify 0000000000cdc:a0blgqsjc &&
57
58 # ambiguous at the object name level, but there is only one
59 # such tree-ish (the other is a blob)
60 git rev-parse --verify 000000000:a0blgqsjc
61 '
62
63 test_expect_success 'disambiguate blob' '
64 sed -e "s/|$//" >patch <<-EOF &&
65 diff --git a/frotz b/frotz
66 index 000000000..ffffff 100644
67 --- a/frotz
68 +++ b/frotz
69 @@ -10,3 +10,4 @@
70 9
71 |
72 b1rwzyc3
73 +irwry
74 EOF
75 (
76 GIT_INDEX_FILE=frotz &&
77 export GIT_INDEX_FILE &&
78 git apply --build-fake-ancestor frotz patch &&
79 git cat-file blob :frotz >actual
80 ) &&
81 test_cmp a0blgqsjc actual
82 '
83
84 test_expect_success 'disambiguate tree' '
85 commit=$(echo "d7xm" | git commit-tree 000000000) &&
86 # this commit is fffff2e and not ambiguous with the 00000* objects
87 test $(git rev-parse $commit^{tree}) = $(git rev-parse 0000000000cdc)
88 '
89
90 test_expect_success 'first commit' '
91 # create one commit 0000000000e4f
92 git commit -m a2onsxbvj
93 '
94
95 test_expect_success 'disambiguate commit-ish' '
96 # feed commit-ish in an unambiguous way
97 git rev-parse --verify 0000000000e4f^{commit} &&
98
99 # ambiguous at the object name level, but there is only one
100 # such commit (the others are tree and blob)
101 git rev-parse --verify 000000000^{commit} &&
102
103 # likewise
104 git rev-parse --verify 000000000^0
105 '
106
107 test_expect_success 'disambiguate commit' '
108 commit=$(echo "hoaxj" | git commit-tree 0000000000cdc -p 000000000) &&
109 # this commit is ffffffd8 and not ambiguous with the 00000* objects
110 test $(git rev-parse $commit^) = $(git rev-parse 0000000000e4f)
111 '
112
113 test_expect_success 'log name1..name2 takes only commit-ishes on both ends' '
114 # These are underspecified from the prefix-length point of view
115 # to disambiguate the commit with other objects, but there is only
116 # one commit that has 00000* prefix at this point.
117 git log 000000000..000000000 &&
118 git log ..000000000 &&
119 git log 000000000.. &&
120 git log 000000000...000000000 &&
121 git log ...000000000 &&
122 git log 000000000...
123 '
124
125 test_expect_success 'rev-parse name1..name2 takes only commit-ishes on both ends' '
126 # Likewise.
127 git rev-parse 000000000..000000000 &&
128 git rev-parse ..000000000 &&
129 git rev-parse 000000000..
130 '
131
132 test_expect_success 'git log takes only commit-ish' '
133 # Likewise.
134 git log 000000000
135 '
136
137 test_expect_success 'git reset takes only commit-ish' '
138 # Likewise.
139 git reset 000000000
140 '
141
142 test_expect_success 'first tag' '
143 # create one tag 0000000000f8f
144 git tag -a -m j7cp83um v1.0.0
145 '
146
147 test_expect_failure 'two semi-ambiguous commit-ish' '
148 # At this point, we have a tag 0000000000f8f that points
149 # at a commit 0000000000e4f, and a tree and a blob that
150 # share 0000000000 prefix with these tag and commit.
151 #
152 # Once the parser becomes ultra-smart, it could notice that
153 # 0000000000 before ^{commit} name many different objects, but
154 # that only two (HEAD and v1.0.0 tag) can be peeled to commit,
155 # and that peeling them down to commit yield the same commit
156 # without ambiguity.
157 git rev-parse --verify 0000000000^{commit} &&
158
159 # likewise
160 git log 0000000000..0000000000 &&
161 git log ..0000000000 &&
162 git log 0000000000.. &&
163 git log 0000000000...0000000000 &&
164 git log ...0000000000 &&
165 git log 0000000000...
166 '
167
168 test_expect_failure 'three semi-ambiguous tree-ish' '
169 # Likewise for tree-ish. HEAD, v1.0.0 and HEAD^{tree} share
170 # the prefix but peeling them to tree yields the same thing
171 git rev-parse --verify 0000000000^{tree}
172 '
173
174 test_expect_success 'parse describe name' '
175 # feed an unambiguous describe name
176 git rev-parse --verify v1.0.0-0-g0000000000e4f &&
177
178 # ambiguous at the object name level, but there is only one
179 # such commit (others are blob, tree and tag)
180 git rev-parse --verify v1.0.0-0-g000000000
181 '
182
183 test_expect_success 'more history' '
184 # commit 0000000000043
185 git mv a0blgqsjc d12cr3h8t &&
186 echo h62xsjeu >>d12cr3h8t &&
187 git add d12cr3h8t &&
188
189 test_tick &&
190 git commit -m czy8f73t &&
191
192 # commit 00000000008ec
193 git mv d12cr3h8t j000jmpzn &&
194 echo j08bekfvt >>j000jmpzn &&
195 git add j000jmpzn &&
196
197 test_tick &&
198 git commit -m ioiley5o &&
199
200 # commit 0000000005b0
201 git checkout v1.0.0^0 &&
202 git mv a0blgqsjc f5518nwu &&
203
204 test_write_lines h62xsjeu j08bekfvt kg7xflhm >>f5518nwu &&
205 git add f5518nwu &&
206
207 test_tick &&
208 git commit -m b3wettvi &&
209 side=$(git rev-parse HEAD) &&
210
211 # commit 000000000066
212 git checkout main &&
213
214 # If you use recursive, merge will fail and you will need to
215 # clean up a0blgqsjc as well. If you use resolve, merge will
216 # succeed.
217 test_might_fail git merge --no-commit -s recursive $side &&
218 git rm -f f5518nwu j000jmpzn &&
219
220 test_might_fail git rm -f a0blgqsjc &&
221 (
222 git cat-file blob $side:f5518nwu &&
223 echo j3l0i9s6
224 ) >ab2gs879 &&
225 git add ab2gs879 &&
226
227 test_tick &&
228 git commit -m ad2uee
229
230 '
231
232 test_expect_failure 'parse describe name taking advantage of generation' '
233 # ambiguous at the object name level, but there is only one
234 # such commit at generation 0
235 git rev-parse --verify v1.0.0-0-g000000000 &&
236
237 # likewise for generation 2 and 4
238 git rev-parse --verify v1.0.0-2-g000000000 &&
239 git rev-parse --verify v1.0.0-4-g000000000
240 '
241
242 # Note: because rev-parse does not even try to disambiguate based on
243 # the generation number, this test currently succeeds for a wrong
244 # reason. When it learns to use the generation number, the previous
245 # test should succeed, and also this test should fail because the
246 # describe name used in the test with generation number can name two
247 # commits. Make sure that such a future enhancement does not randomly
248 # pick one.
249 test_expect_success 'parse describe name not ignoring ambiguity' '
250 # ambiguous at the object name level, and there are two such
251 # commits at generation 1
252 test_must_fail git rev-parse --verify v1.0.0-1-g000000000
253 '
254
255 test_expect_success 'ambiguous commit-ish' '
256 # Now there are many commits that begin with the
257 # common prefix, none of these should pick one at
258 # random. They all should result in ambiguity errors.
259 test_must_fail git rev-parse --verify 00000000^{commit} &&
260
261 # likewise
262 test_must_fail git log 000000000..000000000 &&
263 test_must_fail git log ..000000000 &&
264 test_must_fail git log 000000000.. &&
265 test_must_fail git log 000000000...000000000 &&
266 test_must_fail git log ...000000000 &&
267 test_must_fail git log 000000000...
268 '
269
270 # There are three objects with this prefix: a blob, a tree, and a tag. We know
271 # the blob will not pass as a treeish, but the tree and tag should (and thus
272 # cause an error).
273 test_expect_success 'ambiguous tags peel to treeish' '
274 test_must_fail git rev-parse 0000000000f^{tree}
275 '
276
277 test_expect_success 'rev-parse --disambiguate' '
278 # The test creates 16 objects that share the prefix and two
279 # commits created by commit-tree in earlier tests share a
280 # different prefix.
281 git rev-parse --disambiguate=000000000 >actual &&
282 test_line_count = 16 actual &&
283 test "$(sed -e "s/^\(.........\).*/\1/" actual | sort -u)" = 000000000
284 '
285
286 test_expect_success 'rev-parse --disambiguate drops duplicates' '
287 git rev-parse --disambiguate=000000000 >expect &&
288 git pack-objects .git/objects/pack/pack <expect &&
289 git rev-parse --disambiguate=000000000 >actual &&
290 test_cmp expect actual
291 '
292
293 test_expect_success 'ambiguous 40-hex ref' '
294 TREE=$(git mktree </dev/null) &&
295 REF=$(git rev-parse HEAD) &&
296 VAL=$(git commit-tree $TREE </dev/null) &&
297 git update-ref refs/heads/$REF $VAL &&
298 test $(git rev-parse $REF 2>err) = $REF &&
299 grep "refname.*${REF}.*ambiguous" err
300 '
301
302 test_expect_success 'ambiguous short sha1 ref' '
303 TREE=$(git mktree </dev/null) &&
304 REF=$(git rev-parse --short HEAD) &&
305 VAL=$(git commit-tree $TREE </dev/null) &&
306 git update-ref refs/heads/$REF $VAL &&
307 test $(git rev-parse $REF 2>err) = $VAL &&
308 grep "refname.*${REF}.*ambiguous" err
309 '
310
311 test_expect_success 'ambiguity errors are not repeated (raw)' '
312 test_must_fail git rev-parse 00000 2>stderr &&
313 grep "is ambiguous" stderr >errors &&
314 test_line_count = 1 errors
315 '
316
317 test_expect_success 'ambiguity errors are not repeated (treeish)' '
318 test_must_fail git rev-parse 00000:foo 2>stderr &&
319 grep "is ambiguous" stderr >errors &&
320 test_line_count = 1 errors
321 '
322
323 test_expect_success 'ambiguity errors are not repeated (peel)' '
324 test_must_fail git rev-parse 00000^{commit} 2>stderr &&
325 grep "is ambiguous" stderr >errors &&
326 test_line_count = 1 errors
327 '
328
329 test_expect_success 'ambiguity hints' '
330 test_must_fail git rev-parse 000000000 2>stderr &&
331 grep ^hint: stderr >hints &&
332 # 16 candidates, plus one intro line
333 test_line_count = 17 hints
334 '
335
336 test_expect_success 'ambiguity hints respect type' '
337 test_must_fail git rev-parse 000000000^{commit} 2>stderr &&
338 grep ^hint: stderr >hints &&
339 # 5 commits, 1 tag (which is a committish), plus intro line
340 test_line_count = 7 hints
341 '
342
343 test_expect_success 'failed type-selector still shows hint' '
344 # these two blobs share the same prefix "ee3d", but neither
345 # will pass for a commit
346 echo 851 | git hash-object --stdin -w &&
347 echo 872 | git hash-object --stdin -w &&
348 test_must_fail git rev-parse ee3d^{commit} 2>stderr &&
349 grep ^hint: stderr >hints &&
350 test_line_count = 3 hints
351 '
352
353 test_expect_success 'core.disambiguate config can prefer types' '
354 # ambiguous between tree and tag
355 sha1=0000000000f &&
356 test_must_fail git rev-parse $sha1 &&
357 git rev-parse $sha1^{commit} &&
358 git -c core.disambiguate=committish rev-parse $sha1
359 '
360
361 test_expect_success 'core.disambiguate does not override context' '
362 # treeish ambiguous between tag and tree
363 test_must_fail \
364 git -c core.disambiguate=committish rev-parse $sha1^{tree}
365 '
366
367 test_expect_success 'ambiguous commits are printed by type first, then hash order' '
368 test_must_fail git rev-parse 0000 2>stderr &&
369 grep ^hint: stderr >hints &&
370 grep 0000 hints >objects &&
371 cat >expected <<-\EOF &&
372 tag
373 commit
374 tree
375 blob
376 EOF
377 awk "{print \$3}" <objects >objects.types &&
378 uniq <objects.types >objects.types.uniq &&
379 test_cmp expected objects.types.uniq &&
380 for type in tag commit tree blob
381 do
382 grep $type objects >$type.objects &&
383 sort $type.objects >$type.objects.sorted &&
384 test_cmp $type.objects.sorted $type.objects || return 1
385 done
386 '
387
388 test_expect_success 'cat-file --batch and --batch-check show ambiguous' '
389 echo "0000 ambiguous" >expect &&
390 echo 0000 | git cat-file --batch-check >actual 2>err &&
391 test_cmp expect actual &&
392 test_i18ngrep hint: err &&
393 echo 0000 | git cat-file --batch >actual 2>err &&
394 test_cmp expect actual &&
395 test_i18ngrep hint: err
396 '
397
398 test_done