3 test_description
='test describe'
5 # o---o-----o----o----o-------o----x
7 # \---o-------------o-'
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_PASSES_SANITIZE_LEAK
=true
35 indir
=${indir:+"$indir"/} &&
39 test_expect_success
"describe $describe_opts" '
40 git ${indir:+ -C "$indir"} describe $describe_opts >raw &&
41 sed -e "s/-g[0-9a-f]*\$/-gHASH/" <raw >actual &&
42 echo "$expect" >expect &&
43 test_cmp expect actual
47 test_expect_success setup
'
48 test_commit initial file one &&
49 test_commit second file two &&
50 test_commit third file three &&
51 test_commit --annotate A file A &&
52 test_commit c file c &&
54 git reset --hard second &&
55 test_commit --annotate B side B &&
58 git merge -m Merged c &&
59 merged=$(git rev-parse HEAD) &&
61 git reset --hard second &&
62 test_commit --no-tag D another D &&
67 test_commit e another DD &&
68 test_commit --no-tag "yet another" another DDD &&
71 git merge -m Merged $merged &&
73 test_commit --no-tag x file
76 check_describe A-8-gHASH HEAD
77 check_describe A-7-gHASH HEAD^
78 check_describe R-2-gHASH HEAD^^
79 check_describe A-3-gHASH HEAD^^
2
80 check_describe B HEAD^^
2^
81 check_describe R-1-gHASH HEAD^^^
83 check_describe c-7-gHASH
--tags HEAD
84 check_describe c-6-gHASH
--tags HEAD^
85 check_describe e-1-gHASH
--tags HEAD^^
86 check_describe c-2-gHASH
--tags HEAD^^
2
87 check_describe B
--tags HEAD^^
2^
88 check_describe e
--tags HEAD^^^
89 check_describe e
--tags --exact-match HEAD^^^
91 check_describe
heads
/main
--all HEAD
92 check_describe tags
/c-6-gHASH
--all HEAD^
93 check_describe tags
/e
--all HEAD^^^
95 check_describe B-0-gHASH
--long HEAD^^
2^
96 check_describe A-3-gHASH
--long HEAD^^
2
98 check_describe c-7-gHASH
--tags
99 check_describe e-3-gHASH
--first-parent --tags
101 check_describe c-7-gHASH
--tags --no-exact-match HEAD
102 check_describe e-3-gHASH
--first-parent --tags --no-exact-match HEAD
104 test_expect_success
'--exact-match failure' '
105 test_must_fail git describe --exact-match HEAD 2>err
108 test_expect_success
'describe --contains defaults to HEAD without commit-ish' '
109 echo "A^0" >expect &&
111 test_when_finished "git checkout -" &&
112 git describe --contains >actual &&
113 test_cmp expect actual
116 check_describe tags
/A
--all A^
0
118 test_expect_success
'renaming tag A to Q locally produces a warning' "
119 git update-ref refs/tags/Q $(git rev-parse refs/tags/A) &&
120 git update-ref -d refs/tags/A &&
121 git describe HEAD 2>err >out &&
122 cat >expected <<-\EOF &&
123 warning: tag 'Q' is externally known as 'A'
125 test_cmp expected err &&
126 grep -E '^A-8-g[0-9a-f]+$' out
129 test_expect_success
'misnamed annotated tag forces long output' '
130 description=$(git describe --no-long Q^0) &&
131 expr "$description" : "A-0-g[0-9a-f]*$" &&
132 git rev-parse --verify "$description" >actual &&
133 git rev-parse --verify Q^0 >expect &&
134 test_cmp expect actual
137 test_expect_success
'abbrev=0 will not break misplaced tag (1)' '
138 description=$(git describe --abbrev=0 Q^0) &&
139 expr "$description" : "A-0-g[0-9a-f]*$"
142 test_expect_success
'abbrev=0 will not break misplaced tag (2)' '
143 description=$(git describe --abbrev=0 c^0) &&
144 expr "$description" : "A-1-g[0-9a-f]*$"
147 test_expect_success
'rename tag Q back to A' '
148 git update-ref refs/tags/A $(git rev-parse refs/tags/Q) &&
149 git update-ref -d refs/tags/Q
152 test_expect_success
'pack tag refs' 'git pack-refs'
153 check_describe A-8-gHASH HEAD
155 test_expect_success
'describe works from outside repo using --git-dir' '
156 git clone --bare "$TRASH_DIRECTORY" "$TRASH_DIRECTORY/bare" &&
157 git --git-dir "$TRASH_DIRECTORY/bare" describe >out &&
158 grep -E "^A-8-g[0-9a-f]+$" out
161 check_describe
"A-8-gHASH" --dirty
163 test_expect_success
'describe --dirty with --work-tree' '
165 cd "$TEST_DIRECTORY" &&
166 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
168 grep -E "^A-8-g[0-9a-f]+$" out
171 test_expect_success
'set-up dirty work tree' '
175 test_expect_success
'describe --dirty with --work-tree (dirty)' '
176 git describe --dirty >expected &&
178 cd "$TEST_DIRECTORY" &&
179 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
181 grep -E "^A-8-g[0-9a-f]+-dirty$" out &&
182 test_cmp expected out
185 test_expect_success
'describe --dirty=.mod with --work-tree (dirty)' '
186 git describe --dirty=.mod >expected &&
188 cd "$TEST_DIRECTORY" &&
189 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
191 grep -E "^A-8-g[0-9a-f]+.mod$" out &&
192 test_cmp expected out
195 test_expect_success
'describe --dirty HEAD' '
196 test_must_fail git describe --dirty HEAD
199 test_expect_success
'set-up matching pattern tests' '
200 git tag -a -m test-annotated test-annotated &&
203 git commit -a -m "one more" &&
204 git tag test1-lightweight &&
207 git commit -a -m "yet another" &&
208 git tag test2-lightweight &&
211 git commit -a -m "even more"
215 check_describe
"test-annotated-3-gHASH" --match="test-*"
217 check_describe
"test1-lightweight-2-gHASH" --tags --match="test1-*"
219 check_describe
"test2-lightweight-1-gHASH" --tags --match="test2-*"
221 check_describe
"test2-lightweight-0-gHASH" --long --tags --match="test2-*" HEAD^
223 check_describe
"test2-lightweight-0-gHASH" --long --tags --match="test1-*" --match="test2-*" HEAD^
225 check_describe
"test2-lightweight-0-gHASH" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
227 check_describe
"test1-lightweight-2-gHASH" --long --tags --match="test1-*" --match="test3-*" HEAD
229 check_describe
"test1-lightweight-2-gHASH" --long --tags --match="test3-*" --match="test1-*" HEAD
231 test_expect_success
'set-up branches' '
232 git branch branch_A A &&
233 git branch branch_C c &&
234 git update-ref refs/remotes/origin/remote_branch_A "A^{commit}" &&
235 git update-ref refs/remotes/origin/remote_branch_C "c^{commit}" &&
236 git update-ref refs/original/original_branch_A test-annotated~2
239 check_describe
"heads/branch_A-11-gHASH" --all --match="branch_*" --exclude="branch_C" HEAD
241 check_describe
"remotes/origin/remote_branch_A-11-gHASH" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
243 check_describe
"original/original_branch_A-6-gHASH" --all test-annotated~
1
245 test_expect_success
'--match does not work for other types' '
246 test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
249 test_expect_success
'--exclude does not work for other types' '
250 R=$(git describe --all --exclude="any_pattern_even_not_matching" test-annotated~1) &&
252 *original_branch_A*) echo "fail: Found unknown reference $R with --exclude"
254 *) echo ok: Found some known type;;
258 test_expect_success
'name-rev with exact tags' '
260 tag_object=$(git rev-parse refs/tags/A) &&
261 git name-rev --tags --name-only $tag_object >actual &&
262 test_cmp expect actual &&
264 echo "A^0" >expect &&
265 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
266 git name-rev --tags --name-only $tagged_commit >actual &&
267 test_cmp expect actual
270 test_expect_success
'name-rev --all' '
272 for rev in $(git rev-list --all)
274 git name-rev $rev >>expect.unsorted || return 1
276 sort <expect.unsorted >expect &&
277 git name-rev --all >actual.unsorted &&
278 sort <actual.unsorted >actual &&
279 test_cmp expect actual
282 test_expect_success
'name-rev --annotate-stdin' '
284 for rev in $(git rev-list --all)
286 name=$(git name-rev --name-only $rev) &&
287 echo "$rev ($name)" >>expect.unsorted || return 1
289 sort <expect.unsorted >expect &&
290 git rev-list --all | git name-rev --annotate-stdin >actual.unsorted &&
291 sort <actual.unsorted >actual &&
292 test_cmp expect actual
295 test_expect_success
'name-rev --stdin deprecated' "
296 git rev-list --all | git name-rev --stdin 2>actual &&
297 grep -E 'warning: --stdin is deprecated' actual
300 test_expect_success
'describe --contains with the exact tags' '
301 echo "A^0" >expect &&
302 tag_object=$(git rev-parse refs/tags/A) &&
303 git describe --contains $tag_object >actual &&
304 test_cmp expect actual &&
306 echo "A^0" >expect &&
307 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
308 git describe --contains $tagged_commit >actual &&
309 test_cmp expect actual
312 test_expect_success
'describe --contains and --match' '
313 echo "A^0" >expect &&
314 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
315 test_must_fail git describe --contains --match="B" $tagged_commit &&
316 git describe --contains --match="B" --match="A" $tagged_commit >actual &&
317 test_cmp expect actual
320 test_expect_success
'describe --exclude' '
321 echo "c~1" >expect &&
322 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
323 test_must_fail git describe --contains --match="B" $tagged_commit &&
324 git describe --contains --match="?" --exclude="A" $tagged_commit >actual &&
325 test_cmp expect actual
328 test_expect_success
'describe --contains and --no-match' '
329 echo "A^0" >expect &&
330 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
331 git describe --contains --match="B" --no-match $tagged_commit >actual &&
332 test_cmp expect actual
335 test_expect_success
'setup and absorb a submodule' '
336 test_create_repo sub1 &&
337 test_commit -C sub1 initial &&
338 git submodule add ./sub1 &&
339 git submodule absorbgitdirs &&
340 git commit -a -m "add submodule" &&
341 git describe --dirty >expect &&
342 git describe --broken >out &&
346 test_expect_success
'describe chokes on severely broken submodules' '
347 mv .git/modules/sub1/ .git/modules/sub_moved &&
348 test_must_fail git describe --dirty
351 test_expect_success
'describe ignoring a broken submodule' '
352 git describe --broken >out &&
356 test_expect_success
'describe with --work-tree ignoring a broken submodule' '
358 cd "$TEST_DIRECTORY" &&
359 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --broken >"$TRASH_DIRECTORY/out"
361 test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
365 test_expect_success
'describe a blob at a directly tagged commit' '
366 echo "make it a unique blob" >file &&
367 git add file && git commit -m "content in file" &&
368 git tag -a -m "latest annotated tag" unique-file &&
369 git describe HEAD:file >actual &&
370 echo "unique-file:file" >expect &&
371 test_cmp expect actual
374 test_expect_success
'describe a blob with its first introduction' '
375 git commit --allow-empty -m "empty commit" &&
377 git commit -m "delete blob" &&
379 git commit --allow-empty -m "empty commit" &&
380 git describe HEAD:file >actual &&
381 echo "unique-file:file" >expect &&
382 test_cmp expect actual
385 test_expect_success
'describe directly tagged blob' '
386 git tag test-blob unique-file:file &&
387 git describe test-blob >actual &&
388 echo "unique-file:file" >expect &&
389 # suboptimal: we rather want to see "test-blob"
390 test_cmp expect actual
393 test_expect_success
'describe tag object' '
394 git tag test-blob-1 -a -m msg unique-file:file &&
395 test_must_fail git describe test-blob-1 2>actual &&
396 test_grep "fatal: test-blob-1 is neither a commit nor blob" actual
399 test_expect_success ULIMIT_STACK_SIZE
'name-rev works in a deep repo' '
401 while test $i -lt 8000
403 echo "commit refs/heads/main
404 committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
410 echo "from refs/heads/main^0"
412 i=$(($i + 1)) || return 1
413 done | git fast-import &&
415 git tag far-far-away HEAD^ &&
416 echo "HEAD~4000 tags/far-far-away~3999" >expect &&
417 git name-rev HEAD~4000 >actual &&
418 test_cmp expect actual &&
419 run_with_limited_stack git name-rev HEAD~4000 >actual &&
420 test_cmp expect actual
423 test_expect_success ULIMIT_STACK_SIZE
'describe works in a deep repo' '
424 git tag -f far-far-away HEAD~7999 &&
425 echo "far-far-away" >expect &&
426 git describe --tags --abbrev=0 HEAD~4000 >actual &&
427 test_cmp expect actual &&
428 run_with_limited_stack git describe --tags --abbrev=0 HEAD~4000 >actual &&
429 test_cmp expect actual
432 check_describe tags
/A
--all A
433 check_describe tags
/c
--all c
434 check_describe
heads
/branch_A
--all --match='branch_*' branch_A
436 test_expect_success
'describe complains about tree object' '
437 test_must_fail git describe HEAD^{tree}
440 test_expect_success
'describe complains about missing object' '
441 test_must_fail git describe $ZERO_OID
444 test_expect_success
'name-rev a rev shortly after epoch' '
445 test_when_finished "git checkout main" &&
447 git checkout --orphan no-timestamp-underflow &&
448 # Any date closer to epoch than the CUTOFF_DATE_SLOP constant
449 # in builtin/name-rev.c.
450 GIT_COMMITTER_DATE="@1234 +0000" \
451 git commit -m "committer date shortly after epoch" &&
452 old_commit_oid=$(git rev-parse HEAD) &&
454 echo "$old_commit_oid no-timestamp-underflow" >expect &&
455 git name-rev $old_commit_oid >actual &&
456 test_cmp expect actual
459 # A--------------main
466 test_expect_success
'name-rev covers all conditions while looking at parents' '
474 A=$(git rev-parse HEAD) &&
476 git checkout --detach &&
478 git commit -m B file &&
479 B=$(git rev-parse HEAD) &&
482 git merge --no-ff $B && # M1
485 git commit -m C file &&
488 git merge --no-ff HEAD@{1} && # M2
491 git merge --no-ff HEAD@{1} &&
493 echo "$B main^2^2~1^2" >expect &&
494 git name-rev $B >actual &&
496 test_cmp expect actual
502 # Where C has a non-monotonically increasing commit timestamp w.r.t. other
504 test_expect_success
'non-monotonic commit dates setup' '
505 UNIX_EPOCH_ZERO="@0 +0000" &&
506 git init non-monotonic &&
507 test_commit -C non-monotonic A &&
508 test_commit -C non-monotonic --no-tag B &&
509 test_commit -C non-monotonic --no-tag --date "$UNIX_EPOCH_ZERO" C &&
510 test_commit -C non-monotonic D &&
511 test_commit -C non-monotonic E
514 test_expect_success
'name-rev with commitGraph handles non-monotonic timestamps' '
515 test_config -C non-monotonic core.commitGraph true &&
519 git commit-graph write --reachable &&
521 echo "main~3 tags/D~2" >expect &&
522 git name-rev --tags main~3 >actual &&
524 test_cmp expect actual
528 test_expect_success
'name-rev --all works with non-monotonic timestamps' '
529 test_config -C non-monotonic core.commitGraph false &&
533 rm -rf .git/info/commit-graph* &&
543 git log --pretty=%H >revs &&
545 paste -d" " revs tags | sort >expect &&
547 git name-rev --tags --all | sort >actual &&
548 test_cmp expect actual
552 test_expect_success
'name-rev --annotate-stdin works with non-monotonic timestamps' '
553 test_config -C non-monotonic core.commitGraph false &&
557 rm -rf .git/info/commit-graph* &&
559 cat >expect <<-\EOF &&
567 git log --pretty=%H >revs &&
568 git name-rev --tags --annotate-stdin --name-only <revs >actual &&
569 test_cmp expect actual
573 test_expect_success
'name-rev --all works with commitGraph' '
574 test_config -C non-monotonic core.commitGraph true &&
578 git commit-graph write --reachable &&
588 git log --pretty=%H >revs &&
590 paste -d" " revs tags | sort >expect &&
592 git name-rev --tags --all | sort >actual &&
593 test_cmp expect actual
597 test_expect_success
'name-rev --annotate-stdin works with commitGraph' '
598 test_config -C non-monotonic core.commitGraph true &&
602 git commit-graph write --reachable &&
604 cat >expect <<-\EOF &&
612 git log --pretty=%H >revs &&
613 git name-rev --tags --annotate-stdin --name-only <revs >actual &&
614 test_cmp expect actual
624 test_expect_success
'setup: describe commits with disjoint bases' '
625 git init disjoint1 &&
629 echo o >> file && git add file && git commit -m o &&
630 echo A >> file && git add file && git commit -m A &&
632 echo o >> file && git add file && git commit -m o &&
634 git checkout --orphan branch && rm file &&
635 echo B > file2 && git add file2 && git commit -m B &&
637 git merge --no-ff --allow-unrelated-histories main -m x
641 check_describe
-C disjoint1
"A-3-gHASH" HEAD
644 # o---o---o------------.
649 test_expect_success
'setup: describe commits with disjoint bases 2' '
650 git init disjoint2 &&
654 echo A >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:00" git commit -m A &&
656 echo o >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:01" git commit -m o &&
658 git checkout --orphan branch &&
659 echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:00" git commit -m o &&
660 echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:01" git commit -m o &&
661 echo B >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:02" git commit -m B &&
663 git merge --no-ff --allow-unrelated-histories main -m x
667 check_describe
-C disjoint2
"B-3-gHASH" HEAD
669 test_expect_success
'setup misleading taggerdates' '
670 GIT_COMMITTER_DATE="2006-12-12 12:31" git tag -a -m "another tag" newer-tag-older-commit unique-file~1
673 check_describe newer-tag-older-commit~
1 --contains unique-file~
2
675 test_expect_success
'describe --dirty with a file with changed stat' '
676 test_when_finished "rm -fr stat-dirty" &&
677 git init stat-dirty &&
687 test-tool chmtime -10 file &&
688 git describe --dirty >actual &&
689 test_cmp expect actual
693 test_expect_success
'describe --broken --dirty with a file with changed stat' '
694 test_when_finished "rm -fr stat-dirty" &&
695 git init stat-dirty &&
705 test-tool chmtime -10 file &&
706 git describe --dirty --broken >actual &&
707 test_cmp expect actual