]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6120-describe.sh
Merge branch 'ab/detox-gettext-tests'
[thirdparty/git.git] / t / t6120-describe.sh
CommitLineData
5312ab11
JH
1#!/bin/sh
2
d91ce887
SG
3test_description='test describe'
4
5# o---o-----o----o----o-------o----x
6# \ D,R e /
7# \---o-------------o-'
8# \ B /
9# `-o----o----o-'
10# A c
11#
12# First parent of a merge commit is on the same line, second parent below.
13
1550bb6e 14GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
15export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
16
5312ab11
JH
17. ./test-lib.sh
18
19check_describe () {
20 expect="$1"
21 shift
c593a263
SG
22 describe_opts="$@"
23 test_expect_success "describe $describe_opts" '
24 R=$(git describe $describe_opts 2>err.actual) &&
5312ab11
JH
25 case "$R" in
26 $expect) echo happy ;;
c593a263 27 *) echo "Oops - $R is not $expect" &&
5312ab11
JH
28 false ;;
29 esac
30 '
31}
32
33test_expect_success setup '
34
35 test_tick &&
3604e7c5 36 echo one >file && git add file && git commit -m initial &&
5be60078 37 one=$(git rev-parse HEAD) &&
5312ab11 38
024ab976
JH
39 git describe --always HEAD &&
40
5312ab11 41 test_tick &&
3604e7c5 42 echo two >file && git add file && git commit -m second &&
5be60078 43 two=$(git rev-parse HEAD) &&
5312ab11
JH
44
45 test_tick &&
3604e7c5 46 echo three >file && git add file && git commit -m third &&
5312ab11
JH
47
48 test_tick &&
3604e7c5 49 echo A >file && git add file && git commit -m A &&
5312ab11 50 test_tick &&
3604e7c5 51 git tag -a -m A A &&
5312ab11
JH
52
53 test_tick &&
3604e7c5 54 echo c >file && git add file && git commit -m c &&
5312ab11 55 test_tick &&
3604e7c5 56 git tag c &&
5312ab11
JH
57
58 git reset --hard $two &&
59 test_tick &&
3604e7c5 60 echo B >side && git add side && git commit -m B &&
5312ab11 61 test_tick &&
3604e7c5 62 git tag -a -m B B &&
5312ab11
JH
63
64 test_tick &&
3604e7c5 65 git merge -m Merged c &&
5be60078 66 merged=$(git rev-parse HEAD) &&
5312ab11
JH
67
68 git reset --hard $two &&
69 test_tick &&
3604e7c5 70 echo D >another && git add another && git commit -m D &&
5312ab11 71 test_tick &&
3604e7c5 72 git tag -a -m D D &&
03e8b541
SP
73 test_tick &&
74 git tag -a -m R R &&
5312ab11
JH
75
76 test_tick &&
77 echo DD >another && git commit -a -m another &&
78
79 test_tick &&
3604e7c5 80 git tag e &&
5312ab11
JH
81
82 test_tick &&
83 echo DDD >another && git commit -a -m "yet another" &&
84
85 test_tick &&
3604e7c5 86 git merge -m Merged $merged &&
5312ab11
JH
87
88 test_tick &&
5be60078 89 echo X >file && echo X >side && git add file side &&
3604e7c5 90 git commit -m x
5312ab11
JH
91
92'
93
94check_describe A-* HEAD
95check_describe A-* HEAD^
03e8b541 96check_describe R-* HEAD^^
5312ab11
JH
97check_describe A-* HEAD^^2
98check_describe B HEAD^^2^
03e8b541 99check_describe R-* HEAD^^^
5312ab11 100
7e425c4f
SP
101check_describe c-* --tags HEAD
102check_describe c-* --tags HEAD^
103check_describe e-* --tags HEAD^^
104check_describe c-* --tags HEAD^^2
5312ab11 105check_describe B --tags HEAD^^2^
7a0d61bb
TR
106check_describe e --tags HEAD^^^
107
1550bb6e 108check_describe heads/main --all HEAD
7a0d61bb
TR
109check_describe tags/c-* --all HEAD^
110check_describe tags/e --all HEAD^^^
5312ab11 111
518120e3 112check_describe B-0-* --long HEAD^^2^
4d4c3e1c 113check_describe A-3-* --long HEAD^^2
518120e3 114
e00dd1e9
MC
115check_describe c-7-* --tags
116check_describe e-3-* --first-parent --tags
117
2bd07065
SG
118test_expect_success 'describe --contains defaults to HEAD without commit-ish' '
119 echo "A^0" >expect &&
120 git checkout A &&
121 test_when_finished "git checkout -" &&
122 git describe --contains >actual &&
123 test_cmp expect actual
124'
125
1bba0013 126check_describe tags/A --all A^0
81dc223d 127test_expect_success 'no warning was displayed for A' '
1c5e94f4 128 test_must_be_empty err.actual
81dc223d
SP
129'
130
3291fe40
SP
131test_expect_success 'rename tag A to Q locally' '
132 mv .git/refs/tags/A .git/refs/tags/Q
133'
134cat - >err.expect <<EOF
ff165f00 135warning: tag 'Q' is externally known as 'A'
3291fe40
SP
136EOF
137check_describe A-* HEAD
b3e1900a 138test_expect_success 'warning was displayed for Q' '
1108cea7 139 test_cmp err.expect err.actual
3291fe40 140'
ff165f00
JH
141test_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
147'
148
149test_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]*$"
152'
153
154test_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]*$"
157'
158
3291fe40
SP
159test_expect_success 'rename tag Q back to A' '
160 mv .git/refs/tags/Q .git/refs/tags/A
161'
162
d1b28f51
SP
163test_expect_success 'pack tag refs' 'git pack-refs'
164check_describe A-* HEAD
165
c801170b
SS
166test_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 &&
4abf20f0 169 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
c801170b
SS
170'
171
9f67d2e8
JP
172check_describe "A-*[0-9a-f]" --dirty
173
2ed5c8e1
SS
174test_expect_success 'describe --dirty with --work-tree' '
175 (
176 cd "$TEST_DIRECTORY" &&
177 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
178 ) &&
4abf20f0 179 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
2ed5c8e1
SS
180'
181
9f67d2e8
JP
182test_expect_success 'set-up dirty work tree' '
183 echo >>file
184'
185
186check_describe "A-*[0-9a-f]-dirty" --dirty
187
2ed5c8e1
SS
188test_expect_success 'describe --dirty with --work-tree (dirty)' '
189 (
190 cd "$TEST_DIRECTORY" &&
191 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
192 ) &&
4abf20f0 193 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+-dirty$" out
2ed5c8e1
SS
194'
195
9f67d2e8
JP
196check_describe "A-*[0-9a-f].mod" --dirty=.mod
197
2ed5c8e1
SS
198test_expect_success 'describe --dirty=.mod with --work-tree (dirty)' '
199 (
200 cd "$TEST_DIRECTORY" &&
201 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
202 ) &&
4abf20f0 203 grep -E "^A-[1-9][0-9]?-g[0-9a-f]+.mod$" out
2ed5c8e1
SS
204'
205
9f67d2e8
JP
206test_expect_success 'describe --dirty HEAD' '
207 test_must_fail git describe --dirty HEAD
208'
209
4ed19a3c
MD
210test_expect_success 'set-up matching pattern tests' '
211 git tag -a -m test-annotated test-annotated &&
212 echo >>file &&
213 test_tick &&
214 git commit -a -m "one more" &&
215 git tag test1-lightweight &&
216 echo >>file &&
217 test_tick &&
218 git commit -a -m "yet another" &&
219 git tag test2-lightweight &&
220 echo >>file &&
221 test_tick &&
222 git commit -a -m "even more"
223
224'
225
226check_describe "test-annotated-*" --match="test-*"
227
228check_describe "test1-lightweight-*" --tags --match="test1-*"
229
230check_describe "test2-lightweight-*" --tags --match="test2-*"
231
14d4642e
SP
232check_describe "test2-lightweight-*" --long --tags --match="test2-*" HEAD^
233
da769d29 234check_describe "test2-lightweight-*" --long --tags --match="test1-*" --match="test2-*" HEAD^
43f8080e
JK
235
236check_describe "test2-lightweight-*" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
237
da769d29
MK
238check_describe "test1-lightweight-*" --long --tags --match="test1-*" --match="test3-*" HEAD
239
240check_describe "test1-lightweight-*" --long --tags --match="test3-*" --match="test1-*" HEAD
241
6d68b2ab
MK
242test_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
248'
249
250check_describe "heads/branch_A*" --all --match="branch_*" --exclude="branch_C" HEAD
251
252check_describe "remotes/origin/remote_branch_A*" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
253
254check_describe "original/original_branch_A*" --all test-annotated~1
255
256test_expect_success '--match does not work for other types' '
257 test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
258'
259
260test_expect_success '--exclude does not work for other types' '
261 R=$(git describe --all --exclude="any_pattern_even_not_matching" test-annotated~1) &&
262 case "$R" in
263 *original_branch_A*) echo "fail: Found unknown reference $R with --exclude"
264 false;;
265 *) echo ok: Found some known type;;
266 esac
267'
268
118aa4ac
JH
269test_expect_success 'name-rev with exact tags' '
270 echo A >expect &&
271 tag_object=$(git rev-parse refs/tags/A) &&
272 git name-rev --tags --name-only $tag_object >actual &&
273 test_cmp expect actual &&
274
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
279'
280
a24fa652
MG
281test_expect_success 'name-rev --all' '
282 >expect.unsorted &&
283 for rev in $(git rev-list --all)
284 do
285 git name-rev $rev >>expect.unsorted
286 done &&
287 sort <expect.unsorted >expect &&
288 git name-rev --all >actual.unsorted &&
289 sort <actual.unsorted >actual &&
290 test_cmp expect actual
291'
292
293test_expect_success 'name-rev --stdin' '
294 >expect.unsorted &&
295 for rev in $(git rev-list --all)
296 do
297 name=$(git name-rev --name-only $rev) &&
298 echo "$rev ($name)" >>expect.unsorted
299 done &&
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
304'
305
adfc1857
JH
306test_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 &&
311
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
316'
317
43f8080e
JK
318test_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
324'
325
77d21f29
JK
326test_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
332'
333
43f8080e
JK
334test_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
339'
340
b0176ce6
SB
341test_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 &&
349 test_cmp expect out
350'
351
64127575 352test_expect_success 'describe chokes on severely broken submodules' '
b0176ce6
SB
353 mv .git/modules/sub1/ .git/modules/sub_moved &&
354 test_must_fail git describe --dirty
355'
2ed5c8e1 356
2deda007 357test_expect_success 'describe ignoring a broken submodule' '
b0176ce6 358 git describe --broken >out &&
2ed5c8e1
SS
359 grep broken out
360'
361
362test_expect_success 'describe with --work-tree ignoring a broken submodule' '
363 (
364 cd "$TEST_DIRECTORY" &&
365 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --broken >"$TRASH_DIRECTORY/out"
366 ) &&
ac9b2401 367 test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
b0176ce6
SB
368 grep broken out
369'
370
644eb60b
SB
371test_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
378'
379
380test_expect_success 'describe a blob with its first introduction' '
381 git commit --allow-empty -m "empty commit" &&
382 git rm file &&
383 git commit -m "delete blob" &&
384 git revert HEAD &&
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
389'
390
391test_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
397'
398
399test_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
403'
404
49f7a2fd 405test_expect_success ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
31625b34
MG
406 i=1 &&
407 while test $i -lt 8000
408 do
1550bb6e 409 echo "commit refs/heads/main
31625b34
MG
410committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
411data <<EOF
412commit #$i
413EOF"
1550bb6e 414 test $i = 1 && echo "from refs/heads/main^0"
31625b34
MG
415 i=$(($i + 1))
416 done | git fast-import &&
1550bb6e 417 git checkout main &&
31625b34
MG
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
424'
425
426test_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
433'
434
1bba0013
DKF
435check_describe tags/A --all A
436check_describe tags/c --all c
437check_describe heads/branch_A --all --match='branch_*' branch_A
438
a8e7a2bf
JK
439test_expect_success 'describe complains about tree object' '
440 test_must_fail git describe HEAD^{tree}
441'
442
443test_expect_success 'describe complains about missing object' '
8125a58b 444 test_must_fail git describe $ZERO_OID
a8e7a2bf
JK
445'
446
2e09c012 447test_expect_success 'name-rev a rev shortly after epoch' '
1550bb6e 448 test_when_finished "git checkout main" &&
2e09c012
SG
449
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) &&
456
457 echo "$old_commit_oid no-timestamp-underflow" >expect &&
458 git name-rev $old_commit_oid >actual &&
459 test_cmp expect actual
460'
461
1550bb6e 462# A--------------main
d59fc836
SG
463# \ /
464# \----------M2
465# \ /
466# \---M1-C
467# \ /
468# B
469test_expect_success 'name-rev covers all conditions while looking at parents' '
470 git init repo &&
471 (
472 cd repo &&
473
474 echo A >file &&
475 git add file &&
476 git commit -m A &&
477 A=$(git rev-parse HEAD) &&
478
479 git checkout --detach &&
480 echo B >file &&
481 git commit -m B file &&
482 B=$(git rev-parse HEAD) &&
483
484 git checkout $A &&
485 git merge --no-ff $B && # M1
486
487 echo C >file &&
488 git commit -m C file &&
489
490 git checkout $A &&
491 git merge --no-ff HEAD@{1} && # M2
492
1550bb6e 493 git checkout main &&
d59fc836
SG
494 git merge --no-ff HEAD@{1} &&
495
1550bb6e 496 echo "$B main^2^2~1^2" >expect &&
d59fc836
SG
497 git name-rev $B >actual &&
498
499 test_cmp expect actual
500 )
501'
502
30b1c7ad
BE
503# B
504# o
505# \
506# o-----o---o----x
507# A
508#
509test_expect_success 'describe commits with disjoint bases' '
510 git init disjoint1 &&
511 (
512 cd disjoint1 &&
513
514 echo o >> file && git add file && git commit -m o &&
515 echo A >> file && git add file && git commit -m A &&
516 git tag A -a -m A &&
517 echo o >> file && git add file && git commit -m o &&
518
519 git checkout --orphan branch && rm file &&
520 echo B > file2 && git add file2 && git commit -m B &&
521 git tag B -a -m B &&
1550bb6e 522 git merge --no-ff --allow-unrelated-histories main -m x &&
30b1c7ad
BE
523
524 check_describe "A-3-*" HEAD
525 )
526'
527
528# B
529# o---o---o------------.
530# \
531# o---o---x
532# A
533#
534test_expect_success 'describe commits with disjoint bases 2' '
535 git init disjoint2 &&
536 (
537 cd disjoint2 &&
538
539 echo A >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:00" git commit -m A &&
540 git tag A -a -m A &&
541 echo o >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:01" git commit -m o &&
542
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 &&
547 git tag B -a -m B &&
1550bb6e 548 git merge --no-ff --allow-unrelated-histories main -m x &&
30b1c7ad
BE
549
550 check_describe "B-3-*" HEAD
551 )
552'
553
5312ab11 554test_done