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 15:18:43 2006 +0000
12datestamp=1151939923
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> 1151939924 +0200'
65test_atom head authorname 'A U Thor'
66test_atom head authoremail '<author@example.com>'
67test_atom head authordate 'Mon Jul 3 17:18:44 2006 +0200'
68test_atom head committer 'C O Mitter <committer@example.com> 1151939923 +0200'
69test_atom head committername 'C O Mitter'
70test_atom head committeremail '<committer@example.com>'
71test_atom head committerdate 'Mon Jul 3 17: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> 1151939923 +0200'
78test_atom head creatordate 'Mon Jul 3 17: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' '67a36f10722846e891fbada1ba48ed035de75581'
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> 1151939925 +0200'
111test_atom tag taggername 'C O Mitter'
112test_atom tag taggeremail '<committer@example.com>'
113test_atom tag taggerdate 'Mon Jul 3 17:18:45 2006 +0200'
114test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
115test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
116test_atom tag subject 'Tagging at 1151939927'
117test_atom tag contents:subject 'Tagging at 1151939927'
118test_atom tag body ''
119test_atom tag contents:body ''
120test_atom tag contents:signature ''
121test_atom tag contents 'Tagging at 1151939927
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
149cat >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'
152EOF
153
154test_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
160test_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#
176test_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
182cat >expected <<\EOF
183'refs/heads/master' '2006-07-03' '2006-07-03'
184'refs/tags/testtag' '2006-07-03'
185EOF
186
187test_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
194cat >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'
197EOF
198
199test_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
206cat >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'
209EOF
210
211test_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
218cat >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'
221EOF
222
223test_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
230test_expect_success 'Check format of strftime date fields' '
231 echo "my date is 2006-07-03" >expected &&
232 git for-each-ref \
233 --format="%(authordate:format:my date is %Y-%m-%d)" \
234 refs/heads >actual &&
235 test_cmp expected actual
236'
237
238test_expect_success 'exercise strftime with odd fields' '
239 echo >expected &&
240 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
241 test_cmp expected actual &&
242 long="long format -- $_z40$_z40$_z40$_z40$_z40$_z40$_z40" &&
243 echo $long >expected &&
244 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
245 test_cmp expected actual
246'
247
248cat >expected <<\EOF
249refs/heads/master
250refs/remotes/origin/master
251refs/tags/testtag
252EOF
253
254test_expect_success 'Verify ascending sort' '
255 git for-each-ref --format="%(refname)" --sort=refname >actual &&
256 test_cmp expected actual
257'
258
259
260cat >expected <<\EOF
261refs/tags/testtag
262refs/remotes/origin/master
263refs/heads/master
264EOF
265
266test_expect_success 'Verify descending sort' '
267 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
268 test_cmp expected actual
269'
270
271cat >expected <<\EOF
272'refs/heads/master'
273'refs/remotes/origin/master'
274'refs/tags/testtag'
275EOF
276
277test_expect_success 'Quoting style: shell' '
278 git for-each-ref --shell --format="%(refname)" >actual &&
279 test_cmp expected actual
280'
281
282test_expect_success 'Quoting style: perl' '
283 git for-each-ref --perl --format="%(refname)" >actual &&
284 test_cmp expected actual
285'
286
287test_expect_success 'Quoting style: python' '
288 git for-each-ref --python --format="%(refname)" >actual &&
289 test_cmp expected actual
290'
291
292cat >expected <<\EOF
293"refs/heads/master"
294"refs/remotes/origin/master"
295"refs/tags/testtag"
296EOF
297
298test_expect_success 'Quoting style: tcl' '
299 git for-each-ref --tcl --format="%(refname)" >actual &&
300 test_cmp expected actual
301'
302
303for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
304 test_expect_success "more than one quoting style: $i" "
305 git for-each-ref $i 2>&1 | (read line &&
306 case \$line in
307 \"error: more than one quoting style\"*) : happy;;
308 *) false
309 esac)
310 "
311done
312
313cat >expected <<\EOF
314master
315testtag
316EOF
317
318test_expect_success 'Check short refname format' '
319 (git for-each-ref --format="%(refname:short)" refs/heads &&
320 git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
321 test_cmp expected actual
322'
323
324cat >expected <<EOF
325origin/master
326EOF
327
328test_expect_success 'Check short upstream format' '
329 git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
330 test_cmp expected actual
331'
332
333test_expect_success 'setup for upstream:track[short]' '
334 test_commit two
335'
336
337cat >expected <<EOF
338[ahead 1]
339EOF
340
341test_expect_success 'Check upstream:track format' '
342 git for-each-ref --format="%(upstream:track)" refs/heads >actual &&
343 test_cmp expected actual
344'
345
346cat >expected <<EOF
347>
348EOF
349
350test_expect_success 'Check upstream:trackshort format' '
351 git for-each-ref --format="%(upstream:trackshort)" refs/heads >actual &&
352 test_cmp expected actual
353'
354
355test_expect_success 'Check that :track[short] cannot be used with other atoms' '
356 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
357 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
358'
359
360test_expect_success 'Check that :track[short] works when upstream is invalid' '
361 cat >expected <<-\EOF &&
362
363
364 EOF
365 test_when_finished "git config branch.master.merge refs/heads/master" &&
366 git config branch.master.merge refs/heads/does-not-exist &&
367 git for-each-ref \
368 --format="%(upstream:track)$LF%(upstream:trackshort)" \
369 refs/heads >actual &&
370 test_cmp expected actual
371'
372
373test_expect_success '%(push) supports tracking specifiers, too' '
374 echo "[ahead 1]" >expected &&
375 git for-each-ref --format="%(push:track)" refs/heads >actual &&
376 test_cmp expected actual
377'
378
379cat >expected <<EOF
380$(git rev-parse --short HEAD)
381EOF
382
383test_expect_success 'Check short objectname format' '
384 git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
385 test_cmp expected actual
386'
387
388test_expect_success 'Check for invalid refname format' '
389 test_must_fail git for-each-ref --format="%(refname:INVALID)"
390'
391
392get_color ()
393{
394 git config --get-color no.such.slot "$1"
395}
396
397cat >expected <<EOF
398$(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
399$(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
400$(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
401$(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
402EOF
403
404test_expect_success 'Check %(color:...) ' '
405 git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
406 test_cmp expected actual
407'
408
409cat >expected <<\EOF
410heads/master
411tags/master
412EOF
413
414test_expect_success 'Check ambiguous head and tag refs (strict)' '
415 git config --bool core.warnambiguousrefs true &&
416 git checkout -b newtag &&
417 echo "Using $datestamp" > one &&
418 git add one &&
419 git commit -m "Branch" &&
420 setdate_and_increment &&
421 git tag -m "Tagging at $datestamp" master &&
422 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
423 test_cmp expected actual
424'
425
426cat >expected <<\EOF
427heads/master
428master
429EOF
430
431test_expect_success 'Check ambiguous head and tag refs (loose)' '
432 git config --bool core.warnambiguousrefs false &&
433 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
434 test_cmp expected actual
435'
436
437cat >expected <<\EOF
438heads/ambiguous
439ambiguous
440EOF
441
442test_expect_success 'Check ambiguous head and tag refs II (loose)' '
443 git checkout master &&
444 git tag ambiguous testtag^0 &&
445 git branch ambiguous testtag^0 &&
446 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
447 test_cmp expected actual
448'
449
450test_expect_success 'an unusual tag with an incomplete line' '
451
452 git tag -m "bogo" bogo &&
453 bogo=$(git cat-file tag bogo) &&
454 bogo=$(printf "%s" "$bogo" | git mktag) &&
455 git tag -f bogo "$bogo" &&
456 git for-each-ref --format "%(body)" refs/tags/bogo
457
458'
459
460test_expect_success 'create tag with subject and body content' '
461 cat >>msg <<-\EOF &&
462 the subject line
463
464 first body line
465 second body line
466 EOF
467 git tag -F msg subject-body
468'
469test_atom refs/tags/subject-body subject 'the subject line'
470test_atom refs/tags/subject-body body 'first body line
471second body line
472'
473test_atom refs/tags/subject-body contents 'the subject line
474
475first body line
476second body line
477'
478
479test_expect_success 'create tag with multiline subject' '
480 cat >msg <<-\EOF &&
481 first subject line
482 second subject line
483
484 first body line
485 second body line
486 EOF
487 git tag -F msg multiline
488'
489test_atom refs/tags/multiline subject 'first subject line second subject line'
490test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
491test_atom refs/tags/multiline body 'first body line
492second body line
493'
494test_atom refs/tags/multiline contents:body 'first body line
495second body line
496'
497test_atom refs/tags/multiline contents:signature ''
498test_atom refs/tags/multiline contents 'first subject line
499second subject line
500
501first body line
502second body line
503'
504
505test_expect_success GPG 'create signed tags' '
506 git tag -s -m "" signed-empty &&
507 git tag -s -m "subject line" signed-short &&
508 cat >msg <<-\EOF &&
509 subject line
510
511 body contents
512 EOF
513 git tag -s -F msg signed-long
514'
515
516sig='-----BEGIN PGP SIGNATURE-----
517-----END PGP SIGNATURE-----
518'
519
520PREREQ=GPG
521test_atom refs/tags/signed-empty subject ''
522test_atom refs/tags/signed-empty contents:subject ''
523test_atom refs/tags/signed-empty body "$sig"
524test_atom refs/tags/signed-empty contents:body ''
525test_atom refs/tags/signed-empty contents:signature "$sig"
526test_atom refs/tags/signed-empty contents "$sig"
527
528test_atom refs/tags/signed-short subject 'subject line'
529test_atom refs/tags/signed-short contents:subject 'subject line'
530test_atom refs/tags/signed-short body "$sig"
531test_atom refs/tags/signed-short contents:body ''
532test_atom refs/tags/signed-short contents:signature "$sig"
533test_atom refs/tags/signed-short contents "subject line
534$sig"
535
536test_atom refs/tags/signed-long subject 'subject line'
537test_atom refs/tags/signed-long contents:subject 'subject line'
538test_atom refs/tags/signed-long body "body contents
539$sig"
540test_atom refs/tags/signed-long contents:body 'body contents
541'
542test_atom refs/tags/signed-long contents:signature "$sig"
543test_atom refs/tags/signed-long contents "subject line
544
545body contents
546$sig"
547
548cat >expected <<EOF
549$(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
550$(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
551EOF
552
553test_expect_success 'Verify sort with multiple keys' '
554 git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
555 refs/tags/bogo refs/tags/master > actual &&
556 test_cmp expected actual
557'
558test_done