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