Merge branch 'ab/test-lib-updates'
[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         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         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 &&
39
40         git reset --hard second &&
41         test_commit --annotate B side B &&
42
43         test_tick &&
44         git merge -m Merged c &&
45         merged=$(git rev-parse HEAD) &&
46
47         git reset --hard second &&
48         test_commit --no-tag D another D &&
49
50         test_tick &&
51         git tag -a -m R R &&
52
53         test_commit e another DD &&
54         test_commit --no-tag "yet another" another DDD &&
55
56         test_tick &&
57         git merge -m Merged $merged &&
58
59         test_commit --no-tag x file
60 '
61
62 check_describe A-* HEAD
63 check_describe A-* HEAD^
64 check_describe R-* HEAD^^
65 check_describe A-* HEAD^^2
66 check_describe B HEAD^^2^
67 check_describe R-* HEAD^^^
68
69 check_describe c-* --tags HEAD
70 check_describe c-* --tags HEAD^
71 check_describe e-* --tags HEAD^^
72 check_describe c-* --tags HEAD^^2
73 check_describe B --tags HEAD^^2^
74 check_describe e --tags HEAD^^^
75
76 check_describe heads/main --all HEAD
77 check_describe tags/c-* --all HEAD^
78 check_describe tags/e --all HEAD^^^
79
80 check_describe B-0-* --long HEAD^^2^
81 check_describe A-3-* --long HEAD^^2
82
83 check_describe c-7-* --tags
84 check_describe e-3-* --first-parent --tags
85
86 test_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
94 check_describe tags/A --all A^0
95 test_expect_success 'no warning was displayed for A' '
96         test_must_be_empty err.actual
97 '
98
99 test_expect_success 'rename tag A to Q locally' '
100         mv .git/refs/tags/A .git/refs/tags/Q
101 '
102 cat - >err.expect <<EOF
103 warning: tag 'Q' is externally known as 'A'
104 EOF
105 check_describe A-* HEAD
106 test_expect_success 'warning was displayed for Q' '
107         test_cmp err.expect err.actual
108 '
109 test_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
117 test_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
122 test_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
127 test_expect_success 'rename tag Q back to A' '
128         mv .git/refs/tags/Q .git/refs/tags/A
129 '
130
131 test_expect_success 'pack tag refs' 'git pack-refs'
132 check_describe A-* HEAD
133
134 test_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 &&
137         grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
138 '
139
140 check_describe "A-*[0-9a-f]" --dirty
141
142 test_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         ) &&
147         grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
148 '
149
150 test_expect_success 'set-up dirty work tree' '
151         echo >>file
152 '
153
154 check_describe "A-*[0-9a-f]-dirty" --dirty
155
156 test_expect_success 'describe --dirty with --work-tree (dirty)' '
157         (
158                 cd "$TEST_DIRECTORY" &&
159                 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
160         ) &&
161         grep -E "^A-[1-9][0-9]?-g[0-9a-f]+-dirty$" out
162 '
163
164 check_describe "A-*[0-9a-f].mod" --dirty=.mod
165
166 test_expect_success 'describe --dirty=.mod with --work-tree (dirty)' '
167         (
168                 cd "$TEST_DIRECTORY" &&
169                 git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
170         ) &&
171         grep -E "^A-[1-9][0-9]?-g[0-9a-f]+.mod$" out
172 '
173
174 test_expect_success 'describe --dirty HEAD' '
175         test_must_fail git describe --dirty HEAD
176 '
177
178 test_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
194 check_describe "test-annotated-*" --match="test-*"
195
196 check_describe "test1-lightweight-*" --tags --match="test1-*"
197
198 check_describe "test2-lightweight-*" --tags --match="test2-*"
199
200 check_describe "test2-lightweight-*" --long --tags --match="test2-*" HEAD^
201
202 check_describe "test2-lightweight-*" --long --tags --match="test1-*" --match="test2-*" HEAD^
203
204 check_describe "test2-lightweight-*" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
205
206 check_describe "test1-lightweight-*" --long --tags --match="test1-*" --match="test3-*" HEAD
207
208 check_describe "test1-lightweight-*" --long --tags --match="test3-*" --match="test1-*" HEAD
209
210 test_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
218 check_describe "heads/branch_A*" --all --match="branch_*" --exclude="branch_C" HEAD
219
220 check_describe "remotes/origin/remote_branch_A*" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
221
222 check_describe "original/original_branch_A*" --all test-annotated~1
223
224 test_expect_success '--match does not work for other types' '
225         test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
226 '
227
228 test_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
237 test_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
249 test_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
261 test_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
274 test_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
286 test_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
294 test_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
302 test_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
309 test_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
320 test_expect_success 'describe chokes on severely broken submodules' '
321         mv .git/modules/sub1/ .git/modules/sub_moved &&
322         test_must_fail git describe --dirty
323 '
324
325 test_expect_success 'describe ignoring a broken submodule' '
326         git describe --broken >out &&
327         grep broken out
328 '
329
330 test_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         ) &&
335         test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
336         grep broken out
337 '
338
339 test_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
348 test_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
359 test_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
367 test_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
373 test_expect_success ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
374         i=1 &&
375         while test $i -lt 8000
376         do
377                 echo "commit refs/heads/main
378 committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
379 data <<EOF
380 commit #$i
381 EOF"
382                 test $i = 1 && echo "from refs/heads/main^0"
383                 i=$(($i + 1))
384         done | git fast-import &&
385         git checkout main &&
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
394 test_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
403 check_describe tags/A --all A
404 check_describe tags/c --all c
405 check_describe heads/branch_A --all --match='branch_*' branch_A
406
407 test_expect_success 'describe complains about tree object' '
408         test_must_fail git describe HEAD^{tree}
409 '
410
411 test_expect_success 'describe complains about missing object' '
412         test_must_fail git describe $ZERO_OID
413 '
414
415 test_expect_success 'name-rev a rev shortly after epoch' '
416         test_when_finished "git checkout main" &&
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
430 # A--------------main
431 #  \            /
432 #   \----------M2
433 #    \        /
434 #     \---M1-C
435 #      \ /
436 #       B
437 test_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
461                 git checkout main &&
462                 git merge --no-ff HEAD@{1} &&
463
464                 echo "$B main^2^2~1^2" >expect &&
465                 git name-rev $B >actual &&
466
467                 test_cmp expect actual
468         )
469 '
470
471 #               B
472 #               o
473 #                \
474 #  o-----o---o----x
475 #        A
476 #
477 test_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 &&
490                 git merge --no-ff --allow-unrelated-histories main -m x &&
491
492                 check_describe "A-3-*" HEAD
493         )
494 '
495
496 #           B
497 #   o---o---o------------.
498 #                         \
499 #                  o---o---x
500 #                  A
501 #
502 test_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 &&
516                 git merge --no-ff --allow-unrelated-histories main -m x &&
517
518                 check_describe "B-3-*" HEAD
519         )
520 '
521
522 test_done