Merge branch 'rs/diff-parseopts-cleanup'
[git/gitster.git] / t / t6120-describe.sh
blobe78315d23d7d32c6db1f9405aa8788862bf37cd5
1 #!/bin/sh
3 test_description='test describe'
5 # o---o-----o----o----o-------o----x
6 # \ D,R e /
7 # \---o-------------o-'
8 # \ B /
9 # `-o----o----o-'
10 # A c
12 # First parent of a merge commit is on the same line, second parent below.
14 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
15 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
17 . ./test-lib.sh
19 check_describe () {
20 indir= &&
21 while test $# != 0
23 case "$1" in
24 -C)
25 indir="$2"
26 shift
29 break
31 esac
32 shift
33 done &&
34 indir=${indir:+"$indir"/} &&
35 expect="$1"
36 shift
37 describe_opts="$@"
38 test_expect_success "describe $describe_opts" '
39 git ${indir:+ -C "$indir"} describe $describe_opts >raw &&
40 sed -e "s/-g[0-9a-f]*\$/-gHASH/" <raw >actual &&
41 echo "$expect" >expect &&
42 test_cmp expect actual
46 test_expect_success setup '
47 test_commit initial file one &&
48 test_commit second file two &&
49 test_commit third file three &&
50 test_commit --annotate A file A &&
51 test_commit c file c &&
53 git reset --hard second &&
54 test_commit --annotate B side B &&
56 test_tick &&
57 git merge -m Merged c &&
58 merged=$(git rev-parse HEAD) &&
60 git reset --hard second &&
61 test_commit --no-tag D another D &&
63 test_tick &&
64 git tag -a -m R R &&
66 test_commit e another DD &&
67 test_commit --no-tag "yet another" another DDD &&
69 test_tick &&
70 git merge -m Merged $merged &&
72 test_commit --no-tag x file
75 check_describe A-8-gHASH HEAD
76 check_describe A-7-gHASH HEAD^
77 check_describe R-2-gHASH HEAD^^
78 check_describe A-3-gHASH HEAD^^2
79 check_describe B HEAD^^2^
80 check_describe R-1-gHASH HEAD^^^
82 check_describe c-7-gHASH --tags HEAD
83 check_describe c-6-gHASH --tags HEAD^
84 check_describe e-1-gHASH --tags HEAD^^
85 check_describe c-2-gHASH --tags HEAD^^2
86 check_describe B --tags HEAD^^2^
87 check_describe e --tags HEAD^^^
88 check_describe e --tags --exact-match HEAD^^^
90 check_describe heads/main --all HEAD
91 check_describe tags/c-6-gHASH --all HEAD^
92 check_describe tags/e --all HEAD^^^
94 check_describe B-0-gHASH --long HEAD^^2^
95 check_describe A-3-gHASH --long HEAD^^2
97 check_describe c-7-gHASH --tags
98 check_describe e-3-gHASH --first-parent --tags
100 check_describe c-7-gHASH --tags --no-exact-match HEAD
101 check_describe e-3-gHASH --first-parent --tags --no-exact-match HEAD
103 test_expect_success '--exact-match failure' '
104 test_must_fail git describe --exact-match HEAD 2>err
107 test_expect_success 'describe --contains defaults to HEAD without commit-ish' '
108 echo "A^0" >expect &&
109 git checkout A &&
110 test_when_finished "git checkout -" &&
111 git describe --contains >actual &&
112 test_cmp expect actual
115 check_describe tags/A --all A^0
117 test_expect_success 'renaming tag A to Q locally produces a warning' "
118 git update-ref refs/tags/Q $(git rev-parse refs/tags/A) &&
119 git update-ref -d refs/tags/A &&
120 git describe HEAD 2>err >out &&
121 cat >expected <<-\EOF &&
122 warning: tag 'Q' is externally known as 'A'
124 test_cmp expected err &&
125 grep -E '^A-8-g[0-9a-f]+$' out
128 test_expect_success 'misnamed annotated tag forces long output' '
129 description=$(git describe --no-long Q^0) &&
130 expr "$description" : "A-0-g[0-9a-f]*$" &&
131 git rev-parse --verify "$description" >actual &&
132 git rev-parse --verify Q^0 >expect &&
133 test_cmp expect actual
136 test_expect_success 'abbrev=0 will not break misplaced tag (1)' '
137 description=$(git describe --abbrev=0 Q^0) &&
138 expr "$description" : "A-0-g[0-9a-f]*$"
141 test_expect_success 'abbrev=0 will not break misplaced tag (2)' '
142 description=$(git describe --abbrev=0 c^0) &&
143 expr "$description" : "A-1-g[0-9a-f]*$"
146 test_expect_success 'rename tag Q back to A' '
147 git update-ref refs/tags/A $(git rev-parse refs/tags/Q) &&
148 git update-ref -d refs/tags/Q
151 test_expect_success 'pack tag refs' 'git pack-refs'
152 check_describe A-8-gHASH HEAD
154 test_expect_success 'describe works from outside repo using --git-dir' '
155 git clone --bare "$TRASH_DIRECTORY" "$TRASH_DIRECTORY/bare" &&
156 git --git-dir "$TRASH_DIRECTORY/bare" describe >out &&
157 grep -E "^A-8-g[0-9a-f]+$" out
160 check_describe "A-8-gHASH" --dirty
162 test_expect_success 'describe --dirty with --work-tree' '
164 cd "$TEST_DIRECTORY" &&
165 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
166 ) &&
167 grep -E "^A-8-g[0-9a-f]+$" out
170 test_expect_success 'set-up dirty work tree' '
171 echo >>file
174 test_expect_success 'describe --dirty with --work-tree (dirty)' '
175 git describe --dirty >expected &&
177 cd "$TEST_DIRECTORY" &&
178 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
179 ) &&
180 grep -E "^A-8-g[0-9a-f]+-dirty$" out &&
181 test_cmp expected out
184 test_expect_success 'describe --dirty=.mod with --work-tree (dirty)' '
185 git describe --dirty=.mod >expected &&
187 cd "$TEST_DIRECTORY" &&
188 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
189 ) &&
190 grep -E "^A-8-g[0-9a-f]+.mod$" out &&
191 test_cmp expected out
194 test_expect_success 'describe --dirty HEAD' '
195 test_must_fail git describe --dirty HEAD
198 test_expect_success 'set-up matching pattern tests' '
199 git tag -a -m test-annotated test-annotated &&
200 echo >>file &&
201 test_tick &&
202 git commit -a -m "one more" &&
203 git tag test1-lightweight &&
204 echo >>file &&
205 test_tick &&
206 git commit -a -m "yet another" &&
207 git tag test2-lightweight &&
208 echo >>file &&
209 test_tick &&
210 git commit -a -m "even more"
214 check_describe "test-annotated-3-gHASH" --match="test-*"
216 check_describe "test1-lightweight-2-gHASH" --tags --match="test1-*"
218 check_describe "test2-lightweight-1-gHASH" --tags --match="test2-*"
220 check_describe "test2-lightweight-0-gHASH" --long --tags --match="test2-*" HEAD^
222 check_describe "test2-lightweight-0-gHASH" --long --tags --match="test1-*" --match="test2-*" HEAD^
224 check_describe "test2-lightweight-0-gHASH" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
226 check_describe "test1-lightweight-2-gHASH" --long --tags --match="test1-*" --match="test3-*" HEAD
228 check_describe "test1-lightweight-2-gHASH" --long --tags --match="test3-*" --match="test1-*" HEAD
230 test_expect_success 'set-up branches' '
231 git branch branch_A A &&
232 git branch branch_C c &&
233 git update-ref refs/remotes/origin/remote_branch_A "A^{commit}" &&
234 git update-ref refs/remotes/origin/remote_branch_C "c^{commit}" &&
235 git update-ref refs/original/original_branch_A test-annotated~2
238 check_describe "heads/branch_A-11-gHASH" --all --match="branch_*" --exclude="branch_C" HEAD
240 check_describe "remotes/origin/remote_branch_A-11-gHASH" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
242 check_describe "original/original_branch_A-6-gHASH" --all test-annotated~1
244 test_expect_success '--match does not work for other types' '
245 test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
248 test_expect_success '--exclude does not work for other types' '
249 R=$(git describe --all --exclude="any_pattern_even_not_matching" test-annotated~1) &&
250 case "$R" in
251 *original_branch_A*) echo "fail: Found unknown reference $R with --exclude"
252 false;;
253 *) echo ok: Found some known type;;
254 esac
257 test_expect_success 'name-rev with exact tags' '
258 echo A >expect &&
259 tag_object=$(git rev-parse refs/tags/A) &&
260 git name-rev --tags --name-only $tag_object >actual &&
261 test_cmp expect actual &&
263 echo "A^0" >expect &&
264 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
265 git name-rev --tags --name-only $tagged_commit >actual &&
266 test_cmp expect actual
269 test_expect_success 'name-rev --all' '
270 >expect.unsorted &&
271 for rev in $(git rev-list --all)
273 git name-rev $rev >>expect.unsorted || return 1
274 done &&
275 sort <expect.unsorted >expect &&
276 git name-rev --all >actual.unsorted &&
277 sort <actual.unsorted >actual &&
278 test_cmp expect actual
281 test_expect_success 'name-rev --annotate-stdin' '
282 >expect.unsorted &&
283 for rev in $(git rev-list --all)
285 name=$(git name-rev --name-only $rev) &&
286 echo "$rev ($name)" >>expect.unsorted || return 1
287 done &&
288 sort <expect.unsorted >expect &&
289 git rev-list --all | git name-rev --annotate-stdin >actual.unsorted &&
290 sort <actual.unsorted >actual &&
291 test_cmp expect actual
294 test_expect_success 'name-rev --stdin deprecated' "
295 git rev-list --all | git name-rev --stdin 2>actual &&
296 grep -E 'warning: --stdin is deprecated' actual
299 test_expect_success 'describe --contains with the exact tags' '
300 echo "A^0" >expect &&
301 tag_object=$(git rev-parse refs/tags/A) &&
302 git describe --contains $tag_object >actual &&
303 test_cmp expect actual &&
305 echo "A^0" >expect &&
306 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
307 git describe --contains $tagged_commit >actual &&
308 test_cmp expect actual
311 test_expect_success 'describe --contains and --match' '
312 echo "A^0" >expect &&
313 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
314 test_must_fail git describe --contains --match="B" $tagged_commit &&
315 git describe --contains --match="B" --match="A" $tagged_commit >actual &&
316 test_cmp expect actual
319 test_expect_success 'describe --exclude' '
320 echo "c~1" >expect &&
321 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
322 test_must_fail git describe --contains --match="B" $tagged_commit &&
323 git describe --contains --match="?" --exclude="A" $tagged_commit >actual &&
324 test_cmp expect actual
327 test_expect_success 'describe --contains and --no-match' '
328 echo "A^0" >expect &&
329 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
330 git describe --contains --match="B" --no-match $tagged_commit >actual &&
331 test_cmp expect actual
334 test_expect_success 'setup and absorb a submodule' '
335 test_create_repo sub1 &&
336 test_commit -C sub1 initial &&
337 git submodule add ./sub1 &&
338 git submodule absorbgitdirs &&
339 git commit -a -m "add submodule" &&
340 git describe --dirty >expect &&
341 git describe --broken >out &&
342 test_cmp expect out
345 test_expect_success 'describe chokes on severely broken submodules' '
346 mv .git/modules/sub1/ .git/modules/sub_moved &&
347 test_must_fail git describe --dirty
350 test_expect_success 'describe ignoring a broken submodule' '
351 git describe --broken >out &&
352 grep broken out
355 test_expect_success 'describe with --work-tree ignoring a broken submodule' '
357 cd "$TEST_DIRECTORY" &&
358 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --broken >"$TRASH_DIRECTORY/out"
359 ) &&
360 test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
361 grep broken out
364 test_expect_success 'describe a blob at a directly tagged commit' '
365 echo "make it a unique blob" >file &&
366 git add file && git commit -m "content in file" &&
367 git tag -a -m "latest annotated tag" unique-file &&
368 git describe HEAD:file >actual &&
369 echo "unique-file:file" >expect &&
370 test_cmp expect actual
373 test_expect_success 'describe a blob with its first introduction' '
374 git commit --allow-empty -m "empty commit" &&
375 git rm file &&
376 git commit -m "delete blob" &&
377 git revert HEAD &&
378 git commit --allow-empty -m "empty commit" &&
379 git describe HEAD:file >actual &&
380 echo "unique-file:file" >expect &&
381 test_cmp expect actual
384 test_expect_success 'describe directly tagged blob' '
385 git tag test-blob unique-file:file &&
386 git describe test-blob >actual &&
387 echo "unique-file:file" >expect &&
388 # suboptimal: we rather want to see "test-blob"
389 test_cmp expect actual
392 test_expect_success 'describe tag object' '
393 git tag test-blob-1 -a -m msg unique-file:file &&
394 test_must_fail git describe test-blob-1 2>actual &&
395 test_grep "fatal: test-blob-1 is neither a commit nor blob" actual
398 test_expect_success ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
399 i=1 &&
400 while test $i -lt 8000
402 echo "commit refs/heads/main
403 committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
404 data <<EOF
405 commit #$i
406 EOF" &&
407 if test $i = 1
408 then
409 echo "from refs/heads/main^0"
410 fi &&
411 i=$(($i + 1)) || return 1
412 done | git fast-import &&
413 git checkout main &&
414 git tag far-far-away HEAD^ &&
415 echo "HEAD~4000 tags/far-far-away~3999" >expect &&
416 git name-rev HEAD~4000 >actual &&
417 test_cmp expect actual &&
418 run_with_limited_stack git name-rev HEAD~4000 >actual &&
419 test_cmp expect actual
422 test_expect_success ULIMIT_STACK_SIZE 'describe works in a deep repo' '
423 git tag -f far-far-away HEAD~7999 &&
424 echo "far-far-away" >expect &&
425 git describe --tags --abbrev=0 HEAD~4000 >actual &&
426 test_cmp expect actual &&
427 run_with_limited_stack git describe --tags --abbrev=0 HEAD~4000 >actual &&
428 test_cmp expect actual
431 check_describe tags/A --all A
432 check_describe tags/c --all c
433 check_describe heads/branch_A --all --match='branch_*' branch_A
435 test_expect_success 'describe complains about tree object' '
436 test_must_fail git describe HEAD^{tree}
439 test_expect_success 'describe complains about missing object' '
440 test_must_fail git describe $ZERO_OID
443 test_expect_success 'name-rev a rev shortly after epoch' '
444 test_when_finished "git checkout main" &&
446 git checkout --orphan no-timestamp-underflow &&
447 # Any date closer to epoch than the CUTOFF_DATE_SLOP constant
448 # in builtin/name-rev.c.
449 GIT_COMMITTER_DATE="@1234 +0000" \
450 git commit -m "committer date shortly after epoch" &&
451 old_commit_oid=$(git rev-parse HEAD) &&
453 echo "$old_commit_oid no-timestamp-underflow" >expect &&
454 git name-rev $old_commit_oid >actual &&
455 test_cmp expect actual
458 # A--------------main
459 # \ /
460 # \----------M2
461 # \ /
462 # \---M1-C
463 # \ /
465 test_expect_success 'name-rev covers all conditions while looking at parents' '
466 git init repo &&
468 cd repo &&
470 echo A >file &&
471 git add file &&
472 git commit -m A &&
473 A=$(git rev-parse HEAD) &&
475 git checkout --detach &&
476 echo B >file &&
477 git commit -m B file &&
478 B=$(git rev-parse HEAD) &&
480 git checkout $A &&
481 git merge --no-ff $B && # M1
483 echo C >file &&
484 git commit -m C file &&
486 git checkout $A &&
487 git merge --no-ff HEAD@{1} && # M2
489 git checkout main &&
490 git merge --no-ff HEAD@{1} &&
492 echo "$B main^2^2~1^2" >expect &&
493 git name-rev $B >actual &&
495 test_cmp expect actual
499 # A-B-C-D-E-main
501 # Where C has a non-monotonically increasing commit timestamp w.r.t. other
502 # commits
503 test_expect_success 'non-monotonic commit dates setup' '
504 UNIX_EPOCH_ZERO="@0 +0000" &&
505 git init non-monotonic &&
506 test_commit -C non-monotonic A &&
507 test_commit -C non-monotonic --no-tag B &&
508 test_commit -C non-monotonic --no-tag --date "$UNIX_EPOCH_ZERO" C &&
509 test_commit -C non-monotonic D &&
510 test_commit -C non-monotonic E
513 test_expect_success 'name-rev with commitGraph handles non-monotonic timestamps' '
514 test_config -C non-monotonic core.commitGraph true &&
516 cd non-monotonic &&
518 git commit-graph write --reachable &&
520 echo "main~3 tags/D~2" >expect &&
521 git name-rev --tags main~3 >actual &&
523 test_cmp expect actual
527 test_expect_success 'name-rev --all works with non-monotonic timestamps' '
528 test_config -C non-monotonic core.commitGraph false &&
530 cd non-monotonic &&
532 rm -rf .git/info/commit-graph* &&
534 cat >tags <<-\EOF &&
535 tags/E
536 tags/D
537 tags/D~1
538 tags/D~2
539 tags/A
542 git log --pretty=%H >revs &&
544 paste -d" " revs tags | sort >expect &&
546 git name-rev --tags --all | sort >actual &&
547 test_cmp expect actual
551 test_expect_success 'name-rev --annotate-stdin works with non-monotonic timestamps' '
552 test_config -C non-monotonic core.commitGraph false &&
554 cd non-monotonic &&
556 rm -rf .git/info/commit-graph* &&
558 cat >expect <<-\EOF &&
566 git log --pretty=%H >revs &&
567 git name-rev --tags --annotate-stdin --name-only <revs >actual &&
568 test_cmp expect actual
572 test_expect_success 'name-rev --all works with commitGraph' '
573 test_config -C non-monotonic core.commitGraph true &&
575 cd non-monotonic &&
577 git commit-graph write --reachable &&
579 cat >tags <<-\EOF &&
580 tags/E
581 tags/D
582 tags/D~1
583 tags/D~2
584 tags/A
587 git log --pretty=%H >revs &&
589 paste -d" " revs tags | sort >expect &&
591 git name-rev --tags --all | sort >actual &&
592 test_cmp expect actual
596 test_expect_success 'name-rev --annotate-stdin works with commitGraph' '
597 test_config -C non-monotonic core.commitGraph true &&
599 cd non-monotonic &&
601 git commit-graph write --reachable &&
603 cat >expect <<-\EOF &&
611 git log --pretty=%H >revs &&
612 git name-rev --tags --annotate-stdin --name-only <revs >actual &&
613 test_cmp expect actual
620 # o-----o---o----x
623 test_expect_success 'setup: describe commits with disjoint bases' '
624 git init disjoint1 &&
626 cd disjoint1 &&
628 echo o >> file && git add file && git commit -m o &&
629 echo A >> file && git add file && git commit -m A &&
630 git tag A -a -m A &&
631 echo o >> file && git add file && git commit -m o &&
633 git checkout --orphan branch && rm file &&
634 echo B > file2 && git add file2 && git commit -m B &&
635 git tag B -a -m B &&
636 git merge --no-ff --allow-unrelated-histories main -m x
640 check_describe -C disjoint1 "A-3-gHASH" HEAD
643 # o---o---o------------.
645 # o---o---x
648 test_expect_success 'setup: describe commits with disjoint bases 2' '
649 git init disjoint2 &&
651 cd disjoint2 &&
653 echo A >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:00" git commit -m A &&
654 git tag A -a -m A &&
655 echo o >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:01" git commit -m o &&
657 git checkout --orphan branch &&
658 echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:00" git commit -m o &&
659 echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:01" git commit -m o &&
660 echo B >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:02" git commit -m B &&
661 git tag B -a -m B &&
662 git merge --no-ff --allow-unrelated-histories main -m x
666 check_describe -C disjoint2 "B-3-gHASH" HEAD
668 test_expect_success 'setup misleading taggerdates' '
669 GIT_COMMITTER_DATE="2006-12-12 12:31" git tag -a -m "another tag" newer-tag-older-commit unique-file~1
672 check_describe newer-tag-older-commit~1 --contains unique-file~2
674 test_done