Merge branch 'en/keep-cwd' into maint
[git/debian.git] / t / t1512-rev-parse-disambiguation.sh
blobb0119bf8bc8417439bee91bb7c1add21bc460c2e
1 #!/bin/sh
3 test_description='object name disambiguation
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.
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.
13 +-------------------------------------------+
14 | |
15 | .-------b3wettvi---- ad2uee |
16 | / / |
17 | a2onsxbvj---czy8f73t--ioiley5o |
18 | |
19 +-------------------------------------------+
23 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
24 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
26 . ./test-lib.sh
28 if ! test_have_prereq SHA1
29 then
30 skip_all='not using SHA-1 for objects'
31 test_done
34 test_expect_success 'blob and tree' '
35 test_tick &&
37 test_write_lines 0 1 2 3 4 5 6 7 8 9 &&
38 echo &&
39 echo b1rwzyc3
40 ) >a0blgqsjc &&
42 # create one blob 0000000000b36
43 git add a0blgqsjc &&
45 # create one tree 0000000000cdc
46 git write-tree
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
54 test_expect_success 'disambiguate tree-ish' '
55 # feed tree-ish in an unambiguous way
56 git rev-parse --verify 0000000000cdc:a0blgqsjc &&
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
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 @@
72 b1rwzyc3
73 +irwry
74 EOF
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
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)
90 test_expect_success 'first commit' '
91 # create one commit 0000000000e4f
92 git commit -m a2onsxbvj
95 test_expect_success 'disambiguate commit-ish' '
96 # feed commit-ish in an unambiguous way
97 git rev-parse --verify 0000000000e4f^{commit} &&
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} &&
103 # likewise
104 git rev-parse --verify 000000000^0
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)
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...
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..
132 test_expect_success 'git log takes only commit-ish' '
133 # Likewise.
134 git log 000000000
137 test_expect_success 'git reset takes only commit-ish' '
138 # Likewise.
139 git reset 000000000
142 test_expect_success 'first tag' '
143 # create one tag 0000000000f8f
144 git tag -a -m j7cp83um v1.0.0
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.
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} &&
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...
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}
174 test_expect_success 'parse describe name' '
175 # feed an unambiguous describe name
176 git rev-parse --verify v1.0.0-0-g0000000000e4f &&
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
183 test_expect_success 'more history' '
184 # commit 0000000000043
185 git mv a0blgqsjc d12cr3h8t &&
186 echo h62xsjeu >>d12cr3h8t &&
187 git add d12cr3h8t &&
189 test_tick &&
190 git commit -m czy8f73t &&
192 # commit 00000000008ec
193 git mv d12cr3h8t j000jmpzn &&
194 echo j08bekfvt >>j000jmpzn &&
195 git add j000jmpzn &&
197 test_tick &&
198 git commit -m ioiley5o &&
200 # commit 0000000005b0
201 git checkout v1.0.0^0 &&
202 git mv a0blgqsjc f5518nwu &&
204 test_write_lines h62xsjeu j08bekfvt kg7xflhm >>f5518nwu &&
205 git add f5518nwu &&
207 test_tick &&
208 git commit -m b3wettvi &&
209 side=$(git rev-parse HEAD) &&
211 # commit 000000000066
212 git checkout main &&
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 &&
220 test_might_fail git rm -f a0blgqsjc &&
222 git cat-file blob $side:f5518nwu &&
223 echo j3l0i9s6
224 ) >ab2gs879 &&
225 git add ab2gs879 &&
227 test_tick &&
228 git commit -m ad2uee
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 &&
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
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
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} &&
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...
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}
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
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
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
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
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
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
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
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
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
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
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
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}
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 &&
373 commit
374 tree
375 blob
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
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
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
398 test_done