ref-filter: Do not abruptly die when using the 'lstrip=<N>' option
[git.git] / t / t6300-for-each-ref.sh
blobd3d1a97db9709c29b829cf64df4fa6cce04ed6fe
1 #!/bin/sh
3 # Copyright (c) 2007 Andy Parkins
6 test_description='for-each-ref test'
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-gpg.sh
11 # Mon Jul 3 23:18:43 2006 +0000
12 datestamp=1151968723
13 setdate_and_increment () {
14 GIT_COMMITTER_DATE="$datestamp +0200"
15 datestamp=$(expr "$datestamp" + 1)
16 GIT_AUTHOR_DATE="$datestamp +0200"
17 datestamp=$(expr "$datestamp" + 1)
18 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
21 test_expect_success setup '
22 setdate_and_increment &&
23 echo "Using $datestamp" > one &&
24 git add one &&
25 git commit -m "Initial" &&
26 setdate_and_increment &&
27 git tag -a -m "Tagging at $datestamp" testtag &&
28 git update-ref refs/remotes/origin/master master &&
29 git remote add origin nowhere &&
30 git config branch.master.remote origin &&
31 git config branch.master.merge refs/heads/master &&
32 git remote add myfork elsewhere &&
33 git config remote.pushdefault myfork &&
34 git config push.default current
37 test_atom() {
38 case "$1" in
39 head) ref=refs/heads/master ;;
40 tag) ref=refs/tags/testtag ;;
41 sym) ref=refs/heads/sym ;;
42 *) ref=$1 ;;
43 esac
44 printf '%s\n' "$3" >expected
45 test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
46 git for-each-ref --format='%($2)' $ref >actual &&
47 sanitize_pgp <actual >actual.clean &&
48 test_cmp expected actual.clean
52 test_atom head refname refs/heads/master
53 test_atom head refname:short master
54 test_atom head refname:lstrip=1 heads/master
55 test_atom head refname:lstrip=2 master
56 test_atom head upstream refs/remotes/origin/master
57 test_atom head upstream:short origin/master
58 test_atom head upstream:lstrip=2 origin/master
59 test_atom head push refs/remotes/myfork/master
60 test_atom head push:short myfork/master
61 test_atom head push:lstrip=1 remotes/myfork/master
62 test_atom head objecttype commit
63 test_atom head objectsize 171
64 test_atom head objectname $(git rev-parse refs/heads/master)
65 test_atom head objectname:short $(git rev-parse --short refs/heads/master)
66 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
67 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
68 test_atom head tree $(git rev-parse refs/heads/master^{tree})
69 test_atom head parent ''
70 test_atom head numparent 0
71 test_atom head object ''
72 test_atom head type ''
73 test_atom head '*objectname' ''
74 test_atom head '*objecttype' ''
75 test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
76 test_atom head authorname 'A U Thor'
77 test_atom head authoremail '<author@example.com>'
78 test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
79 test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
80 test_atom head committername 'C O Mitter'
81 test_atom head committeremail '<committer@example.com>'
82 test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
83 test_atom head tag ''
84 test_atom head tagger ''
85 test_atom head taggername ''
86 test_atom head taggeremail ''
87 test_atom head taggerdate ''
88 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
89 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
90 test_atom head subject 'Initial'
91 test_atom head contents:subject 'Initial'
92 test_atom head body ''
93 test_atom head contents:body ''
94 test_atom head contents:signature ''
95 test_atom head contents 'Initial
97 test_atom head HEAD '*'
99 test_atom tag refname refs/tags/testtag
100 test_atom tag refname:short testtag
101 test_atom tag upstream ''
102 test_atom tag push ''
103 test_atom tag objecttype tag
104 test_atom tag objectsize 154
105 test_atom tag objectname $(git rev-parse refs/tags/testtag)
106 test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
107 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
108 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
109 test_atom tag tree ''
110 test_atom tag parent ''
111 test_atom tag numparent ''
112 test_atom tag object $(git rev-parse refs/tags/testtag^0)
113 test_atom tag type 'commit'
114 test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
115 test_atom tag '*objecttype' 'commit'
116 test_atom tag author ''
117 test_atom tag authorname ''
118 test_atom tag authoremail ''
119 test_atom tag authordate ''
120 test_atom tag committer ''
121 test_atom tag committername ''
122 test_atom tag committeremail ''
123 test_atom tag committerdate ''
124 test_atom tag tag 'testtag'
125 test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
126 test_atom tag taggername 'C O Mitter'
127 test_atom tag taggeremail '<committer@example.com>'
128 test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
129 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
130 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
131 test_atom tag subject 'Tagging at 1151968727'
132 test_atom tag contents:subject 'Tagging at 1151968727'
133 test_atom tag body ''
134 test_atom tag contents:body ''
135 test_atom tag contents:signature ''
136 test_atom tag contents 'Tagging at 1151968727
138 test_atom tag HEAD ' '
140 test_expect_success 'Check invalid atoms names are errors' '
141 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
144 test_expect_success 'arguments to :lstrip must be positive integers' '
145 test_must_fail git for-each-ref --format="%(refname:lstrip=0)" &&
146 test_must_fail git for-each-ref --format="%(refname:lstrip=-1)" &&
147 test_must_fail git for-each-ref --format="%(refname:lstrip=foo)"
150 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
151 git for-each-ref --format="%(authordate)" refs/heads &&
152 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
153 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
154 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
157 test_expect_success 'Check valid format specifiers for date fields' '
158 git for-each-ref --format="%(authordate:default)" refs/heads &&
159 git for-each-ref --format="%(authordate:relative)" refs/heads &&
160 git for-each-ref --format="%(authordate:short)" refs/heads &&
161 git for-each-ref --format="%(authordate:local)" refs/heads &&
162 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
163 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
166 test_expect_success 'Check invalid format specifiers are errors' '
167 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
170 test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
171 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
172 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
173 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
176 test_date () {
177 f=$1 &&
178 committer_date=$2 &&
179 author_date=$3 &&
180 tagger_date=$4 &&
181 cat >expected <<-EOF &&
182 'refs/heads/master' '$committer_date' '$author_date'
183 'refs/tags/testtag' '$tagger_date'
186 git for-each-ref --shell \
187 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
188 refs/heads &&
189 git for-each-ref --shell \
190 --format="%(refname) %(taggerdate${f:+:$f})" \
191 refs/tags
192 ) >actual &&
193 test_cmp expected actual
196 test_expect_success 'Check unformatted date fields output' '
197 test_date "" \
198 "Tue Jul 4 01:18:43 2006 +0200" \
199 "Tue Jul 4 01:18:44 2006 +0200" \
200 "Tue Jul 4 01:18:45 2006 +0200"
203 test_expect_success 'Check format "default" formatted date fields output' '
204 test_date default \
205 "Tue Jul 4 01:18:43 2006 +0200" \
206 "Tue Jul 4 01:18:44 2006 +0200" \
207 "Tue Jul 4 01:18:45 2006 +0200"
210 test_expect_success 'Check format "default-local" date fields output' '
211 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"
214 # Don't know how to do relative check because I can't know when this script
215 # is going to be run and can't fake the current time to git, and hence can't
216 # provide expected output. Instead, I'll just make sure that "relative"
217 # doesn't exit in error
218 test_expect_success 'Check format "relative" date fields output' '
219 f=relative &&
220 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
221 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
224 # We just check that this is the same as "relative" for now.
225 test_expect_success 'Check format "relative-local" date fields output' '
226 test_date relative-local \
227 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
228 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
229 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
232 test_expect_success 'Check format "short" date fields output' '
233 test_date short 2006-07-04 2006-07-04 2006-07-04
236 test_expect_success 'Check format "short-local" date fields output' '
237 test_date short-local 2006-07-03 2006-07-03 2006-07-03
240 test_expect_success 'Check format "local" date fields output' '
241 test_date local \
242 "Mon Jul 3 23:18:43 2006" \
243 "Mon Jul 3 23:18:44 2006" \
244 "Mon Jul 3 23:18:45 2006"
247 test_expect_success 'Check format "iso8601" date fields output' '
248 test_date iso8601 \
249 "2006-07-04 01:18:43 +0200" \
250 "2006-07-04 01:18:44 +0200" \
251 "2006-07-04 01:18:45 +0200"
254 test_expect_success 'Check format "iso8601-local" date fields output' '
255 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"
258 test_expect_success 'Check format "rfc2822" date fields output' '
259 test_date rfc2822 \
260 "Tue, 4 Jul 2006 01:18:43 +0200" \
261 "Tue, 4 Jul 2006 01:18:44 +0200" \
262 "Tue, 4 Jul 2006 01:18:45 +0200"
265 test_expect_success 'Check format "rfc2822-local" date fields output' '
266 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"
269 test_expect_success 'Check format "raw" date fields output' '
270 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
273 test_expect_success 'Check format "raw-local" date fields output' '
274 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
277 test_expect_success 'Check format of strftime date fields' '
278 echo "my date is 2006-07-04" >expected &&
279 git for-each-ref \
280 --format="%(authordate:format:my date is %Y-%m-%d)" \
281 refs/heads >actual &&
282 test_cmp expected actual
285 test_expect_success 'Check format of strftime-local date fields' '
286 echo "my date is 2006-07-03" >expected &&
287 git for-each-ref \
288 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
289 refs/heads >actual &&
290 test_cmp expected actual
293 test_expect_success 'exercise strftime with odd fields' '
294 echo >expected &&
295 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
296 test_cmp expected actual &&
297 long="long format -- $_z40$_z40$_z40$_z40$_z40$_z40$_z40" &&
298 echo $long >expected &&
299 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
300 test_cmp expected actual
303 cat >expected <<\EOF
304 refs/heads/master
305 refs/remotes/origin/master
306 refs/tags/testtag
309 test_expect_success 'Verify ascending sort' '
310 git for-each-ref --format="%(refname)" --sort=refname >actual &&
311 test_cmp expected actual
315 cat >expected <<\EOF
316 refs/tags/testtag
317 refs/remotes/origin/master
318 refs/heads/master
321 test_expect_success 'Verify descending sort' '
322 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
323 test_cmp expected actual
326 cat >expected <<\EOF
327 'refs/heads/master'
328 'refs/remotes/origin/master'
329 'refs/tags/testtag'
332 test_expect_success 'Quoting style: shell' '
333 git for-each-ref --shell --format="%(refname)" >actual &&
334 test_cmp expected actual
337 test_expect_success 'Quoting style: perl' '
338 git for-each-ref --perl --format="%(refname)" >actual &&
339 test_cmp expected actual
342 test_expect_success 'Quoting style: python' '
343 git for-each-ref --python --format="%(refname)" >actual &&
344 test_cmp expected actual
347 cat >expected <<\EOF
348 "refs/heads/master"
349 "refs/remotes/origin/master"
350 "refs/tags/testtag"
353 test_expect_success 'Quoting style: tcl' '
354 git for-each-ref --tcl --format="%(refname)" >actual &&
355 test_cmp expected actual
358 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
359 test_expect_success "more than one quoting style: $i" "
360 git for-each-ref $i 2>&1 | (read line &&
361 case \$line in
362 \"error: more than one quoting style\"*) : happy;;
363 *) false
364 esac)
366 done
368 test_expect_success 'setup for upstream:track[short]' '
369 test_commit two
372 test_atom head upstream:track '[ahead 1]'
373 test_atom head upstream:trackshort '>'
374 test_atom head upstream:track,nobracket 'ahead 1'
375 test_atom head upstream:nobracket,track 'ahead 1'
376 test_atom head push:track '[ahead 1]'
377 test_atom head push:trackshort '>'
379 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
380 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
381 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
384 test_expect_success 'Check that :track[short] works when upstream is invalid' '
385 cat >expected <<-\EOF &&
386 [gone]
389 test_when_finished "git config branch.master.merge refs/heads/master" &&
390 git config branch.master.merge refs/heads/does-not-exist &&
391 git for-each-ref \
392 --format="%(upstream:track)$LF%(upstream:trackshort)" \
393 refs/heads >actual &&
394 test_cmp expected actual
397 test_expect_success 'Check for invalid refname format' '
398 test_must_fail git for-each-ref --format="%(refname:INVALID)"
401 get_color ()
403 git config --get-color no.such.slot "$1"
406 cat >expected <<EOF
407 $(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
408 $(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
409 $(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
410 $(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
413 test_expect_success 'Check %(color:...) ' '
414 git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
415 test_cmp expected actual
418 cat >expected <<\EOF
419 heads/master
420 tags/master
423 test_expect_success 'Check ambiguous head and tag refs (strict)' '
424 git config --bool core.warnambiguousrefs true &&
425 git checkout -b newtag &&
426 echo "Using $datestamp" > one &&
427 git add one &&
428 git commit -m "Branch" &&
429 setdate_and_increment &&
430 git tag -m "Tagging at $datestamp" master &&
431 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
432 test_cmp expected actual
435 cat >expected <<\EOF
436 heads/master
437 master
440 test_expect_success 'Check ambiguous head and tag refs (loose)' '
441 git config --bool core.warnambiguousrefs false &&
442 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
443 test_cmp expected actual
446 cat >expected <<\EOF
447 heads/ambiguous
448 ambiguous
451 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
452 git checkout master &&
453 git tag ambiguous testtag^0 &&
454 git branch ambiguous testtag^0 &&
455 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
456 test_cmp expected actual
459 test_expect_success 'an unusual tag with an incomplete line' '
461 git tag -m "bogo" bogo &&
462 bogo=$(git cat-file tag bogo) &&
463 bogo=$(printf "%s" "$bogo" | git mktag) &&
464 git tag -f bogo "$bogo" &&
465 git for-each-ref --format "%(body)" refs/tags/bogo
469 test_expect_success 'create tag with subject and body content' '
470 cat >>msg <<-\EOF &&
471 the subject line
473 first body line
474 second body line
476 git tag -F msg subject-body
478 test_atom refs/tags/subject-body subject 'the subject line'
479 test_atom refs/tags/subject-body body 'first body line
480 second body line
482 test_atom refs/tags/subject-body contents 'the subject line
484 first body line
485 second body line
488 test_expect_success 'create tag with multiline subject' '
489 cat >msg <<-\EOF &&
490 first subject line
491 second subject line
493 first body line
494 second body line
496 git tag -F msg multiline
498 test_atom refs/tags/multiline subject 'first subject line second subject line'
499 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
500 test_atom refs/tags/multiline body 'first body line
501 second body line
503 test_atom refs/tags/multiline contents:body 'first body line
504 second body line
506 test_atom refs/tags/multiline contents:signature ''
507 test_atom refs/tags/multiline contents 'first subject line
508 second subject line
510 first body line
511 second body line
514 test_expect_success GPG 'create signed tags' '
515 git tag -s -m "" signed-empty &&
516 git tag -s -m "subject line" signed-short &&
517 cat >msg <<-\EOF &&
518 subject line
520 body contents
522 git tag -s -F msg signed-long
525 sig='-----BEGIN PGP SIGNATURE-----
526 -----END PGP SIGNATURE-----
529 PREREQ=GPG
530 test_atom refs/tags/signed-empty subject ''
531 test_atom refs/tags/signed-empty contents:subject ''
532 test_atom refs/tags/signed-empty body "$sig"
533 test_atom refs/tags/signed-empty contents:body ''
534 test_atom refs/tags/signed-empty contents:signature "$sig"
535 test_atom refs/tags/signed-empty contents "$sig"
537 test_atom refs/tags/signed-short subject 'subject line'
538 test_atom refs/tags/signed-short contents:subject 'subject line'
539 test_atom refs/tags/signed-short body "$sig"
540 test_atom refs/tags/signed-short contents:body ''
541 test_atom refs/tags/signed-short contents:signature "$sig"
542 test_atom refs/tags/signed-short contents "subject line
543 $sig"
545 test_atom refs/tags/signed-long subject 'subject line'
546 test_atom refs/tags/signed-long contents:subject 'subject line'
547 test_atom refs/tags/signed-long body "body contents
548 $sig"
549 test_atom refs/tags/signed-long contents:body 'body contents
551 test_atom refs/tags/signed-long contents:signature "$sig"
552 test_atom refs/tags/signed-long contents "subject line
554 body contents
555 $sig"
557 cat >expected <<EOF
558 $(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
559 $(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
562 test_expect_success 'Verify sort with multiple keys' '
563 git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
564 refs/tags/bogo refs/tags/master > actual &&
565 test_cmp expected actual
569 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
570 test_when_finished "git checkout master" &&
571 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
572 sed -e "s/^\* / /" actual >expect &&
573 git checkout --orphan HEAD &&
574 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
575 test_cmp expect actual
578 cat >trailers <<EOF
579 Reviewed-by: A U Thor <author@example.com>
580 Signed-off-by: A U Thor <author@example.com>
583 test_expect_success 'basic atom: head contents:trailers' '
584 echo "Some contents" > two &&
585 git add two &&
586 git commit -F - <<-EOF &&
587 trailers: this commit message has trailers
589 Some message contents
591 $(cat trailers)
593 git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
594 sanitize_pgp <actual >actual.clean &&
595 # git for-each-ref ends with a blank line
596 cat >expect <<-EOF &&
597 $(cat trailers)
600 test_cmp expect actual.clean
603 test_expect_success 'Add symbolic ref for the following tests' '
604 git symbolic-ref refs/heads/sym refs/heads/master
607 cat >expected <<EOF
608 refs/heads/master
611 test_expect_success 'Verify usage of %(symref) atom' '
612 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
613 test_cmp expected actual
616 cat >expected <<EOF
617 heads/master
620 test_expect_success 'Verify usage of %(symref:short) atom' '
621 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
622 test_cmp expected actual
625 cat >expected <<EOF
626 master
629 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
630 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
631 test_cmp expected actual
634 test_done