1#!/bin/sh
2#
3# Copyright (c) 2007 Andy Parkins
4#
5
6test_description='for-each-ref test'
7
8. ./test-lib.sh
9. "$TEST_DIRECTORY"/lib-gpg.sh
10
11# Mon Jul 3 23:18:43 2006 +0000
12datestamp=1151968723
13setdate_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
19}
20
21test_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
35'
36
37test_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
49 "
50}
51
52test_atom head refname refs/heads/master
53test_atom head refname:short master
54test_atom head refname:lstrip=1 heads/master
55test_atom head refname:lstrip=2 master
56test_atom head upstream refs/remotes/origin/master
57test_atom head upstream:short origin/master
58test_atom head upstream:lstrip=2 origin/master
59test_atom head push refs/remotes/myfork/master
60test_atom head push:short myfork/master
61test_atom head push:lstrip=1 remotes/myfork/master
62test_atom head objecttype commit
63test_atom head objectsize 171
64test_atom head objectname $(git rev-parse refs/heads/master)
65test_atom head objectname:short $(git rev-parse --short refs/heads/master)
66test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
67test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
68test_atom head tree $(git rev-parse refs/heads/master^{tree})
69test_atom head parent ''
70test_atom head numparent 0
71test_atom head object ''
72test_atom head type ''
73test_atom head '*objectname' ''
74test_atom head '*objecttype' ''
75test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
76test_atom head authorname 'A U Thor'
77test_atom head authoremail '<author@example.com>'
78test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
79test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
80test_atom head committername 'C O Mitter'
81test_atom head committeremail '<committer@example.com>'
82test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
83test_atom head tag ''
84test_atom head tagger ''
85test_atom head taggername ''
86test_atom head taggeremail ''
87test_atom head taggerdate ''
88test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
89test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
90test_atom head subject 'Initial'
91test_atom head contents:subject 'Initial'
92test_atom head body ''
93test_atom head contents:body ''
94test_atom head contents:signature ''
95test_atom head contents 'Initial
96'
97test_atom head HEAD '*'
98
99test_atom tag refname refs/tags/testtag
100test_atom tag refname:short testtag
101test_atom tag upstream ''
102test_atom tag push ''
103test_atom tag objecttype tag
104test_atom tag objectsize 154
105test_atom tag objectname $(git rev-parse refs/tags/testtag)
106test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
107test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
108test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
109test_atom tag tree ''
110test_atom tag parent ''
111test_atom tag numparent ''
112test_atom tag object $(git rev-parse refs/tags/testtag^0)
113test_atom tag type 'commit'
114test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
115test_atom tag '*objecttype' 'commit'
116test_atom tag author ''
117test_atom tag authorname ''
118test_atom tag authoremail ''
119test_atom tag authordate ''
120test_atom tag committer ''
121test_atom tag committername ''
122test_atom tag committeremail ''
123test_atom tag committerdate ''
124test_atom tag tag 'testtag'
125test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
126test_atom tag taggername 'C O Mitter'
127test_atom tag taggeremail '<committer@example.com>'
128test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
129test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
130test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
131test_atom tag subject 'Tagging at 1151968727'
132test_atom tag contents:subject 'Tagging at 1151968727'
133test_atom tag body ''
134test_atom tag contents:body ''
135test_atom tag contents:signature ''
136test_atom tag contents 'Tagging at 1151968727
137'
138test_atom tag HEAD ' '
139
140test_expect_success 'Check invalid atoms names are errors' '
141 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
142'
143
144test_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)"
148'
149
150test_expect_success 'stripping refnames too far gives an error' '
151 test_must_fail git for-each-ref --format="%(refname:lstrip=3)"
152'
153
154test_expect_success 'Check format specifiers are ignored in naming date atoms' '
155 git for-each-ref --format="%(authordate)" refs/heads &&
156 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
157 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
158 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
159'
160
161test_expect_success 'Check valid format specifiers for date fields' '
162 git for-each-ref --format="%(authordate:default)" refs/heads &&
163 git for-each-ref --format="%(authordate:relative)" refs/heads &&
164 git for-each-ref --format="%(authordate:short)" refs/heads &&
165 git for-each-ref --format="%(authordate:local)" refs/heads &&
166 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
167 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
168'
169
170test_expect_success 'Check invalid format specifiers are errors' '
171 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
172'
173
174test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
175 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
176 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
177 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
178'
179
180test_date () {
181 f=$1 &&
182 committer_date=$2 &&
183 author_date=$3 &&
184 tagger_date=$4 &&
185 cat >expected <<-EOF &&
186 'refs/heads/master' '$committer_date' '$author_date'
187 'refs/tags/testtag' '$tagger_date'
188 EOF
189 (
190 git for-each-ref --shell \
191 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
192 refs/heads &&
193 git for-each-ref --shell \
194 --format="%(refname) %(taggerdate${f:+:$f})" \
195 refs/tags
196 ) >actual &&
197 test_cmp expected actual
198}
199
200test_expect_success 'Check unformatted date fields output' '
201 test_date "" \
202 "Tue Jul 4 01:18:43 2006 +0200" \
203 "Tue Jul 4 01:18:44 2006 +0200" \
204 "Tue Jul 4 01:18:45 2006 +0200"
205'
206
207test_expect_success 'Check format "default" formatted date fields output' '
208 test_date default \
209 "Tue Jul 4 01:18:43 2006 +0200" \
210 "Tue Jul 4 01:18:44 2006 +0200" \
211 "Tue Jul 4 01:18:45 2006 +0200"
212'
213
214test_expect_success 'Check format "default-local" date fields output' '
215 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"
216'
217
218# Don't know how to do relative check because I can't know when this script
219# is going to be run and can't fake the current time to git, and hence can't
220# provide expected output. Instead, I'll just make sure that "relative"
221# doesn't exit in error
222test_expect_success 'Check format "relative" date fields output' '
223 f=relative &&
224 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
225 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
226'
227
228# We just check that this is the same as "relative" for now.
229test_expect_success 'Check format "relative-local" date fields output' '
230 test_date relative-local \
231 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
232 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
233 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
234'
235
236test_expect_success 'Check format "short" date fields output' '
237 test_date short 2006-07-04 2006-07-04 2006-07-04
238'
239
240test_expect_success 'Check format "short-local" date fields output' '
241 test_date short-local 2006-07-03 2006-07-03 2006-07-03
242'
243
244test_expect_success 'Check format "local" date fields output' '
245 test_date local \
246 "Mon Jul 3 23:18:43 2006" \
247 "Mon Jul 3 23:18:44 2006" \
248 "Mon Jul 3 23:18:45 2006"
249'
250
251test_expect_success 'Check format "iso8601" date fields output' '
252 test_date iso8601 \
253 "2006-07-04 01:18:43 +0200" \
254 "2006-07-04 01:18:44 +0200" \
255 "2006-07-04 01:18:45 +0200"
256'
257
258test_expect_success 'Check format "iso8601-local" date fields output' '
259 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"
260'
261
262test_expect_success 'Check format "rfc2822" date fields output' '
263 test_date rfc2822 \
264 "Tue, 4 Jul 2006 01:18:43 +0200" \
265 "Tue, 4 Jul 2006 01:18:44 +0200" \
266 "Tue, 4 Jul 2006 01:18:45 +0200"
267'
268
269test_expect_success 'Check format "rfc2822-local" date fields output' '
270 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"
271'
272
273test_expect_success 'Check format "raw" date fields output' '
274 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
275'
276
277test_expect_success 'Check format "raw-local" date fields output' '
278 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
279'
280
281test_expect_success 'Check format of strftime date fields' '
282 echo "my date is 2006-07-04" >expected &&
283 git for-each-ref \
284 --format="%(authordate:format:my date is %Y-%m-%d)" \
285 refs/heads >actual &&
286 test_cmp expected actual
287'
288
289test_expect_success 'Check format of strftime-local date fields' '
290 echo "my date is 2006-07-03" >expected &&
291 git for-each-ref \
292 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
293 refs/heads >actual &&
294 test_cmp expected actual
295'
296
297test_expect_success 'exercise strftime with odd fields' '
298 echo >expected &&
299 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
300 test_cmp expected actual &&
301 long="long format -- $_z40$_z40$_z40$_z40$_z40$_z40$_z40" &&
302 echo $long >expected &&
303 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
304 test_cmp expected actual
305'
306
307cat >expected <<\EOF
308refs/heads/master
309refs/remotes/origin/master
310refs/tags/testtag
311EOF
312
313test_expect_success 'Verify ascending sort' '
314 git for-each-ref --format="%(refname)" --sort=refname >actual &&
315 test_cmp expected actual
316'
317
318
319cat >expected <<\EOF
320refs/tags/testtag
321refs/remotes/origin/master
322refs/heads/master
323EOF
324
325test_expect_success 'Verify descending sort' '
326 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
327 test_cmp expected actual
328'
329
330cat >expected <<\EOF
331'refs/heads/master'
332'refs/remotes/origin/master'
333'refs/tags/testtag'
334EOF
335
336test_expect_success 'Quoting style: shell' '
337 git for-each-ref --shell --format="%(refname)" >actual &&
338 test_cmp expected actual
339'
340
341test_expect_success 'Quoting style: perl' '
342 git for-each-ref --perl --format="%(refname)" >actual &&
343 test_cmp expected actual
344'
345
346test_expect_success 'Quoting style: python' '
347 git for-each-ref --python --format="%(refname)" >actual &&
348 test_cmp expected actual
349'
350
351cat >expected <<\EOF
352"refs/heads/master"
353"refs/remotes/origin/master"
354"refs/tags/testtag"
355EOF
356
357test_expect_success 'Quoting style: tcl' '
358 git for-each-ref --tcl --format="%(refname)" >actual &&
359 test_cmp expected actual
360'
361
362for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
363 test_expect_success "more than one quoting style: $i" "
364 git for-each-ref $i 2>&1 | (read line &&
365 case \$line in
366 \"error: more than one quoting style\"*) : happy;;
367 *) false
368 esac)
369 "
370done
371
372test_expect_success 'setup for upstream:track[short]' '
373 test_commit two
374'
375
376test_atom head upstream:track '[ahead 1]'
377test_atom head upstream:trackshort '>'
378test_atom head upstream:track,nobracket 'ahead 1'
379test_atom head upstream:nobracket,track 'ahead 1'
380test_atom head push:track '[ahead 1]'
381test_atom head push:trackshort '>'
382
383test_expect_success 'Check that :track[short] cannot be used with other atoms' '
384 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
385 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
386'
387
388test_expect_success 'Check that :track[short] works when upstream is invalid' '
389 cat >expected <<-\EOF &&
390 [gone]
391
392 EOF
393 test_when_finished "git config branch.master.merge refs/heads/master" &&
394 git config branch.master.merge refs/heads/does-not-exist &&
395 git for-each-ref \
396 --format="%(upstream:track)$LF%(upstream:trackshort)" \
397 refs/heads >actual &&
398 test_cmp expected actual
399'
400
401test_expect_success 'Check for invalid refname format' '
402 test_must_fail git for-each-ref --format="%(refname:INVALID)"
403'
404
405get_color ()
406{
407 git config --get-color no.such.slot "$1"
408}
409
410cat >expected <<EOF
411$(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
412$(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
413$(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
414$(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
415EOF
416
417test_expect_success 'Check %(color:...) ' '
418 git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
419 test_cmp expected actual
420'
421
422cat >expected <<\EOF
423heads/master
424tags/master
425EOF
426
427test_expect_success 'Check ambiguous head and tag refs (strict)' '
428 git config --bool core.warnambiguousrefs true &&
429 git checkout -b newtag &&
430 echo "Using $datestamp" > one &&
431 git add one &&
432 git commit -m "Branch" &&
433 setdate_and_increment &&
434 git tag -m "Tagging at $datestamp" master &&
435 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
436 test_cmp expected actual
437'
438
439cat >expected <<\EOF
440heads/master
441master
442EOF
443
444test_expect_success 'Check ambiguous head and tag refs (loose)' '
445 git config --bool core.warnambiguousrefs false &&
446 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
447 test_cmp expected actual
448'
449
450cat >expected <<\EOF
451heads/ambiguous
452ambiguous
453EOF
454
455test_expect_success 'Check ambiguous head and tag refs II (loose)' '
456 git checkout master &&
457 git tag ambiguous testtag^0 &&
458 git branch ambiguous testtag^0 &&
459 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
460 test_cmp expected actual
461'
462
463test_expect_success 'an unusual tag with an incomplete line' '
464
465 git tag -m "bogo" bogo &&
466 bogo=$(git cat-file tag bogo) &&
467 bogo=$(printf "%s" "$bogo" | git mktag) &&
468 git tag -f bogo "$bogo" &&
469 git for-each-ref --format "%(body)" refs/tags/bogo
470
471'
472
473test_expect_success 'create tag with subject and body content' '
474 cat >>msg <<-\EOF &&
475 the subject line
476
477 first body line
478 second body line
479 EOF
480 git tag -F msg subject-body
481'
482test_atom refs/tags/subject-body subject 'the subject line'
483test_atom refs/tags/subject-body body 'first body line
484second body line
485'
486test_atom refs/tags/subject-body contents 'the subject line
487
488first body line
489second body line
490'
491
492test_expect_success 'create tag with multiline subject' '
493 cat >msg <<-\EOF &&
494 first subject line
495 second subject line
496
497 first body line
498 second body line
499 EOF
500 git tag -F msg multiline
501'
502test_atom refs/tags/multiline subject 'first subject line second subject line'
503test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
504test_atom refs/tags/multiline body 'first body line
505second body line
506'
507test_atom refs/tags/multiline contents:body 'first body line
508second body line
509'
510test_atom refs/tags/multiline contents:signature ''
511test_atom refs/tags/multiline contents 'first subject line
512second subject line
513
514first body line
515second body line
516'
517
518test_expect_success GPG 'create signed tags' '
519 git tag -s -m "" signed-empty &&
520 git tag -s -m "subject line" signed-short &&
521 cat >msg <<-\EOF &&
522 subject line
523
524 body contents
525 EOF
526 git tag -s -F msg signed-long
527'
528
529sig='-----BEGIN PGP SIGNATURE-----
530-----END PGP SIGNATURE-----
531'
532
533PREREQ=GPG
534test_atom refs/tags/signed-empty subject ''
535test_atom refs/tags/signed-empty contents:subject ''
536test_atom refs/tags/signed-empty body "$sig"
537test_atom refs/tags/signed-empty contents:body ''
538test_atom refs/tags/signed-empty contents:signature "$sig"
539test_atom refs/tags/signed-empty contents "$sig"
540
541test_atom refs/tags/signed-short subject 'subject line'
542test_atom refs/tags/signed-short contents:subject 'subject line'
543test_atom refs/tags/signed-short body "$sig"
544test_atom refs/tags/signed-short contents:body ''
545test_atom refs/tags/signed-short contents:signature "$sig"
546test_atom refs/tags/signed-short contents "subject line
547$sig"
548
549test_atom refs/tags/signed-long subject 'subject line'
550test_atom refs/tags/signed-long contents:subject 'subject line'
551test_atom refs/tags/signed-long body "body contents
552$sig"
553test_atom refs/tags/signed-long contents:body 'body contents
554'
555test_atom refs/tags/signed-long contents:signature "$sig"
556test_atom refs/tags/signed-long contents "subject line
557
558body contents
559$sig"
560
561cat >expected <<EOF
562$(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
563$(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
564EOF
565
566test_expect_success 'Verify sort with multiple keys' '
567 git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
568 refs/tags/bogo refs/tags/master > actual &&
569 test_cmp expected actual
570'
571
572
573test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
574 test_when_finished "git checkout master" &&
575 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
576 sed -e "s/^\* / /" actual >expect &&
577 git checkout --orphan HEAD &&
578 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
579 test_cmp expect actual
580'
581
582cat >trailers <<EOF
583Reviewed-by: A U Thor <author@example.com>
584Signed-off-by: A U Thor <author@example.com>
585EOF
586
587test_expect_success 'basic atom: head contents:trailers' '
588 echo "Some contents" > two &&
589 git add two &&
590 git commit -F - <<-EOF &&
591 trailers: this commit message has trailers
592
593 Some message contents
594
595 $(cat trailers)
596 EOF
597 git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
598 sanitize_pgp <actual >actual.clean &&
599 # git for-each-ref ends with a blank line
600 cat >expect <<-EOF &&
601 $(cat trailers)
602
603 EOF
604 test_cmp expect actual.clean
605'
606
607test_expect_success 'Add symbolic ref for the following tests' '
608 git symbolic-ref refs/heads/sym refs/heads/master
609'
610
611cat >expected <<EOF
612refs/heads/master
613EOF
614
615test_expect_success 'Verify usage of %(symref) atom' '
616 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
617 test_cmp expected actual
618'
619
620cat >expected <<EOF
621heads/master
622EOF
623
624test_expect_success 'Verify usage of %(symref:short) atom' '
625 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
626 test_cmp expected actual
627'
628
629cat >expected <<EOF
630master
631EOF
632
633test_expect_success 'Verify usage of %(symref:lstrip) atom' '
634 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
635 test_cmp expected actual
636'
637
638test_done