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 upstream refs/remotes/origin/master
53test_atom head push refs/remotes/myfork/master
54test_atom head objecttype commit
55test_atom head objectsize 171
56test_atom head objectname $(git rev-parse refs/heads/master)
57test_atom head tree $(git rev-parse refs/heads/master^{tree})
58test_atom head parent ''
59test_atom head numparent 0
60test_atom head object ''
61test_atom head type ''
62test_atom head '*objectname' ''
63test_atom head '*objecttype' ''
64test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
65test_atom head authorname 'A U Thor'
66test_atom head authoremail '<author@example.com>'
67test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
68test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
69test_atom head committername 'C O Mitter'
70test_atom head committeremail '<committer@example.com>'
71test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
72test_atom head tag ''
73test_atom head tagger ''
74test_atom head taggername ''
75test_atom head taggeremail ''
76test_atom head taggerdate ''
77test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
78test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
79test_atom head subject 'Initial'
80test_atom head contents:subject 'Initial'
81test_atom head body ''
82test_atom head contents:body ''
83test_atom head contents:signature ''
84test_atom head contents 'Initial
85'
86test_atom head HEAD '*'
87
88test_atom tag refname refs/tags/testtag
89test_atom tag upstream ''
90test_atom tag push ''
91test_atom tag objecttype tag
92test_atom tag objectsize 154
93test_atom tag objectname $(git rev-parse refs/tags/testtag)
94test_atom tag tree ''
95test_atom tag parent ''
96test_atom tag numparent ''
97test_atom tag object $(git rev-parse refs/tags/testtag^0)
98test_atom tag type 'commit'
99test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
100test_atom tag '*objecttype' 'commit'
101test_atom tag author ''
102test_atom tag authorname ''
103test_atom tag authoremail ''
104test_atom tag authordate ''
105test_atom tag committer ''
106test_atom tag committername ''
107test_atom tag committeremail ''
108test_atom tag committerdate ''
109test_atom tag tag 'testtag'
110test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
111test_atom tag taggername 'C O Mitter'
112test_atom tag taggeremail '<committer@example.com>'
113test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
114test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
115test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
116test_atom tag subject 'Tagging at 1151968727'
117test_atom tag contents:subject 'Tagging at 1151968727'
118test_atom tag body ''
119test_atom tag contents:body ''
120test_atom tag contents:signature ''
121test_atom tag contents 'Tagging at 1151968727
122'
123test_atom tag HEAD ' '
124
125test_expect_success 'Check invalid atoms names are errors' '
126 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
127'
128
129test_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
136test_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
145test_expect_success 'Check invalid format specifiers are errors' '
146 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
147'
148
149test_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'
157 EOF
158 (
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
167}
168
169test_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"
174'
175
176test_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"
181'
182
183test_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"
185'
186
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
191test_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
195'
196
197# We just check that this is the same as "relative" for now.
198test_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)"
203'
204
205test_expect_success 'Check format "short" date fields output' '
206 test_date short 2006-07-04 2006-07-04 2006-07-04
207'
208
209test_expect_success 'Check format "short-local" date fields output' '
210 test_date short-local 2006-07-03 2006-07-03 2006-07-03
211'
212
213test_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"
218'
219
220test_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"
225'
226
227test_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"
229'
230
231test_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"
236'
237
238test_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"
240'
241
242test_expect_success 'Check format "raw" date fields output' '
243 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
244'
245
246test_expect_success 'Check format "raw-local" date fields output' '
247 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
248'
249
250test_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
256'
257
258test_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
264'
265
266test_expect_success 'exercise strftime with odd fields' '
267 echo >expected &&
268 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
269 test_cmp expected actual &&
270 long="long format -- $_z40$_z40$_z40$_z40$_z40$_z40$_z40" &&
271 echo $long >expected &&
272 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
273 test_cmp expected actual
274'
275
276cat >expected <<\EOF
277refs/heads/master
278refs/remotes/origin/master
279refs/tags/testtag
280EOF
281
282test_expect_success 'Verify ascending sort' '
283 git for-each-ref --format="%(refname)" --sort=refname >actual &&
284 test_cmp expected actual
285'
286
287
288cat >expected <<\EOF
289refs/tags/testtag
290refs/remotes/origin/master
291refs/heads/master
292EOF
293
294test_expect_success 'Verify descending sort' '
295 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
296 test_cmp expected actual
297'
298
299cat >expected <<\EOF
300'refs/heads/master'
301'refs/remotes/origin/master'
302'refs/tags/testtag'
303EOF
304
305test_expect_success 'Quoting style: shell' '
306 git for-each-ref --shell --format="%(refname)" >actual &&
307 test_cmp expected actual
308'
309
310test_expect_success 'Quoting style: perl' '
311 git for-each-ref --perl --format="%(refname)" >actual &&
312 test_cmp expected actual
313'
314
315test_expect_success 'Quoting style: python' '
316 git for-each-ref --python --format="%(refname)" >actual &&
317 test_cmp expected actual
318'
319
320cat >expected <<\EOF
321"refs/heads/master"
322"refs/remotes/origin/master"
323"refs/tags/testtag"
324EOF
325
326test_expect_success 'Quoting style: tcl' '
327 git for-each-ref --tcl --format="%(refname)" >actual &&
328 test_cmp expected actual
329'
330
331for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
332 test_expect_success "more than one quoting style: $i" "
333 git for-each-ref $i 2>&1 | (read line &&
334 case \$line in
335 \"error: more than one quoting style\"*) : happy;;
336 *) false
337 esac)
338 "
339done
340
341cat >expected <<\EOF
342master
343testtag
344EOF
345
346test_expect_success 'Check short refname format' '
347 (git for-each-ref --format="%(refname:short)" refs/heads &&
348 git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
349 test_cmp expected actual
350'
351
352cat >expected <<EOF
353origin/master
354EOF
355
356test_expect_success 'Check short upstream format' '
357 git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
358 test_cmp expected actual
359'
360
361test_expect_success 'setup for upstream:track[short]' '
362 test_commit two
363'
364
365cat >expected <<EOF
366[ahead 1]
367EOF
368
369test_expect_success 'Check upstream:track format' '
370 git for-each-ref --format="%(upstream:track)" refs/heads >actual &&
371 test_cmp expected actual
372'
373
374cat >expected <<EOF
375>
376EOF
377
378test_expect_success 'Check upstream:trackshort format' '
379 git for-each-ref --format="%(upstream:trackshort)" refs/heads >actual &&
380 test_cmp expected actual
381'
382
383test_expect_success 'Check that :track[short] cannot be used with other atoms' '
384 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
385 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
386'
387
388test_expect_success 'Check that :track[short] works when upstream is invalid' '
389 cat >expected <<-\EOF &&
390
391
392 EOF
393 test_when_finished "git config branch.master.merge refs/heads/master" &&
394 git config branch.master.merge refs/heads/does-not-exist &&
395 git for-each-ref \
396 --format="%(upstream:track)$LF%(upstream:trackshort)" \
397 refs/heads >actual &&
398 test_cmp expected actual
399'
400
401test_expect_success '%(push) supports tracking specifiers, too' '
402 echo "[ahead 1]" >expected &&
403 git for-each-ref --format="%(push:track)" refs/heads >actual &&
404 test_cmp expected actual
405'
406
407cat >expected <<EOF
408$(git rev-parse --short HEAD)
409EOF
410
411test_expect_success 'Check short objectname format' '
412 git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
413 test_cmp expected actual
414'
415
416test_expect_success 'Check for invalid refname format' '
417 test_must_fail git for-each-ref --format="%(refname:INVALID)"
418'
419
420get_color ()
421{
422 git config --get-color no.such.slot "$1"
423}
424
425cat >expected <<EOF
426$(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
427$(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
428$(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
429$(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
430EOF
431
432test_expect_success 'Check %(color:...) ' '
433 git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
434 test_cmp expected actual
435'
436
437cat >expected <<\EOF
438heads/master
439tags/master
440EOF
441
442test_expect_success 'Check ambiguous head and tag refs (strict)' '
443 git config --bool core.warnambiguousrefs true &&
444 git checkout -b newtag &&
445 echo "Using $datestamp" > one &&
446 git add one &&
447 git commit -m "Branch" &&
448 setdate_and_increment &&
449 git tag -m "Tagging at $datestamp" master &&
450 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
451 test_cmp expected actual
452'
453
454cat >expected <<\EOF
455heads/master
456master
457EOF
458
459test_expect_success 'Check ambiguous head and tag refs (loose)' '
460 git config --bool core.warnambiguousrefs false &&
461 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
462 test_cmp expected actual
463'
464
465cat >expected <<\EOF
466heads/ambiguous
467ambiguous
468EOF
469
470test_expect_success 'Check ambiguous head and tag refs II (loose)' '
471 git checkout master &&
472 git tag ambiguous testtag^0 &&
473 git branch ambiguous testtag^0 &&
474 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
475 test_cmp expected actual
476'
477
478test_expect_success 'an unusual tag with an incomplete line' '
479
480 git tag -m "bogo" bogo &&
481 bogo=$(git cat-file tag bogo) &&
482 bogo=$(printf "%s" "$bogo" | git mktag) &&
483 git tag -f bogo "$bogo" &&
484 git for-each-ref --format "%(body)" refs/tags/bogo
485
486'
487
488test_expect_success 'create tag with subject and body content' '
489 cat >>msg <<-\EOF &&
490 the subject line
491
492 first body line
493 second body line
494 EOF
495 git tag -F msg subject-body
496'
497test_atom refs/tags/subject-body subject 'the subject line'
498test_atom refs/tags/subject-body body 'first body line
499second body line
500'
501test_atom refs/tags/subject-body contents 'the subject line
502
503first body line
504second body line
505'
506
507test_expect_success 'create tag with multiline subject' '
508 cat >msg <<-\EOF &&
509 first subject line
510 second subject line
511
512 first body line
513 second body line
514 EOF
515 git tag -F msg multiline
516'
517test_atom refs/tags/multiline subject 'first subject line second subject line'
518test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
519test_atom refs/tags/multiline body 'first body line
520second body line
521'
522test_atom refs/tags/multiline contents:body 'first body line
523second body line
524'
525test_atom refs/tags/multiline contents:signature ''
526test_atom refs/tags/multiline contents 'first subject line
527second subject line
528
529first body line
530second body line
531'
532
533test_expect_success GPG 'create signed tags' '
534 git tag -s -m "" signed-empty &&
535 git tag -s -m "subject line" signed-short &&
536 cat >msg <<-\EOF &&
537 subject line
538
539 body contents
540 EOF
541 git tag -s -F msg signed-long
542'
543
544sig='-----BEGIN PGP SIGNATURE-----
545-----END PGP SIGNATURE-----
546'
547
548PREREQ=GPG
549test_atom refs/tags/signed-empty subject ''
550test_atom refs/tags/signed-empty contents:subject ''
551test_atom refs/tags/signed-empty body "$sig"
552test_atom refs/tags/signed-empty contents:body ''
553test_atom refs/tags/signed-empty contents:signature "$sig"
554test_atom refs/tags/signed-empty contents "$sig"
555
556test_atom refs/tags/signed-short subject 'subject line'
557test_atom refs/tags/signed-short contents:subject 'subject line'
558test_atom refs/tags/signed-short body "$sig"
559test_atom refs/tags/signed-short contents:body ''
560test_atom refs/tags/signed-short contents:signature "$sig"
561test_atom refs/tags/signed-short contents "subject line
562$sig"
563
564test_atom refs/tags/signed-long subject 'subject line'
565test_atom refs/tags/signed-long contents:subject 'subject line'
566test_atom refs/tags/signed-long body "body contents
567$sig"
568test_atom refs/tags/signed-long contents:body 'body contents
569'
570test_atom refs/tags/signed-long contents:signature "$sig"
571test_atom refs/tags/signed-long contents "subject line
572
573body contents
574$sig"
575
576cat >expected <<EOF
577$(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
578$(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
579EOF
580
581test_expect_success 'Verify sort with multiple keys' '
582 git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
583 refs/tags/bogo refs/tags/master > actual &&
584 test_cmp expected actual
585'
586test_done