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