d3d1a97db9709c29b829cf64df4fa6cce04ed6fe
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 '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
155'
156
157test_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
164'
165
166test_expect_success 'Check invalid format specifiers are errors' '
167 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
168'
169
170test_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)"
174'
175
176test_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'
184 EOF
185 (
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
194}
195
196test_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"
201'
202
203test_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"
208'
209
210test_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"
212'
213
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
218test_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
222'
223
224# We just check that this is the same as "relative" for now.
225test_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)"
230'
231
232test_expect_success 'Check format "short" date fields output' '
233 test_date short 2006-07-04 2006-07-04 2006-07-04
234'
235
236test_expect_success 'Check format "short-local" date fields output' '
237 test_date short-local 2006-07-03 2006-07-03 2006-07-03
238'
239
240test_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"
245'
246
247test_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"
252'
253
254test_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"
256'
257
258test_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"
263'
264
265test_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"
267'
268
269test_expect_success 'Check format "raw" date fields output' '
270 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
271'
272
273test_expect_success 'Check format "raw-local" date fields output' '
274 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
275'
276
277test_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
283'
284
285test_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
291'
292
293test_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
301'
302
303cat >expected <<\EOF
304refs/heads/master
305refs/remotes/origin/master
306refs/tags/testtag
307EOF
308
309test_expect_success 'Verify ascending sort' '
310 git for-each-ref --format="%(refname)" --sort=refname >actual &&
311 test_cmp expected actual
312'
313
314
315cat >expected <<\EOF
316refs/tags/testtag
317refs/remotes/origin/master
318refs/heads/master
319EOF
320
321test_expect_success 'Verify descending sort' '
322 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
323 test_cmp expected actual
324'
325
326cat >expected <<\EOF
327'refs/heads/master'
328'refs/remotes/origin/master'
329'refs/tags/testtag'
330EOF
331
332test_expect_success 'Quoting style: shell' '
333 git for-each-ref --shell --format="%(refname)" >actual &&
334 test_cmp expected actual
335'
336
337test_expect_success 'Quoting style: perl' '
338 git for-each-ref --perl --format="%(refname)" >actual &&
339 test_cmp expected actual
340'
341
342test_expect_success 'Quoting style: python' '
343 git for-each-ref --python --format="%(refname)" >actual &&
344 test_cmp expected actual
345'
346
347cat >expected <<\EOF
348"refs/heads/master"
349"refs/remotes/origin/master"
350"refs/tags/testtag"
351EOF
352
353test_expect_success 'Quoting style: tcl' '
354 git for-each-ref --tcl --format="%(refname)" >actual &&
355 test_cmp expected actual
356'
357
358for 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)
365 "
366done
367
368test_expect_success 'setup for upstream:track[short]' '
369 test_commit two
370'
371
372test_atom head upstream:track '[ahead 1]'
373test_atom head upstream:trackshort '>'
374test_atom head upstream:track,nobracket 'ahead 1'
375test_atom head upstream:nobracket,track 'ahead 1'
376test_atom head push:track '[ahead 1]'
377test_atom head push:trackshort '>'
378
379test_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
382'
383
384test_expect_success 'Check that :track[short] works when upstream is invalid' '
385 cat >expected <<-\EOF &&
386 [gone]
387
388 EOF
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
395'
396
397test_expect_success 'Check for invalid refname format' '
398 test_must_fail git for-each-ref --format="%(refname:INVALID)"
399'
400
401get_color ()
402{
403 git config --get-color no.such.slot "$1"
404}
405
406cat >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)
411EOF
412
413test_expect_success 'Check %(color:...) ' '
414 git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
415 test_cmp expected actual
416'
417
418cat >expected <<\EOF
419heads/master
420tags/master
421EOF
422
423test_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
433'
434
435cat >expected <<\EOF
436heads/master
437master
438EOF
439
440test_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
444'
445
446cat >expected <<\EOF
447heads/ambiguous
448ambiguous
449EOF
450
451test_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
457'
458
459test_expect_success 'an unusual tag with an incomplete line' '
460
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
466
467'
468
469test_expect_success 'create tag with subject and body content' '
470 cat >>msg <<-\EOF &&
471 the subject line
472
473 first body line
474 second body line
475 EOF
476 git tag -F msg subject-body
477'
478test_atom refs/tags/subject-body subject 'the subject line'
479test_atom refs/tags/subject-body body 'first body line
480second body line
481'
482test_atom refs/tags/subject-body contents 'the subject line
483
484first body line
485second body line
486'
487
488test_expect_success 'create tag with multiline subject' '
489 cat >msg <<-\EOF &&
490 first subject line
491 second subject line
492
493 first body line
494 second body line
495 EOF
496 git tag -F msg multiline
497'
498test_atom refs/tags/multiline subject 'first subject line second subject line'
499test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
500test_atom refs/tags/multiline body 'first body line
501second body line
502'
503test_atom refs/tags/multiline contents:body 'first body line
504second body line
505'
506test_atom refs/tags/multiline contents:signature ''
507test_atom refs/tags/multiline contents 'first subject line
508second subject line
509
510first body line
511second body line
512'
513
514test_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
519
520 body contents
521 EOF
522 git tag -s -F msg signed-long
523'
524
525sig='-----BEGIN PGP SIGNATURE-----
526-----END PGP SIGNATURE-----
527'
528
529PREREQ=GPG
530test_atom refs/tags/signed-empty subject ''
531test_atom refs/tags/signed-empty contents:subject ''
532test_atom refs/tags/signed-empty body "$sig"
533test_atom refs/tags/signed-empty contents:body ''
534test_atom refs/tags/signed-empty contents:signature "$sig"
535test_atom refs/tags/signed-empty contents "$sig"
536
537test_atom refs/tags/signed-short subject 'subject line'
538test_atom refs/tags/signed-short contents:subject 'subject line'
539test_atom refs/tags/signed-short body "$sig"
540test_atom refs/tags/signed-short contents:body ''
541test_atom refs/tags/signed-short contents:signature "$sig"
542test_atom refs/tags/signed-short contents "subject line
543$sig"
544
545test_atom refs/tags/signed-long subject 'subject line'
546test_atom refs/tags/signed-long contents:subject 'subject line'
547test_atom refs/tags/signed-long body "body contents
548$sig"
549test_atom refs/tags/signed-long contents:body 'body contents
550'
551test_atom refs/tags/signed-long contents:signature "$sig"
552test_atom refs/tags/signed-long contents "subject line
553
554body contents
555$sig"
556
557cat >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
560EOF
561
562test_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
566'
567
568
569test_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
576'
577
578cat >trailers <<EOF
579Reviewed-by: A U Thor <author@example.com>
580Signed-off-by: A U Thor <author@example.com>
581EOF
582
583test_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
588
589 Some message contents
590
591 $(cat trailers)
592 EOF
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)
598
599 EOF
600 test_cmp expect actual.clean
601'
602
603test_expect_success 'Add symbolic ref for the following tests' '
604 git symbolic-ref refs/heads/sym refs/heads/master
605'
606
607cat >expected <<EOF
608refs/heads/master
609EOF
610
611test_expect_success 'Verify usage of %(symref) atom' '
612 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
613 test_cmp expected actual
614'
615
616cat >expected <<EOF
617heads/master
618EOF
619
620test_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
623'
624
625cat >expected <<EOF
626master
627EOF
628
629test_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
632'
633
634test_done