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