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