Documentation/stash: remove mention of git reset --hard
[git.git] / t / t6300-for-each-ref.sh
blob039509a9cb94ef5c653df09e0453ac83157bf184
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 refname:short master
53 test_atom head refname:strip=1 heads/master
54 test_atom head refname:strip=2 master
55 test_atom head upstream refs/remotes/origin/master
56 test_atom head upstream:short origin/master
57 test_atom head push refs/remotes/myfork/master
58 test_atom head push:short myfork/master
59 test_atom head objecttype commit
60 test_atom head objectsize 171
61 test_atom head objectname $(git rev-parse refs/heads/master)
62 test_atom head objectname:short $(git rev-parse --short refs/heads/master)
63 test_atom head tree $(git rev-parse refs/heads/master^{tree})
64 test_atom head parent ''
65 test_atom head numparent 0
66 test_atom head object ''
67 test_atom head type ''
68 test_atom head '*objectname' ''
69 test_atom head '*objecttype' ''
70 test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
71 test_atom head authorname 'A U Thor'
72 test_atom head authoremail '<author@example.com>'
73 test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
74 test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
75 test_atom head committername 'C O Mitter'
76 test_atom head committeremail '<committer@example.com>'
77 test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
78 test_atom head tag ''
79 test_atom head tagger ''
80 test_atom head taggername ''
81 test_atom head taggeremail ''
82 test_atom head taggerdate ''
83 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
84 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
85 test_atom head subject 'Initial'
86 test_atom head contents:subject 'Initial'
87 test_atom head body ''
88 test_atom head contents:body ''
89 test_atom head contents:signature ''
90 test_atom head contents 'Initial
92 test_atom head HEAD '*'
94 test_atom tag refname refs/tags/testtag
95 test_atom tag refname:short testtag
96 test_atom tag upstream ''
97 test_atom tag push ''
98 test_atom tag objecttype tag
99 test_atom tag objectsize 154
100 test_atom tag objectname $(git rev-parse refs/tags/testtag)
101 test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
102 test_atom tag tree ''
103 test_atom tag parent ''
104 test_atom tag numparent ''
105 test_atom tag object $(git rev-parse refs/tags/testtag^0)
106 test_atom tag type 'commit'
107 test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
108 test_atom tag '*objecttype' 'commit'
109 test_atom tag author ''
110 test_atom tag authorname ''
111 test_atom tag authoremail ''
112 test_atom tag authordate ''
113 test_atom tag committer ''
114 test_atom tag committername ''
115 test_atom tag committeremail ''
116 test_atom tag committerdate ''
117 test_atom tag tag 'testtag'
118 test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
119 test_atom tag taggername 'C O Mitter'
120 test_atom tag taggeremail '<committer@example.com>'
121 test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
122 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
123 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
124 test_atom tag subject 'Tagging at 1151968727'
125 test_atom tag contents:subject 'Tagging at 1151968727'
126 test_atom tag body ''
127 test_atom tag contents:body ''
128 test_atom tag contents:signature ''
129 test_atom tag contents 'Tagging at 1151968727
131 test_atom tag HEAD ' '
133 test_expect_success 'Check invalid atoms names are errors' '
134 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
137 test_expect_success 'arguments to :strip must be positive integers' '
138 test_must_fail git for-each-ref --format="%(refname:strip=0)" &&
139 test_must_fail git for-each-ref --format="%(refname:strip=-1)" &&
140 test_must_fail git for-each-ref --format="%(refname:strip=foo)"
143 test_expect_success 'stripping refnames too far gives an error' '
144 test_must_fail git for-each-ref --format="%(refname:strip=3)"
147 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
148 git for-each-ref --format="%(authordate)" refs/heads &&
149 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
150 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
151 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
154 test_expect_success 'Check valid format specifiers for date fields' '
155 git for-each-ref --format="%(authordate:default)" refs/heads &&
156 git for-each-ref --format="%(authordate:relative)" refs/heads &&
157 git for-each-ref --format="%(authordate:short)" refs/heads &&
158 git for-each-ref --format="%(authordate:local)" refs/heads &&
159 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
160 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
163 test_expect_success 'Check invalid format specifiers are errors' '
164 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
167 test_date () {
168 f=$1 &&
169 committer_date=$2 &&
170 author_date=$3 &&
171 tagger_date=$4 &&
172 cat >expected <<-EOF &&
173 'refs/heads/master' '$committer_date' '$author_date'
174 'refs/tags/testtag' '$tagger_date'
177 git for-each-ref --shell \
178 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
179 refs/heads &&
180 git for-each-ref --shell \
181 --format="%(refname) %(taggerdate${f:+:$f})" \
182 refs/tags
183 ) >actual &&
184 test_cmp expected actual
187 test_expect_success 'Check unformatted date fields output' '
188 test_date "" \
189 "Tue Jul 4 01:18:43 2006 +0200" \
190 "Tue Jul 4 01:18:44 2006 +0200" \
191 "Tue Jul 4 01:18:45 2006 +0200"
194 test_expect_success 'Check format "default" formatted date fields output' '
195 test_date default \
196 "Tue Jul 4 01:18:43 2006 +0200" \
197 "Tue Jul 4 01:18:44 2006 +0200" \
198 "Tue Jul 4 01:18:45 2006 +0200"
201 test_expect_success 'Check format "default-local" date fields output' '
202 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"
205 # Don't know how to do relative check because I can't know when this script
206 # is going to be run and can't fake the current time to git, and hence can't
207 # provide expected output. Instead, I'll just make sure that "relative"
208 # doesn't exit in error
209 test_expect_success 'Check format "relative" date fields output' '
210 f=relative &&
211 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
212 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
215 # We just check that this is the same as "relative" for now.
216 test_expect_success 'Check format "relative-local" date fields output' '
217 test_date relative-local \
218 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
219 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
220 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
223 test_expect_success 'Check format "short" date fields output' '
224 test_date short 2006-07-04 2006-07-04 2006-07-04
227 test_expect_success 'Check format "short-local" date fields output' '
228 test_date short-local 2006-07-03 2006-07-03 2006-07-03
231 test_expect_success 'Check format "local" date fields output' '
232 test_date local \
233 "Mon Jul 3 23:18:43 2006" \
234 "Mon Jul 3 23:18:44 2006" \
235 "Mon Jul 3 23:18:45 2006"
238 test_expect_success 'Check format "iso8601" date fields output' '
239 test_date iso8601 \
240 "2006-07-04 01:18:43 +0200" \
241 "2006-07-04 01:18:44 +0200" \
242 "2006-07-04 01:18:45 +0200"
245 test_expect_success 'Check format "iso8601-local" date fields output' '
246 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"
249 test_expect_success 'Check format "rfc2822" date fields output' '
250 test_date rfc2822 \
251 "Tue, 4 Jul 2006 01:18:43 +0200" \
252 "Tue, 4 Jul 2006 01:18:44 +0200" \
253 "Tue, 4 Jul 2006 01:18:45 +0200"
256 test_expect_success 'Check format "rfc2822-local" date fields output' '
257 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"
260 test_expect_success 'Check format "raw" date fields output' '
261 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
264 test_expect_success 'Check format "raw-local" date fields output' '
265 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
268 test_expect_success 'Check format of strftime date fields' '
269 echo "my date is 2006-07-04" >expected &&
270 git for-each-ref \
271 --format="%(authordate:format:my date is %Y-%m-%d)" \
272 refs/heads >actual &&
273 test_cmp expected actual
276 test_expect_success 'Check format of strftime-local date fields' '
277 echo "my date is 2006-07-03" >expected &&
278 git for-each-ref \
279 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
280 refs/heads >actual &&
281 test_cmp expected actual
284 test_expect_success 'exercise strftime with odd fields' '
285 echo >expected &&
286 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
287 test_cmp expected actual &&
288 long="long format -- $_z40$_z40$_z40$_z40$_z40$_z40$_z40" &&
289 echo $long >expected &&
290 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
291 test_cmp expected actual
294 cat >expected <<\EOF
295 refs/heads/master
296 refs/remotes/origin/master
297 refs/tags/testtag
300 test_expect_success 'Verify ascending sort' '
301 git for-each-ref --format="%(refname)" --sort=refname >actual &&
302 test_cmp expected actual
306 cat >expected <<\EOF
307 refs/tags/testtag
308 refs/remotes/origin/master
309 refs/heads/master
312 test_expect_success 'Verify descending sort' '
313 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
314 test_cmp expected actual
317 cat >expected <<\EOF
318 'refs/heads/master'
319 'refs/remotes/origin/master'
320 'refs/tags/testtag'
323 test_expect_success 'Quoting style: shell' '
324 git for-each-ref --shell --format="%(refname)" >actual &&
325 test_cmp expected actual
328 test_expect_success 'Quoting style: perl' '
329 git for-each-ref --perl --format="%(refname)" >actual &&
330 test_cmp expected actual
333 test_expect_success 'Quoting style: python' '
334 git for-each-ref --python --format="%(refname)" >actual &&
335 test_cmp expected actual
338 cat >expected <<\EOF
339 "refs/heads/master"
340 "refs/remotes/origin/master"
341 "refs/tags/testtag"
344 test_expect_success 'Quoting style: tcl' '
345 git for-each-ref --tcl --format="%(refname)" >actual &&
346 test_cmp expected actual
349 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
350 test_expect_success "more than one quoting style: $i" "
351 git for-each-ref $i 2>&1 | (read line &&
352 case \$line in
353 \"error: more than one quoting style\"*) : happy;;
354 *) false
355 esac)
357 done
359 test_expect_success 'setup for upstream:track[short]' '
360 test_commit two
363 test_atom head upstream:track '[ahead 1]'
364 test_atom head upstream:trackshort '>'
365 test_atom head push:track '[ahead 1]'
366 test_atom head push:trackshort '>'
368 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
369 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
370 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
373 test_expect_success 'Check that :track[short] works when upstream is invalid' '
374 cat >expected <<-\EOF &&
378 test_when_finished "git config branch.master.merge refs/heads/master" &&
379 git config branch.master.merge refs/heads/does-not-exist &&
380 git for-each-ref \
381 --format="%(upstream:track)$LF%(upstream:trackshort)" \
382 refs/heads >actual &&
383 test_cmp expected actual
386 test_expect_success 'Check for invalid refname format' '
387 test_must_fail git for-each-ref --format="%(refname:INVALID)"
390 get_color ()
392 git config --get-color no.such.slot "$1"
395 cat >expected <<EOF
396 $(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
397 $(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
398 $(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
399 $(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
402 test_expect_success 'Check %(color:...) ' '
403 git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
404 test_cmp expected actual
407 cat >expected <<\EOF
408 heads/master
409 tags/master
412 test_expect_success 'Check ambiguous head and tag refs (strict)' '
413 git config --bool core.warnambiguousrefs true &&
414 git checkout -b newtag &&
415 echo "Using $datestamp" > one &&
416 git add one &&
417 git commit -m "Branch" &&
418 setdate_and_increment &&
419 git tag -m "Tagging at $datestamp" master &&
420 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
421 test_cmp expected actual
424 cat >expected <<\EOF
425 heads/master
426 master
429 test_expect_success 'Check ambiguous head and tag refs (loose)' '
430 git config --bool core.warnambiguousrefs false &&
431 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
432 test_cmp expected actual
435 cat >expected <<\EOF
436 heads/ambiguous
437 ambiguous
440 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
441 git checkout master &&
442 git tag ambiguous testtag^0 &&
443 git branch ambiguous testtag^0 &&
444 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
445 test_cmp expected actual
448 test_expect_success 'an unusual tag with an incomplete line' '
450 git tag -m "bogo" bogo &&
451 bogo=$(git cat-file tag bogo) &&
452 bogo=$(printf "%s" "$bogo" | git mktag) &&
453 git tag -f bogo "$bogo" &&
454 git for-each-ref --format "%(body)" refs/tags/bogo
458 test_expect_success 'create tag with subject and body content' '
459 cat >>msg <<-\EOF &&
460 the subject line
462 first body line
463 second body line
465 git tag -F msg subject-body
467 test_atom refs/tags/subject-body subject 'the subject line'
468 test_atom refs/tags/subject-body body 'first body line
469 second body line
471 test_atom refs/tags/subject-body contents 'the subject line
473 first body line
474 second body line
477 test_expect_success 'create tag with multiline subject' '
478 cat >msg <<-\EOF &&
479 first subject line
480 second subject line
482 first body line
483 second body line
485 git tag -F msg multiline
487 test_atom refs/tags/multiline subject 'first subject line second subject line'
488 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
489 test_atom refs/tags/multiline body 'first body line
490 second body line
492 test_atom refs/tags/multiline contents:body 'first body line
493 second body line
495 test_atom refs/tags/multiline contents:signature ''
496 test_atom refs/tags/multiline contents 'first subject line
497 second subject line
499 first body line
500 second body line
503 test_expect_success GPG 'create signed tags' '
504 git tag -s -m "" signed-empty &&
505 git tag -s -m "subject line" signed-short &&
506 cat >msg <<-\EOF &&
507 subject line
509 body contents
511 git tag -s -F msg signed-long
514 sig='-----BEGIN PGP SIGNATURE-----
515 -----END PGP SIGNATURE-----
518 PREREQ=GPG
519 test_atom refs/tags/signed-empty subject ''
520 test_atom refs/tags/signed-empty contents:subject ''
521 test_atom refs/tags/signed-empty body "$sig"
522 test_atom refs/tags/signed-empty contents:body ''
523 test_atom refs/tags/signed-empty contents:signature "$sig"
524 test_atom refs/tags/signed-empty contents "$sig"
526 test_atom refs/tags/signed-short subject 'subject line'
527 test_atom refs/tags/signed-short contents:subject 'subject line'
528 test_atom refs/tags/signed-short body "$sig"
529 test_atom refs/tags/signed-short contents:body ''
530 test_atom refs/tags/signed-short contents:signature "$sig"
531 test_atom refs/tags/signed-short contents "subject line
532 $sig"
534 test_atom refs/tags/signed-long subject 'subject line'
535 test_atom refs/tags/signed-long contents:subject 'subject line'
536 test_atom refs/tags/signed-long body "body contents
537 $sig"
538 test_atom refs/tags/signed-long contents:body 'body contents
540 test_atom refs/tags/signed-long contents:signature "$sig"
541 test_atom refs/tags/signed-long contents "subject line
543 body contents
544 $sig"
546 cat >expected <<EOF
547 $(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
548 $(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
551 test_expect_success 'Verify sort with multiple keys' '
552 git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
553 refs/tags/bogo refs/tags/master > actual &&
554 test_cmp expected actual
557 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
558 test_when_finished "git checkout master" &&
559 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
560 sed -e "s/^\* / /" actual >expect &&
561 git checkout --orphan HEAD &&
562 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
563 test_cmp expect actual
566 test_done