Imported Upstream version 2.27.0
[platform/upstream/git.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 . ./test-lib.sh
15
16 check_describe () {
17         expect="$1"
18         shift
19         describe_opts="$@"
20         test_expect_success "describe $describe_opts" '
21         R=$(git describe $describe_opts 2>err.actual) &&
22         case "$R" in
23         $expect)        echo happy ;;
24         *)      echo "Oops - $R is not $expect" &&
25                 false ;;
26         esac
27         '
28 }
29
30 test_expect_success setup '
31
32         test_tick &&
33         echo one >file && git add file && git commit -m initial &&
34         one=$(git rev-parse HEAD) &&
35
36         git describe --always HEAD &&
37
38         test_tick &&
39         echo two >file && git add file && git commit -m second &&
40         two=$(git rev-parse HEAD) &&
41
42         test_tick &&
43         echo three >file && git add file && git commit -m third &&
44
45         test_tick &&
46         echo A >file && git add file && git commit -m A &&
47         test_tick &&
48         git tag -a -m A A &&
49
50         test_tick &&
51         echo c >file && git add file && git commit -m c &&
52         test_tick &&
53         git tag c &&
54
55         git reset --hard $two &&
56         test_tick &&
57         echo B >side && git add side && git commit -m B &&
58         test_tick &&
59         git tag -a -m B B &&
60
61         test_tick &&
62         git merge -m Merged c &&
63         merged=$(git rev-parse HEAD) &&
64
65         git reset --hard $two &&
66         test_tick &&
67         echo D >another && git add another && git commit -m D &&
68         test_tick &&
69         git tag -a -m D D &&
70         test_tick &&
71         git tag -a -m R R &&
72
73         test_tick &&
74         echo DD >another && git commit -a -m another &&
75
76         test_tick &&
77         git tag e &&
78
79         test_tick &&
80         echo DDD >another && git commit -a -m "yet another" &&
81
82         test_tick &&
83         git merge -m Merged $merged &&
84
85         test_tick &&
86         echo X >file && echo X >side && git add file side &&
87         git commit -m x
88
89 '
90
91 check_describe A-* HEAD
92 check_describe A-* HEAD^
93 check_describe R-* HEAD^^
94 check_describe A-* HEAD^^2
95 check_describe B HEAD^^2^
96 check_describe R-* HEAD^^^
97
98 check_describe c-* --tags HEAD
99 check_describe c-* --tags HEAD^
100 check_describe e-* --tags HEAD^^
101 check_describe c-* --tags HEAD^^2
102 check_describe B --tags HEAD^^2^
103 check_describe e --tags HEAD^^^
104
105 check_describe heads/master --all HEAD
106 check_describe tags/c-* --all HEAD^
107 check_describe tags/e --all HEAD^^^
108
109 check_describe B-0-* --long HEAD^^2^
110 check_describe A-3-* --long HEAD^^2
111
112 check_describe c-7-* --tags
113 check_describe e-3-* --first-parent --tags
114
115 test_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
123 check_describe tags/A --all A^0
124 test_expect_success 'no warning was displayed for A' '
125         test_must_be_empty err.actual
126 '
127
128 test_expect_success 'rename tag A to Q locally' '
129         mv .git/refs/tags/A .git/refs/tags/Q
130 '
131 cat - >err.expect <<EOF
132 warning: tag 'Q' is externally known as 'A'
133 EOF
134 check_describe A-* HEAD
135 test_expect_success 'warning was displayed for Q' '
136         test_i18ncmp err.expect err.actual
137 '
138 test_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
146 test_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
151 test_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
156 test_expect_success 'rename tag Q back to A' '
157         mv .git/refs/tags/Q .git/refs/tags/A
158 '
159
160 test_expect_success 'pack tag refs' 'git pack-refs'
161 check_describe A-* HEAD
162
163 test_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 &&
166         grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
167 '
168
169 check_describe "A-*[0-9a-f]" --dirty
170
171 test_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         ) &&
176         grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
177 '
178
179 test_expect_success 'set-up dirty work tree' '
180         echo >>file
181 '
182
183 check_describe "A-*[0-9a-f]-dirty" --dirty
184
185 test_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         ) &&
190         grep -E "^A-[1-9][0-9]?-g[0-9a-f]+-dirty$" out
191 '
192
193 check_describe "A-*[0-9a-f].mod" --dirty=.mod
194
195 test_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         ) &&
200         grep -E "^A-[1-9][0-9]?-g[0-9a-f]+.mod$" out
201 '
202
203 test_expect_success 'describe --dirty HEAD' '
204         test_must_fail git describe --dirty HEAD
205 '
206
207 test_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
223 check_describe "test-annotated-*" --match="test-*"
224
225 check_describe "test1-lightweight-*" --tags --match="test1-*"
226
227 check_describe "test2-lightweight-*" --tags --match="test2-*"
228
229 check_describe "test2-lightweight-*" --long --tags --match="test2-*" HEAD^
230
231 check_describe "test2-lightweight-*" --long --tags --match="test1-*" --match="test2-*" HEAD^
232
233 check_describe "test2-lightweight-*" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
234
235 check_describe "test1-lightweight-*" --long --tags --match="test1-*" --match="test3-*" HEAD
236
237 check_describe "test1-lightweight-*" --long --tags --match="test3-*" --match="test1-*" HEAD
238
239 test_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
247 check_describe "heads/branch_A*" --all --match="branch_*" --exclude="branch_C" HEAD
248
249 check_describe "remotes/origin/remote_branch_A*" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
250
251 check_describe "original/original_branch_A*" --all test-annotated~1
252
253 test_expect_success '--match does not work for other types' '
254         test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
255 '
256
257 test_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
266 test_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
278 test_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
290 test_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
303 test_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
315 test_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
323 test_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
331 test_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
338 test_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
349 test_expect_success 'describe chokes on severely broken submodules' '
350         mv .git/modules/sub1/ .git/modules/sub_moved &&
351         test_must_fail git describe --dirty
352 '
353
354 test_expect_success 'describe ignoring a broken submodule' '
355         git describe --broken >out &&
356         grep broken out
357 '
358
359 test_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         ) &&
364         test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
365         grep broken out
366 '
367
368 test_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
377 test_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
388 test_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
396 test_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
402 test_expect_success ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
403         i=1 &&
404         while test $i -lt 8000
405         do
406                 echo "commit refs/heads/master
407 committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
408 data <<EOF
409 commit #$i
410 EOF"
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
423 test_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
432 check_describe tags/A --all A
433 check_describe tags/c --all c
434 check_describe heads/branch_A --all --match='branch_*' branch_A
435
436 test_expect_success 'describe complains about tree object' '
437         test_must_fail git describe HEAD^{tree}
438 '
439
440 test_expect_success 'describe complains about missing object' '
441         test_must_fail git describe $ZERO_OID
442 '
443
444 test_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
459 # A--------------master
460 #  \            /
461 #   \----------M2
462 #    \        /
463 #     \---M1-C
464 #      \ /
465 #       B
466 test_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
500 #               B
501 #               o
502 #                \
503 #  o-----o---o----x
504 #        A
505 #
506 test_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 #
531 test_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
551 test_done