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