Code

t6300: add more body-parsing tests
[git.git] / t / t6300-for-each-ref.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Andy Parkins
4 #
6 test_description='for-each-ref test'
8 . ./test-lib.sh
10 # Mon Jul 3 15:18:43 2006 +0000
11 datestamp=1151939923
12 setdate_and_increment () {
13     GIT_COMMITTER_DATE="$datestamp +0200"
14     datestamp=$(expr "$datestamp" + 1)
15     GIT_AUTHOR_DATE="$datestamp +0200"
16     datestamp=$(expr "$datestamp" + 1)
17     export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
18 }
20 test_expect_success 'Create sample commit with known timestamp' '
21         setdate_and_increment &&
22         echo "Using $datestamp" > one &&
23         git add one &&
24         git commit -m "Initial" &&
25         setdate_and_increment &&
26         git tag -a -m "Tagging at $datestamp" testtag
27 '
29 test_expect_success 'Create upstream config' '
30         git update-ref refs/remotes/origin/master master &&
31         git remote add origin nowhere &&
32         git config branch.master.remote origin &&
33         git config branch.master.merge refs/heads/master
34 '
36 test_atom() {
37         case "$1" in
38                 head) ref=refs/heads/master ;;
39                  tag) ref=refs/tags/testtag ;;
40                    *) ref=$1 ;;
41         esac
42         printf '%s\n' "$3" >expected
43         test_expect_${4:-success} "basic atom: $1 $2" "
44                 git for-each-ref --format='%($2)' $ref >actual &&
45                 test_cmp expected actual
46         "
47 }
49 test_atom head refname refs/heads/master
50 test_atom head upstream refs/remotes/origin/master
51 test_atom head objecttype commit
52 test_atom head objectsize 171
53 test_atom head objectname 67a36f10722846e891fbada1ba48ed035de75581
54 test_atom head tree 0e51c00fcb93dffc755546f27593d511e1bdb46f
55 test_atom head parent ''
56 test_atom head numparent 0
57 test_atom head object ''
58 test_atom head type ''
59 test_atom head author 'A U Thor <author@example.com> 1151939924 +0200'
60 test_atom head authorname 'A U Thor'
61 test_atom head authoremail '<author@example.com>'
62 test_atom head authordate 'Mon Jul 3 17:18:44 2006 +0200'
63 test_atom head committer 'C O Mitter <committer@example.com> 1151939923 +0200'
64 test_atom head committername 'C O Mitter'
65 test_atom head committeremail '<committer@example.com>'
66 test_atom head committerdate 'Mon Jul 3 17:18:43 2006 +0200'
67 test_atom head tag ''
68 test_atom head tagger ''
69 test_atom head taggername ''
70 test_atom head taggeremail ''
71 test_atom head taggerdate ''
72 test_atom head creator 'C O Mitter <committer@example.com> 1151939923 +0200'
73 test_atom head creatordate 'Mon Jul 3 17:18:43 2006 +0200'
74 test_atom head subject 'Initial'
75 test_atom head body ''
76 test_atom head contents 'Initial
77 '
79 test_atom tag refname refs/tags/testtag
80 test_atom tag upstream ''
81 test_atom tag objecttype tag
82 test_atom tag objectsize 154
83 test_atom tag objectname 98b46b1d36e5b07909de1b3886224e3e81e87322
84 test_atom tag tree ''
85 test_atom tag parent ''
86 test_atom tag numparent ''
87 test_atom tag object '67a36f10722846e891fbada1ba48ed035de75581'
88 test_atom tag type 'commit'
89 test_atom tag author ''
90 test_atom tag authorname ''
91 test_atom tag authoremail ''
92 test_atom tag authordate ''
93 test_atom tag committer ''
94 test_atom tag committername ''
95 test_atom tag committeremail ''
96 test_atom tag committerdate ''
97 test_atom tag tag 'testtag'
98 test_atom tag tagger 'C O Mitter <committer@example.com> 1151939925 +0200'
99 test_atom tag taggername 'C O Mitter'
100 test_atom tag taggeremail '<committer@example.com>'
101 test_atom tag taggerdate 'Mon Jul 3 17:18:45 2006 +0200'
102 test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
103 test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
104 test_atom tag subject 'Tagging at 1151939927'
105 test_atom tag body ''
106 test_atom tag contents 'Tagging at 1151939927
109 test_expect_success 'Check invalid atoms names are errors' '
110         test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
113 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
114         git for-each-ref --format="%(authordate)" refs/heads &&
115         git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
116         git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
117         git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
120 test_expect_success 'Check valid format specifiers for date fields' '
121         git for-each-ref --format="%(authordate:default)" refs/heads &&
122         git for-each-ref --format="%(authordate:relative)" refs/heads &&
123         git for-each-ref --format="%(authordate:short)" refs/heads &&
124         git for-each-ref --format="%(authordate:local)" refs/heads &&
125         git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
126         git for-each-ref --format="%(authordate:rfc2822)" refs/heads
129 test_expect_success 'Check invalid format specifiers are errors' '
130         test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
133 cat >expected <<\EOF
134 'refs/heads/master' 'Mon Jul 3 17:18:43 2006 +0200' 'Mon Jul 3 17:18:44 2006 +0200'
135 'refs/tags/testtag' 'Mon Jul 3 17:18:45 2006 +0200'
136 EOF
138 test_expect_success 'Check unformatted date fields output' '
139         (git for-each-ref --shell --format="%(refname) %(committerdate) %(authordate)" refs/heads &&
140         git for-each-ref --shell --format="%(refname) %(taggerdate)" refs/tags) >actual &&
141         test_cmp expected actual
144 test_expect_success 'Check format "default" formatted date fields output' '
145         f=default &&
146         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
147         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
148         test_cmp expected actual
151 # Don't know how to do relative check because I can't know when this script
152 # is going to be run and can't fake the current time to git, and hence can't
153 # provide expected output.  Instead, I'll just make sure that "relative"
154 # doesn't exit in error
156 #cat >expected <<\EOF
158 #EOF
160 test_expect_success 'Check format "relative" date fields output' '
161         f=relative &&
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
166 cat >expected <<\EOF
167 'refs/heads/master' '2006-07-03' '2006-07-03'
168 'refs/tags/testtag' '2006-07-03'
169 EOF
171 test_expect_success 'Check format "short" date fields output' '
172         f=short &&
173         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
174         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
175         test_cmp expected actual
178 cat >expected <<\EOF
179 'refs/heads/master' 'Mon Jul 3 15:18:43 2006' 'Mon Jul 3 15:18:44 2006'
180 'refs/tags/testtag' 'Mon Jul 3 15:18:45 2006'
181 EOF
183 test_expect_success 'Check format "local" date fields output' '
184         f=local &&
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' '2006-07-03 17:18:43 +0200' '2006-07-03 17:18:44 +0200'
192 'refs/tags/testtag' '2006-07-03 17:18:45 +0200'
193 EOF
195 test_expect_success 'Check format "iso8601" date fields output' '
196         f=iso8601 &&
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' 'Mon, 3 Jul 2006 17:18:43 +0200' 'Mon, 3 Jul 2006 17:18:44 +0200'
204 'refs/tags/testtag' 'Mon, 3 Jul 2006 17:18:45 +0200'
205 EOF
207 test_expect_success 'Check format "rfc2822" date fields output' '
208         f=rfc2822 &&
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
216 refs/remotes/origin/master
217 refs/tags/testtag
218 EOF
220 test_expect_success 'Verify ascending sort' '
221         git for-each-ref --format="%(refname)" --sort=refname >actual &&
222         test_cmp expected actual
226 cat >expected <<\EOF
227 refs/tags/testtag
228 refs/remotes/origin/master
229 refs/heads/master
230 EOF
232 test_expect_success 'Verify descending sort' '
233         git for-each-ref --format="%(refname)" --sort=-refname >actual &&
234         test_cmp expected actual
237 cat >expected <<\EOF
238 'refs/heads/master'
239 'refs/remotes/origin/master'
240 'refs/tags/testtag'
241 EOF
243 test_expect_success 'Quoting style: shell' '
244         git for-each-ref --shell --format="%(refname)" >actual &&
245         test_cmp expected actual
248 test_expect_success 'Quoting style: perl' '
249         git for-each-ref --perl --format="%(refname)" >actual &&
250         test_cmp expected actual
253 test_expect_success 'Quoting style: python' '
254         git for-each-ref --python --format="%(refname)" >actual &&
255         test_cmp expected actual
258 cat >expected <<\EOF
259 "refs/heads/master"
260 "refs/remotes/origin/master"
261 "refs/tags/testtag"
262 EOF
264 test_expect_success 'Quoting style: tcl' '
265         git for-each-ref --tcl --format="%(refname)" >actual &&
266         test_cmp expected actual
269 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
270         test_expect_success "more than one quoting style: $i" "
271                 git for-each-ref $i 2>&1 | (read line &&
272                 case \$line in
273                 \"error: more than one quoting style\"*) : happy;;
274                 *) false
275                 esac)
276         "
277 done
279 cat >expected <<\EOF
280 master
281 testtag
282 EOF
284 test_expect_success 'Check short refname format' '
285         (git for-each-ref --format="%(refname:short)" refs/heads &&
286         git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
287         test_cmp expected actual
290 cat >expected <<EOF
291 origin/master
292 EOF
294 test_expect_success 'Check short upstream format' '
295         git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
296         test_cmp expected actual
299 cat >expected <<EOF
300 67a36f1
301 EOF
303 test_expect_success 'Check short objectname format' '
304         git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
305         test_cmp expected actual
308 test_expect_success 'Check for invalid refname format' '
309         test_must_fail git for-each-ref --format="%(refname:INVALID)"
312 cat >expected <<\EOF
313 heads/master
314 tags/master
315 EOF
317 test_expect_success 'Check ambiguous head and tag refs (strict)' '
318         git config --bool core.warnambiguousrefs true &&
319         git checkout -b newtag &&
320         echo "Using $datestamp" > one &&
321         git add one &&
322         git commit -m "Branch" &&
323         setdate_and_increment &&
324         git tag -m "Tagging at $datestamp" master &&
325         git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
326         test_cmp expected actual
329 cat >expected <<\EOF
330 heads/master
331 master
332 EOF
334 test_expect_success 'Check ambiguous head and tag refs (loose)' '
335         git config --bool core.warnambiguousrefs false &&
336         git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
337         test_cmp expected actual
340 cat >expected <<\EOF
341 heads/ambiguous
342 ambiguous
343 EOF
345 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
346         git checkout master &&
347         git tag ambiguous testtag^0 &&
348         git branch ambiguous testtag^0 &&
349         git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
350         test_cmp expected actual
353 test_expect_success 'an unusual tag with an incomplete line' '
355         git tag -m "bogo" bogo &&
356         bogo=$(git cat-file tag bogo) &&
357         bogo=$(printf "%s" "$bogo" | git mktag) &&
358         git tag -f bogo "$bogo" &&
359         git for-each-ref --format "%(body)" refs/tags/bogo
363 test_expect_success 'create tag with subject and body content' '
364         cat >>msg <<-\EOF &&
365                 the subject line
367                 first body line
368                 second body line
369         EOF
370         git tag -F msg subject-body
372 test_atom refs/tags/subject-body subject 'the subject line'
373 test_atom refs/tags/subject-body body 'first body line
374 second body line
376 test_atom refs/tags/subject-body contents 'the subject line
378 first body line
379 second body line
382 test_done