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
23 test_expect_success
"describe $describe_opts" '
24 R=$(git describe $describe_opts 2>err.actual) &&
26 $expect) echo happy ;;
27 *) echo "Oops - $R is not $expect" &&
33 test_expect_success setup
'
34 test_commit initial file one &&
35 test_commit second file two &&
36 test_commit third file three &&
37 test_commit --annotate A file A &&
38 test_commit c file c &&
40 git reset --hard second &&
41 test_commit --annotate B side B &&
44 git merge -m Merged c &&
45 merged=$(git rev-parse HEAD) &&
47 git reset --hard second &&
48 test_commit --no-tag D another D &&
53 test_commit e another DD &&
54 test_commit --no-tag "yet another" another DDD &&
57 git merge -m Merged $merged &&
59 test_commit --no-tag x file
62 check_describe A-
* HEAD
63 check_describe A-
* HEAD^
64 check_describe R-
* HEAD^^
65 check_describe A-
* HEAD^^
2
66 check_describe B HEAD^^
2^
67 check_describe R-
* HEAD^^^
69 check_describe c-
* --tags HEAD
70 check_describe c-
* --tags HEAD^
71 check_describe e-
* --tags HEAD^^
72 check_describe c-
* --tags HEAD^^
2
73 check_describe B
--tags HEAD^^
2^
74 check_describe e
--tags HEAD^^^
76 check_describe
heads
/main
--all HEAD
77 check_describe tags
/c-
* --all HEAD^
78 check_describe tags
/e
--all HEAD^^^
80 check_describe B-0-
* --long HEAD^^
2^
81 check_describe A-3-
* --long HEAD^^
2
83 check_describe c-7-
* --tags
84 check_describe e-3-
* --first-parent --tags
86 test_expect_success
'describe --contains defaults to HEAD without commit-ish' '
89 test_when_finished "git checkout -" &&
90 git describe --contains >actual &&
91 test_cmp expect actual
94 check_describe tags
/A
--all A^
0
95 test_expect_success
'no warning was displayed for A' '
96 test_must_be_empty err.actual
99 test_expect_success
'rename tag A to Q locally' '
100 mv .git/refs/tags/A .git/refs/tags/Q
102 cat - >err.expect
<<EOF
103 warning: tag 'Q' is externally known as 'A'
105 check_describe A-
* HEAD
106 test_expect_success
'warning was displayed for Q' '
107 test_cmp err.expect err.actual
109 test_expect_success
'misnamed annotated tag forces long output' '
110 description=$(git describe --no-long Q^0) &&
111 expr "$description" : "A-0-g[0-9a-f]*$" &&
112 git rev-parse --verify "$description" >actual &&
113 git rev-parse --verify Q^0 >expect &&
114 test_cmp expect actual
117 test_expect_success
'abbrev=0 will not break misplaced tag (1)' '
118 description=$(git describe --abbrev=0 Q^0) &&
119 expr "$description" : "A-0-g[0-9a-f]*$"
122 test_expect_success
'abbrev=0 will not break misplaced tag (2)' '
123 description=$(git describe --abbrev=0 c^0) &&
124 expr "$description" : "A-1-g[0-9a-f]*$"
127 test_expect_success
'rename tag Q back to A' '
128 mv .git/refs/tags/Q .git/refs/tags/A
131 test_expect_success
'pack tag refs' 'git pack-refs'
132 check_describe A-
* HEAD
134 test_expect_success
'describe works from outside repo using --git-dir' '
135 git clone --bare "$TRASH_DIRECTORY" "$TRASH_DIRECTORY/bare" &&
136 git --git-dir "$TRASH_DIRECTORY/bare" describe >out &&
137 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
140 check_describe
"A-*[0-9a-f]" --dirty
142 test_expect_success
'describe --dirty with --work-tree' '
144 cd "$TEST_DIRECTORY" &&
145 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
147 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
150 test_expect_success
'set-up dirty work tree' '
154 check_describe
"A-*[0-9a-f]-dirty" --dirty
156 test_expect_success
'describe --dirty with --work-tree (dirty)' '
158 cd "$TEST_DIRECTORY" &&
159 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
161 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+-dirty$" out
164 check_describe
"A-*[0-9a-f].mod" --dirty=.mod
166 test_expect_success
'describe --dirty=.mod with --work-tree (dirty)' '
168 cd "$TEST_DIRECTORY" &&
169 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
171 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+.mod$" out
174 test_expect_success
'describe --dirty HEAD' '
175 test_must_fail git describe --dirty HEAD
178 test_expect_success
'set-up matching pattern tests' '
179 git tag -a -m test-annotated test-annotated &&
182 git commit -a -m "one more" &&
183 git tag test1-lightweight &&
186 git commit -a -m "yet another" &&
187 git tag test2-lightweight &&
190 git commit -a -m "even more"
194 check_describe
"test-annotated-*" --match="test-*"
196 check_describe
"test1-lightweight-*" --tags --match="test1-*"
198 check_describe
"test2-lightweight-*" --tags --match="test2-*"
200 check_describe
"test2-lightweight-*" --long --tags --match="test2-*" HEAD^
202 check_describe
"test2-lightweight-*" --long --tags --match="test1-*" --match="test2-*" HEAD^
204 check_describe
"test2-lightweight-*" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
206 check_describe
"test1-lightweight-*" --long --tags --match="test1-*" --match="test3-*" HEAD
208 check_describe
"test1-lightweight-*" --long --tags --match="test3-*" --match="test1-*" HEAD
210 test_expect_success
'set-up branches' '
211 git branch branch_A A &&
212 git branch branch_C c &&
213 git update-ref refs/remotes/origin/remote_branch_A "A^{commit}" &&
214 git update-ref refs/remotes/origin/remote_branch_C "c^{commit}" &&
215 git update-ref refs/original/original_branch_A test-annotated~2
218 check_describe
"heads/branch_A*" --all --match="branch_*" --exclude="branch_C" HEAD
220 check_describe
"remotes/origin/remote_branch_A*" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
222 check_describe
"original/original_branch_A*" --all test-annotated~
1
224 test_expect_success
'--match does not work for other types' '
225 test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
228 test_expect_success
'--exclude does not work for other types' '
229 R=$(git describe --all --exclude="any_pattern_even_not_matching" test-annotated~1) &&
231 *original_branch_A*) echo "fail: Found unknown reference $R with --exclude"
233 *) echo ok: Found some known type;;
237 test_expect_success
'name-rev with exact tags' '
239 tag_object=$(git rev-parse refs/tags/A) &&
240 git name-rev --tags --name-only $tag_object >actual &&
241 test_cmp expect actual &&
243 echo "A^0" >expect &&
244 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
245 git name-rev --tags --name-only $tagged_commit >actual &&
246 test_cmp expect actual
249 test_expect_success
'name-rev --all' '
251 for rev in $(git rev-list --all)
253 git name-rev $rev >>expect.unsorted
255 sort <expect.unsorted >expect &&
256 git name-rev --all >actual.unsorted &&
257 sort <actual.unsorted >actual &&
258 test_cmp expect actual
261 test_expect_success
'name-rev --stdin' '
263 for rev in $(git rev-list --all)
265 name=$(git name-rev --name-only $rev) &&
266 echo "$rev ($name)" >>expect.unsorted
268 sort <expect.unsorted >expect &&
269 git rev-list --all | git name-rev --stdin >actual.unsorted &&
270 sort <actual.unsorted >actual &&
271 test_cmp expect actual
274 test_expect_success
'describe --contains with the exact tags' '
275 echo "A^0" >expect &&
276 tag_object=$(git rev-parse refs/tags/A) &&
277 git describe --contains $tag_object >actual &&
278 test_cmp expect actual &&
280 echo "A^0" >expect &&
281 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
282 git describe --contains $tagged_commit >actual &&
283 test_cmp expect actual
286 test_expect_success
'describe --contains and --match' '
287 echo "A^0" >expect &&
288 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
289 test_must_fail git describe --contains --match="B" $tagged_commit &&
290 git describe --contains --match="B" --match="A" $tagged_commit >actual &&
291 test_cmp expect actual
294 test_expect_success
'describe --exclude' '
295 echo "c~1" >expect &&
296 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
297 test_must_fail git describe --contains --match="B" $tagged_commit &&
298 git describe --contains --match="?" --exclude="A" $tagged_commit >actual &&
299 test_cmp expect actual
302 test_expect_success
'describe --contains and --no-match' '
303 echo "A^0" >expect &&
304 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
305 git describe --contains --match="B" --no-match $tagged_commit >actual &&
306 test_cmp expect actual
309 test_expect_success
'setup and absorb a submodule' '
310 test_create_repo sub1 &&
311 test_commit -C sub1 initial &&
312 git submodule add ./sub1 &&
313 git submodule absorbgitdirs &&
314 git commit -a -m "add submodule" &&
315 git describe --dirty >expect &&
316 git describe --broken >out &&
320 test_expect_success
'describe chokes on severely broken submodules' '
321 mv .git/modules/sub1/ .git/modules/sub_moved &&
322 test_must_fail git describe --dirty
325 test_expect_success
'describe ignoring a broken submodule' '
326 git describe --broken >out &&
330 test_expect_success
'describe with --work-tree ignoring a broken submodule' '
332 cd "$TEST_DIRECTORY" &&
333 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --broken >"$TRASH_DIRECTORY/out"
335 test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
339 test_expect_success
'describe a blob at a directly tagged commit' '
340 echo "make it a unique blob" >file &&
341 git add file && git commit -m "content in file" &&
342 git tag -a -m "latest annotated tag" unique-file &&
343 git describe HEAD:file >actual &&
344 echo "unique-file:file" >expect &&
345 test_cmp expect actual
348 test_expect_success
'describe a blob with its first introduction' '
349 git commit --allow-empty -m "empty commit" &&
351 git commit -m "delete blob" &&
353 git commit --allow-empty -m "empty commit" &&
354 git describe HEAD:file >actual &&
355 echo "unique-file:file" >expect &&
356 test_cmp expect actual
359 test_expect_success
'describe directly tagged blob' '
360 git tag test-blob unique-file:file &&
361 git describe test-blob >actual &&
362 echo "unique-file:file" >expect &&
363 # suboptimal: we rather want to see "test-blob"
364 test_cmp expect actual
367 test_expect_success
'describe tag object' '
368 git tag test-blob-1 -a -m msg unique-file:file &&
369 test_must_fail git describe test-blob-1 2>actual &&
370 test_i18ngrep "fatal: test-blob-1 is neither a commit nor blob" actual
373 test_expect_success ULIMIT_STACK_SIZE
'name-rev works in a deep repo' '
375 while test $i -lt 8000
377 echo "commit refs/heads/main
378 committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
382 test $i = 1 && echo "from refs/heads/main^0"
384 done | git fast-import &&
386 git tag far-far-away HEAD^ &&
387 echo "HEAD~4000 tags/far-far-away~3999" >expect &&
388 git name-rev HEAD~4000 >actual &&
389 test_cmp expect actual &&
390 run_with_limited_stack git name-rev HEAD~4000 >actual &&
391 test_cmp expect actual
394 test_expect_success ULIMIT_STACK_SIZE
'describe works in a deep repo' '
395 git tag -f far-far-away HEAD~7999 &&
396 echo "far-far-away" >expect &&
397 git describe --tags --abbrev=0 HEAD~4000 >actual &&
398 test_cmp expect actual &&
399 run_with_limited_stack git describe --tags --abbrev=0 HEAD~4000 >actual &&
400 test_cmp expect actual
403 check_describe tags
/A
--all A
404 check_describe tags
/c
--all c
405 check_describe
heads
/branch_A
--all --match='branch_*' branch_A
407 test_expect_success
'describe complains about tree object' '
408 test_must_fail git describe HEAD^{tree}
411 test_expect_success
'describe complains about missing object' '
412 test_must_fail git describe $ZERO_OID
415 test_expect_success
'name-rev a rev shortly after epoch' '
416 test_when_finished "git checkout main" &&
418 git checkout --orphan no-timestamp-underflow &&
419 # Any date closer to epoch than the CUTOFF_DATE_SLOP constant
420 # in builtin/name-rev.c.
421 GIT_COMMITTER_DATE="@1234 +0000" \
422 git commit -m "committer date shortly after epoch" &&
423 old_commit_oid=$(git rev-parse HEAD) &&
425 echo "$old_commit_oid no-timestamp-underflow" >expect &&
426 git name-rev $old_commit_oid >actual &&
427 test_cmp expect actual
430 # A--------------main
437 test_expect_success
'name-rev covers all conditions while looking at parents' '
445 A=$(git rev-parse HEAD) &&
447 git checkout --detach &&
449 git commit -m B file &&
450 B=$(git rev-parse HEAD) &&
453 git merge --no-ff $B && # M1
456 git commit -m C file &&
459 git merge --no-ff HEAD@{1} && # M2
462 git merge --no-ff HEAD@{1} &&
464 echo "$B main^2^2~1^2" >expect &&
465 git name-rev $B >actual &&
467 test_cmp expect actual
477 test_expect_success
'describe commits with disjoint bases' '
478 git init disjoint1 &&
482 echo o >> file && git add file && git commit -m o &&
483 echo A >> file && git add file && git commit -m A &&
485 echo o >> file && git add file && git commit -m o &&
487 git checkout --orphan branch && rm file &&
488 echo B > file2 && git add file2 && git commit -m B &&
490 git merge --no-ff --allow-unrelated-histories main -m x &&
492 check_describe "A-3-*" HEAD
497 # o---o---o------------.
502 test_expect_success
'describe commits with disjoint bases 2' '
503 git init disjoint2 &&
507 echo A >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:00" git commit -m A &&
509 echo o >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:01" git commit -m o &&
511 git checkout --orphan branch &&
512 echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:00" git commit -m o &&
513 echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:01" git commit -m o &&
514 echo B >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:02" git commit -m B &&
516 git merge --no-ff --allow-unrelated-histories main -m x &&
518 check_describe "B-3-*" HEAD