3 # Copyright (c) 2007 Andy Parkins
6 test_description
='for-each-ref test'
9 .
"$TEST_DIRECTORY"/lib-gpg.sh
11 # Mon Jul 3 23:18:43 2006 +0000
13 setdate_and_increment
() {
14 GIT_COMMITTER_DATE
="$datestamp +0200"
15 datestamp
=$
(expr "$datestamp" + 1)
16 GIT_AUTHOR_DATE
="$datestamp +0200"
17 datestamp
=$
(expr "$datestamp" + 1)
18 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
21 test_expect_success setup
'
22 setdate_and_increment &&
23 echo "Using $datestamp" > one &&
25 git commit -m "Initial" &&
26 setdate_and_increment &&
27 git tag -a -m "Tagging at $datestamp" testtag &&
28 git update-ref refs/remotes/origin/master master &&
29 git remote add origin nowhere &&
30 git config branch.master.remote origin &&
31 git config branch.master.merge refs/heads/master &&
32 git remote add myfork elsewhere &&
33 git config remote.pushdefault myfork &&
34 git config push.default current
39 head) ref
=refs
/heads
/master
;;
40 tag
) ref
=refs
/tags
/testtag
;;
41 sym
) ref
=refs
/heads
/sym
;;
44 printf '%s\n' "$3" >expected
45 test_expect_
${4:-success} $PREREQ "basic atom: $1 $2" "
46 git for-each-ref --format='%($2)' $ref >actual &&
47 sanitize_pgp <actual >actual.clean &&
48 test_cmp expected actual.clean
52 test_atom
head refname refs
/heads
/master
53 test_atom
head refname
:short master
54 test_atom
head refname
:strip
=1 heads
/master
55 test_atom
head refname
:strip
=2 master
56 test_atom
head upstream refs
/remotes
/origin
/master
57 test_atom
head upstream
:short origin
/master
58 test_atom
head upstream
:strip
=2 origin
/master
59 test_atom
head push refs
/remotes
/myfork
/master
60 test_atom
head push
:short myfork
/master
61 test_atom
head push
:strip
=1 remotes
/myfork
/master
62 test_atom
head objecttype commit
63 test_atom
head objectsize
171
64 test_atom
head objectname $
(git rev-parse refs
/heads
/master
)
65 test_atom
head objectname
:short $
(git rev-parse
--short refs
/heads
/master
)
66 test_atom
head objectname
:short
=1 $
(git rev-parse
--short=1 refs
/heads
/master
)
67 test_atom
head objectname
:short
=10 $
(git rev-parse
--short=10 refs
/heads
/master
)
68 test_atom
head tree $
(git rev-parse refs
/heads
/master^
{tree
})
69 test_atom
head parent
''
70 test_atom
head numparent
0
71 test_atom
head object
''
72 test_atom
head type ''
73 test_atom
head '*objectname' ''
74 test_atom
head '*objecttype' ''
75 test_atom
head author
'A U Thor <author@example.com> 1151968724 +0200'
76 test_atom
head authorname
'A U Thor'
77 test_atom
head authoremail
'<author@example.com>'
78 test_atom
head authordate
'Tue Jul 4 01:18:44 2006 +0200'
79 test_atom
head committer
'C O Mitter <committer@example.com> 1151968723 +0200'
80 test_atom
head committername
'C O Mitter'
81 test_atom
head committeremail
'<committer@example.com>'
82 test_atom
head committerdate
'Tue Jul 4 01:18:43 2006 +0200'
84 test_atom
head tagger
''
85 test_atom
head taggername
''
86 test_atom
head taggeremail
''
87 test_atom
head taggerdate
''
88 test_atom
head creator
'C O Mitter <committer@example.com> 1151968723 +0200'
89 test_atom
head creatordate
'Tue Jul 4 01:18:43 2006 +0200'
90 test_atom
head subject
'Initial'
91 test_atom
head contents
:subject
'Initial'
92 test_atom
head body
''
93 test_atom
head contents
:body
''
94 test_atom
head contents
:signature
''
95 test_atom
head contents
'Initial
97 test_atom
head HEAD
'*'
99 test_atom tag refname refs
/tags
/testtag
100 test_atom tag refname
:short testtag
101 test_atom tag upstream
''
102 test_atom tag push
''
103 test_atom tag objecttype tag
104 test_atom tag objectsize
154
105 test_atom tag objectname $
(git rev-parse refs
/tags
/testtag
)
106 test_atom tag objectname
:short $
(git rev-parse
--short refs
/tags
/testtag
)
107 test_atom
head objectname
:short
=1 $
(git rev-parse
--short=1 refs
/heads
/master
)
108 test_atom
head objectname
:short
=10 $
(git rev-parse
--short=10 refs
/heads
/master
)
109 test_atom tag tree
''
110 test_atom tag parent
''
111 test_atom tag numparent
''
112 test_atom tag object $
(git rev-parse refs
/tags
/testtag^
0)
113 test_atom tag
type 'commit'
114 test_atom tag
'*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
115 test_atom tag
'*objecttype' 'commit'
116 test_atom tag author
''
117 test_atom tag authorname
''
118 test_atom tag authoremail
''
119 test_atom tag authordate
''
120 test_atom tag committer
''
121 test_atom tag committername
''
122 test_atom tag committeremail
''
123 test_atom tag committerdate
''
124 test_atom tag tag
'testtag'
125 test_atom tag tagger
'C O Mitter <committer@example.com> 1151968725 +0200'
126 test_atom tag taggername
'C O Mitter'
127 test_atom tag taggeremail
'<committer@example.com>'
128 test_atom tag taggerdate
'Tue Jul 4 01:18:45 2006 +0200'
129 test_atom tag creator
'C O Mitter <committer@example.com> 1151968725 +0200'
130 test_atom tag creatordate
'Tue Jul 4 01:18:45 2006 +0200'
131 test_atom tag subject
'Tagging at 1151968727'
132 test_atom tag contents
:subject
'Tagging at 1151968727'
133 test_atom tag body
''
134 test_atom tag contents
:body
''
135 test_atom tag contents
:signature
''
136 test_atom tag contents
'Tagging at 1151968727
138 test_atom tag HEAD
' '
140 test_expect_success
'Check invalid atoms names are errors' '
141 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
144 test_expect_success
'arguments to :strip must be positive integers' '
145 test_must_fail git for-each-ref --format="%(refname:strip=0)" &&
146 test_must_fail git for-each-ref --format="%(refname:strip=-1)" &&
147 test_must_fail git for-each-ref --format="%(refname:strip=foo)"
150 test_expect_success
'stripping refnames too far gives an error' '
151 test_must_fail git for-each-ref --format="%(refname:strip=3)"
154 test_expect_success
'Check format specifiers are ignored in naming date atoms' '
155 git for-each-ref --format="%(authordate)" refs/heads &&
156 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
157 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
158 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
161 test_expect_success
'Check valid format specifiers for date fields' '
162 git for-each-ref --format="%(authordate:default)" refs/heads &&
163 git for-each-ref --format="%(authordate:relative)" refs/heads &&
164 git for-each-ref --format="%(authordate:short)" refs/heads &&
165 git for-each-ref --format="%(authordate:local)" refs/heads &&
166 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
167 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
170 test_expect_success
'Check invalid format specifiers are errors' '
171 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
174 test_expect_success
'arguments to %(objectname:short=) must be positive integers' '
175 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
176 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
177 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
185 cat >expected
<<-EOF &&
186 'refs/heads/master' '$committer_date' '$author_date'
187 'refs/tags/testtag' '$tagger_date'
190 git for-each-ref
--shell \
191 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
193 git for-each-ref
--shell \
194 --format="%(refname) %(taggerdate${f:+:$f})" \
197 test_cmp expected actual
200 test_expect_success
'Check unformatted date fields output' '
202 "Tue Jul 4 01:18:43 2006 +0200" \
203 "Tue Jul 4 01:18:44 2006 +0200" \
204 "Tue Jul 4 01:18:45 2006 +0200"
207 test_expect_success
'Check format "default" formatted date fields output' '
209 "Tue Jul 4 01:18:43 2006 +0200" \
210 "Tue Jul 4 01:18:44 2006 +0200" \
211 "Tue Jul 4 01:18:45 2006 +0200"
214 test_expect_success
'Check format "default-local" date fields output' '
215 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"
218 # Don't know how to do relative check because I can't know when this script
219 # is going to be run and can't fake the current time to git, and hence can't
220 # provide expected output. Instead, I'll just make sure that "relative"
221 # doesn't exit in error
222 test_expect_success
'Check format "relative" date fields output' '
224 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
225 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
228 # We just check that this is the same as "relative" for now.
229 test_expect_success
'Check format "relative-local" date fields output' '
230 test_date relative-local \
231 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
232 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
233 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
236 test_expect_success
'Check format "short" date fields output' '
237 test_date short 2006-07-04 2006-07-04 2006-07-04
240 test_expect_success
'Check format "short-local" date fields output' '
241 test_date short-local 2006-07-03 2006-07-03 2006-07-03
244 test_expect_success
'Check format "local" date fields output' '
246 "Mon Jul 3 23:18:43 2006" \
247 "Mon Jul 3 23:18:44 2006" \
248 "Mon Jul 3 23:18:45 2006"
251 test_expect_success
'Check format "iso8601" date fields output' '
253 "2006-07-04 01:18:43 +0200" \
254 "2006-07-04 01:18:44 +0200" \
255 "2006-07-04 01:18:45 +0200"
258 test_expect_success
'Check format "iso8601-local" date fields output' '
259 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"
262 test_expect_success
'Check format "rfc2822" date fields output' '
264 "Tue, 4 Jul 2006 01:18:43 +0200" \
265 "Tue, 4 Jul 2006 01:18:44 +0200" \
266 "Tue, 4 Jul 2006 01:18:45 +0200"
269 test_expect_success
'Check format "rfc2822-local" date fields output' '
270 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"
273 test_expect_success
'Check format "raw" date fields output' '
274 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
277 test_expect_success
'Check format "raw-local" date fields output' '
278 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
281 test_expect_success
'Check format of strftime date fields' '
282 echo "my date is 2006-07-04" >expected &&
284 --format="%(authordate:format:my date is %Y-%m-%d)" \
285 refs/heads >actual &&
286 test_cmp expected actual
289 test_expect_success
'Check format of strftime-local date fields' '
290 echo "my date is 2006-07-03" >expected &&
292 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
293 refs/heads >actual &&
294 test_cmp expected actual
297 test_expect_success
'exercise strftime with odd fields' '
299 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
300 test_cmp expected actual &&
301 long="long format -- $_z40$_z40$_z40$_z40$_z40$_z40$_z40" &&
302 echo $long >expected &&
303 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
304 test_cmp expected actual
309 refs
/remotes
/origin
/master
313 test_expect_success
'Verify ascending sort' '
314 git for-each-ref --format="%(refname)" --sort=refname >actual &&
315 test_cmp expected actual
321 refs
/remotes
/origin
/master
325 test_expect_success
'Verify descending sort' '
326 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
327 test_cmp expected actual
332 'refs/remotes/origin/master'
336 test_expect_success
'Quoting style: shell' '
337 git for-each-ref --shell --format="%(refname)" >actual &&
338 test_cmp expected actual
341 test_expect_success
'Quoting style: perl' '
342 git for-each-ref --perl --format="%(refname)" >actual &&
343 test_cmp expected actual
346 test_expect_success
'Quoting style: python' '
347 git for-each-ref --python --format="%(refname)" >actual &&
348 test_cmp expected actual
353 "refs/remotes/origin/master"
357 test_expect_success
'Quoting style: tcl' '
358 git for-each-ref --tcl --format="%(refname)" >actual &&
359 test_cmp expected actual
362 for i
in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
363 test_expect_success
"more than one quoting style: $i" "
364 git for-each-ref $i 2>&1 | (read line &&
366 \"error: more than one quoting style\"*) : happy;;
372 test_expect_success
'setup for upstream:track[short]' '
376 test_atom
head upstream
:track
'[ahead 1]'
377 test_atom
head upstream
:trackshort
'>'
378 test_atom
head upstream
:track
,nobracket
'ahead 1'
379 test_atom
head upstream
:nobracket
,track
'ahead 1'
380 test_atom
head push
:track
'[ahead 1]'
381 test_atom
head push
:trackshort
'>'
383 test_expect_success
'Check that :track[short] cannot be used with other atoms' '
384 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
385 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
388 test_expect_success
'Check that :track[short] works when upstream is invalid' '
389 cat >expected <<-\EOF &&
393 test_when_finished "git config branch.master.merge refs/heads/master" &&
394 git config branch.master.merge refs/heads/does-not-exist &&
396 --format="%(upstream:track)$LF%(upstream:trackshort)" \
397 refs/heads >actual &&
398 test_cmp expected actual
401 test_expect_success
'Check for invalid refname format' '
402 test_must_fail git for-each-ref --format="%(refname:INVALID)"
407 git config
--get-color no.such.slot
"$1"
411 $(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
412 $(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
413 $(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
414 $(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
417 test_expect_success
'Check %(color:...) ' '
418 git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
419 test_cmp expected actual
427 test_expect_success
'Check ambiguous head and tag refs (strict)' '
428 git config --bool core.warnambiguousrefs true &&
429 git checkout -b newtag &&
430 echo "Using $datestamp" > one &&
432 git commit -m "Branch" &&
433 setdate_and_increment &&
434 git tag -m "Tagging at $datestamp" master &&
435 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
436 test_cmp expected actual
444 test_expect_success
'Check ambiguous head and tag refs (loose)' '
445 git config --bool core.warnambiguousrefs false &&
446 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
447 test_cmp expected actual
455 test_expect_success
'Check ambiguous head and tag refs II (loose)' '
456 git checkout master &&
457 git tag ambiguous testtag^0 &&
458 git branch ambiguous testtag^0 &&
459 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
460 test_cmp expected actual
463 test_expect_success
'an unusual tag with an incomplete line' '
465 git tag -m "bogo" bogo &&
466 bogo=$(git cat-file tag bogo) &&
467 bogo=$(printf "%s" "$bogo" | git mktag) &&
468 git tag -f bogo "$bogo" &&
469 git for-each-ref --format "%(body)" refs/tags/bogo
473 test_expect_success
'create tag with subject and body content' '
480 git tag -F msg subject-body
482 test_atom refs
/tags
/subject-body subject
'the subject line'
483 test_atom refs
/tags
/subject-body body
'first body line
486 test_atom refs
/tags
/subject-body contents
'the subject line
492 test_expect_success
'create tag with multiline subject' '
500 git tag -F msg multiline
502 test_atom refs
/tags
/multiline subject
'first subject line second subject line'
503 test_atom refs
/tags
/multiline contents
:subject
'first subject line second subject line'
504 test_atom refs
/tags
/multiline body
'first body line
507 test_atom refs
/tags
/multiline contents
:body
'first body line
510 test_atom refs
/tags
/multiline contents
:signature
''
511 test_atom refs
/tags
/multiline contents
'first subject line
518 test_expect_success GPG
'create signed tags' '
519 git tag -s -m "" signed-empty &&
520 git tag -s -m "subject line" signed-short &&
526 git tag -s -F msg signed-long
529 sig
='-----BEGIN PGP SIGNATURE-----
530 -----END PGP SIGNATURE-----
534 test_atom refs
/tags
/signed-empty subject
''
535 test_atom refs
/tags
/signed-empty contents
:subject
''
536 test_atom refs
/tags
/signed-empty body
"$sig"
537 test_atom refs
/tags
/signed-empty contents
:body
''
538 test_atom refs
/tags
/signed-empty contents
:signature
"$sig"
539 test_atom refs
/tags
/signed-empty contents
"$sig"
541 test_atom refs
/tags
/signed-short subject
'subject line'
542 test_atom refs
/tags
/signed-short contents
:subject
'subject line'
543 test_atom refs
/tags
/signed-short body
"$sig"
544 test_atom refs
/tags
/signed-short contents
:body
''
545 test_atom refs
/tags
/signed-short contents
:signature
"$sig"
546 test_atom refs
/tags
/signed-short contents
"subject line
549 test_atom refs
/tags
/signed-long subject
'subject line'
550 test_atom refs
/tags
/signed-long contents
:subject
'subject line'
551 test_atom refs
/tags
/signed-long body
"body contents
553 test_atom refs
/tags
/signed-long contents
:body
'body contents
555 test_atom refs
/tags
/signed-long contents
:signature
"$sig"
556 test_atom refs
/tags
/signed-long contents
"subject line
562 $(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
563 $(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
566 test_expect_success
'Verify sort with multiple keys' '
567 git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
568 refs/tags/bogo refs/tags/master > actual &&
569 test_cmp expected actual
573 test_expect_success
'do not dereference NULL upon %(HEAD) on unborn branch' '
574 test_when_finished "git checkout master" &&
575 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
576 sed -e "s/^\* / /" actual >expect &&
577 git checkout --orphan HEAD &&
578 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
579 test_cmp expect actual
583 Reviewed-by: A U Thor <author@example.com>
584 Signed-off-by: A U Thor <author@example.com>
587 test_expect_success
'basic atom: head contents:trailers' '
588 echo "Some contents" > two &&
590 git commit -F - <<-EOF &&
591 trailers: this commit message has trailers
593 Some message contents
597 git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
598 sanitize_pgp <actual >actual.clean &&
599 # git for-each-ref ends with a blank line
600 cat >expect <<-EOF &&
604 test_cmp expect actual.clean
607 test_expect_success
'Add symbolic ref for the following tests' '
608 git symbolic-ref refs/heads/sym refs/heads/master
615 test_expect_success
'Verify usage of %(symref) atom' '
616 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
617 test_cmp expected actual
624 test_expect_success
'Verify usage of %(symref:short) atom' '
625 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
626 test_cmp expected actual
633 test_expect_success
'Verify usage of %(symref:strip) atom' '
634 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
635 test_cmp expected actual