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