1#!/bin/sh
2#
3# Copyright (c) 2007 Andy Parkins
4#
5
6test_description='for-each-ref test'
7
8. ./test-lib.sh
9. "$TEST_DIRECTORY"/lib-gpg.sh
10
11# Mon Jul 3 23:18:43 2006 +0000
12datestamp=1151968723
13setdate_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
21test_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
37test_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
51test_atom head refname refs/heads/master
52test_atom head refname:short master
53test_atom head refname:strip=1 heads/master
54test_atom head refname:strip=2 master
55test_atom head upstream refs/remotes/origin/master
56test_atom head upstream:short origin/master
57test_atom head push refs/remotes/myfork/master
58test_atom head push:short myfork/master
59test_atom head objecttype commit
60test_atom head objectsize 171
61test_atom head objectname $(git rev-parse refs/heads/master)
62test_atom head objectname:short $(git rev-parse --short refs/heads/master)
63test_atom head tree $(git rev-parse refs/heads/master^{tree})
64test_atom head parent ''
65test_atom head numparent 0
66test_atom head object ''
67test_atom head type ''
68test_atom head '*objectname' ''
69test_atom head '*objecttype' ''
70test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
71test_atom head authorname 'A U Thor'
72test_atom head authoremail '<author@example.com>'
73test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
74test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
75test_atom head committername 'C O Mitter'
76test_atom head committeremail '<committer@example.com>'
77test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
78test_atom head tag ''
79test_atom head tagger ''
80test_atom head taggername ''
81test_atom head taggeremail ''
82test_atom head taggerdate ''
83test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
84test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
85test_atom head subject 'Initial'
86test_atom head contents:subject 'Initial'
87test_atom head body ''
88test_atom head contents:body ''
89test_atom head contents:signature ''
90test_atom head contents 'Initial
91'
92test_atom head HEAD '*'
93
94test_atom tag refname refs/tags/testtag
95test_atom tag refname:short testtag
96test_atom tag upstream ''
97test_atom tag push ''
98test_atom tag objecttype tag
99test_atom tag objectsize 154
100test_atom tag objectname $(git rev-parse refs/tags/testtag)
101test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
102test_atom tag tree ''
103test_atom tag parent ''
104test_atom tag numparent ''
105test_atom tag object $(git rev-parse refs/tags/testtag^0)
106test_atom tag type 'commit'
107test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
108test_atom tag '*objecttype' 'commit'
109test_atom tag author ''
110test_atom tag authorname ''
111test_atom tag authoremail ''
112test_atom tag authordate ''
113test_atom tag committer ''
114test_atom tag committername ''
115test_atom tag committeremail ''
116test_atom tag committerdate ''
117test_atom tag tag 'testtag'
118test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
119test_atom tag taggername 'C O Mitter'
120test_atom tag taggeremail '<committer@example.com>'
121test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
122test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
123test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
124test_atom tag subject 'Tagging at 1151968727'
125test_atom tag contents:subject 'Tagging at 1151968727'
126test_atom tag body ''
127test_atom tag contents:body ''
128test_atom tag contents:signature ''
129test_atom tag contents 'Tagging at 1151968727
130'
131test_atom tag HEAD ' '
132
133test_expect_success 'Check invalid atoms names are errors' '
134 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
135'
136
137test_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)"
141'
142
143test_expect_success 'stripping refnames too far gives an error' '
144 test_must_fail git for-each-ref --format="%(refname:strip=3)"
145'
146
147test_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
152'
153
154test_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
161'
162
163test_expect_success 'Check invalid format specifiers are errors' '
164 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
165'
166
167test_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'
175 EOF
176 (
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
185}
186
187test_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"
192'
193
194test_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"
199'
200
201test_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"
203'
204
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
209test_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
213'
214
215# We just check that this is the same as "relative" for now.
216test_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)"
221'
222
223test_expect_success 'Check format "short" date fields output' '
224 test_date short 2006-07-04 2006-07-04 2006-07-04
225'
226
227test_expect_success 'Check format "short-local" date fields output' '
228 test_date short-local 2006-07-03 2006-07-03 2006-07-03
229'
230
231test_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"
236'
237
238test_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"
243'
244
245test_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"
247'
248
249test_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"
254'
255
256test_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"
258'
259
260test_expect_success 'Check format "raw" date fields output' '
261 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
262'
263
264test_expect_success 'Check format "raw-local" date fields output' '
265 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
266'
267
268test_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
274'
275
276test_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
282'
283
284test_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
292'
293
294cat >expected <<\EOF
295refs/heads/master
296refs/remotes/origin/master
297refs/tags/testtag
298EOF
299
300test_expect_success 'Verify ascending sort' '
301 git for-each-ref --format="%(refname)" --sort=refname >actual &&
302 test_cmp expected actual
303'
304
305
306cat >expected <<\EOF
307refs/tags/testtag
308refs/remotes/origin/master
309refs/heads/master
310EOF
311
312test_expect_success 'Verify descending sort' '
313 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
314 test_cmp expected actual
315'
316
317cat >expected <<\EOF
318'refs/heads/master'
319'refs/remotes/origin/master'
320'refs/tags/testtag'
321EOF
322
323test_expect_success 'Quoting style: shell' '
324 git for-each-ref --shell --format="%(refname)" >actual &&
325 test_cmp expected actual
326'
327
328test_expect_success 'Quoting style: perl' '
329 git for-each-ref --perl --format="%(refname)" >actual &&
330 test_cmp expected actual
331'
332
333test_expect_success 'Quoting style: python' '
334 git for-each-ref --python --format="%(refname)" >actual &&
335 test_cmp expected actual
336'
337
338cat >expected <<\EOF
339"refs/heads/master"
340"refs/remotes/origin/master"
341"refs/tags/testtag"
342EOF
343
344test_expect_success 'Quoting style: tcl' '
345 git for-each-ref --tcl --format="%(refname)" >actual &&
346 test_cmp expected actual
347'
348
349for 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)
356 "
357done
358
359test_expect_success 'setup for upstream:track[short]' '
360 test_commit two
361'
362
363test_atom head upstream:track '[ahead 1]'
364test_atom head upstream:trackshort '>'
365test_atom head push:track '[ahead 1]'
366test_atom head push:trackshort '>'
367
368test_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
371'
372
373test_expect_success 'Check that :track[short] works when upstream is invalid' '
374 cat >expected <<-\EOF &&
375
376
377 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
384'
385
386test_expect_success 'Check for invalid refname format' '
387 test_must_fail git for-each-ref --format="%(refname:INVALID)"
388'
389
390get_color ()
391{
392 git config --get-color no.such.slot "$1"
393}
394
395cat >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)
400EOF
401
402test_expect_success 'Check %(color:...) ' '
403 git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
404 test_cmp expected actual
405'
406
407cat >expected <<\EOF
408heads/master
409tags/master
410EOF
411
412test_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
422'
423
424cat >expected <<\EOF
425heads/master
426master
427EOF
428
429test_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
433'
434
435cat >expected <<\EOF
436heads/ambiguous
437ambiguous
438EOF
439
440test_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
446'
447
448test_expect_success 'an unusual tag with an incomplete line' '
449
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
455
456'
457
458test_expect_success 'create tag with subject and body content' '
459 cat >>msg <<-\EOF &&
460 the subject line
461
462 first body line
463 second body line
464 EOF
465 git tag -F msg subject-body
466'
467test_atom refs/tags/subject-body subject 'the subject line'
468test_atom refs/tags/subject-body body 'first body line
469second body line
470'
471test_atom refs/tags/subject-body contents 'the subject line
472
473first body line
474second body line
475'
476
477test_expect_success 'create tag with multiline subject' '
478 cat >msg <<-\EOF &&
479 first subject line
480 second subject line
481
482 first body line
483 second body line
484 EOF
485 git tag -F msg multiline
486'
487test_atom refs/tags/multiline subject 'first subject line second subject line'
488test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
489test_atom refs/tags/multiline body 'first body line
490second body line
491'
492test_atom refs/tags/multiline contents:body 'first body line
493second body line
494'
495test_atom refs/tags/multiline contents:signature ''
496test_atom refs/tags/multiline contents 'first subject line
497second subject line
498
499first body line
500second body line
501'
502
503test_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
508
509 body contents
510 EOF
511 git tag -s -F msg signed-long
512'
513
514sig='-----BEGIN PGP SIGNATURE-----
515-----END PGP SIGNATURE-----
516'
517
518PREREQ=GPG
519test_atom refs/tags/signed-empty subject ''
520test_atom refs/tags/signed-empty contents:subject ''
521test_atom refs/tags/signed-empty body "$sig"
522test_atom refs/tags/signed-empty contents:body ''
523test_atom refs/tags/signed-empty contents:signature "$sig"
524test_atom refs/tags/signed-empty contents "$sig"
525
526test_atom refs/tags/signed-short subject 'subject line'
527test_atom refs/tags/signed-short contents:subject 'subject line'
528test_atom refs/tags/signed-short body "$sig"
529test_atom refs/tags/signed-short contents:body ''
530test_atom refs/tags/signed-short contents:signature "$sig"
531test_atom refs/tags/signed-short contents "subject line
532$sig"
533
534test_atom refs/tags/signed-long subject 'subject line'
535test_atom refs/tags/signed-long contents:subject 'subject line'
536test_atom refs/tags/signed-long body "body contents
537$sig"
538test_atom refs/tags/signed-long contents:body 'body contents
539'
540test_atom refs/tags/signed-long contents:signature "$sig"
541test_atom refs/tags/signed-long contents "subject line
542
543body contents
544$sig"
545
546cat >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
549EOF
550
551test_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
555'
556
557test_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
564'
565
566test_done