]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6300-for-each-ref.sh
t/README: Add a note about the dangers of coverage chasing
[thirdparty/git.git] / t / t6300-for-each-ref.sh
CommitLineData
96b2d4fa
AP
1#!/bin/sh
2#
3# Copyright (c) 2007 Andy Parkins
4#
5
6test_description='for-each-ref test'
7
8. ./test-lib.sh
9
10# Mon Jul 3 15:18:43 2006 +0000
11datestamp=1151939923
12setdate_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}
19
20test_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'
28
8cae19d9
JK
29test_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'
35
d2bf48d2
JK
36test_atom() {
37 case "$1" in
38 head) ref=refs/heads/master ;;
39 tag) ref=refs/tags/testtag ;;
40 esac
41 printf '%s\n' "$3" >expected
42 test_expect_${4:-success} "basic atom: $1 $2" "
43 git for-each-ref --format='%($2)' $ref >actual &&
44 test_cmp expected actual
45 "
46}
47
48test_atom head refname refs/heads/master
8cae19d9 49test_atom head upstream refs/remotes/origin/master
d2bf48d2
JK
50test_atom head objecttype commit
51test_atom head objectsize 171
52test_atom head objectname 67a36f10722846e891fbada1ba48ed035de75581
53test_atom head tree 0e51c00fcb93dffc755546f27593d511e1bdb46f
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 body ''
75test_atom head contents 'Initial
76'
77
78test_atom tag refname refs/tags/testtag
8cae19d9 79test_atom tag upstream ''
d2bf48d2
JK
80test_atom tag objecttype tag
81test_atom tag objectsize 154
82test_atom tag objectname 98b46b1d36e5b07909de1b3886224e3e81e87322
83test_atom tag tree ''
84test_atom tag parent ''
85test_atom tag numparent ''
86test_atom tag object '67a36f10722846e891fbada1ba48ed035de75581'
87test_atom tag type 'commit'
88test_atom tag author ''
89test_atom tag authorname ''
90test_atom tag authoremail ''
91test_atom tag authordate ''
92test_atom tag committer ''
93test_atom tag committername ''
94test_atom tag committeremail ''
95test_atom tag committerdate ''
96test_atom tag tag 'testtag'
97test_atom tag tagger 'C O Mitter <committer@example.com> 1151939925 +0200'
98test_atom tag taggername 'C O Mitter'
99test_atom tag taggeremail '<committer@example.com>'
100test_atom tag taggerdate 'Mon Jul 3 17:18:45 2006 +0200'
101test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
102test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
103test_atom tag subject 'Tagging at 1151939927'
104test_atom tag body ''
105test_atom tag contents 'Tagging at 1151939927
96b2d4fa
AP
106'
107
41ac414e 108test_expect_success 'Check invalid atoms names are errors' '
3604e7c5 109 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
96b2d4fa
AP
110'
111
112test_expect_success 'Check format specifiers are ignored in naming date atoms' '
3604e7c5
NS
113 git for-each-ref --format="%(authordate)" refs/heads &&
114 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
115 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
116 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
96b2d4fa
AP
117'
118
119test_expect_success 'Check valid format specifiers for date fields' '
3604e7c5
NS
120 git for-each-ref --format="%(authordate:default)" refs/heads &&
121 git for-each-ref --format="%(authordate:relative)" refs/heads &&
122 git for-each-ref --format="%(authordate:short)" refs/heads &&
123 git for-each-ref --format="%(authordate:local)" refs/heads &&
124 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
125 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
96b2d4fa
AP
126'
127
41ac414e 128test_expect_success 'Check invalid format specifiers are errors' '
3604e7c5 129 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
96b2d4fa
AP
130'
131
132cat >expected <<\EOF
133'refs/heads/master' 'Mon Jul 3 17:18:43 2006 +0200' 'Mon Jul 3 17:18:44 2006 +0200'
134'refs/tags/testtag' 'Mon Jul 3 17:18:45 2006 +0200'
135EOF
136
137test_expect_success 'Check unformatted date fields output' '
138 (git for-each-ref --shell --format="%(refname) %(committerdate) %(authordate)" refs/heads &&
139 git for-each-ref --shell --format="%(refname) %(taggerdate)" refs/tags) >actual &&
3af82863 140 test_cmp expected actual
96b2d4fa
AP
141'
142
143test_expect_success 'Check format "default" formatted date fields output' '
144 f=default &&
145 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
146 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
3af82863 147 test_cmp expected actual
96b2d4fa
AP
148'
149
150# Don't know how to do relative check because I can't know when this script
151# is going to be run and can't fake the current time to git, and hence can't
152# provide expected output. Instead, I'll just make sure that "relative"
153# doesn't exit in error
154#
155#cat >expected <<\EOF
156#
157#EOF
158#
159test_expect_success 'Check format "relative" date fields output' '
160 f=relative &&
161 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
162 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
163'
164
165cat >expected <<\EOF
166'refs/heads/master' '2006-07-03' '2006-07-03'
167'refs/tags/testtag' '2006-07-03'
168EOF
169
170test_expect_success 'Check format "short" date fields output' '
171 f=short &&
172 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
173 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
3af82863 174 test_cmp expected actual
96b2d4fa
AP
175'
176
177cat >expected <<\EOF
178'refs/heads/master' 'Mon Jul 3 15:18:43 2006' 'Mon Jul 3 15:18:44 2006'
179'refs/tags/testtag' 'Mon Jul 3 15:18:45 2006'
180EOF
181
182test_expect_success 'Check format "local" date fields output' '
183 f=local &&
184 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
185 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
3af82863 186 test_cmp expected actual
96b2d4fa
AP
187'
188
189cat >expected <<\EOF
190'refs/heads/master' '2006-07-03 17:18:43 +0200' '2006-07-03 17:18:44 +0200'
191'refs/tags/testtag' '2006-07-03 17:18:45 +0200'
192EOF
193
194test_expect_success 'Check format "iso8601" date fields output' '
195 f=iso8601 &&
196 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
197 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
3af82863 198 test_cmp expected actual
96b2d4fa
AP
199'
200
201cat >expected <<\EOF
202'refs/heads/master' 'Mon, 3 Jul 2006 17:18:43 +0200' 'Mon, 3 Jul 2006 17:18:44 +0200'
203'refs/tags/testtag' 'Mon, 3 Jul 2006 17:18:45 +0200'
204EOF
205
206test_expect_success 'Check format "rfc2822" date fields output' '
207 f=rfc2822 &&
208 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
209 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
3af82863 210 test_cmp expected actual
96b2d4fa
AP
211'
212
c899a57c
LH
213cat >expected <<\EOF
214refs/heads/master
8cae19d9 215refs/remotes/origin/master
c899a57c
LH
216refs/tags/testtag
217EOF
218
219test_expect_success 'Verify ascending sort' '
3604e7c5 220 git for-each-ref --format="%(refname)" --sort=refname >actual &&
3af82863 221 test_cmp expected actual
c899a57c
LH
222'
223
224
225cat >expected <<\EOF
226refs/tags/testtag
8cae19d9 227refs/remotes/origin/master
c899a57c
LH
228refs/heads/master
229EOF
230
231test_expect_success 'Verify descending sort' '
3604e7c5 232 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
3af82863 233 test_cmp expected actual
c899a57c
LH
234'
235
c9ecf4f1
JS
236cat >expected <<\EOF
237'refs/heads/master'
8cae19d9 238'refs/remotes/origin/master'
c9ecf4f1
JS
239'refs/tags/testtag'
240EOF
241
242test_expect_success 'Quoting style: shell' '
243 git for-each-ref --shell --format="%(refname)" >actual &&
3af82863 244 test_cmp expected actual
c9ecf4f1
JS
245'
246
247test_expect_success 'Quoting style: perl' '
248 git for-each-ref --perl --format="%(refname)" >actual &&
3af82863 249 test_cmp expected actual
c9ecf4f1
JS
250'
251
252test_expect_success 'Quoting style: python' '
253 git for-each-ref --python --format="%(refname)" >actual &&
3af82863 254 test_cmp expected actual
c9ecf4f1
JS
255'
256
257cat >expected <<\EOF
258"refs/heads/master"
8cae19d9 259"refs/remotes/origin/master"
c9ecf4f1
JS
260"refs/tags/testtag"
261EOF
262
263test_expect_success 'Quoting style: tcl' '
264 git for-each-ref --tcl --format="%(refname)" >actual &&
3af82863 265 test_cmp expected actual
c9ecf4f1
JS
266'
267
268for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
269 test_expect_success "more than one quoting style: $i" "
270 git for-each-ref $i 2>&1 | (read line &&
271 case \$line in
272 \"error: more than one quoting style\"*) : happy;;
273 *) false
274 esac)
275 "
276done
c899a57c 277
7d66f21a
BW
278cat >expected <<\EOF
279master
280testtag
281EOF
282
283test_expect_success 'Check short refname format' '
284 (git for-each-ref --format="%(refname:short)" refs/heads &&
285 git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
286 test_cmp expected actual
287'
288
8cae19d9
JK
289cat >expected <<EOF
290origin/master
291EOF
292
293test_expect_success 'Check short upstream format' '
294 git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
295 test_cmp expected actual
296'
297
67687fea
MG
298cat >expected <<EOF
29967a36f1
300EOF
301
302test_expect_success 'Check short objectname format' '
303 git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
304 test_cmp expected actual
305'
306
7d66f21a
BW
307test_expect_success 'Check for invalid refname format' '
308 test_must_fail git for-each-ref --format="%(refname:INVALID)"
309'
310
311cat >expected <<\EOF
312heads/master
2bb98169 313tags/master
7d66f21a
BW
314EOF
315
2bb98169
BW
316test_expect_success 'Check ambiguous head and tag refs (strict)' '
317 git config --bool core.warnambiguousrefs true &&
7d66f21a
BW
318 git checkout -b newtag &&
319 echo "Using $datestamp" > one &&
320 git add one &&
321 git commit -m "Branch" &&
322 setdate_and_increment &&
323 git tag -m "Tagging at $datestamp" master &&
324 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
325 test_cmp expected actual
326'
327
2bb98169
BW
328cat >expected <<\EOF
329heads/master
330master
331EOF
332
333test_expect_success 'Check ambiguous head and tag refs (loose)' '
334 git config --bool core.warnambiguousrefs false &&
335 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
336 test_cmp expected actual
337'
338
7d66f21a
BW
339cat >expected <<\EOF
340heads/ambiguous
341ambiguous
342EOF
343
2bb98169 344test_expect_success 'Check ambiguous head and tag refs II (loose)' '
7d66f21a
BW
345 git checkout master &&
346 git tag ambiguous testtag^0 &&
347 git branch ambiguous testtag^0 &&
348 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
349 test_cmp expected actual
350'
351
e276c26b
JH
352test_expect_success 'an unusual tag with an incomplete line' '
353
354 git tag -m "bogo" bogo &&
355 bogo=$(git cat-file tag bogo) &&
356 bogo=$(printf "%s" "$bogo" | git mktag) &&
357 git tag -f bogo "$bogo" &&
358 git for-each-ref --format "%(body)" refs/tags/bogo
359
360'
361
96b2d4fa 362test_done