Merge branch 'nd/literal-pathspecs'
[git/mingw.git] / t / t6300-for-each-ref.sh
blobda5fb6c9170b3e7fd818a59fc6e48ff3161d12da
1 #!/bin/sh
3 # Copyright (c) 2007 Andy Parkins
6 test_description='for-each-ref test'
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-gpg.sh
11 # Mon Jul 3 15:18:43 2006 +0000
12 datestamp=1151939923
13 setdate_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
21 test_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
30 test_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
37 test_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
51 test_atom head refname refs/heads/master
52 test_atom head upstream refs/remotes/origin/master
53 test_atom head objecttype commit
54 test_atom head objectsize 171
55 test_atom head objectname 67a36f10722846e891fbada1ba48ed035de75581
56 test_atom head tree 0e51c00fcb93dffc755546f27593d511e1bdb46f
57 test_atom head parent ''
58 test_atom head numparent 0
59 test_atom head object ''
60 test_atom head type ''
61 test_atom head '*objectname' ''
62 test_atom head '*objecttype' ''
63 test_atom head author 'A U Thor <author@example.com> 1151939924 +0200'
64 test_atom head authorname 'A U Thor'
65 test_atom head authoremail '<author@example.com>'
66 test_atom head authordate 'Mon Jul 3 17:18:44 2006 +0200'
67 test_atom head committer 'C O Mitter <committer@example.com> 1151939923 +0200'
68 test_atom head committername 'C O Mitter'
69 test_atom head committeremail '<committer@example.com>'
70 test_atom head committerdate 'Mon Jul 3 17:18:43 2006 +0200'
71 test_atom head tag ''
72 test_atom head tagger ''
73 test_atom head taggername ''
74 test_atom head taggeremail ''
75 test_atom head taggerdate ''
76 test_atom head creator 'C O Mitter <committer@example.com> 1151939923 +0200'
77 test_atom head creatordate 'Mon Jul 3 17:18:43 2006 +0200'
78 test_atom head subject 'Initial'
79 test_atom head contents:subject 'Initial'
80 test_atom head body ''
81 test_atom head contents:body ''
82 test_atom head contents:signature ''
83 test_atom head contents 'Initial
86 test_atom tag refname refs/tags/testtag
87 test_atom tag upstream ''
88 test_atom tag objecttype tag
89 test_atom tag objectsize 154
90 test_atom tag objectname 98b46b1d36e5b07909de1b3886224e3e81e87322
91 test_atom tag tree ''
92 test_atom tag parent ''
93 test_atom tag numparent ''
94 test_atom tag object '67a36f10722846e891fbada1ba48ed035de75581'
95 test_atom tag type 'commit'
96 test_atom tag '*objectname' '67a36f10722846e891fbada1ba48ed035de75581'
97 test_atom tag '*objecttype' 'commit'
98 test_atom tag author ''
99 test_atom tag authorname ''
100 test_atom tag authoremail ''
101 test_atom tag authordate ''
102 test_atom tag committer ''
103 test_atom tag committername ''
104 test_atom tag committeremail ''
105 test_atom tag committerdate ''
106 test_atom tag tag 'testtag'
107 test_atom tag tagger 'C O Mitter <committer@example.com> 1151939925 +0200'
108 test_atom tag taggername 'C O Mitter'
109 test_atom tag taggeremail '<committer@example.com>'
110 test_atom tag taggerdate 'Mon Jul 3 17:18:45 2006 +0200'
111 test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
112 test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
113 test_atom tag subject 'Tagging at 1151939927'
114 test_atom tag contents:subject 'Tagging at 1151939927'
115 test_atom tag body ''
116 test_atom tag contents:body ''
117 test_atom tag contents:signature ''
118 test_atom tag contents 'Tagging at 1151939927
121 test_expect_success 'Check invalid atoms names are errors' '
122 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
125 test_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
132 test_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
141 test_expect_success 'Check invalid format specifiers are errors' '
142 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
145 cat >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'
150 test_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
156 test_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
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
168 #cat >expected <<\EOF
170 #EOF
172 test_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
178 cat >expected <<\EOF
179 'refs/heads/master' '2006-07-03' '2006-07-03'
180 'refs/tags/testtag' '2006-07-03'
183 test_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
190 cat >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'
195 test_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
202 cat >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'
207 test_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
214 cat >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'
219 test_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
226 cat >expected <<\EOF
227 refs/heads/master
228 refs/remotes/origin/master
229 refs/tags/testtag
232 test_expect_success 'Verify ascending sort' '
233 git for-each-ref --format="%(refname)" --sort=refname >actual &&
234 test_cmp expected actual
238 cat >expected <<\EOF
239 refs/tags/testtag
240 refs/remotes/origin/master
241 refs/heads/master
244 test_expect_success 'Verify descending sort' '
245 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
246 test_cmp expected actual
249 cat >expected <<\EOF
250 'refs/heads/master'
251 'refs/remotes/origin/master'
252 'refs/tags/testtag'
255 test_expect_success 'Quoting style: shell' '
256 git for-each-ref --shell --format="%(refname)" >actual &&
257 test_cmp expected actual
260 test_expect_success 'Quoting style: perl' '
261 git for-each-ref --perl --format="%(refname)" >actual &&
262 test_cmp expected actual
265 test_expect_success 'Quoting style: python' '
266 git for-each-ref --python --format="%(refname)" >actual &&
267 test_cmp expected actual
270 cat >expected <<\EOF
271 "refs/heads/master"
272 "refs/remotes/origin/master"
273 "refs/tags/testtag"
276 test_expect_success 'Quoting style: tcl' '
277 git for-each-ref --tcl --format="%(refname)" >actual &&
278 test_cmp expected actual
281 for 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)
289 done
291 cat >expected <<\EOF
292 master
293 testtag
296 test_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
302 cat >expected <<EOF
303 origin/master
306 test_expect_success 'Check short upstream format' '
307 git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
308 test_cmp expected actual
311 cat >expected <<EOF
312 67a36f1
315 test_expect_success 'Check short objectname format' '
316 git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
317 test_cmp expected actual
320 test_expect_success 'Check for invalid refname format' '
321 test_must_fail git for-each-ref --format="%(refname:INVALID)"
324 cat >expected <<\EOF
325 heads/master
326 tags/master
329 test_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
341 cat >expected <<\EOF
342 heads/master
343 master
346 test_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
352 cat >expected <<\EOF
353 heads/ambiguous
354 ambiguous
357 test_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
365 test_expect_success 'an unusual tag with an incomplete line' '
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
375 test_expect_success 'create tag with subject and body content' '
376 cat >>msg <<-\EOF &&
377 the subject line
379 first body line
380 second body line
382 git tag -F msg subject-body
384 test_atom refs/tags/subject-body subject 'the subject line'
385 test_atom refs/tags/subject-body body 'first body line
386 second body line
388 test_atom refs/tags/subject-body contents 'the subject line
390 first body line
391 second body line
394 test_expect_success 'create tag with multiline subject' '
395 cat >msg <<-\EOF &&
396 first subject line
397 second subject line
399 first body line
400 second body line
402 git tag -F msg multiline
404 test_atom refs/tags/multiline subject 'first subject line second subject line'
405 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
406 test_atom refs/tags/multiline body 'first body line
407 second body line
409 test_atom refs/tags/multiline contents:body 'first body line
410 second body line
412 test_atom refs/tags/multiline contents:signature ''
413 test_atom refs/tags/multiline contents 'first subject line
414 second subject line
416 first body line
417 second body line
420 test_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
426 body contents
428 git tag -s -F msg signed-long
431 sig='-----BEGIN PGP SIGNATURE-----
432 -----END PGP SIGNATURE-----
435 PREREQ=GPG
436 test_atom refs/tags/signed-empty subject ''
437 test_atom refs/tags/signed-empty contents:subject ''
438 test_atom refs/tags/signed-empty body "$sig"
439 test_atom refs/tags/signed-empty contents:body ''
440 test_atom refs/tags/signed-empty contents:signature "$sig"
441 test_atom refs/tags/signed-empty contents "$sig"
443 test_atom refs/tags/signed-short subject 'subject line'
444 test_atom refs/tags/signed-short contents:subject 'subject line'
445 test_atom refs/tags/signed-short body "$sig"
446 test_atom refs/tags/signed-short contents:body ''
447 test_atom refs/tags/signed-short contents:signature "$sig"
448 test_atom refs/tags/signed-short contents "subject line
449 $sig"
451 test_atom refs/tags/signed-long subject 'subject line'
452 test_atom refs/tags/signed-long contents:subject 'subject line'
453 test_atom refs/tags/signed-long body "body contents
454 $sig"
455 test_atom refs/tags/signed-long contents:body 'body contents
457 test_atom refs/tags/signed-long contents:signature "$sig"
458 test_atom refs/tags/signed-long contents "subject line
460 body contents
461 $sig"
463 cat >expected <<\EOF
464 408fe76d02a785a006c2e9c669b7be5589ede96d <committer@example.com> refs/tags/master
465 90b5ebede4899eda64893bc2a4c8f1d6fb6dfc40 <committer@example.com> refs/tags/bogo
468 test_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
473 test_done