for-each-ref: avoid color leakage
[git/mingw.git] / t / t6300-for-each-ref.sh
blob46866bab01f7c9be0515cc02457224aaa3def859
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 15:18:43 2006 +0000
12 datestamp=1151939923
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
34 test_atom() {
35 case "$1" in
36 head) ref=refs/heads/master ;;
37 tag) ref=refs/tags/testtag ;;
38 *) ref=$1 ;;
39 esac
40 printf '%s\n' "$3" >expected
41 test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
42 git for-each-ref --format='%($2)' $ref >actual &&
43 sanitize_pgp <actual >actual.clean &&
44 test_cmp expected actual.clean
48 test_atom head refname refs/heads/master
49 test_atom head upstream refs/remotes/origin/master
50 test_atom head objecttype commit
51 test_atom head objectsize 171
52 test_atom head objectname $(git rev-parse refs/heads/master)
53 test_atom head tree $(git rev-parse refs/heads/master^{tree})
54 test_atom head parent ''
55 test_atom head numparent 0
56 test_atom head object ''
57 test_atom head type ''
58 test_atom head author 'A U Thor <author@example.com> 1151939924 +0200'
59 test_atom head authorname 'A U Thor'
60 test_atom head authoremail '<author@example.com>'
61 test_atom head authordate 'Mon Jul 3 17:18:44 2006 +0200'
62 test_atom head committer 'C O Mitter <committer@example.com> 1151939923 +0200'
63 test_atom head committername 'C O Mitter'
64 test_atom head committeremail '<committer@example.com>'
65 test_atom head committerdate 'Mon Jul 3 17:18:43 2006 +0200'
66 test_atom head tag ''
67 test_atom head tagger ''
68 test_atom head taggername ''
69 test_atom head taggeremail ''
70 test_atom head taggerdate ''
71 test_atom head creator 'C O Mitter <committer@example.com> 1151939923 +0200'
72 test_atom head creatordate 'Mon Jul 3 17:18:43 2006 +0200'
73 test_atom head subject 'Initial'
74 test_atom head contents:subject 'Initial'
75 test_atom head body ''
76 test_atom head contents:body ''
77 test_atom head contents:signature ''
78 test_atom head contents 'Initial
80 test_atom head HEAD '*'
82 test_atom tag refname refs/tags/testtag
83 test_atom tag upstream ''
84 test_atom tag objecttype tag
85 test_atom tag objectsize 154
86 test_atom tag objectname $(git rev-parse refs/tags/testtag)
87 test_atom tag tree ''
88 test_atom tag parent ''
89 test_atom tag numparent ''
90 test_atom tag object $(git rev-parse refs/tags/testtag^0)
91 test_atom tag type 'commit'
92 test_atom tag author ''
93 test_atom tag authorname ''
94 test_atom tag authoremail ''
95 test_atom tag authordate ''
96 test_atom tag committer ''
97 test_atom tag committername ''
98 test_atom tag committeremail ''
99 test_atom tag committerdate ''
100 test_atom tag tag 'testtag'
101 test_atom tag tagger 'C O Mitter <committer@example.com> 1151939925 +0200'
102 test_atom tag taggername 'C O Mitter'
103 test_atom tag taggeremail '<committer@example.com>'
104 test_atom tag taggerdate 'Mon Jul 3 17:18:45 2006 +0200'
105 test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
106 test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
107 test_atom tag subject 'Tagging at 1151939927'
108 test_atom tag contents:subject 'Tagging at 1151939927'
109 test_atom tag body ''
110 test_atom tag contents:body ''
111 test_atom tag contents:signature ''
112 test_atom tag contents 'Tagging at 1151939927
114 test_atom tag HEAD ' '
116 test_expect_success 'Check invalid atoms names are errors' '
117 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
120 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
121 git for-each-ref --format="%(authordate)" refs/heads &&
122 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
123 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
124 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
127 test_expect_success 'Check valid format specifiers for date fields' '
128 git for-each-ref --format="%(authordate:default)" refs/heads &&
129 git for-each-ref --format="%(authordate:relative)" refs/heads &&
130 git for-each-ref --format="%(authordate:short)" refs/heads &&
131 git for-each-ref --format="%(authordate:local)" refs/heads &&
132 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
133 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
136 test_expect_success 'Check invalid format specifiers are errors' '
137 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
140 cat >expected <<\EOF
141 'refs/heads/master' 'Mon Jul 3 17:18:43 2006 +0200' 'Mon Jul 3 17:18:44 2006 +0200'
142 'refs/tags/testtag' 'Mon Jul 3 17:18:45 2006 +0200'
145 test_expect_success 'Check unformatted date fields output' '
146 (git for-each-ref --shell --format="%(refname) %(committerdate) %(authordate)" refs/heads &&
147 git for-each-ref --shell --format="%(refname) %(taggerdate)" refs/tags) >actual &&
148 test_cmp expected actual
151 test_expect_success 'Check format "default" formatted date fields output' '
152 f=default &&
153 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
154 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
155 test_cmp expected actual
158 # Don't know how to do relative check because I can't know when this script
159 # is going to be run and can't fake the current time to git, and hence can't
160 # provide expected output. Instead, I'll just make sure that "relative"
161 # doesn't exit in error
163 #cat >expected <<\EOF
165 #EOF
167 test_expect_success 'Check format "relative" date fields output' '
168 f=relative &&
169 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
170 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
173 cat >expected <<\EOF
174 'refs/heads/master' '2006-07-03' '2006-07-03'
175 'refs/tags/testtag' '2006-07-03'
178 test_expect_success 'Check format "short" date fields output' '
179 f=short &&
180 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
181 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
182 test_cmp expected actual
185 cat >expected <<\EOF
186 'refs/heads/master' 'Mon Jul 3 15:18:43 2006' 'Mon Jul 3 15:18:44 2006'
187 'refs/tags/testtag' 'Mon Jul 3 15:18:45 2006'
190 test_expect_success 'Check format "local" date fields output' '
191 f=local &&
192 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
193 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
194 test_cmp expected actual
197 cat >expected <<\EOF
198 'refs/heads/master' '2006-07-03 17:18:43 +0200' '2006-07-03 17:18:44 +0200'
199 'refs/tags/testtag' '2006-07-03 17:18:45 +0200'
202 test_expect_success 'Check format "iso8601" date fields output' '
203 f=iso8601 &&
204 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
205 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
206 test_cmp expected actual
209 cat >expected <<\EOF
210 'refs/heads/master' 'Mon, 3 Jul 2006 17:18:43 +0200' 'Mon, 3 Jul 2006 17:18:44 +0200'
211 'refs/tags/testtag' 'Mon, 3 Jul 2006 17:18:45 +0200'
214 test_expect_success 'Check format "rfc2822" date fields output' '
215 f=rfc2822 &&
216 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
217 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
218 test_cmp expected actual
221 cat >expected <<\EOF
222 refs/heads/master
223 refs/remotes/origin/master
224 refs/tags/testtag
227 test_expect_success 'Verify ascending sort' '
228 git for-each-ref --format="%(refname)" --sort=refname >actual &&
229 test_cmp expected actual
233 cat >expected <<\EOF
234 refs/tags/testtag
235 refs/remotes/origin/master
236 refs/heads/master
239 test_expect_success 'Verify descending sort' '
240 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
241 test_cmp expected actual
244 cat >expected <<\EOF
245 'refs/heads/master'
246 'refs/remotes/origin/master'
247 'refs/tags/testtag'
250 test_expect_success 'Quoting style: shell' '
251 git for-each-ref --shell --format="%(refname)" >actual &&
252 test_cmp expected actual
255 test_expect_success 'Quoting style: perl' '
256 git for-each-ref --perl --format="%(refname)" >actual &&
257 test_cmp expected actual
260 test_expect_success 'Quoting style: python' '
261 git for-each-ref --python --format="%(refname)" >actual &&
262 test_cmp expected actual
265 cat >expected <<\EOF
266 "refs/heads/master"
267 "refs/remotes/origin/master"
268 "refs/tags/testtag"
271 test_expect_success 'Quoting style: tcl' '
272 git for-each-ref --tcl --format="%(refname)" >actual &&
273 test_cmp expected actual
276 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
277 test_expect_success "more than one quoting style: $i" "
278 git for-each-ref $i 2>&1 | (read line &&
279 case \$line in
280 \"error: more than one quoting style\"*) : happy;;
281 *) false
282 esac)
284 done
286 cat >expected <<\EOF
287 master
288 testtag
291 test_expect_success 'Check short refname format' '
292 (git for-each-ref --format="%(refname:short)" refs/heads &&
293 git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
294 test_cmp expected actual
297 cat >expected <<EOF
298 origin/master
301 test_expect_success 'Check short upstream format' '
302 git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
303 test_cmp expected actual
306 test_expect_success 'setup for upstream:track[short]' '
307 test_commit two
310 cat >expected <<EOF
311 [ahead 1]
314 test_expect_success 'Check upstream:track format' '
315 git for-each-ref --format="%(upstream:track)" refs/heads >actual &&
316 test_cmp expected actual
319 cat >expected <<EOF
323 test_expect_success 'Check upstream:trackshort format' '
324 git for-each-ref --format="%(upstream:trackshort)" refs/heads >actual &&
325 test_cmp expected actual
328 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
329 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
330 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
333 cat >expected <<EOF
334 $(git rev-parse --short HEAD)
337 test_expect_success 'Check short objectname format' '
338 git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
339 test_cmp expected actual
342 test_expect_success 'Check for invalid refname format' '
343 test_must_fail git for-each-ref --format="%(refname:INVALID)"
346 get_color ()
348 git config --get-color no.such.slot "$1"
351 cat >expected <<EOF
352 $(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
353 $(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
354 $(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
355 $(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
358 test_expect_success 'Check %(color:...) ' '
359 git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
360 test_cmp expected actual
363 cat >expected <<\EOF
364 heads/master
365 tags/master
368 test_expect_success 'Check ambiguous head and tag refs (strict)' '
369 git config --bool core.warnambiguousrefs true &&
370 git checkout -b newtag &&
371 echo "Using $datestamp" > one &&
372 git add one &&
373 git commit -m "Branch" &&
374 setdate_and_increment &&
375 git tag -m "Tagging at $datestamp" master &&
376 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
377 test_cmp expected actual
380 cat >expected <<\EOF
381 heads/master
382 master
385 test_expect_success 'Check ambiguous head and tag refs (loose)' '
386 git config --bool core.warnambiguousrefs false &&
387 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
388 test_cmp expected actual
391 cat >expected <<\EOF
392 heads/ambiguous
393 ambiguous
396 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
397 git checkout master &&
398 git tag ambiguous testtag^0 &&
399 git branch ambiguous testtag^0 &&
400 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
401 test_cmp expected actual
404 test_expect_success 'an unusual tag with an incomplete line' '
406 git tag -m "bogo" bogo &&
407 bogo=$(git cat-file tag bogo) &&
408 bogo=$(printf "%s" "$bogo" | git mktag) &&
409 git tag -f bogo "$bogo" &&
410 git for-each-ref --format "%(body)" refs/tags/bogo
414 test_expect_success 'create tag with subject and body content' '
415 cat >>msg <<-\EOF &&
416 the subject line
418 first body line
419 second body line
421 git tag -F msg subject-body
423 test_atom refs/tags/subject-body subject 'the subject line'
424 test_atom refs/tags/subject-body body 'first body line
425 second body line
427 test_atom refs/tags/subject-body contents 'the subject line
429 first body line
430 second body line
433 test_expect_success 'create tag with multiline subject' '
434 cat >msg <<-\EOF &&
435 first subject line
436 second subject line
438 first body line
439 second body line
441 git tag -F msg multiline
443 test_atom refs/tags/multiline subject 'first subject line second subject line'
444 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
445 test_atom refs/tags/multiline body 'first body line
446 second body line
448 test_atom refs/tags/multiline contents:body 'first body line
449 second body line
451 test_atom refs/tags/multiline contents:signature ''
452 test_atom refs/tags/multiline contents 'first subject line
453 second subject line
455 first body line
456 second body line
459 test_expect_success GPG 'create signed tags' '
460 git tag -s -m "" signed-empty &&
461 git tag -s -m "subject line" signed-short &&
462 cat >msg <<-\EOF &&
463 subject line
465 body contents
467 git tag -s -F msg signed-long
470 sig='-----BEGIN PGP SIGNATURE-----
471 -----END PGP SIGNATURE-----
474 PREREQ=GPG
475 test_atom refs/tags/signed-empty subject ''
476 test_atom refs/tags/signed-empty contents:subject ''
477 test_atom refs/tags/signed-empty body "$sig"
478 test_atom refs/tags/signed-empty contents:body ''
479 test_atom refs/tags/signed-empty contents:signature "$sig"
480 test_atom refs/tags/signed-empty contents "$sig"
482 test_atom refs/tags/signed-short subject 'subject line'
483 test_atom refs/tags/signed-short contents:subject 'subject line'
484 test_atom refs/tags/signed-short body "$sig"
485 test_atom refs/tags/signed-short contents:body ''
486 test_atom refs/tags/signed-short contents:signature "$sig"
487 test_atom refs/tags/signed-short contents "subject line
488 $sig"
490 test_atom refs/tags/signed-long subject 'subject line'
491 test_atom refs/tags/signed-long contents:subject 'subject line'
492 test_atom refs/tags/signed-long body "body contents
493 $sig"
494 test_atom refs/tags/signed-long contents:body 'body contents
496 test_atom refs/tags/signed-long contents:signature "$sig"
497 test_atom refs/tags/signed-long contents "subject line
499 body contents
500 $sig"
502 cat >expected <<EOF
503 $(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
504 $(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
507 test_expect_success 'Verify sort with multiple keys' '
508 git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
509 refs/tags/bogo refs/tags/master > actual &&
510 test_cmp expected actual
512 test_done