]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6120-describe.sh
describe tests: fix nested "test_expect_success" call
[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" '
7f07c1cd 24 git describe $describe_opts >raw &&
a46a8482
ÆAB
25 sed -e "s/-g[0-9a-f]*\$/-gHASH/" <raw >actual &&
26 echo "$expect" >expect &&
27 test_cmp expect actual
5312ab11
JH
28 '
29}
30
31test_expect_success setup '
8cfe386b
ÆAB
32 test_commit initial file one &&
33 test_commit second file two &&
34 test_commit third file three &&
35 test_commit --annotate A file A &&
36 test_commit c file c &&
5312ab11 37
8cfe386b
ÆAB
38 git reset --hard second &&
39 test_commit --annotate B side B &&
5312ab11
JH
40
41 test_tick &&
3604e7c5 42 git merge -m Merged c &&
5be60078 43 merged=$(git rev-parse HEAD) &&
5312ab11 44
8cfe386b
ÆAB
45 git reset --hard second &&
46 test_commit --no-tag D another D &&
5312ab11
JH
47
48 test_tick &&
8cfe386b 49 git tag -a -m R R &&
5312ab11 50
8cfe386b
ÆAB
51 test_commit e another DD &&
52 test_commit --no-tag "yet another" another DDD &&
5312ab11
JH
53
54 test_tick &&
3604e7c5 55 git merge -m Merged $merged &&
5312ab11 56
8cfe386b 57 test_commit --no-tag x file
5312ab11
JH
58'
59
a46a8482
ÆAB
60check_describe A-8-gHASH HEAD
61check_describe A-7-gHASH HEAD^
62check_describe R-2-gHASH HEAD^^
63check_describe A-3-gHASH HEAD^^2
5312ab11 64check_describe B HEAD^^2^
a46a8482 65check_describe R-1-gHASH HEAD^^^
5312ab11 66
a46a8482
ÆAB
67check_describe c-7-gHASH --tags HEAD
68check_describe c-6-gHASH --tags HEAD^
69check_describe e-1-gHASH --tags HEAD^^
70check_describe c-2-gHASH --tags HEAD^^2
5312ab11 71check_describe B --tags HEAD^^2^
7a0d61bb
TR
72check_describe e --tags HEAD^^^
73
1550bb6e 74check_describe heads/main --all HEAD
a46a8482 75check_describe tags/c-6-gHASH --all HEAD^
7a0d61bb 76check_describe tags/e --all HEAD^^^
5312ab11 77
a46a8482
ÆAB
78check_describe B-0-gHASH --long HEAD^^2^
79check_describe A-3-gHASH --long HEAD^^2
518120e3 80
a46a8482
ÆAB
81check_describe c-7-gHASH --tags
82check_describe e-3-gHASH --first-parent --tags
e00dd1e9 83
2bd07065
SG
84test_expect_success 'describe --contains defaults to HEAD without commit-ish' '
85 echo "A^0" >expect &&
86 git checkout A &&
87 test_when_finished "git checkout -" &&
88 git describe --contains >actual &&
89 test_cmp expect actual
90'
91
1bba0013 92check_describe tags/A --all A^0
81dc223d 93
7f07c1cd
ÆAB
94test_expect_success 'renaming tag A to Q locally produces a warning' "
95 mv .git/refs/tags/A .git/refs/tags/Q &&
96 git describe HEAD 2>err >out &&
97 cat >expected <<-\EOF &&
98 warning: tag 'Q' is externally known as 'A'
99 EOF
100 test_cmp expected err &&
101 grep -E '^A-8-g[0-9a-f]+$' out
102"
103
ff165f00
JH
104test_expect_success 'misnamed annotated tag forces long output' '
105 description=$(git describe --no-long Q^0) &&
106 expr "$description" : "A-0-g[0-9a-f]*$" &&
107 git rev-parse --verify "$description" >actual &&
108 git rev-parse --verify Q^0 >expect &&
109 test_cmp expect actual
110'
111
112test_expect_success 'abbrev=0 will not break misplaced tag (1)' '
113 description=$(git describe --abbrev=0 Q^0) &&
114 expr "$description" : "A-0-g[0-9a-f]*$"
115'
116
117test_expect_success 'abbrev=0 will not break misplaced tag (2)' '
118 description=$(git describe --abbrev=0 c^0) &&
119 expr "$description" : "A-1-g[0-9a-f]*$"
120'
121
3291fe40
SP
122test_expect_success 'rename tag Q back to A' '
123 mv .git/refs/tags/Q .git/refs/tags/A
124'
125
d1b28f51 126test_expect_success 'pack tag refs' 'git pack-refs'
a46a8482 127check_describe A-8-gHASH HEAD
d1b28f51 128
c801170b
SS
129test_expect_success 'describe works from outside repo using --git-dir' '
130 git clone --bare "$TRASH_DIRECTORY" "$TRASH_DIRECTORY/bare" &&
131 git --git-dir "$TRASH_DIRECTORY/bare" describe >out &&
a46a8482 132 grep -E "^A-8-g[0-9a-f]+$" out
c801170b
SS
133'
134
a46a8482 135check_describe "A-8-gHASH" --dirty
9f67d2e8 136
2ed5c8e1
SS
137test_expect_success 'describe --dirty with --work-tree' '
138 (
139 cd "$TEST_DIRECTORY" &&
140 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
141 ) &&
a46a8482 142 grep -E "^A-8-g[0-9a-f]+$" out
2ed5c8e1
SS
143'
144
9f67d2e8
JP
145test_expect_success 'set-up dirty work tree' '
146 echo >>file
147'
148
2ed5c8e1 149test_expect_success 'describe --dirty with --work-tree (dirty)' '
2df546e1 150 git describe --dirty >expected &&
2ed5c8e1
SS
151 (
152 cd "$TEST_DIRECTORY" &&
153 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
154 ) &&
a46a8482 155 grep -E "^A-8-g[0-9a-f]+-dirty$" out &&
2df546e1 156 test_cmp expected out
2ed5c8e1
SS
157'
158
2ed5c8e1 159test_expect_success 'describe --dirty=.mod with --work-tree (dirty)' '
2df546e1 160 git describe --dirty=.mod >expected &&
2ed5c8e1
SS
161 (
162 cd "$TEST_DIRECTORY" &&
163 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
164 ) &&
a46a8482 165 grep -E "^A-8-g[0-9a-f]+.mod$" out &&
2df546e1 166 test_cmp expected out
2ed5c8e1
SS
167'
168
9f67d2e8
JP
169test_expect_success 'describe --dirty HEAD' '
170 test_must_fail git describe --dirty HEAD
171'
172
4ed19a3c
MD
173test_expect_success 'set-up matching pattern tests' '
174 git tag -a -m test-annotated test-annotated &&
175 echo >>file &&
176 test_tick &&
177 git commit -a -m "one more" &&
178 git tag test1-lightweight &&
179 echo >>file &&
180 test_tick &&
181 git commit -a -m "yet another" &&
182 git tag test2-lightweight &&
183 echo >>file &&
184 test_tick &&
185 git commit -a -m "even more"
186
187'
188
a46a8482 189check_describe "test-annotated-3-gHASH" --match="test-*"
4ed19a3c 190
a46a8482 191check_describe "test1-lightweight-2-gHASH" --tags --match="test1-*"
4ed19a3c 192
a46a8482 193check_describe "test2-lightweight-1-gHASH" --tags --match="test2-*"
4ed19a3c 194
a46a8482 195check_describe "test2-lightweight-0-gHASH" --long --tags --match="test2-*" HEAD^
14d4642e 196
a46a8482 197check_describe "test2-lightweight-0-gHASH" --long --tags --match="test1-*" --match="test2-*" HEAD^
43f8080e 198
a46a8482 199check_describe "test2-lightweight-0-gHASH" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
43f8080e 200
a46a8482 201check_describe "test1-lightweight-2-gHASH" --long --tags --match="test1-*" --match="test3-*" HEAD
da769d29 202
a46a8482 203check_describe "test1-lightweight-2-gHASH" --long --tags --match="test3-*" --match="test1-*" HEAD
da769d29 204
6d68b2ab
MK
205test_expect_success 'set-up branches' '
206 git branch branch_A A &&
207 git branch branch_C c &&
208 git update-ref refs/remotes/origin/remote_branch_A "A^{commit}" &&
209 git update-ref refs/remotes/origin/remote_branch_C "c^{commit}" &&
210 git update-ref refs/original/original_branch_A test-annotated~2
211'
212
a46a8482 213check_describe "heads/branch_A-11-gHASH" --all --match="branch_*" --exclude="branch_C" HEAD
6d68b2ab 214
a46a8482 215check_describe "remotes/origin/remote_branch_A-11-gHASH" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
6d68b2ab 216
a46a8482 217check_describe "original/original_branch_A-6-gHASH" --all test-annotated~1
6d68b2ab
MK
218
219test_expect_success '--match does not work for other types' '
220 test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
221'
222
223test_expect_success '--exclude does not work for other types' '
224 R=$(git describe --all --exclude="any_pattern_even_not_matching" test-annotated~1) &&
225 case "$R" in
226 *original_branch_A*) echo "fail: Found unknown reference $R with --exclude"
227 false;;
228 *) echo ok: Found some known type;;
229 esac
230'
231
118aa4ac
JH
232test_expect_success 'name-rev with exact tags' '
233 echo A >expect &&
234 tag_object=$(git rev-parse refs/tags/A) &&
235 git name-rev --tags --name-only $tag_object >actual &&
236 test_cmp expect actual &&
237
238 echo "A^0" >expect &&
239 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
240 git name-rev --tags --name-only $tagged_commit >actual &&
241 test_cmp expect actual
242'
243
a24fa652
MG
244test_expect_success 'name-rev --all' '
245 >expect.unsorted &&
246 for rev in $(git rev-list --all)
247 do
248 git name-rev $rev >>expect.unsorted
249 done &&
250 sort <expect.unsorted >expect &&
251 git name-rev --all >actual.unsorted &&
252 sort <actual.unsorted >actual &&
253 test_cmp expect actual
254'
255
256test_expect_success 'name-rev --stdin' '
257 >expect.unsorted &&
258 for rev in $(git rev-list --all)
259 do
260 name=$(git name-rev --name-only $rev) &&
261 echo "$rev ($name)" >>expect.unsorted
262 done &&
263 sort <expect.unsorted >expect &&
264 git rev-list --all | git name-rev --stdin >actual.unsorted &&
265 sort <actual.unsorted >actual &&
266 test_cmp expect actual
267'
268
adfc1857
JH
269test_expect_success 'describe --contains with the exact tags' '
270 echo "A^0" >expect &&
271 tag_object=$(git rev-parse refs/tags/A) &&
272 git describe --contains $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 describe --contains $tagged_commit >actual &&
278 test_cmp expect actual
279'
280
43f8080e
JK
281test_expect_success 'describe --contains and --match' '
282 echo "A^0" >expect &&
283 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
284 test_must_fail git describe --contains --match="B" $tagged_commit &&
285 git describe --contains --match="B" --match="A" $tagged_commit >actual &&
286 test_cmp expect actual
287'
288
77d21f29
JK
289test_expect_success 'describe --exclude' '
290 echo "c~1" >expect &&
291 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
292 test_must_fail git describe --contains --match="B" $tagged_commit &&
293 git describe --contains --match="?" --exclude="A" $tagged_commit >actual &&
294 test_cmp expect actual
295'
296
43f8080e
JK
297test_expect_success 'describe --contains and --no-match' '
298 echo "A^0" >expect &&
299 tagged_commit=$(git rev-parse "refs/tags/A^0") &&
300 git describe --contains --match="B" --no-match $tagged_commit >actual &&
301 test_cmp expect actual
302'
303
b0176ce6
SB
304test_expect_success 'setup and absorb a submodule' '
305 test_create_repo sub1 &&
306 test_commit -C sub1 initial &&
307 git submodule add ./sub1 &&
308 git submodule absorbgitdirs &&
309 git commit -a -m "add submodule" &&
310 git describe --dirty >expect &&
311 git describe --broken >out &&
312 test_cmp expect out
313'
314
64127575 315test_expect_success 'describe chokes on severely broken submodules' '
b0176ce6
SB
316 mv .git/modules/sub1/ .git/modules/sub_moved &&
317 test_must_fail git describe --dirty
318'
2ed5c8e1 319
2deda007 320test_expect_success 'describe ignoring a broken submodule' '
b0176ce6 321 git describe --broken >out &&
2ed5c8e1
SS
322 grep broken out
323'
324
325test_expect_success 'describe with --work-tree ignoring a broken submodule' '
326 (
327 cd "$TEST_DIRECTORY" &&
328 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --broken >"$TRASH_DIRECTORY/out"
329 ) &&
ac9b2401 330 test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
b0176ce6
SB
331 grep broken out
332'
333
644eb60b
SB
334test_expect_success 'describe a blob at a directly tagged commit' '
335 echo "make it a unique blob" >file &&
336 git add file && git commit -m "content in file" &&
337 git tag -a -m "latest annotated tag" unique-file &&
338 git describe HEAD:file >actual &&
339 echo "unique-file:file" >expect &&
340 test_cmp expect actual
341'
342
343test_expect_success 'describe a blob with its first introduction' '
344 git commit --allow-empty -m "empty commit" &&
345 git rm file &&
346 git commit -m "delete blob" &&
347 git revert HEAD &&
348 git commit --allow-empty -m "empty commit" &&
349 git describe HEAD:file >actual &&
350 echo "unique-file:file" >expect &&
351 test_cmp expect actual
352'
353
354test_expect_success 'describe directly tagged blob' '
355 git tag test-blob unique-file:file &&
356 git describe test-blob >actual &&
357 echo "unique-file:file" >expect &&
358 # suboptimal: we rather want to see "test-blob"
359 test_cmp expect actual
360'
361
362test_expect_success 'describe tag object' '
363 git tag test-blob-1 -a -m msg unique-file:file &&
364 test_must_fail git describe test-blob-1 2>actual &&
365 test_i18ngrep "fatal: test-blob-1 is neither a commit nor blob" actual
366'
367
49f7a2fd 368test_expect_success ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
31625b34
MG
369 i=1 &&
370 while test $i -lt 8000
371 do
1550bb6e 372 echo "commit refs/heads/main
31625b34
MG
373committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
374data <<EOF
375commit #$i
376EOF"
1550bb6e 377 test $i = 1 && echo "from refs/heads/main^0"
31625b34
MG
378 i=$(($i + 1))
379 done | git fast-import &&
1550bb6e 380 git checkout main &&
31625b34
MG
381 git tag far-far-away HEAD^ &&
382 echo "HEAD~4000 tags/far-far-away~3999" >expect &&
383 git name-rev HEAD~4000 >actual &&
384 test_cmp expect actual &&
385 run_with_limited_stack git name-rev HEAD~4000 >actual &&
386 test_cmp expect actual
387'
388
389test_expect_success ULIMIT_STACK_SIZE 'describe works in a deep repo' '
390 git tag -f far-far-away HEAD~7999 &&
391 echo "far-far-away" >expect &&
392 git describe --tags --abbrev=0 HEAD~4000 >actual &&
393 test_cmp expect actual &&
394 run_with_limited_stack git describe --tags --abbrev=0 HEAD~4000 >actual &&
395 test_cmp expect actual
396'
397
1bba0013
DKF
398check_describe tags/A --all A
399check_describe tags/c --all c
400check_describe heads/branch_A --all --match='branch_*' branch_A
401
a8e7a2bf
JK
402test_expect_success 'describe complains about tree object' '
403 test_must_fail git describe HEAD^{tree}
404'
405
406test_expect_success 'describe complains about missing object' '
8125a58b 407 test_must_fail git describe $ZERO_OID
a8e7a2bf
JK
408'
409
2e09c012 410test_expect_success 'name-rev a rev shortly after epoch' '
1550bb6e 411 test_when_finished "git checkout main" &&
2e09c012
SG
412
413 git checkout --orphan no-timestamp-underflow &&
414 # Any date closer to epoch than the CUTOFF_DATE_SLOP constant
415 # in builtin/name-rev.c.
416 GIT_COMMITTER_DATE="@1234 +0000" \
417 git commit -m "committer date shortly after epoch" &&
418 old_commit_oid=$(git rev-parse HEAD) &&
419
420 echo "$old_commit_oid no-timestamp-underflow" >expect &&
421 git name-rev $old_commit_oid >actual &&
422 test_cmp expect actual
423'
424
1550bb6e 425# A--------------main
d59fc836
SG
426# \ /
427# \----------M2
428# \ /
429# \---M1-C
430# \ /
431# B
432test_expect_success 'name-rev covers all conditions while looking at parents' '
433 git init repo &&
434 (
435 cd repo &&
436
437 echo A >file &&
438 git add file &&
439 git commit -m A &&
440 A=$(git rev-parse HEAD) &&
441
442 git checkout --detach &&
443 echo B >file &&
444 git commit -m B file &&
445 B=$(git rev-parse HEAD) &&
446
447 git checkout $A &&
448 git merge --no-ff $B && # M1
449
450 echo C >file &&
451 git commit -m C file &&
452
453 git checkout $A &&
454 git merge --no-ff HEAD@{1} && # M2
455
1550bb6e 456 git checkout main &&
d59fc836
SG
457 git merge --no-ff HEAD@{1} &&
458
1550bb6e 459 echo "$B main^2^2~1^2" >expect &&
d59fc836
SG
460 git name-rev $B >actual &&
461
462 test_cmp expect actual
463 )
464'
465
30b1c7ad
BE
466# B
467# o
468# \
469# o-----o---o----x
470# A
471#
33b4ae11 472test_expect_success 'setup: describe commits with disjoint bases' '
30b1c7ad
BE
473 git init disjoint1 &&
474 (
475 cd disjoint1 &&
476
477 echo o >> file && git add file && git commit -m o &&
478 echo A >> file && git add file && git commit -m A &&
479 git tag A -a -m A &&
480 echo o >> file && git add file && git commit -m o &&
481
482 git checkout --orphan branch && rm file &&
483 echo B > file2 && git add file2 && git commit -m B &&
484 git tag B -a -m B &&
33b4ae11 485 git merge --no-ff --allow-unrelated-histories main -m x
30b1c7ad
BE
486 )
487'
488
33b4ae11
ÆAB
489(
490 cd disjoint1 &&
491 check_describe "A-3-gHASH" HEAD
492)
493
30b1c7ad
BE
494# B
495# o---o---o------------.
496# \
497# o---o---x
498# A
499#
33b4ae11 500test_expect_success 'setup: describe commits with disjoint bases 2' '
30b1c7ad
BE
501 git init disjoint2 &&
502 (
503 cd disjoint2 &&
504
505 echo A >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:00" git commit -m A &&
506 git tag A -a -m A &&
507 echo o >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:01" git commit -m o &&
508
509 git checkout --orphan branch &&
510 echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:00" git commit -m o &&
511 echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:01" git commit -m o &&
512 echo B >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:02" git commit -m B &&
513 git tag B -a -m B &&
33b4ae11 514 git merge --no-ff --allow-unrelated-histories main -m x
30b1c7ad
BE
515 )
516'
517
33b4ae11
ÆAB
518(
519 cd disjoint2 &&
520 check_describe "B-3-gHASH" HEAD
521)
522
5312ab11 523test_done