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
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
36test_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}
48
49test_atom head refname refs/heads/master
50test_atom head upstream refs/remotes/origin/master
51test_atom head objecttype commit
52test_atom head objectsize 171
53test_atom head objectname 67a36f10722846e891fbada1ba48ed035de75581
54test_atom head tree 0e51c00fcb93dffc755546f27593d511e1bdb46f
55test_atom head parent ''
56test_atom head numparent 0
57test_atom head object ''
58test_atom head type ''
59test_atom head author 'A U Thor <author@example.com> 1151939924 +0200'
60test_atom head authorname 'A U Thor'
61test_atom head authoremail '<author@example.com>'
62test_atom head authordate 'Mon Jul 3 17:18:44 2006 +0200'
63test_atom head committer 'C O Mitter <committer@example.com> 1151939923 +0200'
64test_atom head committername 'C O Mitter'
65test_atom head committeremail '<committer@example.com>'
66test_atom head committerdate 'Mon Jul 3 17:18:43 2006 +0200'
67test_atom head tag ''
68test_atom head tagger ''
69test_atom head taggername ''
70test_atom head taggeremail ''
71test_atom head taggerdate ''
72test_atom head creator 'C O Mitter <committer@example.com> 1151939923 +0200'
73test_atom head creatordate 'Mon Jul 3 17:18:43 2006 +0200'
74test_atom head subject 'Initial'
75test_atom head body ''
76test_atom head contents 'Initial
77'
78
79test_atom tag refname refs/tags/testtag
80test_atom tag upstream ''
81test_atom tag objecttype tag
82test_atom tag objectsize 154
83test_atom tag objectname 98b46b1d36e5b07909de1b3886224e3e81e87322
84test_atom tag tree ''
85test_atom tag parent ''
86test_atom tag numparent ''
87test_atom tag object '67a36f10722846e891fbada1ba48ed035de75581'
88test_atom tag type 'commit'
89test_atom tag author ''
90test_atom tag authorname ''
91test_atom tag authoremail ''
92test_atom tag authordate ''
93test_atom tag committer ''
94test_atom tag committername ''
95test_atom tag committeremail ''
96test_atom tag committerdate ''
97test_atom tag tag 'testtag'
98test_atom tag tagger 'C O Mitter <committer@example.com> 1151939925 +0200'
99test_atom tag taggername 'C O Mitter'
100test_atom tag taggeremail '<committer@example.com>'
101test_atom tag taggerdate 'Mon Jul 3 17:18:45 2006 +0200'
102test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
103test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
104test_atom tag subject 'Tagging at 1151939927'
105test_atom tag body ''
106test_atom tag contents 'Tagging at 1151939927
107'
108
109test_expect_success 'Check invalid atoms names are errors' '
110 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
111'
112
113test_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
118'
119
120test_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
127'
128
129test_expect_success 'Check invalid format specifiers are errors' '
130 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
131'
132
133cat >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'
136EOF
137
138test_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
142'
143
144test_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
149'
150
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
155#
156#cat >expected <<\EOF
157#
158#EOF
159#
160test_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
164'
165
166cat >expected <<\EOF
167'refs/heads/master' '2006-07-03' '2006-07-03'
168'refs/tags/testtag' '2006-07-03'
169EOF
170
171test_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
176'
177
178cat >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'
181EOF
182
183test_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
188'
189
190cat >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'
193EOF
194
195test_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
200'
201
202cat >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'
205EOF
206
207test_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
212'
213
214cat >expected <<\EOF
215refs/heads/master
216refs/remotes/origin/master
217refs/tags/testtag
218EOF
219
220test_expect_success 'Verify ascending sort' '
221 git for-each-ref --format="%(refname)" --sort=refname >actual &&
222 test_cmp expected actual
223'
224
225
226cat >expected <<\EOF
227refs/tags/testtag
228refs/remotes/origin/master
229refs/heads/master
230EOF
231
232test_expect_success 'Verify descending sort' '
233 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
234 test_cmp expected actual
235'
236
237cat >expected <<\EOF
238'refs/heads/master'
239'refs/remotes/origin/master'
240'refs/tags/testtag'
241EOF
242
243test_expect_success 'Quoting style: shell' '
244 git for-each-ref --shell --format="%(refname)" >actual &&
245 test_cmp expected actual
246'
247
248test_expect_success 'Quoting style: perl' '
249 git for-each-ref --perl --format="%(refname)" >actual &&
250 test_cmp expected actual
251'
252
253test_expect_success 'Quoting style: python' '
254 git for-each-ref --python --format="%(refname)" >actual &&
255 test_cmp expected actual
256'
257
258cat >expected <<\EOF
259"refs/heads/master"
260"refs/remotes/origin/master"
261"refs/tags/testtag"
262EOF
263
264test_expect_success 'Quoting style: tcl' '
265 git for-each-ref --tcl --format="%(refname)" >actual &&
266 test_cmp expected actual
267'
268
269for 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 "
277done
278
279cat >expected <<\EOF
280master
281testtag
282EOF
283
284test_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
288'
289
290cat >expected <<EOF
291origin/master
292EOF
293
294test_expect_success 'Check short upstream format' '
295 git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
296 test_cmp expected actual
297'
298
299cat >expected <<EOF
30067a36f1
301EOF
302
303test_expect_success 'Check short objectname format' '
304 git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
305 test_cmp expected actual
306'
307
308test_expect_success 'Check for invalid refname format' '
309 test_must_fail git for-each-ref --format="%(refname:INVALID)"
310'
311
312cat >expected <<\EOF
313heads/master
314tags/master
315EOF
316
317test_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
327'
328
329cat >expected <<\EOF
330heads/master
331master
332EOF
333
334test_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
338'
339
340cat >expected <<\EOF
341heads/ambiguous
342ambiguous
343EOF
344
345test_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
351'
352
353test_expect_success 'an unusual tag with an incomplete line' '
354
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
360
361'
362
363test_expect_success 'create tag with subject and body content' '
364 cat >>msg <<-\EOF &&
365 the subject line
366
367 first body line
368 second body line
369 EOF
370 git tag -F msg subject-body
371'
372test_atom refs/tags/subject-body subject 'the subject line'
373test_atom refs/tags/subject-body body 'first body line
374second body line
375'
376test_atom refs/tags/subject-body contents 'the subject line
377
378first body line
379second body line
380'
381
382test_done