t6300: add tests for "-local" date formats
[git.git] / t / t6300-for-each-ref.sh
blobd6c9e627acd9d7ee997790ef71bd5aa7db53c90f
1 #!/bin/sh
3 # Copyright (c) 2007 Andy Parkins
6 test_description='for-each-ref test'
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-gpg.sh
11 # Mon Jul 3 23:18:43 2006 +0000
12 datestamp=1151968723
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 &&
24 git add 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
37 test_atom() {
38 case "$1" in
39 head) ref=refs/heads/master ;;
40 tag) ref=refs/tags/testtag ;;
41 *) ref=$1 ;;
42 esac
43 printf '%s\n' "$3" >expected
44 test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
45 git for-each-ref --format='%($2)' $ref >actual &&
46 sanitize_pgp <actual >actual.clean &&
47 test_cmp expected actual.clean
51 test_atom head refname refs/heads/master
52 test_atom head upstream refs/remotes/origin/master
53 test_atom head push refs/remotes/myfork/master
54 test_atom head objecttype commit
55 test_atom head objectsize 171
56 test_atom head objectname $(git rev-parse refs/heads/master)
57 test_atom head tree $(git rev-parse refs/heads/master^{tree})
58 test_atom head parent ''
59 test_atom head numparent 0
60 test_atom head object ''
61 test_atom head type ''
62 test_atom head '*objectname' ''
63 test_atom head '*objecttype' ''
64 test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
65 test_atom head authorname 'A U Thor'
66 test_atom head authoremail '<author@example.com>'
67 test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
68 test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
69 test_atom head committername 'C O Mitter'
70 test_atom head committeremail '<committer@example.com>'
71 test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
72 test_atom head tag ''
73 test_atom head tagger ''
74 test_atom head taggername ''
75 test_atom head taggeremail ''
76 test_atom head taggerdate ''
77 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
78 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
79 test_atom head subject 'Initial'
80 test_atom head contents:subject 'Initial'
81 test_atom head body ''
82 test_atom head contents:body ''
83 test_atom head contents:signature ''
84 test_atom head contents 'Initial
86 test_atom head HEAD '*'
88 test_atom tag refname refs/tags/testtag
89 test_atom tag upstream ''
90 test_atom tag push ''
91 test_atom tag objecttype tag
92 test_atom tag objectsize 154
93 test_atom tag objectname $(git rev-parse refs/tags/testtag)
94 test_atom tag tree ''
95 test_atom tag parent ''
96 test_atom tag numparent ''
97 test_atom tag object $(git rev-parse refs/tags/testtag^0)
98 test_atom tag type 'commit'
99 test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
100 test_atom tag '*objecttype' 'commit'
101 test_atom tag author ''
102 test_atom tag authorname ''
103 test_atom tag authoremail ''
104 test_atom tag authordate ''
105 test_atom tag committer ''
106 test_atom tag committername ''
107 test_atom tag committeremail ''
108 test_atom tag committerdate ''
109 test_atom tag tag 'testtag'
110 test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
111 test_atom tag taggername 'C O Mitter'
112 test_atom tag taggeremail '<committer@example.com>'
113 test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
114 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
115 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
116 test_atom tag subject 'Tagging at 1151968727'
117 test_atom tag contents:subject 'Tagging at 1151968727'
118 test_atom tag body ''
119 test_atom tag contents:body ''
120 test_atom tag contents:signature ''
121 test_atom tag contents 'Tagging at 1151968727
123 test_atom tag HEAD ' '
125 test_expect_success 'Check invalid atoms names are errors' '
126 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
129 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
130 git for-each-ref --format="%(authordate)" refs/heads &&
131 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
132 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
133 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
136 test_expect_success 'Check valid format specifiers for date fields' '
137 git for-each-ref --format="%(authordate:default)" refs/heads &&
138 git for-each-ref --format="%(authordate:relative)" refs/heads &&
139 git for-each-ref --format="%(authordate:short)" refs/heads &&
140 git for-each-ref --format="%(authordate:local)" refs/heads &&
141 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
142 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
145 test_expect_success 'Check invalid format specifiers are errors' '
146 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
149 test_date () {
150 f=$1 &&
151 committer_date=$2 &&
152 author_date=$3 &&
153 tagger_date=$4 &&
154 cat >expected <<-EOF &&
155 'refs/heads/master' '$committer_date' '$author_date'
156 'refs/tags/testtag' '$tagger_date'
159 git for-each-ref --shell \
160 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
161 refs/heads &&
162 git for-each-ref --shell \
163 --format="%(refname) %(taggerdate${f:+:$f})" \
164 refs/tags
165 ) >actual &&
166 test_cmp expected actual
169 test_expect_success 'Check unformatted date fields output' '
170 test_date "" \
171 "Tue Jul 4 01:18:43 2006 +0200" \
172 "Tue Jul 4 01:18:44 2006 +0200" \
173 "Tue Jul 4 01:18:45 2006 +0200"
176 test_expect_success 'Check format "default" formatted date fields output' '
177 test_date default \
178 "Tue Jul 4 01:18:43 2006 +0200" \
179 "Tue Jul 4 01:18:44 2006 +0200" \
180 "Tue Jul 4 01:18:45 2006 +0200"
183 test_expect_success 'Check format "default-local" date fields output' '
184 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"
187 # Don't know how to do relative check because I can't know when this script
188 # is going to be run and can't fake the current time to git, and hence can't
189 # provide expected output. Instead, I'll just make sure that "relative"
190 # doesn't exit in error
191 test_expect_success 'Check format "relative" date fields output' '
192 f=relative &&
193 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
194 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
197 # We just check that this is the same as "relative" for now.
198 test_expect_success 'Check format "relative-local" date fields output' '
199 test_date relative-local \
200 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
201 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
202 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
205 test_expect_success 'Check format "short" date fields output' '
206 test_date short 2006-07-04 2006-07-04 2006-07-04
209 test_expect_success 'Check format "short-local" date fields output' '
210 test_date short-local 2006-07-03 2006-07-03 2006-07-03
213 test_expect_success 'Check format "local" date fields output' '
214 test_date local \
215 "Mon Jul 3 23:18:43 2006" \
216 "Mon Jul 3 23:18:44 2006" \
217 "Mon Jul 3 23:18:45 2006"
220 test_expect_success 'Check format "iso8601" date fields output' '
221 test_date iso8601 \
222 "2006-07-04 01:18:43 +0200" \
223 "2006-07-04 01:18:44 +0200" \
224 "2006-07-04 01:18:45 +0200"
227 test_expect_success 'Check format "iso8601-local" date fields output' '
228 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"
231 test_expect_success 'Check format "rfc2822" date fields output' '
232 test_date rfc2822 \
233 "Tue, 4 Jul 2006 01:18:43 +0200" \
234 "Tue, 4 Jul 2006 01:18:44 +0200" \
235 "Tue, 4 Jul 2006 01:18:45 +0200"
238 test_expect_success 'Check format "rfc2822-local" date fields output' '
239 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"
242 test_expect_success 'Check format "raw" date fields output' '
243 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
246 test_expect_success 'Check format "raw-local" date fields output' '
247 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
250 test_expect_success 'Check format of strftime date fields' '
251 echo "my date is 2006-07-04" >expected &&
252 git for-each-ref \
253 --format="%(authordate:format:my date is %Y-%m-%d)" \
254 refs/heads >actual &&
255 test_cmp expected actual
258 test_expect_success 'Check format of strftime-local date fields' '
259 echo "my date is 2006-07-03" >expected &&
260 git for-each-ref \
261 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
262 refs/heads >actual &&
263 test_cmp expected actual
266 cat >expected <<\EOF
267 refs/heads/master
268 refs/remotes/origin/master
269 refs/tags/testtag
272 test_expect_success 'Verify ascending sort' '
273 git for-each-ref --format="%(refname)" --sort=refname >actual &&
274 test_cmp expected actual
278 cat >expected <<\EOF
279 refs/tags/testtag
280 refs/remotes/origin/master
281 refs/heads/master
284 test_expect_success 'Verify descending sort' '
285 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
286 test_cmp expected actual
289 cat >expected <<\EOF
290 'refs/heads/master'
291 'refs/remotes/origin/master'
292 'refs/tags/testtag'
295 test_expect_success 'Quoting style: shell' '
296 git for-each-ref --shell --format="%(refname)" >actual &&
297 test_cmp expected actual
300 test_expect_success 'Quoting style: perl' '
301 git for-each-ref --perl --format="%(refname)" >actual &&
302 test_cmp expected actual
305 test_expect_success 'Quoting style: python' '
306 git for-each-ref --python --format="%(refname)" >actual &&
307 test_cmp expected actual
310 cat >expected <<\EOF
311 "refs/heads/master"
312 "refs/remotes/origin/master"
313 "refs/tags/testtag"
316 test_expect_success 'Quoting style: tcl' '
317 git for-each-ref --tcl --format="%(refname)" >actual &&
318 test_cmp expected actual
321 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
322 test_expect_success "more than one quoting style: $i" "
323 git for-each-ref $i 2>&1 | (read line &&
324 case \$line in
325 \"error: more than one quoting style\"*) : happy;;
326 *) false
327 esac)
329 done
331 cat >expected <<\EOF
332 master
333 testtag
336 test_expect_success 'Check short refname format' '
337 (git for-each-ref --format="%(refname:short)" refs/heads &&
338 git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
339 test_cmp expected actual
342 cat >expected <<EOF
343 origin/master
346 test_expect_success 'Check short upstream format' '
347 git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
348 test_cmp expected actual
351 test_expect_success 'setup for upstream:track[short]' '
352 test_commit two
355 cat >expected <<EOF
356 [ahead 1]
359 test_expect_success 'Check upstream:track format' '
360 git for-each-ref --format="%(upstream:track)" refs/heads >actual &&
361 test_cmp expected actual
364 cat >expected <<EOF
368 test_expect_success 'Check upstream:trackshort format' '
369 git for-each-ref --format="%(upstream:trackshort)" refs/heads >actual &&
370 test_cmp expected actual
373 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
374 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
375 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
378 test_expect_success 'Check that :track[short] works when upstream is invalid' '
379 cat >expected <<-\EOF &&
383 test_when_finished "git config branch.master.merge refs/heads/master" &&
384 git config branch.master.merge refs/heads/does-not-exist &&
385 git for-each-ref \
386 --format="%(upstream:track)$LF%(upstream:trackshort)" \
387 refs/heads >actual &&
388 test_cmp expected actual
391 test_expect_success '%(push) supports tracking specifiers, too' '
392 echo "[ahead 1]" >expected &&
393 git for-each-ref --format="%(push:track)" refs/heads >actual &&
394 test_cmp expected actual
397 cat >expected <<EOF
398 $(git rev-parse --short HEAD)
401 test_expect_success 'Check short objectname format' '
402 git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
403 test_cmp expected actual
406 test_expect_success 'Check for invalid refname format' '
407 test_must_fail git for-each-ref --format="%(refname:INVALID)"
410 get_color ()
412 git config --get-color no.such.slot "$1"
415 cat >expected <<EOF
416 $(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
417 $(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
418 $(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
419 $(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
422 test_expect_success 'Check %(color:...) ' '
423 git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
424 test_cmp expected actual
427 cat >expected <<\EOF
428 heads/master
429 tags/master
432 test_expect_success 'Check ambiguous head and tag refs (strict)' '
433 git config --bool core.warnambiguousrefs true &&
434 git checkout -b newtag &&
435 echo "Using $datestamp" > one &&
436 git add one &&
437 git commit -m "Branch" &&
438 setdate_and_increment &&
439 git tag -m "Tagging at $datestamp" master &&
440 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
441 test_cmp expected actual
444 cat >expected <<\EOF
445 heads/master
446 master
449 test_expect_success 'Check ambiguous head and tag refs (loose)' '
450 git config --bool core.warnambiguousrefs false &&
451 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
452 test_cmp expected actual
455 cat >expected <<\EOF
456 heads/ambiguous
457 ambiguous
460 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
461 git checkout master &&
462 git tag ambiguous testtag^0 &&
463 git branch ambiguous testtag^0 &&
464 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
465 test_cmp expected actual
468 test_expect_success 'an unusual tag with an incomplete line' '
470 git tag -m "bogo" bogo &&
471 bogo=$(git cat-file tag bogo) &&
472 bogo=$(printf "%s" "$bogo" | git mktag) &&
473 git tag -f bogo "$bogo" &&
474 git for-each-ref --format "%(body)" refs/tags/bogo
478 test_expect_success 'create tag with subject and body content' '
479 cat >>msg <<-\EOF &&
480 the subject line
482 first body line
483 second body line
485 git tag -F msg subject-body
487 test_atom refs/tags/subject-body subject 'the subject line'
488 test_atom refs/tags/subject-body body 'first body line
489 second body line
491 test_atom refs/tags/subject-body contents 'the subject line
493 first body line
494 second body line
497 test_expect_success 'create tag with multiline subject' '
498 cat >msg <<-\EOF &&
499 first subject line
500 second subject line
502 first body line
503 second body line
505 git tag -F msg multiline
507 test_atom refs/tags/multiline subject 'first subject line second subject line'
508 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
509 test_atom refs/tags/multiline body 'first body line
510 second body line
512 test_atom refs/tags/multiline contents:body 'first body line
513 second body line
515 test_atom refs/tags/multiline contents:signature ''
516 test_atom refs/tags/multiline contents 'first subject line
517 second subject line
519 first body line
520 second body line
523 test_expect_success GPG 'create signed tags' '
524 git tag -s -m "" signed-empty &&
525 git tag -s -m "subject line" signed-short &&
526 cat >msg <<-\EOF &&
527 subject line
529 body contents
531 git tag -s -F msg signed-long
534 sig='-----BEGIN PGP SIGNATURE-----
535 -----END PGP SIGNATURE-----
538 PREREQ=GPG
539 test_atom refs/tags/signed-empty subject ''
540 test_atom refs/tags/signed-empty contents:subject ''
541 test_atom refs/tags/signed-empty body "$sig"
542 test_atom refs/tags/signed-empty contents:body ''
543 test_atom refs/tags/signed-empty contents:signature "$sig"
544 test_atom refs/tags/signed-empty contents "$sig"
546 test_atom refs/tags/signed-short subject 'subject line'
547 test_atom refs/tags/signed-short contents:subject 'subject line'
548 test_atom refs/tags/signed-short body "$sig"
549 test_atom refs/tags/signed-short contents:body ''
550 test_atom refs/tags/signed-short contents:signature "$sig"
551 test_atom refs/tags/signed-short contents "subject line
552 $sig"
554 test_atom refs/tags/signed-long subject 'subject line'
555 test_atom refs/tags/signed-long contents:subject 'subject line'
556 test_atom refs/tags/signed-long body "body contents
557 $sig"
558 test_atom refs/tags/signed-long contents:body 'body contents
560 test_atom refs/tags/signed-long contents:signature "$sig"
561 test_atom refs/tags/signed-long contents "subject line
563 body contents
564 $sig"
566 cat >expected <<EOF
567 $(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
568 $(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
571 test_expect_success 'Verify sort with multiple keys' '
572 git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
573 refs/tags/bogo refs/tags/master > actual &&
574 test_cmp expected actual
576 test_done