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