]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6300-for-each-ref.sh
The sixth batch
[thirdparty/git.git] / t / t6300-for-each-ref.sh
CommitLineData
96b2d4fa
AP
1#!/bin/sh
2#
3# Copyright (c) 2007 Andy Parkins
4#
5
6test_description='for-each-ref test'
7
8. ./test-lib.sh
e2b23972 9. "$TEST_DIRECTORY"/lib-gpg.sh
11b087ad 10. "$TEST_DIRECTORY"/lib-terminal.sh
96b2d4fa 11
db7bae25
JK
12# Mon Jul 3 23:18:43 2006 +0000
13datestamp=1151968723
96b2d4fa
AP
14setdate_and_increment () {
15 GIT_COMMITTER_DATE="$datestamp +0200"
16 datestamp=$(expr "$datestamp" + 1)
17 GIT_AUTHOR_DATE="$datestamp +0200"
18 datestamp=$(expr "$datestamp" + 1)
19 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
20}
21
bc147968 22test_expect_success setup '
1f5f8f3e 23 test_oid_cache <<-EOF &&
24 disklen sha1:138
25 disklen sha256:154
26 EOF
96b2d4fa
AP
27 setdate_and_increment &&
28 echo "Using $datestamp" > one &&
29 git add one &&
30 git commit -m "Initial" &&
31 setdate_and_increment &&
bc147968 32 git tag -a -m "Tagging at $datestamp" testtag &&
8cae19d9
JK
33 git update-ref refs/remotes/origin/master master &&
34 git remote add origin nowhere &&
35 git config branch.master.remote origin &&
29bc8850
JK
36 git config branch.master.merge refs/heads/master &&
37 git remote add myfork elsewhere &&
38 git config remote.pushdefault myfork &&
39 git config push.default current
8cae19d9
JK
40'
41
d2bf48d2
JK
42test_atom() {
43 case "$1" in
44 head) ref=refs/heads/master ;;
45 tag) ref=refs/tags/testtag ;;
01f95825 46 sym) ref=refs/heads/sym ;;
7140c22c 47 *) ref=$1 ;;
d2bf48d2
JK
48 esac
49 printf '%s\n' "$3" >expected
e2b23972 50 test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
d2bf48d2 51 git for-each-ref --format='%($2)' $ref >actual &&
e2b23972
MG
52 sanitize_pgp <actual >actual.clean &&
53 test_cmp expected actual.clean
d2bf48d2
JK
54 "
55}
56
1f5f8f3e 57hexlen=$(test_oid hexsz)
58disklen=$(test_oid disklen)
59
d2bf48d2 60test_atom head refname refs/heads/master
bea4dbea 61test_atom head refname: refs/heads/master
1d094db9 62test_atom head refname:short master
17938f17
KN
63test_atom head refname:lstrip=1 heads/master
64test_atom head refname:lstrip=2 master
1a0ca5e3
KN
65test_atom head refname:lstrip=-1 master
66test_atom head refname:lstrip=-2 heads/master
1a34728e
KN
67test_atom head refname:rstrip=1 refs/heads
68test_atom head refname:rstrip=2 refs
69test_atom head refname:rstrip=-1 refs
70test_atom head refname:rstrip=-2 refs/heads
44a6b6ce
JH
71test_atom head refname:strip=1 heads/master
72test_atom head refname:strip=2 master
73test_atom head refname:strip=-1 master
74test_atom head refname:strip=-2 heads/master
8cae19d9 75test_atom head upstream refs/remotes/origin/master
1d094db9 76test_atom head upstream:short origin/master
17938f17 77test_atom head upstream:lstrip=2 origin/master
1a0ca5e3 78test_atom head upstream:lstrip=-2 origin/master
1a34728e
KN
79test_atom head upstream:rstrip=2 refs/remotes
80test_atom head upstream:rstrip=-2 refs/remotes
44a6b6ce
JH
81test_atom head upstream:strip=2 origin/master
82test_atom head upstream:strip=-2 origin/master
29bc8850 83test_atom head push refs/remotes/myfork/master
1d094db9 84test_atom head push:short myfork/master
17938f17 85test_atom head push:lstrip=1 remotes/myfork/master
1a0ca5e3 86test_atom head push:lstrip=-1 master
1a34728e
KN
87test_atom head push:rstrip=1 refs/remotes/myfork
88test_atom head push:rstrip=-1 refs
44a6b6ce
JH
89test_atom head push:strip=1 remotes/myfork/master
90test_atom head push:strip=-1 master
d2bf48d2 91test_atom head objecttype commit
1f5f8f3e 92test_atom head objectsize $((131 + hexlen))
93test_atom head objectsize:disk $disklen
94test_atom head deltabase $ZERO_OID
189a5467 95test_atom head objectname $(git rev-parse refs/heads/master)
1d094db9 96test_atom head objectname:short $(git rev-parse --short refs/heads/master)
42d0eb05
KN
97test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
98test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
189a5467 99test_atom head tree $(git rev-parse refs/heads/master^{tree})
d2bf48d2
JK
100test_atom head parent ''
101test_atom head numparent 0
102test_atom head object ''
103test_atom head type ''
b74cf648
JK
104test_atom head '*objectname' ''
105test_atom head '*objecttype' ''
db7bae25 106test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
d2bf48d2
JK
107test_atom head authorname 'A U Thor'
108test_atom head authoremail '<author@example.com>'
db7bae25
JK
109test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
110test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
d2bf48d2
JK
111test_atom head committername 'C O Mitter'
112test_atom head committeremail '<committer@example.com>'
db7bae25 113test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
d2bf48d2
JK
114test_atom head tag ''
115test_atom head tagger ''
116test_atom head taggername ''
117test_atom head taggeremail ''
118test_atom head taggerdate ''
db7bae25
JK
119test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
120test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
d2bf48d2 121test_atom head subject 'Initial'
e2b23972 122test_atom head contents:subject 'Initial'
d2bf48d2 123test_atom head body ''
e2b23972
MG
124test_atom head contents:body ''
125test_atom head contents:signature ''
d2bf48d2
JK
126test_atom head contents 'Initial
127'
7a48b832 128test_atom head HEAD '*'
d2bf48d2
JK
129
130test_atom tag refname refs/tags/testtag
1d094db9 131test_atom tag refname:short testtag
8cae19d9 132test_atom tag upstream ''
29bc8850 133test_atom tag push ''
d2bf48d2 134test_atom tag objecttype tag
1f5f8f3e 135test_atom tag objectsize $((114 + hexlen))
136test_atom tag objectsize:disk $disklen
137test_atom tag '*objectsize:disk' $disklen
138test_atom tag deltabase $ZERO_OID
139test_atom tag '*deltabase' $ZERO_OID
189a5467 140test_atom tag objectname $(git rev-parse refs/tags/testtag)
1d094db9 141test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
42d0eb05
KN
142test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
143test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
d2bf48d2
JK
144test_atom tag tree ''
145test_atom tag parent ''
146test_atom tag numparent ''
189a5467 147test_atom tag object $(git rev-parse refs/tags/testtag^0)
d2bf48d2 148test_atom tag type 'commit'
1f5f8f3e 149test_atom tag '*objectname' $(git rev-parse refs/tags/testtag^{})
b74cf648 150test_atom tag '*objecttype' 'commit'
d2bf48d2
JK
151test_atom tag author ''
152test_atom tag authorname ''
153test_atom tag authoremail ''
154test_atom tag authordate ''
155test_atom tag committer ''
156test_atom tag committername ''
157test_atom tag committeremail ''
158test_atom tag committerdate ''
159test_atom tag tag 'testtag'
db7bae25 160test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
d2bf48d2
JK
161test_atom tag taggername 'C O Mitter'
162test_atom tag taggeremail '<committer@example.com>'
db7bae25
JK
163test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
164test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
165test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
166test_atom tag subject 'Tagging at 1151968727'
167test_atom tag contents:subject 'Tagging at 1151968727'
d2bf48d2 168test_atom tag body ''
e2b23972
MG
169test_atom tag contents:body ''
170test_atom tag contents:signature ''
db7bae25 171test_atom tag contents 'Tagging at 1151968727
96b2d4fa 172'
7a48b832 173test_atom tag HEAD ' '
96b2d4fa 174
41ac414e 175test_expect_success 'Check invalid atoms names are errors' '
3604e7c5 176 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
96b2d4fa
AP
177'
178
179test_expect_success 'Check format specifiers are ignored in naming date atoms' '
3604e7c5
NS
180 git for-each-ref --format="%(authordate)" refs/heads &&
181 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
182 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
183 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
96b2d4fa
AP
184'
185
186test_expect_success 'Check valid format specifiers for date fields' '
3604e7c5
NS
187 git for-each-ref --format="%(authordate:default)" refs/heads &&
188 git for-each-ref --format="%(authordate:relative)" refs/heads &&
189 git for-each-ref --format="%(authordate:short)" refs/heads &&
190 git for-each-ref --format="%(authordate:local)" refs/heads &&
191 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
192 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
96b2d4fa
AP
193'
194
41ac414e 195test_expect_success 'Check invalid format specifiers are errors' '
3604e7c5 196 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
96b2d4fa
AP
197'
198
42d0eb05
KN
199test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
200 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
201 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
202 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
203'
204
f95cecf4
JK
205test_date () {
206 f=$1 &&
207 committer_date=$2 &&
208 author_date=$3 &&
209 tagger_date=$4 &&
210 cat >expected <<-EOF &&
211 'refs/heads/master' '$committer_date' '$author_date'
212 'refs/tags/testtag' '$tagger_date'
213 EOF
214 (
215 git for-each-ref --shell \
216 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
217 refs/heads &&
218 git for-each-ref --shell \
219 --format="%(refname) %(taggerdate${f:+:$f})" \
220 refs/tags
221 ) >actual &&
222 test_cmp expected actual
223}
96b2d4fa
AP
224
225test_expect_success 'Check unformatted date fields output' '
f95cecf4 226 test_date "" \
db7bae25
JK
227 "Tue Jul 4 01:18:43 2006 +0200" \
228 "Tue Jul 4 01:18:44 2006 +0200" \
229 "Tue Jul 4 01:18:45 2006 +0200"
96b2d4fa
AP
230'
231
232test_expect_success 'Check format "default" formatted date fields output' '
f95cecf4 233 test_date default \
db7bae25
JK
234 "Tue Jul 4 01:18:43 2006 +0200" \
235 "Tue Jul 4 01:18:44 2006 +0200" \
236 "Tue Jul 4 01:18:45 2006 +0200"
96b2d4fa
AP
237'
238
99264e93
JK
239test_expect_success 'Check format "default-local" date fields output' '
240 test_date default-local "Mon Jul 3 23:18:43 2006" "Mon Jul 3 23:18:44 2006" "Mon Jul 3 23:18:45 2006"
96b2d4fa
AP
241'
242
243# Don't know how to do relative check because I can't know when this script
244# is going to be run and can't fake the current time to git, and hence can't
245# provide expected output. Instead, I'll just make sure that "relative"
246# doesn't exit in error
96b2d4fa
AP
247test_expect_success 'Check format "relative" date fields output' '
248 f=relative &&
249 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
250 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
251'
252
99264e93
JK
253# We just check that this is the same as "relative" for now.
254test_expect_success 'Check format "relative-local" date fields output' '
255 test_date relative-local \
256 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
257 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
258 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
259'
96b2d4fa
AP
260
261test_expect_success 'Check format "short" date fields output' '
db7bae25 262 test_date short 2006-07-04 2006-07-04 2006-07-04
96b2d4fa
AP
263'
264
99264e93
JK
265test_expect_success 'Check format "short-local" date fields output' '
266 test_date short-local 2006-07-03 2006-07-03 2006-07-03
267'
96b2d4fa
AP
268
269test_expect_success 'Check format "local" date fields output' '
f95cecf4 270 test_date local \
db7bae25
JK
271 "Mon Jul 3 23:18:43 2006" \
272 "Mon Jul 3 23:18:44 2006" \
273 "Mon Jul 3 23:18:45 2006"
96b2d4fa
AP
274'
275
96b2d4fa 276test_expect_success 'Check format "iso8601" date fields output' '
f95cecf4 277 test_date iso8601 \
db7bae25
JK
278 "2006-07-04 01:18:43 +0200" \
279 "2006-07-04 01:18:44 +0200" \
280 "2006-07-04 01:18:45 +0200"
96b2d4fa
AP
281'
282
99264e93
JK
283test_expect_success 'Check format "iso8601-local" date fields output' '
284 test_date iso8601-local "2006-07-03 23:18:43 +0000" "2006-07-03 23:18:44 +0000" "2006-07-03 23:18:45 +0000"
285'
96b2d4fa
AP
286
287test_expect_success 'Check format "rfc2822" date fields output' '
f95cecf4 288 test_date rfc2822 \
db7bae25
JK
289 "Tue, 4 Jul 2006 01:18:43 +0200" \
290 "Tue, 4 Jul 2006 01:18:44 +0200" \
291 "Tue, 4 Jul 2006 01:18:45 +0200"
96b2d4fa
AP
292'
293
99264e93
JK
294test_expect_success 'Check format "rfc2822-local" date fields output' '
295 test_date rfc2822-local "Mon, 3 Jul 2006 23:18:43 +0000" "Mon, 3 Jul 2006 23:18:44 +0000" "Mon, 3 Jul 2006 23:18:45 +0000"
296'
297
f3c1ba50 298test_expect_success 'Check format "raw" date fields output' '
db7bae25 299 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
f3c1ba50
JK
300'
301
99264e93
JK
302test_expect_success 'Check format "raw-local" date fields output' '
303 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
96b2d4fa
AP
304'
305
aa1462cc 306test_expect_success 'Check format of strftime date fields' '
db7bae25 307 echo "my date is 2006-07-04" >expected &&
aa1462cc
JK
308 git for-each-ref \
309 --format="%(authordate:format:my date is %Y-%m-%d)" \
310 refs/heads >actual &&
311 test_cmp expected actual
312'
313
99264e93
JK
314test_expect_success 'Check format of strftime-local date fields' '
315 echo "my date is 2006-07-03" >expected &&
316 git for-each-ref \
317 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
318 refs/heads >actual &&
319 test_cmp expected actual
320'
321
e4f031e3
JK
322test_expect_success 'exercise strftime with odd fields' '
323 echo >expected &&
324 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
325 test_cmp expected actual &&
8125a58b 326 long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
e4f031e3
JK
327 echo $long >expected &&
328 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
329 test_cmp expected actual
330'
331
c899a57c
LH
332cat >expected <<\EOF
333refs/heads/master
8cae19d9 334refs/remotes/origin/master
c899a57c
LH
335refs/tags/testtag
336EOF
337
338test_expect_success 'Verify ascending sort' '
3604e7c5 339 git for-each-ref --format="%(refname)" --sort=refname >actual &&
3af82863 340 test_cmp expected actual
c899a57c
LH
341'
342
343
344cat >expected <<\EOF
345refs/tags/testtag
8cae19d9 346refs/remotes/origin/master
c899a57c
LH
347refs/heads/master
348EOF
349
350test_expect_success 'Verify descending sort' '
3604e7c5 351 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
3af82863 352 test_cmp expected actual
b31e2680
TB
353'
354
355cat >expected <<\EOF
356refs/tags/testtag
357refs/tags/testtag-2
358EOF
359
360test_expect_success 'exercise patterns with prefixes' '
361 git tag testtag-2 &&
362 test_when_finished "git tag -d testtag-2" &&
363 git for-each-ref --format="%(refname)" \
364 refs/tags/testtag refs/tags/testtag-2 >actual &&
365 test_cmp expected actual
366'
367
368cat >expected <<\EOF
369refs/tags/testtag
370refs/tags/testtag-2
371EOF
372
373test_expect_success 'exercise glob patterns with prefixes' '
374 git tag testtag-2 &&
375 test_when_finished "git tag -d testtag-2" &&
376 git for-each-ref --format="%(refname)" \
377 refs/tags/testtag "refs/tags/testtag-*" >actual &&
378 test_cmp expected actual
c899a57c
LH
379'
380
c9ecf4f1
JS
381cat >expected <<\EOF
382'refs/heads/master'
8cae19d9 383'refs/remotes/origin/master'
c9ecf4f1
JS
384'refs/tags/testtag'
385EOF
386
387test_expect_success 'Quoting style: shell' '
388 git for-each-ref --shell --format="%(refname)" >actual &&
3af82863 389 test_cmp expected actual
c9ecf4f1
JS
390'
391
392test_expect_success 'Quoting style: perl' '
393 git for-each-ref --perl --format="%(refname)" >actual &&
3af82863 394 test_cmp expected actual
c9ecf4f1
JS
395'
396
397test_expect_success 'Quoting style: python' '
398 git for-each-ref --python --format="%(refname)" >actual &&
3af82863 399 test_cmp expected actual
c9ecf4f1
JS
400'
401
402cat >expected <<\EOF
403"refs/heads/master"
8cae19d9 404"refs/remotes/origin/master"
c9ecf4f1
JS
405"refs/tags/testtag"
406EOF
407
408test_expect_success 'Quoting style: tcl' '
409 git for-each-ref --tcl --format="%(refname)" >actual &&
3af82863 410 test_cmp expected actual
c9ecf4f1
JS
411'
412
413for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
414 test_expect_success "more than one quoting style: $i" "
2708ef4a
SG
415 test_must_fail git for-each-ref $i 2>err &&
416 grep '^error: more than one quoting style' err
c9ecf4f1
JS
417 "
418done
c899a57c 419
b28061ce
RR
420test_expect_success 'setup for upstream:track[short]' '
421 test_commit two
422'
423
1d094db9
JK
424test_atom head upstream:track '[ahead 1]'
425test_atom head upstream:trackshort '>'
7743fcca
KN
426test_atom head upstream:track,nobracket 'ahead 1'
427test_atom head upstream:nobracket,track 'ahead 1'
c646d093
DR
428
429test_expect_success 'setup for push:track[short]' '
430 test_commit third &&
431 git update-ref refs/remotes/myfork/master master &&
432 git reset master~1
433'
434
435test_atom head push:track '[behind 1]'
436test_atom head push:trackshort '<'
b28061ce
RR
437
438test_expect_success 'Check that :track[short] cannot be used with other atoms' '
439 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
440 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
441'
442
b6160d95
RKC
443test_expect_success 'Check that :track[short] works when upstream is invalid' '
444 cat >expected <<-\EOF &&
ffd921d3 445 [gone]
b6160d95
RKC
446
447 EOF
448 test_when_finished "git config branch.master.merge refs/heads/master" &&
449 git config branch.master.merge refs/heads/does-not-exist &&
450 git for-each-ref \
451 --format="%(upstream:track)$LF%(upstream:trackshort)" \
452 refs/heads >actual &&
453 test_cmp expected actual
29bc8850
JK
454'
455
7d66f21a
BW
456test_expect_success 'Check for invalid refname format' '
457 test_must_fail git for-each-ref --format="%(refname:INVALID)"
458'
459
11b087ad
JK
460test_expect_success 'set up color tests' '
461 cat >expected.color <<-EOF &&
462 $(git rev-parse --short refs/heads/master) <GREEN>master<RESET>
c646d093 463 $(git rev-parse --short refs/remotes/myfork/master) <GREEN>myfork/master<RESET>
11b087ad
JK
464 $(git rev-parse --short refs/remotes/origin/master) <GREEN>origin/master<RESET>
465 $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
c646d093 466 $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
11b087ad
JK
467 $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
468 EOF
469 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
470 color_format="%(objectname:short) %(color:green)%(refname:short)"
471'
fddb74c9 472
11b087ad 473test_expect_success TTY '%(color) shows color with a tty' '
e433749d 474 test_terminal git for-each-ref --format="$color_format" >actual.raw &&
097b681b 475 test_decode_color <actual.raw >actual &&
11b087ad
JK
476 test_cmp expected.color actual
477'
478
479test_expect_success '%(color) does not show color without tty' '
480 TERM=vt100 git for-each-ref --format="$color_format" >actual &&
481 test_cmp expected.bare actual
482'
483
0c88bf50
JK
484test_expect_success '--color can override tty check' '
485 git for-each-ref --color --format="$color_format" >actual.raw &&
11b087ad
JK
486 test_decode_color <actual.raw >actual &&
487 test_cmp expected.color actual
fddb74c9
RR
488'
489
b521fd12
JK
490test_expect_success 'color.ui=always does not override tty check' '
491 git -c color.ui=always for-each-ref --format="$color_format" >actual &&
492 test_cmp expected.bare actual
493'
494
7d66f21a
BW
495cat >expected <<\EOF
496heads/master
2bb98169 497tags/master
7d66f21a
BW
498EOF
499
2bb98169
BW
500test_expect_success 'Check ambiguous head and tag refs (strict)' '
501 git config --bool core.warnambiguousrefs true &&
7d66f21a
BW
502 git checkout -b newtag &&
503 echo "Using $datestamp" > one &&
504 git add one &&
505 git commit -m "Branch" &&
506 setdate_and_increment &&
507 git tag -m "Tagging at $datestamp" master &&
508 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
509 test_cmp expected actual
510'
511
2bb98169
BW
512cat >expected <<\EOF
513heads/master
514master
515EOF
516
517test_expect_success 'Check ambiguous head and tag refs (loose)' '
518 git config --bool core.warnambiguousrefs false &&
519 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
520 test_cmp expected actual
521'
522
7d66f21a
BW
523cat >expected <<\EOF
524heads/ambiguous
525ambiguous
526EOF
527
2bb98169 528test_expect_success 'Check ambiguous head and tag refs II (loose)' '
7d66f21a
BW
529 git checkout master &&
530 git tag ambiguous testtag^0 &&
531 git branch ambiguous testtag^0 &&
532 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
533 test_cmp expected actual
534'
535
8b3f33ef
MP
536test_expect_success 'create tag without tagger' '
537 git tag -a -m "Broken tag" taggerless &&
538 git tag -f taggerless $(git cat-file tag taggerless |
539 sed -e "/^tagger /d" |
540 git hash-object --stdin -w -t tag)
541'
542
543test_atom refs/tags/taggerless type 'commit'
544test_atom refs/tags/taggerless tag 'taggerless'
545test_atom refs/tags/taggerless tagger ''
546test_atom refs/tags/taggerless taggername ''
547test_atom refs/tags/taggerless taggeremail ''
548test_atom refs/tags/taggerless taggerdate ''
549test_atom refs/tags/taggerless committer ''
550test_atom refs/tags/taggerless committername ''
551test_atom refs/tags/taggerless committeremail ''
552test_atom refs/tags/taggerless committerdate ''
553test_atom refs/tags/taggerless subject 'Broken tag'
554
e276c26b
JH
555test_expect_success 'an unusual tag with an incomplete line' '
556
557 git tag -m "bogo" bogo &&
558 bogo=$(git cat-file tag bogo) &&
559 bogo=$(printf "%s" "$bogo" | git mktag) &&
560 git tag -f bogo "$bogo" &&
561 git for-each-ref --format "%(body)" refs/tags/bogo
562
563'
564
7140c22c
JK
565test_expect_success 'create tag with subject and body content' '
566 cat >>msg <<-\EOF &&
567 the subject line
568
569 first body line
570 second body line
571 EOF
572 git tag -F msg subject-body
573'
574test_atom refs/tags/subject-body subject 'the subject line'
575test_atom refs/tags/subject-body body 'first body line
576second body line
577'
578test_atom refs/tags/subject-body contents 'the subject line
579
580first body line
581second body line
582'
583
7f6e275b
JK
584test_expect_success 'create tag with multiline subject' '
585 cat >msg <<-\EOF &&
586 first subject line
587 second subject line
588
589 first body line
590 second body line
591 EOF
592 git tag -F msg multiline
593'
594test_atom refs/tags/multiline subject 'first subject line second subject line'
e2b23972 595test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
7f6e275b
JK
596test_atom refs/tags/multiline body 'first body line
597second body line
598'
e2b23972
MG
599test_atom refs/tags/multiline contents:body 'first body line
600second body line
601'
602test_atom refs/tags/multiline contents:signature ''
7f6e275b
JK
603test_atom refs/tags/multiline contents 'first subject line
604second subject line
605
606first body line
607second body line
608'
609
e2b23972
MG
610test_expect_success GPG 'create signed tags' '
611 git tag -s -m "" signed-empty &&
612 git tag -s -m "subject line" signed-short &&
613 cat >msg <<-\EOF &&
614 subject line
615
616 body contents
617 EOF
618 git tag -s -F msg signed-long
619'
620
621sig='-----BEGIN PGP SIGNATURE-----
622-----END PGP SIGNATURE-----
623'
624
625PREREQ=GPG
626test_atom refs/tags/signed-empty subject ''
627test_atom refs/tags/signed-empty contents:subject ''
628test_atom refs/tags/signed-empty body "$sig"
629test_atom refs/tags/signed-empty contents:body ''
630test_atom refs/tags/signed-empty contents:signature "$sig"
631test_atom refs/tags/signed-empty contents "$sig"
632
633test_atom refs/tags/signed-short subject 'subject line'
634test_atom refs/tags/signed-short contents:subject 'subject line'
635test_atom refs/tags/signed-short body "$sig"
636test_atom refs/tags/signed-short contents:body ''
637test_atom refs/tags/signed-short contents:signature "$sig"
638test_atom refs/tags/signed-short contents "subject line
639$sig"
640
641test_atom refs/tags/signed-long subject 'subject line'
642test_atom refs/tags/signed-long contents:subject 'subject line'
643test_atom refs/tags/signed-long body "body contents
644$sig"
645test_atom refs/tags/signed-long contents:body 'body contents
646'
647test_atom refs/tags/signed-long contents:signature "$sig"
648test_atom refs/tags/signed-long contents "subject line
649
650body contents
651$sig"
652
7c5045fc
JK
653test_expect_success 'set up multiple-sort tags' '
654 for when in 100000 200000
655 do
656 for email in user1 user2
657 do
658 for ref in ref1 ref2
659 do
660 GIT_COMMITTER_DATE="@$when +0000" \
661 GIT_COMMITTER_EMAIL="$email@example.com" \
662 git tag -m "tag $ref-$when-$email" \
663 multi-$ref-$when-$email || return 1
664 done
665 done
666 done
667'
912072d5 668
3b51222c 669test_expect_success 'Verify sort with multiple keys' '
7c5045fc
JK
670 cat >expected <<-\EOF &&
671 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
672 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
673 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
674 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
675 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
676 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
677 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
678 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
679 EOF
680 git for-each-ref \
681 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
682 --sort=-refname \
683 --sort=taggeremail \
684 --sort=taggerdate \
685 "refs/tags/multi-*" >actual &&
912072d5
KK
686 test_cmp expected actual
687'
84679d47 688
7c5045fc
JK
689test_expect_success 'equivalent sorts fall back on refname' '
690 cat >expected <<-\EOF &&
691 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
692 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
693 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
694 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
695 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
696 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
697 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
698 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
699 EOF
700 git for-each-ref \
701 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
702 --sort=taggerdate \
703 "refs/tags/multi-*" >actual &&
704 test_cmp expected actual
705'
01f95825 706
84679d47
JH
707test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
708 test_when_finished "git checkout master" &&
709 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
710 sed -e "s/^\* / /" actual >expect &&
f0252ca2 711 git checkout --orphan orphaned-branch &&
84679d47
JH
712 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
713 test_cmp expect actual
714'
715
b1d31c89
JK
716cat >trailers <<EOF
717Reviewed-by: A U Thor <author@example.com>
718Signed-off-by: A U Thor <author@example.com>
624b44d3
TB
719[ v2 updated patch description ]
720Acked-by: A U Thor
721 <author@example.com>
b1d31c89
JK
722EOF
723
67a20a00
TB
724unfold () {
725 perl -0pe 's/\n\s+/ /g'
726}
624b44d3
TB
727
728test_expect_success 'set up trailers for next test' '
b1d31c89
JK
729 echo "Some contents" > two &&
730 git add two &&
624b44d3 731 git commit -F - <<-EOF
b1d31c89
JK
732 trailers: this commit message has trailers
733
734 Some message contents
735
736 $(cat trailers)
737 EOF
624b44d3
TB
738'
739
67a20a00
TB
740test_expect_success '%(trailers:unfold) unfolds trailers' '
741 git for-each-ref --format="%(trailers:unfold)" refs/heads/master >actual &&
742 {
743 unfold <trailers
744 echo
745 } >expect &&
746 test_cmp expect actual
747'
748
749test_expect_success '%(trailers:only) shows only "key: value" trailers' '
750 git for-each-ref --format="%(trailers:only)" refs/heads/master >actual &&
751 {
752 grep -v patch.description <trailers &&
753 echo
754 } >expect &&
755 test_cmp expect actual
756'
757
758test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
759 git for-each-ref --format="%(trailers:only,unfold)" refs/heads/master >actual &&
760 git for-each-ref --format="%(trailers:unfold,only)" refs/heads/master >reverse &&
761 test_cmp actual reverse &&
762 {
763 grep -v patch.description <trailers | unfold &&
764 echo
765 } >expect &&
766 test_cmp expect actual
767'
768
7a5edbdb
TB
769test_expect_success '%(contents:trailers:unfold) unfolds trailers' '
770 git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/master >actual &&
771 {
772 unfold <trailers
773 echo
774 } >expect &&
775 test_cmp expect actual
776'
777
778test_expect_success '%(contents:trailers:only) shows only "key: value" trailers' '
779 git for-each-ref --format="%(contents:trailers:only)" refs/heads/master >actual &&
780 {
781 grep -v patch.description <trailers &&
782 echo
783 } >expect &&
784 test_cmp expect actual
785'
786
787test_expect_success '%(contents:trailers:only) and %(contents:trailers:unfold) work together' '
788 git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/master >actual &&
789 git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/master >reverse &&
790 test_cmp actual reverse &&
791 {
792 grep -v patch.description <trailers | unfold &&
793 echo
794 } >expect &&
795 test_cmp expect actual
796'
797
67a20a00
TB
798test_expect_success '%(trailers) rejects unknown trailers arguments' '
799 # error message cannot be checked under i18n
800 cat >expect <<-EOF &&
801 fatal: unknown %(trailers) argument: unsupported
802 EOF
803 test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual &&
804 test_i18ncmp expect actual
805'
806
7a5edbdb
TB
807test_expect_success '%(contents:trailers) rejects unknown trailers arguments' '
808 # error message cannot be checked under i18n
809 cat >expect <<-EOF &&
810 fatal: unknown %(trailers) argument: unsupported
811 EOF
812 test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual &&
813 test_i18ncmp expect actual
814'
815
624b44d3 816test_expect_success 'basic atom: head contents:trailers' '
b1d31c89
JK
817 git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
818 sanitize_pgp <actual >actual.clean &&
819 # git for-each-ref ends with a blank line
820 cat >expect <<-EOF &&
821 $(cat trailers)
822
823 EOF
824 test_cmp expect actual.clean
825'
826
e5fba5d5
JK
827test_expect_success 'trailer parsing not fooled by --- line' '
828 git commit --allow-empty -F - <<-\EOF &&
829 this is the subject
830
831 This is the body. The message has a "---" line which would confuse a
832 message+patch parser. But here we know we have only a commit message,
833 so we get it right.
834
835 trailer: wrong
836 ---
837 This is more body.
838
839 trailer: right
840 EOF
841
842 {
843 echo "trailer: right" &&
844 echo
845 } >expect &&
846 git for-each-ref --format="%(trailers)" refs/heads/master >actual &&
847 test_cmp expect actual
848'
849
01f95825
KN
850test_expect_success 'Add symbolic ref for the following tests' '
851 git symbolic-ref refs/heads/sym refs/heads/master
852'
853
854cat >expected <<EOF
855refs/heads/master
856EOF
857
858test_expect_success 'Verify usage of %(symref) atom' '
859 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
860 test_cmp expected actual
861'
862
863cat >expected <<EOF
864heads/master
865EOF
866
867test_expect_success 'Verify usage of %(symref:short) atom' '
868 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
869 test_cmp expected actual
870'
871
a7984101
KN
872cat >expected <<EOF
873master
1a0ca5e3 874heads/master
a7984101
KN
875EOF
876
17938f17
KN
877test_expect_success 'Verify usage of %(symref:lstrip) atom' '
878 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
1a0ca5e3 879 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
44a6b6ce
JH
880 test_cmp expected actual &&
881
882 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
883 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
a7984101
KN
884 test_cmp expected actual
885'
886
1a34728e
KN
887cat >expected <<EOF
888refs
889refs/heads
890EOF
891
892test_expect_success 'Verify usage of %(symref:rstrip) atom' '
893 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
894 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
895 test_cmp expected actual
896'
897
1b586867
JS
898test_expect_success ':remotename and :remoteref' '
899 git init remote-tests &&
900 (
901 cd remote-tests &&
902 test_commit initial &&
903 git remote add from fifth.coffee:blub &&
904 git config branch.master.remote from &&
905 git config branch.master.merge refs/heads/stable &&
906 git remote add to southridge.audio:repo &&
907 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
908 git config branch.master.pushRemote to &&
909 for pair in "%(upstream)=refs/remotes/from/stable" \
910 "%(upstream:remotename)=from" \
911 "%(upstream:remoteref)=refs/heads/stable" \
912 "%(push)=refs/remotes/to/pushed/master" \
913 "%(push:remotename)=to" \
914 "%(push:remoteref)=refs/heads/pushed/master"
915 do
916 echo "${pair#*=}" >expect &&
917 git for-each-ref --format="${pair%=*}" \
918 refs/heads/master >actual &&
919 test_cmp expect actual
920 done &&
921 git branch push-simple &&
922 git config branch.push-simple.pushRemote from &&
923 actual="$(git for-each-ref \
924 --format="%(push:remotename),%(push:remoteref)" \
925 refs/heads/push-simple)" &&
926 test from, = "$actual"
927 )
928'
929
e674eb25 930test_expect_success 'for-each-ref --ignore-case ignores case' '
ee0f3e22 931 git for-each-ref --format="%(refname)" refs/heads/MASTER >actual &&
d3c6751b 932 test_must_be_empty actual &&
ee0f3e22
JK
933
934 echo refs/heads/master >expect &&
935 git for-each-ref --format="%(refname)" --ignore-case \
936 refs/heads/MASTER >actual &&
937 test_cmp expect actual
938'
939
76f9e569
JK
940test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
941 # name refs numerically to avoid case-insensitive filesystem conflicts
942 nr=0 &&
943 for email in a A b B
944 do
945 for subject in a A b B
946 do
947 GIT_COMMITTER_EMAIL="$email@example.com" \
948 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
949 nr=$((nr+1))||
950 return 1
951 done
952 done &&
953 git for-each-ref --ignore-case \
954 --format="%(taggeremail) %(subject) %(refname)" \
955 --sort=refname \
956 --sort=subject \
957 --sort=taggeremail \
958 refs/tags/icase-* >actual &&
959 cat >expect <<-\EOF &&
960 <a@example.com> tag a refs/tags/icase-00
961 <a@example.com> tag A refs/tags/icase-01
962 <A@example.com> tag a refs/tags/icase-04
963 <A@example.com> tag A refs/tags/icase-05
964 <a@example.com> tag b refs/tags/icase-02
965 <a@example.com> tag B refs/tags/icase-03
966 <A@example.com> tag b refs/tags/icase-06
967 <A@example.com> tag B refs/tags/icase-07
968 <b@example.com> tag a refs/tags/icase-08
969 <b@example.com> tag A refs/tags/icase-09
970 <B@example.com> tag a refs/tags/icase-12
971 <B@example.com> tag A refs/tags/icase-13
972 <b@example.com> tag b refs/tags/icase-10
973 <b@example.com> tag B refs/tags/icase-11
974 <B@example.com> tag b refs/tags/icase-14
975 <B@example.com> tag B refs/tags/icase-15
976 EOF
977 test_cmp expect actual
978'
979
96b2d4fa 980test_done