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