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