Merge branch 'ee/clean-test-fixes' into maint
[git] / t / t6300-for-each-ref.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Andy Parkins
4 #
5
6 test_description='for-each-ref test'
7
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-gpg.sh
10
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
19 }
20
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
35 '
36
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
48         "
49 }
50
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> 1151939924 +0200'
65 test_atom head authorname 'A U Thor'
66 test_atom head authoremail '<author@example.com>'
67 test_atom head authordate 'Mon Jul 3 17:18:44 2006 +0200'
68 test_atom head committer 'C O Mitter <committer@example.com> 1151939923 +0200'
69 test_atom head committername 'C O Mitter'
70 test_atom head committeremail '<committer@example.com>'
71 test_atom head committerdate 'Mon Jul 3 17: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> 1151939923 +0200'
78 test_atom head creatordate 'Mon Jul 3 17: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
85 '
86 test_atom head HEAD '*'
87
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' '67a36f10722846e891fbada1ba48ed035de75581'
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> 1151939925 +0200'
111 test_atom tag taggername 'C O Mitter'
112 test_atom tag taggeremail '<committer@example.com>'
113 test_atom tag taggerdate 'Mon Jul 3 17:18:45 2006 +0200'
114 test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
115 test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
116 test_atom tag subject 'Tagging at 1151939927'
117 test_atom tag contents:subject 'Tagging at 1151939927'
118 test_atom tag body ''
119 test_atom tag contents:body ''
120 test_atom tag contents:signature ''
121 test_atom tag contents 'Tagging at 1151939927
122 '
123 test_atom tag HEAD ' '
124
125 test_expect_success 'Check invalid atoms names are errors' '
126         test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
127 '
128
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
134 '
135
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
143 '
144
145 test_expect_success 'Check invalid format specifiers are errors' '
146         test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
147 '
148
149 cat >expected <<\EOF
150 'refs/heads/master' 'Mon Jul 3 17:18:43 2006 +0200' 'Mon Jul 3 17:18:44 2006 +0200'
151 'refs/tags/testtag' 'Mon Jul 3 17:18:45 2006 +0200'
152 EOF
153
154 test_expect_success 'Check unformatted date fields output' '
155         (git for-each-ref --shell --format="%(refname) %(committerdate) %(authordate)" refs/heads &&
156         git for-each-ref --shell --format="%(refname) %(taggerdate)" refs/tags) >actual &&
157         test_cmp expected actual
158 '
159
160 test_expect_success 'Check format "default" formatted date fields output' '
161         f=default &&
162         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
163         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
164         test_cmp expected actual
165 '
166
167 # Don't know how to do relative check because I can't know when this script
168 # is going to be run and can't fake the current time to git, and hence can't
169 # provide expected output.  Instead, I'll just make sure that "relative"
170 # doesn't exit in error
171 #
172 #cat >expected <<\EOF
173 #
174 #EOF
175 #
176 test_expect_success 'Check format "relative" date fields output' '
177         f=relative &&
178         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
179         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
180 '
181
182 cat >expected <<\EOF
183 'refs/heads/master' '2006-07-03' '2006-07-03'
184 'refs/tags/testtag' '2006-07-03'
185 EOF
186
187 test_expect_success 'Check format "short" date fields output' '
188         f=short &&
189         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
190         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
191         test_cmp expected actual
192 '
193
194 cat >expected <<\EOF
195 'refs/heads/master' 'Mon Jul 3 15:18:43 2006' 'Mon Jul 3 15:18:44 2006'
196 'refs/tags/testtag' 'Mon Jul 3 15:18:45 2006'
197 EOF
198
199 test_expect_success 'Check format "local" date fields output' '
200         f=local &&
201         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
202         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
203         test_cmp expected actual
204 '
205
206 cat >expected <<\EOF
207 'refs/heads/master' '2006-07-03 17:18:43 +0200' '2006-07-03 17:18:44 +0200'
208 'refs/tags/testtag' '2006-07-03 17:18:45 +0200'
209 EOF
210
211 test_expect_success 'Check format "iso8601" date fields output' '
212         f=iso8601 &&
213         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
214         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
215         test_cmp expected actual
216 '
217
218 cat >expected <<\EOF
219 'refs/heads/master' 'Mon, 3 Jul 2006 17:18:43 +0200' 'Mon, 3 Jul 2006 17:18:44 +0200'
220 'refs/tags/testtag' 'Mon, 3 Jul 2006 17:18:45 +0200'
221 EOF
222
223 test_expect_success 'Check format "rfc2822" date fields output' '
224         f=rfc2822 &&
225         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
226         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
227         test_cmp expected actual
228 '
229
230 cat >expected <<\EOF
231 refs/heads/master
232 refs/remotes/origin/master
233 refs/tags/testtag
234 EOF
235
236 test_expect_success 'Verify ascending sort' '
237         git for-each-ref --format="%(refname)" --sort=refname >actual &&
238         test_cmp expected actual
239 '
240
241
242 cat >expected <<\EOF
243 refs/tags/testtag
244 refs/remotes/origin/master
245 refs/heads/master
246 EOF
247
248 test_expect_success 'Verify descending sort' '
249         git for-each-ref --format="%(refname)" --sort=-refname >actual &&
250         test_cmp expected actual
251 '
252
253 cat >expected <<\EOF
254 'refs/heads/master'
255 'refs/remotes/origin/master'
256 'refs/tags/testtag'
257 EOF
258
259 test_expect_success 'Quoting style: shell' '
260         git for-each-ref --shell --format="%(refname)" >actual &&
261         test_cmp expected actual
262 '
263
264 test_expect_success 'Quoting style: perl' '
265         git for-each-ref --perl --format="%(refname)" >actual &&
266         test_cmp expected actual
267 '
268
269 test_expect_success 'Quoting style: python' '
270         git for-each-ref --python --format="%(refname)" >actual &&
271         test_cmp expected actual
272 '
273
274 cat >expected <<\EOF
275 "refs/heads/master"
276 "refs/remotes/origin/master"
277 "refs/tags/testtag"
278 EOF
279
280 test_expect_success 'Quoting style: tcl' '
281         git for-each-ref --tcl --format="%(refname)" >actual &&
282         test_cmp expected actual
283 '
284
285 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
286         test_expect_success "more than one quoting style: $i" "
287                 git for-each-ref $i 2>&1 | (read line &&
288                 case \$line in
289                 \"error: more than one quoting style\"*) : happy;;
290                 *) false
291                 esac)
292         "
293 done
294
295 cat >expected <<\EOF
296 master
297 testtag
298 EOF
299
300 test_expect_success 'Check short refname format' '
301         (git for-each-ref --format="%(refname:short)" refs/heads &&
302         git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
303         test_cmp expected actual
304 '
305
306 cat >expected <<EOF
307 origin/master
308 EOF
309
310 test_expect_success 'Check short upstream format' '
311         git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
312         test_cmp expected actual
313 '
314
315 test_expect_success 'setup for upstream:track[short]' '
316         test_commit two
317 '
318
319 cat >expected <<EOF
320 [ahead 1]
321 EOF
322
323 test_expect_success 'Check upstream:track format' '
324         git for-each-ref --format="%(upstream:track)" refs/heads >actual &&
325         test_cmp expected actual
326 '
327
328 cat >expected <<EOF
329 >
330 EOF
331
332 test_expect_success 'Check upstream:trackshort format' '
333         git for-each-ref --format="%(upstream:trackshort)" refs/heads >actual &&
334         test_cmp expected actual
335 '
336
337 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
338         test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
339         test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
340 '
341
342 test_expect_success 'Check that :track[short] works when upstream is invalid' '
343         cat >expected <<-\EOF &&
344
345
346         EOF
347         test_when_finished "git config branch.master.merge refs/heads/master" &&
348         git config branch.master.merge refs/heads/does-not-exist &&
349         git for-each-ref \
350                 --format="%(upstream:track)$LF%(upstream:trackshort)" \
351                 refs/heads >actual &&
352         test_cmp expected actual
353 '
354
355 test_expect_success '%(push) supports tracking specifiers, too' '
356         echo "[ahead 1]" >expected &&
357         git for-each-ref --format="%(push:track)" refs/heads >actual &&
358         test_cmp expected actual
359 '
360
361 cat >expected <<EOF
362 $(git rev-parse --short HEAD)
363 EOF
364
365 test_expect_success 'Check short objectname format' '
366         git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
367         test_cmp expected actual
368 '
369
370 test_expect_success 'Check for invalid refname format' '
371         test_must_fail git for-each-ref --format="%(refname:INVALID)"
372 '
373
374 get_color ()
375 {
376         git config --get-color no.such.slot "$1"
377 }
378
379 cat >expected <<EOF
380 $(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
381 $(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
382 $(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
383 $(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
384 EOF
385
386 test_expect_success 'Check %(color:...) ' '
387         git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
388         test_cmp expected actual
389 '
390
391 cat >expected <<\EOF
392 heads/master
393 tags/master
394 EOF
395
396 test_expect_success 'Check ambiguous head and tag refs (strict)' '
397         git config --bool core.warnambiguousrefs true &&
398         git checkout -b newtag &&
399         echo "Using $datestamp" > one &&
400         git add one &&
401         git commit -m "Branch" &&
402         setdate_and_increment &&
403         git tag -m "Tagging at $datestamp" master &&
404         git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
405         test_cmp expected actual
406 '
407
408 cat >expected <<\EOF
409 heads/master
410 master
411 EOF
412
413 test_expect_success 'Check ambiguous head and tag refs (loose)' '
414         git config --bool core.warnambiguousrefs false &&
415         git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
416         test_cmp expected actual
417 '
418
419 cat >expected <<\EOF
420 heads/ambiguous
421 ambiguous
422 EOF
423
424 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
425         git checkout master &&
426         git tag ambiguous testtag^0 &&
427         git branch ambiguous testtag^0 &&
428         git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
429         test_cmp expected actual
430 '
431
432 test_expect_success 'an unusual tag with an incomplete line' '
433
434         git tag -m "bogo" bogo &&
435         bogo=$(git cat-file tag bogo) &&
436         bogo=$(printf "%s" "$bogo" | git mktag) &&
437         git tag -f bogo "$bogo" &&
438         git for-each-ref --format "%(body)" refs/tags/bogo
439
440 '
441
442 test_expect_success 'create tag with subject and body content' '
443         cat >>msg <<-\EOF &&
444                 the subject line
445
446                 first body line
447                 second body line
448         EOF
449         git tag -F msg subject-body
450 '
451 test_atom refs/tags/subject-body subject 'the subject line'
452 test_atom refs/tags/subject-body body 'first body line
453 second body line
454 '
455 test_atom refs/tags/subject-body contents 'the subject line
456
457 first body line
458 second body line
459 '
460
461 test_expect_success 'create tag with multiline subject' '
462         cat >msg <<-\EOF &&
463                 first subject line
464                 second subject line
465
466                 first body line
467                 second body line
468         EOF
469         git tag -F msg multiline
470 '
471 test_atom refs/tags/multiline subject 'first subject line second subject line'
472 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
473 test_atom refs/tags/multiline body 'first body line
474 second body line
475 '
476 test_atom refs/tags/multiline contents:body 'first body line
477 second body line
478 '
479 test_atom refs/tags/multiline contents:signature ''
480 test_atom refs/tags/multiline contents 'first subject line
481 second subject line
482
483 first body line
484 second body line
485 '
486
487 test_expect_success GPG 'create signed tags' '
488         git tag -s -m "" signed-empty &&
489         git tag -s -m "subject line" signed-short &&
490         cat >msg <<-\EOF &&
491         subject line
492
493         body contents
494         EOF
495         git tag -s -F msg signed-long
496 '
497
498 sig='-----BEGIN PGP SIGNATURE-----
499 -----END PGP SIGNATURE-----
500 '
501
502 PREREQ=GPG
503 test_atom refs/tags/signed-empty subject ''
504 test_atom refs/tags/signed-empty contents:subject ''
505 test_atom refs/tags/signed-empty body "$sig"
506 test_atom refs/tags/signed-empty contents:body ''
507 test_atom refs/tags/signed-empty contents:signature "$sig"
508 test_atom refs/tags/signed-empty contents "$sig"
509
510 test_atom refs/tags/signed-short subject 'subject line'
511 test_atom refs/tags/signed-short contents:subject 'subject line'
512 test_atom refs/tags/signed-short body "$sig"
513 test_atom refs/tags/signed-short contents:body ''
514 test_atom refs/tags/signed-short contents:signature "$sig"
515 test_atom refs/tags/signed-short contents "subject line
516 $sig"
517
518 test_atom refs/tags/signed-long subject 'subject line'
519 test_atom refs/tags/signed-long contents:subject 'subject line'
520 test_atom refs/tags/signed-long body "body contents
521 $sig"
522 test_atom refs/tags/signed-long contents:body 'body contents
523 '
524 test_atom refs/tags/signed-long contents:signature "$sig"
525 test_atom refs/tags/signed-long contents "subject line
526
527 body contents
528 $sig"
529
530 cat >expected <<EOF
531 $(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
532 $(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
533 EOF
534
535 test_expect_success 'Verify sort with multiple keys' '
536         git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
537                 refs/tags/bogo refs/tags/master > actual &&
538         test_cmp expected actual
539 '
540 test_done