3 # Copyright (c) 2007 Andy Parkins
6 test_description
='for-each-ref test'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=master
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12 .
"$TEST_DIRECTORY"/lib-gpg.sh
13 .
"$TEST_DIRECTORY"/lib-terminal.sh
15 # Mon Jul 3 23:18:43 2006 +0000
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
25 test_expect_success setup
'
26 test_oid_cache <<-EOF &&
30 setdate_and_increment &&
31 echo "Using $datestamp" > one &&
33 git commit -m "Initial" &&
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
48 head) ref
=refs
/heads
/main
;;
49 tag
) ref
=refs
/tags
/testtag
;;
50 sym
) ref
=refs
/heads
/sym
;;
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
59 # Automatically test "contents:size" atom after testing "contents"
60 if test "$2" = "contents"
62 # for commit leg, $3 is changed there
63 expect
=$
(printf '%s' "$3" |
wc -c)
64 test_expect_
${4:-success} $PREREQ "basic atom: $1 contents:size" '
65 type=$(git cat-file -t "$ref") &&
68 # We cannot use $3 as it expects sanitize_pgp to run
69 git cat-file tag $ref >out &&
70 expect=$(tail -n +6 out | wc -c) &&
75 : "use the calculated expect" ;;
77 BUG "unknown object type" ;;
79 # Leave $expect unquoted to lose possible leading whitespaces
80 echo $expect >expected &&
81 git for-each-ref --format="%(contents:size)" "$ref" >actual &&
82 test_cmp expected actual
87 hexlen
=$
(test_oid hexsz
)
88 disklen
=$
(test_oid disklen
)
90 test_atom
head refname refs
/heads
/main
91 test_atom
head refname
: refs
/heads
/main
92 test_atom
head refname
:short main
93 test_atom
head refname
:lstrip
=1 heads
/main
94 test_atom
head refname
:lstrip
=2 main
95 test_atom
head refname
:lstrip
=-1 main
96 test_atom
head refname
:lstrip
=-2 heads
/main
97 test_atom
head refname
:rstrip
=1 refs
/heads
98 test_atom
head refname
:rstrip
=2 refs
99 test_atom
head refname
:rstrip
=-1 refs
100 test_atom
head refname
:rstrip
=-2 refs
/heads
101 test_atom
head refname
:strip
=1 heads
/main
102 test_atom
head refname
:strip
=2 main
103 test_atom
head refname
:strip
=-1 main
104 test_atom
head refname
:strip
=-2 heads
/main
105 test_atom
head upstream refs
/remotes
/origin
/main
106 test_atom
head upstream
:short origin
/main
107 test_atom
head upstream
:lstrip
=2 origin
/main
108 test_atom
head upstream
:lstrip
=-2 origin
/main
109 test_atom
head upstream
:rstrip
=2 refs
/remotes
110 test_atom
head upstream
:rstrip
=-2 refs
/remotes
111 test_atom
head upstream
:strip
=2 origin
/main
112 test_atom
head upstream
:strip
=-2 origin
/main
113 test_atom
head push refs
/remotes
/myfork
/main
114 test_atom
head push
:short myfork
/main
115 test_atom
head push
:lstrip
=1 remotes
/myfork
/main
116 test_atom
head push
:lstrip
=-1 main
117 test_atom
head push
:rstrip
=1 refs
/remotes
/myfork
118 test_atom
head push
:rstrip
=-1 refs
119 test_atom
head push
:strip
=1 remotes
/myfork
/main
120 test_atom
head push
:strip
=-1 main
121 test_atom
head objecttype commit
122 test_atom
head objectsize $
((131 + hexlen
))
123 test_atom
head objectsize
:disk
$disklen
124 test_atom
head deltabase
$ZERO_OID
125 test_atom
head objectname $
(git rev-parse refs
/heads
/main
)
126 test_atom
head objectname
:short $
(git rev-parse
--short refs
/heads
/main
)
127 test_atom
head objectname
:short
=1 $
(git rev-parse
--short=1 refs
/heads
/main
)
128 test_atom
head objectname
:short
=10 $
(git rev-parse
--short=10 refs
/heads
/main
)
129 test_atom
head tree $
(git rev-parse refs
/heads
/main^
{tree
})
130 test_atom
head tree
:short $
(git rev-parse
--short refs
/heads
/main^
{tree
})
131 test_atom
head tree
:short
=1 $
(git rev-parse
--short=1 refs
/heads
/main^
{tree
})
132 test_atom
head tree
:short
=10 $
(git rev-parse
--short=10 refs
/heads
/main^
{tree
})
133 test_atom
head parent
''
134 test_atom
head parent
:short
''
135 test_atom
head parent
:short
=1 ''
136 test_atom
head parent
:short
=10 ''
137 test_atom
head numparent
0
138 test_atom
head object
''
139 test_atom
head type ''
140 test_atom
head '*objectname' ''
141 test_atom
head '*objecttype' ''
142 test_atom
head author
'A U Thor <author@example.com> 1151968724 +0200'
143 test_atom
head authorname
'A U Thor'
144 test_atom
head authoremail
'<author@example.com>'
145 test_atom
head authoremail
:trim
'author@example.com'
146 test_atom
head authoremail
:localpart
'author'
147 test_atom
head authordate
'Tue Jul 4 01:18:44 2006 +0200'
148 test_atom
head committer
'C O Mitter <committer@example.com> 1151968723 +0200'
149 test_atom
head committername
'C O Mitter'
150 test_atom
head committeremail
'<committer@example.com>'
151 test_atom
head committeremail
:trim
'committer@example.com'
152 test_atom
head committeremail
:localpart
'committer'
153 test_atom
head committerdate
'Tue Jul 4 01:18:43 2006 +0200'
154 test_atom
head tag
''
155 test_atom
head tagger
''
156 test_atom
head taggername
''
157 test_atom
head taggeremail
''
158 test_atom
head taggeremail
:trim
''
159 test_atom
head taggeremail
:localpart
''
160 test_atom
head taggerdate
''
161 test_atom
head creator
'C O Mitter <committer@example.com> 1151968723 +0200'
162 test_atom
head creatordate
'Tue Jul 4 01:18:43 2006 +0200'
163 test_atom
head subject
'Initial'
164 test_atom
head subject
:sanitize
'Initial'
165 test_atom
head contents
:subject
'Initial'
166 test_atom
head body
''
167 test_atom
head contents
:body
''
168 test_atom
head contents
:signature
''
169 test_atom
head contents
'Initial
171 test_atom
head HEAD
'*'
173 test_atom tag refname refs
/tags
/testtag
174 test_atom tag refname
:short testtag
175 test_atom tag upstream
''
176 test_atom tag push
''
177 test_atom tag objecttype tag
178 test_atom tag objectsize $
((114 + hexlen
))
179 test_atom tag objectsize
:disk
$disklen
180 test_atom tag
'*objectsize:disk' $disklen
181 test_atom tag deltabase
$ZERO_OID
182 test_atom tag
'*deltabase' $ZERO_OID
183 test_atom tag objectname $
(git rev-parse refs
/tags
/testtag
)
184 test_atom tag objectname
:short $
(git rev-parse
--short refs
/tags
/testtag
)
185 test_atom
head objectname
:short
=1 $
(git rev-parse
--short=1 refs
/heads
/main
)
186 test_atom
head objectname
:short
=10 $
(git rev-parse
--short=10 refs
/heads
/main
)
187 test_atom tag tree
''
188 test_atom tag tree
:short
''
189 test_atom tag tree
:short
=1 ''
190 test_atom tag tree
:short
=10 ''
191 test_atom tag parent
''
192 test_atom tag parent
:short
''
193 test_atom tag parent
:short
=1 ''
194 test_atom tag parent
:short
=10 ''
195 test_atom tag numparent
''
196 test_atom tag object $
(git rev-parse refs
/tags
/testtag^
0)
197 test_atom tag
type 'commit'
198 test_atom tag
'*objectname' $
(git rev-parse refs
/tags
/testtag^
{})
199 test_atom tag
'*objecttype' 'commit'
200 test_atom tag author
''
201 test_atom tag authorname
''
202 test_atom tag authoremail
''
203 test_atom tag authoremail
:trim
''
204 test_atom tag authoremail
:localpart
''
205 test_atom tag authordate
''
206 test_atom tag committer
''
207 test_atom tag committername
''
208 test_atom tag committeremail
''
209 test_atom tag committeremail
:trim
''
210 test_atom tag committeremail
:localpart
''
211 test_atom tag committerdate
''
212 test_atom tag tag
'testtag'
213 test_atom tag tagger
'C O Mitter <committer@example.com> 1151968725 +0200'
214 test_atom tag taggername
'C O Mitter'
215 test_atom tag taggeremail
'<committer@example.com>'
216 test_atom tag taggeremail
:trim
'committer@example.com'
217 test_atom tag taggeremail
:localpart
'committer'
218 test_atom tag taggerdate
'Tue Jul 4 01:18:45 2006 +0200'
219 test_atom tag creator
'C O Mitter <committer@example.com> 1151968725 +0200'
220 test_atom tag creatordate
'Tue Jul 4 01:18:45 2006 +0200'
221 test_atom tag subject
'Tagging at 1151968727'
222 test_atom tag subject
:sanitize
'Tagging-at-1151968727'
223 test_atom tag contents
:subject
'Tagging at 1151968727'
224 test_atom tag body
''
225 test_atom tag contents
:body
''
226 test_atom tag contents
:signature
''
227 test_atom tag contents
'Tagging at 1151968727
229 test_atom tag HEAD
' '
231 test_expect_success
'Check invalid atoms names are errors' '
232 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
235 test_expect_success
'Check format specifiers are ignored in naming date atoms' '
236 git for-each-ref --format="%(authordate)" refs/heads &&
237 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
238 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
239 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
242 test_expect_success
'Check valid format specifiers for date fields' '
243 git for-each-ref --format="%(authordate:default)" refs/heads &&
244 git for-each-ref --format="%(authordate:relative)" refs/heads &&
245 git for-each-ref --format="%(authordate:short)" refs/heads &&
246 git for-each-ref --format="%(authordate:local)" refs/heads &&
247 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
248 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
251 test_expect_success
'Check invalid format specifiers are errors' '
252 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
255 test_expect_success
'arguments to %(objectname:short=) must be positive integers' '
256 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
257 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
258 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
266 cat >expected
<<-EOF &&
267 'refs/heads/main' '$committer_date' '$author_date'
268 'refs/tags/testtag' '$tagger_date'
271 git for-each-ref
--shell \
272 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
274 git for-each-ref
--shell \
275 --format="%(refname) %(taggerdate${f:+:$f})" \
278 test_cmp expected actual
281 test_expect_success
'Check unformatted date fields output' '
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"
288 test_expect_success
'Check format "default" formatted date fields output' '
290 "Tue Jul 4 01:18:43 2006 +0200" \
291 "Tue Jul 4 01:18:44 2006 +0200" \
292 "Tue Jul 4 01:18:45 2006 +0200"
295 test_expect_success
'Check format "default-local" date fields output' '
296 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"
299 # Don't know how to do relative check because I can't know when this script
300 # is going to be run and can't fake the current time to git, and hence can't
301 # provide expected output. Instead, I'll just make sure that "relative"
302 # doesn't exit in error
303 test_expect_success
'Check format "relative" date fields output' '
305 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
306 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
309 # We just check that this is the same as "relative" for now.
310 test_expect_success
'Check format "relative-local" date fields output' '
311 test_date relative-local \
312 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
313 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
314 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
317 test_expect_success
'Check format "short" date fields output' '
318 test_date short 2006-07-04 2006-07-04 2006-07-04
321 test_expect_success
'Check format "short-local" date fields output' '
322 test_date short-local 2006-07-03 2006-07-03 2006-07-03
325 test_expect_success
'Check format "local" date fields output' '
327 "Mon Jul 3 23:18:43 2006" \
328 "Mon Jul 3 23:18:44 2006" \
329 "Mon Jul 3 23:18:45 2006"
332 test_expect_success
'Check format "iso8601" date fields output' '
334 "2006-07-04 01:18:43 +0200" \
335 "2006-07-04 01:18:44 +0200" \
336 "2006-07-04 01:18:45 +0200"
339 test_expect_success
'Check format "iso8601-local" date fields output' '
340 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"
343 test_expect_success
'Check format "rfc2822" date fields output' '
345 "Tue, 4 Jul 2006 01:18:43 +0200" \
346 "Tue, 4 Jul 2006 01:18:44 +0200" \
347 "Tue, 4 Jul 2006 01:18:45 +0200"
350 test_expect_success
'Check format "rfc2822-local" date fields output' '
351 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"
354 test_expect_success
'Check format "raw" date fields output' '
355 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
358 test_expect_success
'Check format "raw-local" date fields output' '
359 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
362 test_expect_success
'Check format of strftime date fields' '
363 echo "my date is 2006-07-04" >expected &&
365 --format="%(authordate:format:my date is %Y-%m-%d)" \
366 refs/heads >actual &&
367 test_cmp expected actual
370 test_expect_success
'Check format of strftime-local date fields' '
371 echo "my date is 2006-07-03" >expected &&
373 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
374 refs/heads >actual &&
375 test_cmp expected actual
378 test_expect_success
'exercise strftime with odd fields' '
380 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
381 test_cmp expected actual &&
382 long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
383 echo $long >expected &&
384 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
385 test_cmp expected actual
390 refs
/remotes
/origin
/main
394 test_expect_success
'Verify ascending sort' '
395 git for-each-ref --format="%(refname)" --sort=refname >actual &&
396 test_cmp expected actual
402 refs
/remotes
/origin
/main
406 test_expect_success
'Verify descending sort' '
407 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
408 test_cmp expected actual
416 test_expect_success
'exercise patterns with prefixes' '
418 test_when_finished "git tag -d testtag-2" &&
419 git for-each-ref --format="%(refname)" \
420 refs/tags/testtag refs/tags/testtag-2 >actual &&
421 test_cmp expected actual
429 test_expect_success
'exercise glob patterns with prefixes' '
431 test_when_finished "git tag -d testtag-2" &&
432 git for-each-ref --format="%(refname)" \
433 refs/tags/testtag "refs/tags/testtag-*" >actual &&
434 test_cmp expected actual
439 'refs/remotes/origin/main'
443 test_expect_success
'Quoting style: shell' '
444 git for-each-ref --shell --format="%(refname)" >actual &&
445 test_cmp expected actual
448 test_expect_success
'Quoting style: perl' '
449 git for-each-ref --perl --format="%(refname)" >actual &&
450 test_cmp expected actual
453 test_expect_success
'Quoting style: python' '
454 git for-each-ref --python --format="%(refname)" >actual &&
455 test_cmp expected actual
460 "refs/remotes/origin/main"
464 test_expect_success
'Quoting style: tcl' '
465 git for-each-ref --tcl --format="%(refname)" >actual &&
466 test_cmp expected actual
469 for i
in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
470 test_expect_success
"more than one quoting style: $i" "
471 test_must_fail git for-each-ref $i 2>err &&
472 grep '^error: more than one quoting style' err
476 test_expect_success
'setup for upstream:track[short]' '
480 test_atom
head upstream
:track
'[ahead 1]'
481 test_atom
head upstream
:trackshort
'>'
482 test_atom
head upstream
:track
,nobracket
'ahead 1'
483 test_atom
head upstream
:nobracket
,track
'ahead 1'
485 test_expect_success
'setup for push:track[short]' '
487 git update-ref refs/remotes/myfork/main main &&
491 test_atom
head push
:track
'[behind 1]'
492 test_atom
head push
:trackshort
'<'
494 test_expect_success
'Check that :track[short] cannot be used with other atoms' '
495 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
496 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
499 test_expect_success
'Check that :track[short] works when upstream is invalid' '
500 cat >expected <<-\EOF &&
504 test_when_finished "git config branch.main.merge refs/heads/main" &&
505 git config branch.main.merge refs/heads/does-not-exist &&
507 --format="%(upstream:track)$LF%(upstream:trackshort)" \
508 refs/heads >actual &&
509 test_cmp expected actual
512 test_expect_success
'Check for invalid refname format' '
513 test_must_fail git for-each-ref --format="%(refname:INVALID)"
516 test_expect_success
'set up color tests' '
517 cat >expected.color <<-EOF &&
518 $(git rev-parse --short refs/heads/main) <GREEN>main<RESET>
519 $(git rev-parse --short refs/remotes/myfork/main) <GREEN>myfork/main<RESET>
520 $(git rev-parse --short refs/remotes/origin/main) <GREEN>origin/main<RESET>
521 $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
522 $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
523 $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
525 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
526 color_format="%(objectname:short) %(color:green)%(refname:short)"
529 test_expect_success TTY
'%(color) shows color with a tty' '
530 test_terminal git for-each-ref --format="$color_format" >actual.raw &&
531 test_decode_color <actual.raw >actual &&
532 test_cmp expected.color actual
535 test_expect_success
'%(color) does not show color without tty' '
536 TERM=vt100 git for-each-ref --format="$color_format" >actual &&
537 test_cmp expected.bare actual
540 test_expect_success
'--color can override tty check' '
541 git for-each-ref --color --format="$color_format" >actual.raw &&
542 test_decode_color <actual.raw >actual &&
543 test_cmp expected.color actual
546 test_expect_success
'color.ui=always does not override tty check' '
547 git -c color.ui=always for-each-ref --format="$color_format" >actual &&
548 test_cmp expected.bare actual
556 test_expect_success
'Check ambiguous head and tag refs (strict)' '
557 git config --bool core.warnambiguousrefs true &&
558 git checkout -b newtag &&
559 echo "Using $datestamp" > one &&
561 git commit -m "Branch" &&
562 setdate_and_increment &&
563 git tag -m "Tagging at $datestamp" main &&
564 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
565 test_cmp expected actual
573 test_expect_success
'Check ambiguous head and tag refs (loose)' '
574 git config --bool core.warnambiguousrefs false &&
575 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
576 test_cmp expected actual
584 test_expect_success
'Check ambiguous head and tag refs II (loose)' '
586 git tag ambiguous testtag^0 &&
587 git branch ambiguous testtag^0 &&
588 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
589 test_cmp expected actual
592 test_expect_success
'create tag without tagger' '
593 git tag -a -m "Broken tag" taggerless &&
594 git tag -f taggerless $(git cat-file tag taggerless |
595 sed -e "/^tagger /d" |
596 git hash-object --stdin -w -t tag)
599 test_atom refs
/tags
/taggerless
type 'commit'
600 test_atom refs
/tags
/taggerless tag
'taggerless'
601 test_atom refs
/tags
/taggerless tagger
''
602 test_atom refs
/tags
/taggerless taggername
''
603 test_atom refs
/tags
/taggerless taggeremail
''
604 test_atom refs
/tags
/taggerless taggeremail
:trim
''
605 test_atom refs
/tags
/taggerless taggeremail
:localpart
''
606 test_atom refs
/tags
/taggerless taggerdate
''
607 test_atom refs
/tags
/taggerless committer
''
608 test_atom refs
/tags
/taggerless committername
''
609 test_atom refs
/tags
/taggerless committeremail
''
610 test_atom refs
/tags
/taggerless committeremail
:trim
''
611 test_atom refs
/tags
/taggerless committeremail
:localpart
''
612 test_atom refs
/tags
/taggerless committerdate
''
613 test_atom refs
/tags
/taggerless subject
'Broken tag'
615 test_expect_success
'an unusual tag with an incomplete line' '
617 git tag -m "bogo" bogo &&
618 bogo=$(git cat-file tag bogo) &&
619 bogo=$(printf "%s" "$bogo" | git mktag) &&
620 git tag -f bogo "$bogo" &&
621 git for-each-ref --format "%(body)" refs/tags/bogo
625 test_expect_success
'create tag with subject and body content' '
632 git tag -F msg subject-body
634 test_atom refs
/tags
/subject-body subject
'the subject line'
635 test_atom refs
/tags
/subject-body subject
:sanitize
'the-subject-line'
636 test_atom refs
/tags
/subject-body body
'first body line
639 test_atom refs
/tags
/subject-body contents
'the subject line
645 test_expect_success
'create tag with multiline subject' '
653 git tag -F msg multiline
655 test_atom refs
/tags
/multiline subject
'first subject line second subject line'
656 test_atom refs
/tags
/multiline subject
:sanitize
'first-subject-line-second-subject-line'
657 test_atom refs
/tags
/multiline contents
:subject
'first subject line second subject line'
658 test_atom refs
/tags
/multiline body
'first body line
661 test_atom refs
/tags
/multiline contents
:body
'first body line
664 test_atom refs
/tags
/multiline contents
:signature
''
665 test_atom refs
/tags
/multiline contents
'first subject line
672 test_expect_success GPG
'create signed tags' '
673 git tag -s -m "" signed-empty &&
674 git tag -s -m "subject line" signed-short &&
680 git tag -s -F msg signed-long
683 sig
='-----BEGIN PGP SIGNATURE-----
684 -----END PGP SIGNATURE-----
688 test_atom refs
/tags
/signed-empty subject
''
689 test_atom refs
/tags
/signed-empty subject
:sanitize
''
690 test_atom refs
/tags
/signed-empty contents
:subject
''
691 test_atom refs
/tags
/signed-empty body
"$sig"
692 test_atom refs
/tags
/signed-empty contents
:body
''
693 test_atom refs
/tags
/signed-empty contents
:signature
"$sig"
694 test_atom refs
/tags
/signed-empty contents
"$sig"
696 test_atom refs
/tags
/signed-short subject
'subject line'
697 test_atom refs
/tags
/signed-short subject
:sanitize
'subject-line'
698 test_atom refs
/tags
/signed-short contents
:subject
'subject line'
699 test_atom refs
/tags
/signed-short body
"$sig"
700 test_atom refs
/tags
/signed-short contents
:body
''
701 test_atom refs
/tags
/signed-short contents
:signature
"$sig"
702 test_atom refs
/tags
/signed-short contents
"subject line
705 test_atom refs
/tags
/signed-long subject
'subject line'
706 test_atom refs
/tags
/signed-long subject
:sanitize
'subject-line'
707 test_atom refs
/tags
/signed-long contents
:subject
'subject line'
708 test_atom refs
/tags
/signed-long body
"body contents
710 test_atom refs
/tags
/signed-long contents
:body
'body contents
712 test_atom refs
/tags
/signed-long contents
:signature
"$sig"
713 test_atom refs
/tags
/signed-long contents
"subject line
718 test_expect_success
'set up refs pointing to tree and blob' '
719 git update-ref refs/mytrees/first refs/heads/main^{tree} &&
720 git update-ref refs/myblobs/first refs/heads/main:one
723 test_atom refs
/mytrees
/first subject
""
724 test_atom refs
/mytrees
/first contents
:subject
""
725 test_atom refs
/mytrees
/first body
""
726 test_atom refs
/mytrees
/first contents
:body
""
727 test_atom refs
/mytrees
/first contents
:signature
""
728 test_atom refs
/mytrees
/first contents
""
730 test_atom refs
/myblobs
/first subject
""
731 test_atom refs
/myblobs
/first contents
:subject
""
732 test_atom refs
/myblobs
/first body
""
733 test_atom refs
/myblobs
/first contents
:body
""
734 test_atom refs
/myblobs
/first contents
:signature
""
735 test_atom refs
/myblobs
/first contents
""
737 test_expect_success
'set up multiple-sort tags' '
738 for when in 100000 200000
740 for email in user1 user2
744 GIT_COMMITTER_DATE="@$when +0000" \
745 GIT_COMMITTER_EMAIL="$email@example.com" \
746 git tag -m "tag $ref-$when-$email" \
747 multi-$ref-$when-$email || return 1
753 test_expect_success
'Verify sort with multiple keys' '
754 cat >expected <<-\EOF &&
755 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
756 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
757 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
758 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
759 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
760 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
761 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
762 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
765 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
769 "refs/tags/multi-*" >actual &&
770 test_cmp expected actual
773 test_expect_success
'equivalent sorts fall back on refname' '
774 cat >expected <<-\EOF &&
775 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
776 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
777 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
778 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
779 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
780 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
781 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
782 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
785 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
787 "refs/tags/multi-*" >actual &&
788 test_cmp expected actual
791 test_expect_success
'do not dereference NULL upon %(HEAD) on unborn branch' '
792 test_when_finished "git checkout main" &&
793 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
794 sed -e "s/^\* / /" actual >expect &&
795 git checkout --orphan orphaned-branch &&
796 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
797 test_cmp expect actual
801 Reviewed-by: A U Thor <author@example.com>
802 Signed-off-by: A U Thor <author@example.com>
803 [ v2 updated patch description ]
809 perl
-0pe 's/\n\s+/ /g'
812 test_expect_success
'set up trailers for next test' '
813 echo "Some contents" > two &&
815 git commit -F - <<-EOF
816 trailers: this commit message has trailers
818 Some message contents
824 test_trailer_option
() {
827 test_expect_success
"$title" '
828 git for-each-ref --format="%($option)" refs/heads/main >actual &&
829 test_cmp expect actual &&
830 git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
831 test_cmp expect actual
835 test_trailer_option
'%(trailers:unfold) unfolds trailers' \
836 'trailers:unfold' <<-EOF
841 test_trailer_option
'%(trailers:only) shows only "key: value" trailers' \
842 'trailers:only' <<-EOF
843 $(grep -v patch.description <trailers)
847 test_trailer_option
'%(trailers:only=no,only=true) shows only "key: value" trailers' \
848 'trailers:only=no,only=true' <<-EOF
849 $(grep -v patch.description <trailers)
853 test_trailer_option
'%(trailers:only=yes) shows only "key: value" trailers' \
854 'trailers:only=yes' <<-EOF
855 $(grep -v patch.description <trailers)
859 test_trailer_option
'%(trailers:only=no) shows all trailers' \
860 'trailers:only=no' <<-EOF
865 test_trailer_option
'%(trailers:only) and %(trailers:unfold) work together' \
866 'trailers:only,unfold' <<-EOF
867 $(grep -v patch.description <trailers | unfold)
871 test_trailer_option
'%(trailers:unfold) and %(trailers:only) work together' \
872 'trailers:unfold,only' <<-EOF
873 $(grep -v patch.description <trailers | unfold)
877 test_trailer_option
'%(trailers:key=foo) shows that trailer' \
878 'trailers:key=Signed-off-by' <<-EOF
879 Signed-off-by: A U Thor <author@example.com>
883 test_trailer_option
'%(trailers:key=foo) is case insensitive' \
884 'trailers:key=SiGned-oFf-bY' <<-EOF
885 Signed-off-by: A U Thor <author@example.com>
889 test_trailer_option
'%(trailers:key=foo:) trailing colon also works' \
890 'trailers:key=Signed-off-by:' <<-EOF
891 Signed-off-by: A U Thor <author@example.com>
895 test_trailer_option
'%(trailers:key=foo) multiple keys' \
896 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF
897 Reviewed-by: A U Thor <author@example.com>
898 Signed-off-by: A U Thor <author@example.com>
902 test_trailer_option
'%(trailers:key=nonexistent) becomes empty' \
903 'trailers:key=Shined-off-by:' <<-EOF
907 test_trailer_option
'%(trailers:key=foo) handles multiple lines even if folded' \
908 'trailers:key=Acked-by' <<-EOF
909 $(grep -v patch.description <trailers | grep -v Signed-off-by | grep -v Reviewed-by)
913 test_trailer_option
'%(trailers:key=foo,unfold) properly unfolds' \
914 'trailers:key=Signed-Off-by,unfold' <<-EOF
915 $(unfold <trailers | grep Signed-off-by)
919 test_trailer_option
'%(trailers:key=foo,only=no) also includes nontrailer lines' \
920 'trailers:key=Signed-off-by,only=no' <<-EOF
921 Signed-off-by: A U Thor <author@example.com>
922 $(grep patch.description <trailers)
926 test_trailer_option
'%(trailers:key=foo,valueonly) shows only value' \
927 'trailers:key=Signed-off-by,valueonly' <<-EOF
928 A U Thor <author@example.com>
932 test_trailer_option
'%(trailers:separator) changes separator' \
933 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
934 Reviewed-by: A U Thor <author@example.com>,Signed-off-by: A U Thor <author@example.com>
937 test_trailer_option
'%(trailers:key_value_separator) changes key-value separator' \
938 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
939 Reviewed-by,A U Thor <author@example.com>
940 Signed-off-by,A U Thor <author@example.com>
944 test_trailer_option
'%(trailers:separator,key_value_separator) changes both separators' \
945 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
946 Reviewed-by,A U Thor <author@example.com>,Signed-off-by,A U Thor <author@example.com>
949 test_failing_trailer_option
() {
952 test_expect_success
"$title" '
953 # error message cannot be checked under i18n
954 test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
955 test_cmp expect actual &&
956 test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
957 test_cmp expect actual
961 test_failing_trailer_option
'%(trailers) rejects unknown trailers arguments' \
962 'trailers:unsupported' <<-\EOF
963 fatal: unknown %(trailers) argument: unsupported
966 test_failing_trailer_option '%(trailers:key) without value is error' \
967 'trailers:key' <<-\
EOF
968 fatal: expected %(trailers:key=<value>)
971 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
972 cat >expect <<-EOF &&
973 fatal: unrecognized %(contents) argument: trailersonly
975 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
976 test_cmp expect actual
979 test_expect_success 'basic atom: head contents:trailers' '
980 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
981 sanitize_pgp <actual >actual.clean &&
982 # git for-each-ref ends with a blank line
983 cat >expect <<-EOF &&
987 test_cmp expect actual.clean
990 test_expect_success 'trailer parsing not fooled by --- line' '
991 git commit --allow-empty -F - <<-\EOF &&
994 This is the body. The message has a "---" line which would confuse a
995 message+patch parser. But here we know we have only a commit message,
1006 echo "trailer: right" &&
1009 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
1010 test_cmp expect actual
1013 test_expect_success 'Add symbolic ref for the following tests' '
1014 git symbolic-ref refs/heads/sym refs/heads/main
1021 test_expect_success 'Verify usage of %(symref) atom' '
1022 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
1023 test_cmp expected actual
1030 test_expect_success 'Verify usage of %(symref:short) atom' '
1031 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
1032 test_cmp expected actual
1040 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
1041 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
1042 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
1043 test_cmp expected actual &&
1045 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
1046 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
1047 test_cmp expected actual
1055 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
1056 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
1057 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
1058 test_cmp expected actual
1061 test_expect_success ':remotename and :remoteref' '
1062 git init remote-tests &&
1065 test_commit initial &&
1066 git branch -M main &&
1067 git remote add from fifth.coffee:blub &&
1068 git config branch.main.remote from &&
1069 git config branch.main.merge refs/heads/stable &&
1070 git remote add to southridge.audio:repo &&
1071 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
1072 git config branch.main.pushRemote to &&
1073 for pair in "%(upstream)=refs/remotes/from/stable" \
1074 "%(upstream:remotename)=from" \
1075 "%(upstream:remoteref)=refs/heads/stable" \
1076 "%(push)=refs/remotes/to/pushed/main" \
1077 "%(push:remotename)=to" \
1078 "%(push:remoteref)=refs/heads/pushed/main"
1080 echo "${pair#*=}" >expect &&
1081 git for-each-ref --format="${pair%=*}" \
1082 refs/heads/main >actual &&
1083 test_cmp expect actual
1085 git branch push-simple &&
1086 git config branch.push-simple.pushRemote from &&
1087 actual="$(git for-each-ref \
1088 --format="%(push:remotename),%(push:remoteref)" \
1089 refs/heads/push-simple)" &&
1090 test from, = "$actual"
1094 test_expect_success 'for-each-ref --ignore-case ignores case' '
1095 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
1096 test_must_be_empty actual &&
1098 echo refs/heads/main >expect &&
1099 git for-each-ref --format="%(refname)" --ignore-case \
1100 refs/heads/MAIN >actual &&
1101 test_cmp expect actual
1104 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
1105 # name refs numerically to avoid case-insensitive filesystem conflicts
1107 for email in a A b B
1109 for subject in a A b B
1111 GIT_COMMITTER_EMAIL="$email@example.com" \
1112 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1117 git for-each-ref --ignore-case \
1118 --format="%(taggeremail) %(subject) %(refname)" \
1121 --sort=taggeremail \
1122 refs/tags/icase-* >actual &&
1123 cat >expect <<-\
EOF &&
1124 <a@example.com> tag a refs/tags/icase-00
1125 <a@example.com> tag A refs/tags/icase-01
1126 <A@example.com> tag a refs/tags/icase-04
1127 <A@example.com> tag A refs/tags/icase-05
1128 <a@example.com> tag b refs/tags/icase-02
1129 <a@example.com> tag B refs/tags/icase-03
1130 <A@example.com> tag b refs/tags/icase-06
1131 <A@example.com> tag B refs/tags/icase-07
1132 <b@example.com> tag a refs/tags/icase-08
1133 <b@example.com> tag A refs/tags/icase-09
1134 <B@example.com> tag a refs/tags/icase-12
1135 <B@example.com> tag A refs/tags/icase-13
1136 <b@example.com> tag b refs/tags/icase-10
1137 <b@example.com> tag B refs/tags/icase-11
1138 <B@example.com> tag b refs/tags/icase-14
1139 <B@example.com> tag B refs/tags/icase-15
1141 test_cmp expect actual
1144 test_expect_success 'for-each-ref reports broken tags' '
1145 git tag -m "good tag" broken-tag-good HEAD &&
1146 git cat-file tag broken-tag-good >good &&
1147 sed s/commit/blob/ <good >bad &&
1148 bad=$(git hash-object -w -t tag bad) &&
1149 git update-ref refs/tags/broken-tag-bad $bad &&
1150 test_must_fail git for-each-ref --format="%(*objectname)" \
1151 refs/tags/broken-tag-*