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 '
36 echo one >file && git add file && git commit -m initial &&
37 one=$(git rev-parse HEAD) &&
39 git describe --always HEAD &&
42 echo two >file && git add file && git commit -m second &&
43 two=$(git rev-parse HEAD) &&
46 echo three >file && git add file && git commit -m third &&
49 echo A >file && git add file && git commit -m A &&
54 echo c >file && git add file && git commit -m c &&
58 git reset --hard $two &&
60 echo B >side && git add side && git commit -m B &&
65 git merge -m Merged c &&
66 merged=$(git rev-parse HEAD) &&
68 git reset --hard $two &&
70 echo D >another && git add another && git commit -m D &&
77 echo DD >another && git commit -a -m another &&
83 echo DDD >another && git commit -a -m "yet another" &&
86 git merge -m Merged $merged &&
89 echo X >file && echo X >side && git add file side &&
94 check_describe A-* HEAD
95 check_describe A-* HEAD^
96 check_describe R-* HEAD^^
97 check_describe A-* HEAD^^2
98 check_describe B HEAD^^2^
99 check_describe R-* HEAD^^^
101 check_describe c-* --tags HEAD
102 check_describe c-* --tags HEAD^
103 check_describe e-* --tags HEAD^^
104 check_describe c-* --tags HEAD^^2
105 check_describe B --tags HEAD^^2^
106 check_describe e --tags HEAD^^^
108 check_describe heads/main --all HEAD
109 check_describe tags/c-* --all HEAD^
110 check_describe tags/e --all HEAD^^^
112 check_describe B-0-* --long HEAD^^2^
113 check_describe A-3-* --long HEAD^^2
115 check_describe c-7-* --tags
116 check_describe e-3-* --first-parent --tags
118 test_expect_success 'describe --contains defaults to HEAD without commit-ish' '
119 echo "A^0" >expect &&
121 test_when_finished "git checkout -" &&
122 git describe --contains >actual &&
123 test_cmp expect actual
126 check_describe tags/A --all A^0
127 test_expect_success 'no warning was displayed for A' '
128 test_must_be_empty err.actual
131 test_expect_success 'rename tag A to Q locally' '
132 mv .git/refs/tags/A .git/refs/tags/Q
134 cat - >err.expect <<EOF
135 warning: tag 'Q' is externally known as 'A'
137 check_describe A-* HEAD
138 test_expect_success 'warning was displayed for Q' '
139 test_i18ncmp err.expect err.actual
141 test_expect_success 'misnamed annotated tag forces long output' '
142 description=$(git describe --no-long Q^0) &&
143 expr "$description" : "A-0-g[0-9a-f]*$" &&
144 git rev-parse --verify "$description" >actual &&
145 git rev-parse --verify Q^0 >expect &&
146 test_cmp expect actual
149 test_expect_success 'abbrev=0 will not break misplaced tag (1)' '
150 description=$(git describe --abbrev=0 Q^0) &&
151 expr "$description" : "A-0-g[0-9a-f]*$"
154 test_expect_success 'abbrev=0 will not break misplaced tag (2)' '
155 description=$(git describe --abbrev=0 c^0) &&
156 expr "$description" : "A-1-g[0-9a-f]*$"
159 test_expect_success 'rename tag Q back to A' '
160 mv .git/refs/tags/Q .git/refs/tags/A
163 test_expect_success 'pack tag refs' 'git pack-refs'
164 check_describe A-* HEAD
166 test_expect_success 'describe works from outside repo using --git-dir' '
167 git clone --bare "$TRASH_DIRECTORY" "$TRASH_DIRECTORY/bare" &&
168 git --git-dir "$TRASH_DIRECTORY/bare" describe >out &&
169 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
172 check_describe "A-*[0-9a-f]" --dirty
174 test_expect_success 'describe --dirty with --work-tree' '
176 cd "$TEST_DIRECTORY" &&
177 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
179 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
182 test_expect_success 'set-up dirty work tree' '
186 check_describe "A-*[0-9a-f]-dirty" --dirty
188 test_expect_success 'describe --dirty with --work-tree (dirty)' '
190 cd "$TEST_DIRECTORY" &&
191 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
193 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+-dirty$" out
196 check_describe "A-*[0-9a-f].mod" --dirty=.mod
198 test_expect_success 'describe --dirty=.mod with --work-tree (dirty)' '
200 cd "$TEST_DIRECTORY" &&
201 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
203 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+.mod$" out
206 test_expect_success 'describe --dirty HEAD' '
207 test_must_fail git describe --dirty HEAD
210 test_expect_success 'set-up matching pattern tests' '
211 git tag -a -m test-annotated test-annotated &&
214 git commit -a -m "one more" &&
215 git tag test1-lightweight &&
218 git commit -a -m "yet another" &&
219 git tag test2-lightweight &&
222 git commit -a -m "even more"
226 check_describe "test-annotated-*" --match="test-*"
228 check_describe "test1-lightweight-*" --tags --match="test1-*"
230 check_describe "test2-lightweight-*" --tags --match="test2-*"
232 check_describe "test2-lightweight-*" --long --tags --match="test2-*" HEAD^
234 check_describe "test2-lightweight-*" --long --tags --match="test1-*" --match="test2-*" HEAD^
236 check_describe "test2-lightweight-*" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
238 check_describe "test1-lightweight-*" --long --tags --match="test1-*" --match="test3-*" HEAD
240 check_describe "test1-lightweight-*" --long --tags --match="test3-*" --match="test1-*" HEAD
242 test_expect_success 'set-up branches' '
243 git branch branch_A A &&
244 git branch branch_C c &&
245 git update-ref refs/remotes/origin/remote_branch_A "A^{commit}" &&
246 git update-ref refs/remotes/origin/remote_branch_C "c^{commit}" &&
247 git update-ref refs/original/original_branch_A test-annotated~2
250 check_describe "heads/branch_A*" --all --match="branch_*" --exclude="branch_C" HEAD
252 check_describe "remotes/origin/remote_branch_A*" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
254 check_describe "original/original_branch_A*" --all test-annotated~1
256 test_expect_success '--match does not work for other types' '
257 test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
260 test_expect_success '--exclude does not work for other types' '
261 R=$(git describe --all --exclude="any_pattern_even_not_matching" test-annotated~1) &&
263 *original_branch_A*) echo "fail: Found unknown reference $R with --exclude"
265 *) echo ok: Found some known type;;
269 test_expect_success 'name-rev with exact tags' '
271 tag_object=$(git rev-parse refs/tags/A) &&
272 git name-rev --tags --name-only $tag_object >actual &&
273 test_cmp expect actual &&
275 echo "A^0" >expect &&
276 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
277 git name-rev --tags --name-only $tagged_commit >actual &&
278 test_cmp expect actual
281 test_expect_success 'name-rev --all' '
283 for rev in $(git rev-list --all)
285 git name-rev $rev >>expect.unsorted
287 sort <expect.unsorted >expect &&
288 git name-rev --all >actual.unsorted &&
289 sort <actual.unsorted >actual &&
290 test_cmp expect actual
293 test_expect_success 'name-rev --stdin' '
295 for rev in $(git rev-list --all)
297 name=$(git name-rev --name-only $rev) &&
298 echo "$rev ($name)" >>expect.unsorted
300 sort <expect.unsorted >expect &&
301 git rev-list --all | git name-rev --stdin >actual.unsorted &&
302 sort <actual.unsorted >actual &&
303 test_cmp expect actual
306 test_expect_success 'describe --contains with the exact tags' '
307 echo "A^0" >expect &&
308 tag_object=$(git rev-parse refs/tags/A) &&
309 git describe --contains $tag_object >actual &&
310 test_cmp expect actual &&
312 echo "A^0" >expect &&
313 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
314 git describe --contains $tagged_commit >actual &&
315 test_cmp expect actual
318 test_expect_success 'describe --contains and --match' '
319 echo "A^0" >expect &&
320 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
321 test_must_fail git describe --contains --match="B" $tagged_commit &&
322 git describe --contains --match="B" --match="A" $tagged_commit >actual &&
323 test_cmp expect actual
326 test_expect_success 'describe --exclude' '
327 echo "c~1" >expect &&
328 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
329 test_must_fail git describe --contains --match="B" $tagged_commit &&
330 git describe --contains --match="?" --exclude="A" $tagged_commit >actual &&
331 test_cmp expect actual
334 test_expect_success 'describe --contains and --no-match' '
335 echo "A^0" >expect &&
336 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
337 git describe --contains --match="B" --no-match $tagged_commit >actual &&
338 test_cmp expect actual
341 test_expect_success 'setup and absorb a submodule' '
342 test_create_repo sub1 &&
343 test_commit -C sub1 initial &&
344 git submodule add ./sub1 &&
345 git submodule absorbgitdirs &&
346 git commit -a -m "add submodule" &&
347 git describe --dirty >expect &&
348 git describe --broken >out &&
352 test_expect_success 'describe chokes on severely broken submodules' '
353 mv .git/modules/sub1/ .git/modules/sub_moved &&
354 test_must_fail git describe --dirty
357 test_expect_success 'describe ignoring a broken submodule' '
358 git describe --broken >out &&
362 test_expect_success 'describe with --work-tree ignoring a broken submodule' '
364 cd "$TEST_DIRECTORY" &&
365 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --broken >"$TRASH_DIRECTORY/out"
367 test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
371 test_expect_success 'describe a blob at a directly tagged commit' '
372 echo "make it a unique blob" >file &&
373 git add file && git commit -m "content in file" &&
374 git tag -a -m "latest annotated tag" unique-file &&
375 git describe HEAD:file >actual &&
376 echo "unique-file:file" >expect &&
377 test_cmp expect actual
380 test_expect_success 'describe a blob with its first introduction' '
381 git commit --allow-empty -m "empty commit" &&
383 git commit -m "delete blob" &&
385 git commit --allow-empty -m "empty commit" &&
386 git describe HEAD:file >actual &&
387 echo "unique-file:file" >expect &&
388 test_cmp expect actual
391 test_expect_success 'describe directly tagged blob' '
392 git tag test-blob unique-file:file &&
393 git describe test-blob >actual &&
394 echo "unique-file:file" >expect &&
395 # suboptimal: we rather want to see "test-blob"
396 test_cmp expect actual
399 test_expect_success 'describe tag object' '
400 git tag test-blob-1 -a -m msg unique-file:file &&
401 test_must_fail git describe test-blob-1 2>actual &&
402 test_i18ngrep "fatal: test-blob-1 is neither a commit nor blob" actual
405 test_expect_success ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
407 while test $i -lt 8000
409 echo "commit refs/heads/main
410 committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
414 test $i = 1 && echo "from refs/heads/main^0"
416 done | git fast-import &&
418 git tag far-far-away HEAD^ &&
419 echo "HEAD~4000 tags/far-far-away~3999" >expect &&
420 git name-rev HEAD~4000 >actual &&
421 test_cmp expect actual &&
422 run_with_limited_stack git name-rev HEAD~4000 >actual &&
423 test_cmp expect actual
426 test_expect_success ULIMIT_STACK_SIZE 'describe works in a deep repo' '
427 git tag -f far-far-away HEAD~7999 &&
428 echo "far-far-away" >expect &&
429 git describe --tags --abbrev=0 HEAD~4000 >actual &&
430 test_cmp expect actual &&
431 run_with_limited_stack git describe --tags --abbrev=0 HEAD~4000 >actual &&
432 test_cmp expect actual
435 check_describe tags/A --all A
436 check_describe tags/c --all c
437 check_describe heads/branch_A --all --match='branch_*' branch_A
439 test_expect_success 'describe complains about tree object' '
440 test_must_fail git describe HEAD^{tree}
443 test_expect_success 'describe complains about missing object' '
444 test_must_fail git describe $ZERO_OID
447 test_expect_success 'name-rev a rev shortly after epoch' '
448 test_when_finished "git checkout main" &&
450 git checkout --orphan no-timestamp-underflow &&
451 # Any date closer to epoch than the CUTOFF_DATE_SLOP constant
452 # in builtin/name-rev.c.
453 GIT_COMMITTER_DATE="@1234 +0000" \
454 git commit -m "committer date shortly after epoch" &&
455 old_commit_oid=$(git rev-parse HEAD) &&
457 echo "$old_commit_oid no-timestamp-underflow" >expect &&
458 git name-rev $old_commit_oid >actual &&
459 test_cmp expect actual
462 # A--------------main
469 test_expect_success 'name-rev covers all conditions while looking at parents' '
477 A=$(git rev-parse HEAD) &&
479 git checkout --detach &&
481 git commit -m B file &&
482 B=$(git rev-parse HEAD) &&
485 git merge --no-ff $B && # M1
488 git commit -m C file &&
491 git merge --no-ff HEAD@{1} && # M2
494 git merge --no-ff HEAD@{1} &&
496 echo "$B main^2^2~1^2" >expect &&
497 git name-rev $B >actual &&
499 test_cmp expect actual
509 test_expect_success 'describe commits with disjoint bases' '
510 git init disjoint1 &&
514 echo o >> file && git add file && git commit -m o &&
515 echo A >> file && git add file && git commit -m A &&
517 echo o >> file && git add file && git commit -m o &&
519 git checkout --orphan branch && rm file &&
520 echo B > file2 && git add file2 && git commit -m B &&
522 git merge --no-ff --allow-unrelated-histories main -m x &&
524 check_describe "A-3-*" HEAD
529 # o---o---o------------.
534 test_expect_success 'describe commits with disjoint bases 2' '
535 git init disjoint2 &&
539 echo A >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:00" git commit -m A &&
541 echo o >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:01" git commit -m o &&
543 git checkout --orphan branch &&
544 echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:00" git commit -m o &&
545 echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:01" git commit -m o &&
546 echo B >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:02" git commit -m B &&
548 git merge --no-ff --allow-unrelated-histories main -m x &&
550 check_describe "B-3-*" HEAD