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