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