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