]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6300-for-each-ref.sh
tests: mark tests relying on the current default for `init.defaultBranch`
[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
334afbc7
JS
8GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
9export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
96b2d4fa 11. ./test-lib.sh
e2b23972 12. "$TEST_DIRECTORY"/lib-gpg.sh
11b087ad 13. "$TEST_DIRECTORY"/lib-terminal.sh
96b2d4fa 14
db7bae25
JK
15# Mon Jul 3 23:18:43 2006 +0000
16datestamp=1151968723
96b2d4fa
AP
17setdate_and_increment () {
18 GIT_COMMITTER_DATE="$datestamp +0200"
19 datestamp=$(expr "$datestamp" + 1)
20 GIT_AUTHOR_DATE="$datestamp +0200"
21 datestamp=$(expr "$datestamp" + 1)
22 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
23}
24
bc147968 25test_expect_success setup '
1f5f8f3e 26 test_oid_cache <<-EOF &&
27 disklen sha1:138
28 disklen sha256:154
29 EOF
96b2d4fa
AP
30 setdate_and_increment &&
31 echo "Using $datestamp" > one &&
32 git add one &&
33 git commit -m "Initial" &&
34 setdate_and_increment &&
bc147968 35 git tag -a -m "Tagging at $datestamp" testtag &&
8cae19d9
JK
36 git update-ref refs/remotes/origin/master master &&
37 git remote add origin nowhere &&
38 git config branch.master.remote origin &&
29bc8850
JK
39 git config branch.master.merge refs/heads/master &&
40 git remote add myfork elsewhere &&
41 git config remote.pushdefault myfork &&
42 git config push.default current
8cae19d9
JK
43'
44
d2bf48d2
JK
45test_atom() {
46 case "$1" in
47 head) ref=refs/heads/master ;;
48 tag) ref=refs/tags/testtag ;;
01f95825 49 sym) ref=refs/heads/sym ;;
7140c22c 50 *) ref=$1 ;;
d2bf48d2
JK
51 esac
52 printf '%s\n' "$3" >expected
e2b23972 53 test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
d2bf48d2 54 git for-each-ref --format='%($2)' $ref >actual &&
e2b23972
MG
55 sanitize_pgp <actual >actual.clean &&
56 test_cmp expected actual.clean
d2bf48d2 57 "
b6839fda
CC
58 # Automatically test "contents:size" atom after testing "contents"
59 if test "$2" = "contents"
60 then
61 case $(git cat-file -t "$ref") in
62 tag)
63 # We cannot use $3 as it expects sanitize_pgp to run
64 expect=$(git cat-file tag $ref | tail -n +6 | wc -c) ;;
65 tree | blob)
66 expect='' ;;
67 commit)
68 expect=$(printf '%s' "$3" | wc -c) ;;
69 esac
70 # Leave $expect unquoted to lose possible leading whitespaces
71 echo $expect >expected
3db796c1 72 test_expect_${4:-success} $PREREQ "basic atom: $1 contents:size" '
b6839fda 73 git for-each-ref --format="%(contents:size)" "$ref" >actual &&
3db796c1 74 test_cmp expected actual
b6839fda
CC
75 '
76 fi
d2bf48d2
JK
77}
78
1f5f8f3e 79hexlen=$(test_oid hexsz)
80disklen=$(test_oid disklen)
81
d2bf48d2 82test_atom head refname refs/heads/master
bea4dbea 83test_atom head refname: refs/heads/master
1d094db9 84test_atom head refname:short master
17938f17
KN
85test_atom head refname:lstrip=1 heads/master
86test_atom head refname:lstrip=2 master
1a0ca5e3
KN
87test_atom head refname:lstrip=-1 master
88test_atom head refname:lstrip=-2 heads/master
1a34728e
KN
89test_atom head refname:rstrip=1 refs/heads
90test_atom head refname:rstrip=2 refs
91test_atom head refname:rstrip=-1 refs
92test_atom head refname:rstrip=-2 refs/heads
44a6b6ce
JH
93test_atom head refname:strip=1 heads/master
94test_atom head refname:strip=2 master
95test_atom head refname:strip=-1 master
96test_atom head refname:strip=-2 heads/master
8cae19d9 97test_atom head upstream refs/remotes/origin/master
1d094db9 98test_atom head upstream:short origin/master
17938f17 99test_atom head upstream:lstrip=2 origin/master
1a0ca5e3 100test_atom head upstream:lstrip=-2 origin/master
1a34728e
KN
101test_atom head upstream:rstrip=2 refs/remotes
102test_atom head upstream:rstrip=-2 refs/remotes
44a6b6ce
JH
103test_atom head upstream:strip=2 origin/master
104test_atom head upstream:strip=-2 origin/master
29bc8850 105test_atom head push refs/remotes/myfork/master
1d094db9 106test_atom head push:short myfork/master
17938f17 107test_atom head push:lstrip=1 remotes/myfork/master
1a0ca5e3 108test_atom head push:lstrip=-1 master
1a34728e
KN
109test_atom head push:rstrip=1 refs/remotes/myfork
110test_atom head push:rstrip=-1 refs
44a6b6ce
JH
111test_atom head push:strip=1 remotes/myfork/master
112test_atom head push:strip=-1 master
d2bf48d2 113test_atom head objecttype commit
1f5f8f3e 114test_atom head objectsize $((131 + hexlen))
115test_atom head objectsize:disk $disklen
116test_atom head deltabase $ZERO_OID
189a5467 117test_atom head objectname $(git rev-parse refs/heads/master)
1d094db9 118test_atom head objectname:short $(git rev-parse --short refs/heads/master)
42d0eb05
KN
119test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
120test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
189a5467 121test_atom head tree $(git rev-parse refs/heads/master^{tree})
837adb10
HV
122test_atom head tree:short $(git rev-parse --short refs/heads/master^{tree})
123test_atom head tree:short=1 $(git rev-parse --short=1 refs/heads/master^{tree})
124test_atom head tree:short=10 $(git rev-parse --short=10 refs/heads/master^{tree})
d2bf48d2 125test_atom head parent ''
26bc0aaf
HV
126test_atom head parent:short ''
127test_atom head parent:short=1 ''
128test_atom head parent:short=10 ''
d2bf48d2
JK
129test_atom head numparent 0
130test_atom head object ''
131test_atom head type ''
b74cf648
JK
132test_atom head '*objectname' ''
133test_atom head '*objecttype' ''
db7bae25 134test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
d2bf48d2
JK
135test_atom head authorname 'A U Thor'
136test_atom head authoremail '<author@example.com>'
b82445dc
HV
137test_atom head authoremail:trim 'author@example.com'
138test_atom head authoremail:localpart 'author'
db7bae25
JK
139test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
140test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
d2bf48d2
JK
141test_atom head committername 'C O Mitter'
142test_atom head committeremail '<committer@example.com>'
b82445dc
HV
143test_atom head committeremail:trim 'committer@example.com'
144test_atom head committeremail:localpart 'committer'
db7bae25 145test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
d2bf48d2
JK
146test_atom head tag ''
147test_atom head tagger ''
148test_atom head taggername ''
149test_atom head taggeremail ''
b82445dc
HV
150test_atom head taggeremail:trim ''
151test_atom head taggeremail:localpart ''
d2bf48d2 152test_atom head taggerdate ''
db7bae25
JK
153test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
154test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
d2bf48d2 155test_atom head subject 'Initial'
905f0a4e 156test_atom head subject:sanitize 'Initial'
e2b23972 157test_atom head contents:subject 'Initial'
d2bf48d2 158test_atom head body ''
e2b23972
MG
159test_atom head contents:body ''
160test_atom head contents:signature ''
d2bf48d2
JK
161test_atom head contents 'Initial
162'
7a48b832 163test_atom head HEAD '*'
d2bf48d2
JK
164
165test_atom tag refname refs/tags/testtag
1d094db9 166test_atom tag refname:short testtag
8cae19d9 167test_atom tag upstream ''
29bc8850 168test_atom tag push ''
d2bf48d2 169test_atom tag objecttype tag
1f5f8f3e 170test_atom tag objectsize $((114 + hexlen))
171test_atom tag objectsize:disk $disklen
172test_atom tag '*objectsize:disk' $disklen
173test_atom tag deltabase $ZERO_OID
174test_atom tag '*deltabase' $ZERO_OID
189a5467 175test_atom tag objectname $(git rev-parse refs/tags/testtag)
1d094db9 176test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
42d0eb05
KN
177test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
178test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
d2bf48d2 179test_atom tag tree ''
837adb10
HV
180test_atom tag tree:short ''
181test_atom tag tree:short=1 ''
182test_atom tag tree:short=10 ''
d2bf48d2 183test_atom tag parent ''
26bc0aaf
HV
184test_atom tag parent:short ''
185test_atom tag parent:short=1 ''
186test_atom tag parent:short=10 ''
d2bf48d2 187test_atom tag numparent ''
189a5467 188test_atom tag object $(git rev-parse refs/tags/testtag^0)
d2bf48d2 189test_atom tag type 'commit'
1f5f8f3e 190test_atom tag '*objectname' $(git rev-parse refs/tags/testtag^{})
b74cf648 191test_atom tag '*objecttype' 'commit'
d2bf48d2
JK
192test_atom tag author ''
193test_atom tag authorname ''
194test_atom tag authoremail ''
b82445dc
HV
195test_atom tag authoremail:trim ''
196test_atom tag authoremail:localpart ''
d2bf48d2
JK
197test_atom tag authordate ''
198test_atom tag committer ''
199test_atom tag committername ''
200test_atom tag committeremail ''
b82445dc
HV
201test_atom tag committeremail:trim ''
202test_atom tag committeremail:localpart ''
d2bf48d2
JK
203test_atom tag committerdate ''
204test_atom tag tag 'testtag'
db7bae25 205test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
d2bf48d2
JK
206test_atom tag taggername 'C O Mitter'
207test_atom tag taggeremail '<committer@example.com>'
b82445dc
HV
208test_atom tag taggeremail:trim 'committer@example.com'
209test_atom tag taggeremail:localpart 'committer'
db7bae25
JK
210test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
211test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
212test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
213test_atom tag subject 'Tagging at 1151968727'
905f0a4e 214test_atom tag subject:sanitize 'Tagging-at-1151968727'
db7bae25 215test_atom tag contents:subject 'Tagging at 1151968727'
d2bf48d2 216test_atom tag body ''
e2b23972
MG
217test_atom tag contents:body ''
218test_atom tag contents:signature ''
db7bae25 219test_atom tag contents 'Tagging at 1151968727
96b2d4fa 220'
7a48b832 221test_atom tag HEAD ' '
96b2d4fa 222
41ac414e 223test_expect_success 'Check invalid atoms names are errors' '
3604e7c5 224 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
96b2d4fa
AP
225'
226
227test_expect_success 'Check format specifiers are ignored in naming date atoms' '
3604e7c5
NS
228 git for-each-ref --format="%(authordate)" refs/heads &&
229 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
230 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
231 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
96b2d4fa
AP
232'
233
234test_expect_success 'Check valid format specifiers for date fields' '
3604e7c5
NS
235 git for-each-ref --format="%(authordate:default)" refs/heads &&
236 git for-each-ref --format="%(authordate:relative)" refs/heads &&
237 git for-each-ref --format="%(authordate:short)" refs/heads &&
238 git for-each-ref --format="%(authordate:local)" refs/heads &&
239 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
240 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
96b2d4fa
AP
241'
242
41ac414e 243test_expect_success 'Check invalid format specifiers are errors' '
3604e7c5 244 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
96b2d4fa
AP
245'
246
42d0eb05
KN
247test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
248 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
249 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
250 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
251'
252
f95cecf4
JK
253test_date () {
254 f=$1 &&
255 committer_date=$2 &&
256 author_date=$3 &&
257 tagger_date=$4 &&
258 cat >expected <<-EOF &&
259 'refs/heads/master' '$committer_date' '$author_date'
260 'refs/tags/testtag' '$tagger_date'
261 EOF
262 (
263 git for-each-ref --shell \
264 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
265 refs/heads &&
266 git for-each-ref --shell \
267 --format="%(refname) %(taggerdate${f:+:$f})" \
268 refs/tags
269 ) >actual &&
270 test_cmp expected actual
271}
96b2d4fa
AP
272
273test_expect_success 'Check unformatted date fields output' '
f95cecf4 274 test_date "" \
db7bae25
JK
275 "Tue Jul 4 01:18:43 2006 +0200" \
276 "Tue Jul 4 01:18:44 2006 +0200" \
277 "Tue Jul 4 01:18:45 2006 +0200"
96b2d4fa
AP
278'
279
280test_expect_success 'Check format "default" formatted date fields output' '
f95cecf4 281 test_date default \
db7bae25
JK
282 "Tue Jul 4 01:18:43 2006 +0200" \
283 "Tue Jul 4 01:18:44 2006 +0200" \
284 "Tue Jul 4 01:18:45 2006 +0200"
96b2d4fa
AP
285'
286
99264e93
JK
287test_expect_success 'Check format "default-local" date fields output' '
288 test_date default-local "Mon Jul 3 23:18:43 2006" "Mon Jul 3 23:18:44 2006" "Mon Jul 3 23:18:45 2006"
96b2d4fa
AP
289'
290
291# Don't know how to do relative check because I can't know when this script
292# is going to be run and can't fake the current time to git, and hence can't
293# provide expected output. Instead, I'll just make sure that "relative"
294# doesn't exit in error
96b2d4fa
AP
295test_expect_success 'Check format "relative" date fields output' '
296 f=relative &&
297 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
298 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
299'
300
99264e93
JK
301# We just check that this is the same as "relative" for now.
302test_expect_success 'Check format "relative-local" date fields output' '
303 test_date relative-local \
304 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
305 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
306 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
307'
96b2d4fa
AP
308
309test_expect_success 'Check format "short" date fields output' '
db7bae25 310 test_date short 2006-07-04 2006-07-04 2006-07-04
96b2d4fa
AP
311'
312
99264e93
JK
313test_expect_success 'Check format "short-local" date fields output' '
314 test_date short-local 2006-07-03 2006-07-03 2006-07-03
315'
96b2d4fa
AP
316
317test_expect_success 'Check format "local" date fields output' '
f95cecf4 318 test_date local \
db7bae25
JK
319 "Mon Jul 3 23:18:43 2006" \
320 "Mon Jul 3 23:18:44 2006" \
321 "Mon Jul 3 23:18:45 2006"
96b2d4fa
AP
322'
323
96b2d4fa 324test_expect_success 'Check format "iso8601" date fields output' '
f95cecf4 325 test_date iso8601 \
db7bae25
JK
326 "2006-07-04 01:18:43 +0200" \
327 "2006-07-04 01:18:44 +0200" \
328 "2006-07-04 01:18:45 +0200"
96b2d4fa
AP
329'
330
99264e93
JK
331test_expect_success 'Check format "iso8601-local" date fields output' '
332 test_date iso8601-local "2006-07-03 23:18:43 +0000" "2006-07-03 23:18:44 +0000" "2006-07-03 23:18:45 +0000"
333'
96b2d4fa
AP
334
335test_expect_success 'Check format "rfc2822" date fields output' '
f95cecf4 336 test_date rfc2822 \
db7bae25
JK
337 "Tue, 4 Jul 2006 01:18:43 +0200" \
338 "Tue, 4 Jul 2006 01:18:44 +0200" \
339 "Tue, 4 Jul 2006 01:18:45 +0200"
96b2d4fa
AP
340'
341
99264e93
JK
342test_expect_success 'Check format "rfc2822-local" date fields output' '
343 test_date rfc2822-local "Mon, 3 Jul 2006 23:18:43 +0000" "Mon, 3 Jul 2006 23:18:44 +0000" "Mon, 3 Jul 2006 23:18:45 +0000"
344'
345
f3c1ba50 346test_expect_success 'Check format "raw" date fields output' '
db7bae25 347 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
f3c1ba50
JK
348'
349
99264e93
JK
350test_expect_success 'Check format "raw-local" date fields output' '
351 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
96b2d4fa
AP
352'
353
aa1462cc 354test_expect_success 'Check format of strftime date fields' '
db7bae25 355 echo "my date is 2006-07-04" >expected &&
aa1462cc
JK
356 git for-each-ref \
357 --format="%(authordate:format:my date is %Y-%m-%d)" \
358 refs/heads >actual &&
359 test_cmp expected actual
360'
361
99264e93
JK
362test_expect_success 'Check format of strftime-local date fields' '
363 echo "my date is 2006-07-03" >expected &&
364 git for-each-ref \
365 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
366 refs/heads >actual &&
367 test_cmp expected actual
368'
369
e4f031e3
JK
370test_expect_success 'exercise strftime with odd fields' '
371 echo >expected &&
372 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
373 test_cmp expected actual &&
8125a58b 374 long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
e4f031e3
JK
375 echo $long >expected &&
376 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
377 test_cmp expected actual
378'
379
c899a57c
LH
380cat >expected <<\EOF
381refs/heads/master
8cae19d9 382refs/remotes/origin/master
c899a57c
LH
383refs/tags/testtag
384EOF
385
386test_expect_success 'Verify ascending sort' '
3604e7c5 387 git for-each-ref --format="%(refname)" --sort=refname >actual &&
3af82863 388 test_cmp expected actual
c899a57c
LH
389'
390
391
392cat >expected <<\EOF
393refs/tags/testtag
8cae19d9 394refs/remotes/origin/master
c899a57c
LH
395refs/heads/master
396EOF
397
398test_expect_success 'Verify descending sort' '
3604e7c5 399 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
3af82863 400 test_cmp expected actual
b31e2680
TB
401'
402
403cat >expected <<\EOF
404refs/tags/testtag
405refs/tags/testtag-2
406EOF
407
408test_expect_success 'exercise patterns with prefixes' '
409 git tag testtag-2 &&
410 test_when_finished "git tag -d testtag-2" &&
411 git for-each-ref --format="%(refname)" \
412 refs/tags/testtag refs/tags/testtag-2 >actual &&
413 test_cmp expected actual
414'
415
416cat >expected <<\EOF
417refs/tags/testtag
418refs/tags/testtag-2
419EOF
420
421test_expect_success 'exercise glob patterns with prefixes' '
422 git tag testtag-2 &&
423 test_when_finished "git tag -d testtag-2" &&
424 git for-each-ref --format="%(refname)" \
425 refs/tags/testtag "refs/tags/testtag-*" >actual &&
426 test_cmp expected actual
c899a57c
LH
427'
428
c9ecf4f1
JS
429cat >expected <<\EOF
430'refs/heads/master'
8cae19d9 431'refs/remotes/origin/master'
c9ecf4f1
JS
432'refs/tags/testtag'
433EOF
434
435test_expect_success 'Quoting style: shell' '
436 git for-each-ref --shell --format="%(refname)" >actual &&
3af82863 437 test_cmp expected actual
c9ecf4f1
JS
438'
439
440test_expect_success 'Quoting style: perl' '
441 git for-each-ref --perl --format="%(refname)" >actual &&
3af82863 442 test_cmp expected actual
c9ecf4f1
JS
443'
444
445test_expect_success 'Quoting style: python' '
446 git for-each-ref --python --format="%(refname)" >actual &&
3af82863 447 test_cmp expected actual
c9ecf4f1
JS
448'
449
450cat >expected <<\EOF
451"refs/heads/master"
8cae19d9 452"refs/remotes/origin/master"
c9ecf4f1
JS
453"refs/tags/testtag"
454EOF
455
456test_expect_success 'Quoting style: tcl' '
457 git for-each-ref --tcl --format="%(refname)" >actual &&
3af82863 458 test_cmp expected actual
c9ecf4f1
JS
459'
460
461for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
462 test_expect_success "more than one quoting style: $i" "
2708ef4a
SG
463 test_must_fail git for-each-ref $i 2>err &&
464 grep '^error: more than one quoting style' err
c9ecf4f1
JS
465 "
466done
c899a57c 467
b28061ce
RR
468test_expect_success 'setup for upstream:track[short]' '
469 test_commit two
470'
471
1d094db9
JK
472test_atom head upstream:track '[ahead 1]'
473test_atom head upstream:trackshort '>'
7743fcca
KN
474test_atom head upstream:track,nobracket 'ahead 1'
475test_atom head upstream:nobracket,track 'ahead 1'
c646d093
DR
476
477test_expect_success 'setup for push:track[short]' '
478 test_commit third &&
479 git update-ref refs/remotes/myfork/master master &&
480 git reset master~1
481'
482
483test_atom head push:track '[behind 1]'
484test_atom head push:trackshort '<'
b28061ce
RR
485
486test_expect_success 'Check that :track[short] cannot be used with other atoms' '
487 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
488 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
489'
490
b6160d95
RKC
491test_expect_success 'Check that :track[short] works when upstream is invalid' '
492 cat >expected <<-\EOF &&
ffd921d3 493 [gone]
b6160d95
RKC
494
495 EOF
496 test_when_finished "git config branch.master.merge refs/heads/master" &&
497 git config branch.master.merge refs/heads/does-not-exist &&
498 git for-each-ref \
499 --format="%(upstream:track)$LF%(upstream:trackshort)" \
500 refs/heads >actual &&
501 test_cmp expected actual
29bc8850
JK
502'
503
7d66f21a
BW
504test_expect_success 'Check for invalid refname format' '
505 test_must_fail git for-each-ref --format="%(refname:INVALID)"
506'
507
11b087ad
JK
508test_expect_success 'set up color tests' '
509 cat >expected.color <<-EOF &&
510 $(git rev-parse --short refs/heads/master) <GREEN>master<RESET>
c646d093 511 $(git rev-parse --short refs/remotes/myfork/master) <GREEN>myfork/master<RESET>
11b087ad
JK
512 $(git rev-parse --short refs/remotes/origin/master) <GREEN>origin/master<RESET>
513 $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
c646d093 514 $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
11b087ad
JK
515 $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
516 EOF
517 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
518 color_format="%(objectname:short) %(color:green)%(refname:short)"
519'
fddb74c9 520
11b087ad 521test_expect_success TTY '%(color) shows color with a tty' '
e433749d 522 test_terminal git for-each-ref --format="$color_format" >actual.raw &&
097b681b 523 test_decode_color <actual.raw >actual &&
11b087ad
JK
524 test_cmp expected.color actual
525'
526
527test_expect_success '%(color) does not show color without tty' '
528 TERM=vt100 git for-each-ref --format="$color_format" >actual &&
529 test_cmp expected.bare actual
530'
531
0c88bf50
JK
532test_expect_success '--color can override tty check' '
533 git for-each-ref --color --format="$color_format" >actual.raw &&
11b087ad
JK
534 test_decode_color <actual.raw >actual &&
535 test_cmp expected.color actual
fddb74c9
RR
536'
537
b521fd12
JK
538test_expect_success 'color.ui=always does not override tty check' '
539 git -c color.ui=always for-each-ref --format="$color_format" >actual &&
540 test_cmp expected.bare actual
541'
542
7d66f21a
BW
543cat >expected <<\EOF
544heads/master
2bb98169 545tags/master
7d66f21a
BW
546EOF
547
2bb98169
BW
548test_expect_success 'Check ambiguous head and tag refs (strict)' '
549 git config --bool core.warnambiguousrefs true &&
7d66f21a
BW
550 git checkout -b newtag &&
551 echo "Using $datestamp" > one &&
552 git add one &&
553 git commit -m "Branch" &&
554 setdate_and_increment &&
555 git tag -m "Tagging at $datestamp" master &&
556 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
557 test_cmp expected actual
558'
559
2bb98169
BW
560cat >expected <<\EOF
561heads/master
562master
563EOF
564
565test_expect_success 'Check ambiguous head and tag refs (loose)' '
566 git config --bool core.warnambiguousrefs false &&
567 git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
568 test_cmp expected actual
569'
570
7d66f21a
BW
571cat >expected <<\EOF
572heads/ambiguous
573ambiguous
574EOF
575
2bb98169 576test_expect_success 'Check ambiguous head and tag refs II (loose)' '
7d66f21a
BW
577 git checkout master &&
578 git tag ambiguous testtag^0 &&
579 git branch ambiguous testtag^0 &&
580 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
581 test_cmp expected actual
582'
583
8b3f33ef
MP
584test_expect_success 'create tag without tagger' '
585 git tag -a -m "Broken tag" taggerless &&
586 git tag -f taggerless $(git cat-file tag taggerless |
587 sed -e "/^tagger /d" |
588 git hash-object --stdin -w -t tag)
589'
590
591test_atom refs/tags/taggerless type 'commit'
592test_atom refs/tags/taggerless tag 'taggerless'
593test_atom refs/tags/taggerless tagger ''
594test_atom refs/tags/taggerless taggername ''
595test_atom refs/tags/taggerless taggeremail ''
b82445dc
HV
596test_atom refs/tags/taggerless taggeremail:trim ''
597test_atom refs/tags/taggerless taggeremail:localpart ''
8b3f33ef
MP
598test_atom refs/tags/taggerless taggerdate ''
599test_atom refs/tags/taggerless committer ''
600test_atom refs/tags/taggerless committername ''
601test_atom refs/tags/taggerless committeremail ''
b82445dc
HV
602test_atom refs/tags/taggerless committeremail:trim ''
603test_atom refs/tags/taggerless committeremail:localpart ''
8b3f33ef
MP
604test_atom refs/tags/taggerless committerdate ''
605test_atom refs/tags/taggerless subject 'Broken tag'
606
e276c26b
JH
607test_expect_success 'an unusual tag with an incomplete line' '
608
609 git tag -m "bogo" bogo &&
610 bogo=$(git cat-file tag bogo) &&
611 bogo=$(printf "%s" "$bogo" | git mktag) &&
612 git tag -f bogo "$bogo" &&
613 git for-each-ref --format "%(body)" refs/tags/bogo
614
615'
616
7140c22c
JK
617test_expect_success 'create tag with subject and body content' '
618 cat >>msg <<-\EOF &&
619 the subject line
620
621 first body line
622 second body line
623 EOF
624 git tag -F msg subject-body
625'
626test_atom refs/tags/subject-body subject 'the subject line'
905f0a4e 627test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
7140c22c
JK
628test_atom refs/tags/subject-body body 'first body line
629second body line
630'
631test_atom refs/tags/subject-body contents 'the subject line
632
633first body line
634second body line
635'
636
7f6e275b
JK
637test_expect_success 'create tag with multiline subject' '
638 cat >msg <<-\EOF &&
639 first subject line
640 second subject line
641
642 first body line
643 second body line
644 EOF
645 git tag -F msg multiline
646'
647test_atom refs/tags/multiline subject 'first subject line second subject line'
905f0a4e 648test_atom refs/tags/multiline subject:sanitize 'first-subject-line-second-subject-line'
e2b23972 649test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
7f6e275b
JK
650test_atom refs/tags/multiline body 'first body line
651second body line
652'
e2b23972
MG
653test_atom refs/tags/multiline contents:body 'first body line
654second body line
655'
656test_atom refs/tags/multiline contents:signature ''
7f6e275b
JK
657test_atom refs/tags/multiline contents 'first subject line
658second subject line
659
660first body line
661second body line
662'
663
e2b23972
MG
664test_expect_success GPG 'create signed tags' '
665 git tag -s -m "" signed-empty &&
666 git tag -s -m "subject line" signed-short &&
667 cat >msg <<-\EOF &&
668 subject line
669
670 body contents
671 EOF
672 git tag -s -F msg signed-long
673'
674
675sig='-----BEGIN PGP SIGNATURE-----
676-----END PGP SIGNATURE-----
677'
678
679PREREQ=GPG
680test_atom refs/tags/signed-empty subject ''
905f0a4e 681test_atom refs/tags/signed-empty subject:sanitize ''
e2b23972
MG
682test_atom refs/tags/signed-empty contents:subject ''
683test_atom refs/tags/signed-empty body "$sig"
684test_atom refs/tags/signed-empty contents:body ''
685test_atom refs/tags/signed-empty contents:signature "$sig"
686test_atom refs/tags/signed-empty contents "$sig"
687
688test_atom refs/tags/signed-short subject 'subject line'
905f0a4e 689test_atom refs/tags/signed-short subject:sanitize 'subject-line'
e2b23972
MG
690test_atom refs/tags/signed-short contents:subject 'subject line'
691test_atom refs/tags/signed-short body "$sig"
692test_atom refs/tags/signed-short contents:body ''
693test_atom refs/tags/signed-short contents:signature "$sig"
694test_atom refs/tags/signed-short contents "subject line
695$sig"
696
697test_atom refs/tags/signed-long subject 'subject line'
905f0a4e 698test_atom refs/tags/signed-long subject:sanitize 'subject-line'
e2b23972
MG
699test_atom refs/tags/signed-long contents:subject 'subject line'
700test_atom refs/tags/signed-long body "body contents
701$sig"
702test_atom refs/tags/signed-long contents:body 'body contents
703'
704test_atom refs/tags/signed-long contents:signature "$sig"
705test_atom refs/tags/signed-long contents "subject line
706
707body contents
708$sig"
709
6e2ef8eb
CC
710test_expect_success 'set up refs pointing to tree and blob' '
711 git update-ref refs/mytrees/first refs/heads/master^{tree} &&
712 git update-ref refs/myblobs/first refs/heads/master:one
713'
714
715test_atom refs/mytrees/first subject ""
716test_atom refs/mytrees/first contents:subject ""
717test_atom refs/mytrees/first body ""
718test_atom refs/mytrees/first contents:body ""
719test_atom refs/mytrees/first contents:signature ""
720test_atom refs/mytrees/first contents ""
721
722test_atom refs/myblobs/first subject ""
723test_atom refs/myblobs/first contents:subject ""
724test_atom refs/myblobs/first body ""
725test_atom refs/myblobs/first contents:body ""
726test_atom refs/myblobs/first contents:signature ""
727test_atom refs/myblobs/first contents ""
728
7c5045fc
JK
729test_expect_success 'set up multiple-sort tags' '
730 for when in 100000 200000
731 do
732 for email in user1 user2
733 do
734 for ref in ref1 ref2
735 do
736 GIT_COMMITTER_DATE="@$when +0000" \
737 GIT_COMMITTER_EMAIL="$email@example.com" \
738 git tag -m "tag $ref-$when-$email" \
739 multi-$ref-$when-$email || return 1
740 done
741 done
742 done
743'
912072d5 744
3b51222c 745test_expect_success 'Verify sort with multiple keys' '
7c5045fc
JK
746 cat >expected <<-\EOF &&
747 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
748 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
749 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
750 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
751 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
752 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
753 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
754 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
755 EOF
756 git for-each-ref \
757 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
758 --sort=-refname \
759 --sort=taggeremail \
760 --sort=taggerdate \
761 "refs/tags/multi-*" >actual &&
912072d5
KK
762 test_cmp expected actual
763'
84679d47 764
7c5045fc
JK
765test_expect_success 'equivalent sorts fall back on refname' '
766 cat >expected <<-\EOF &&
767 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
768 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
769 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
770 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
771 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
772 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
773 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
774 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
775 EOF
776 git for-each-ref \
777 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
778 --sort=taggerdate \
779 "refs/tags/multi-*" >actual &&
780 test_cmp expected actual
781'
01f95825 782
84679d47
JH
783test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
784 test_when_finished "git checkout master" &&
785 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
786 sed -e "s/^\* / /" actual >expect &&
f0252ca2 787 git checkout --orphan orphaned-branch &&
84679d47
JH
788 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
789 test_cmp expect actual
790'
791
b1d31c89
JK
792cat >trailers <<EOF
793Reviewed-by: A U Thor <author@example.com>
794Signed-off-by: A U Thor <author@example.com>
624b44d3
TB
795[ v2 updated patch description ]
796Acked-by: A U Thor
797 <author@example.com>
b1d31c89
JK
798EOF
799
67a20a00
TB
800unfold () {
801 perl -0pe 's/\n\s+/ /g'
802}
624b44d3
TB
803
804test_expect_success 'set up trailers for next test' '
b1d31c89
JK
805 echo "Some contents" > two &&
806 git add two &&
624b44d3 807 git commit -F - <<-EOF
b1d31c89
JK
808 trailers: this commit message has trailers
809
810 Some message contents
811
812 $(cat trailers)
813 EOF
624b44d3
TB
814'
815
67a20a00 816test_expect_success '%(trailers:unfold) unfolds trailers' '
67a20a00
TB
817 {
818 unfold <trailers
819 echo
820 } >expect &&
a8e0f50e
HV
821 git for-each-ref --format="%(trailers:unfold)" refs/heads/master >actual &&
822 test_cmp expect actual &&
823 git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/master >actual &&
67a20a00
TB
824 test_cmp expect actual
825'
826
827test_expect_success '%(trailers:only) shows only "key: value" trailers' '
67a20a00
TB
828 {
829 grep -v patch.description <trailers &&
830 echo
831 } >expect &&
a8e0f50e
HV
832 git for-each-ref --format="%(trailers:only)" refs/heads/master >actual &&
833 test_cmp expect actual &&
834 git for-each-ref --format="%(contents:trailers:only)" refs/heads/master >actual &&
67a20a00
TB
835 test_cmp expect actual
836'
837
838test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
67a20a00
TB
839 {
840 grep -v patch.description <trailers | unfold &&
841 echo
842 } >expect &&
a8e0f50e
HV
843 git for-each-ref --format="%(trailers:only,unfold)" refs/heads/master >actual &&
844 test_cmp expect actual &&
845 git for-each-ref --format="%(trailers:unfold,only)" refs/heads/master >actual &&
846 test_cmp actual actual &&
7a5edbdb 847 git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/master >actual &&
a8e0f50e
HV
848 test_cmp expect actual &&
849 git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/master >actual &&
850 test_cmp actual actual
7a5edbdb
TB
851'
852
67a20a00
TB
853test_expect_success '%(trailers) rejects unknown trailers arguments' '
854 # error message cannot be checked under i18n
855 cat >expect <<-EOF &&
856 fatal: unknown %(trailers) argument: unsupported
857 EOF
858 test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual &&
a8e0f50e 859 test_i18ncmp expect actual &&
7a5edbdb 860 test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual &&
67a20a00
TB
861 test_i18ncmp expect actual
862'
863
2c22e102 864test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
7a5edbdb 865 cat >expect <<-EOF &&
2c22e102 866 fatal: unrecognized %(contents) argument: trailersonly
7a5edbdb 867 EOF
2c22e102 868 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
7a5edbdb
TB
869 test_i18ncmp expect actual
870'
871
624b44d3 872test_expect_success 'basic atom: head contents:trailers' '
b1d31c89
JK
873 git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
874 sanitize_pgp <actual >actual.clean &&
875 # git for-each-ref ends with a blank line
876 cat >expect <<-EOF &&
877 $(cat trailers)
878
879 EOF
880 test_cmp expect actual.clean
881'
882
e5fba5d5
JK
883test_expect_success 'trailer parsing not fooled by --- line' '
884 git commit --allow-empty -F - <<-\EOF &&
885 this is the subject
886
887 This is the body. The message has a "---" line which would confuse a
888 message+patch parser. But here we know we have only a commit message,
889 so we get it right.
890
891 trailer: wrong
892 ---
893 This is more body.
894
895 trailer: right
896 EOF
897
898 {
899 echo "trailer: right" &&
900 echo
901 } >expect &&
902 git for-each-ref --format="%(trailers)" refs/heads/master >actual &&
903 test_cmp expect actual
904'
905
01f95825
KN
906test_expect_success 'Add symbolic ref for the following tests' '
907 git symbolic-ref refs/heads/sym refs/heads/master
908'
909
910cat >expected <<EOF
911refs/heads/master
912EOF
913
914test_expect_success 'Verify usage of %(symref) atom' '
915 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
916 test_cmp expected actual
917'
918
919cat >expected <<EOF
920heads/master
921EOF
922
923test_expect_success 'Verify usage of %(symref:short) atom' '
924 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
925 test_cmp expected actual
926'
927
a7984101
KN
928cat >expected <<EOF
929master
1a0ca5e3 930heads/master
a7984101
KN
931EOF
932
17938f17
KN
933test_expect_success 'Verify usage of %(symref:lstrip) atom' '
934 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
1a0ca5e3 935 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
44a6b6ce
JH
936 test_cmp expected actual &&
937
938 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
939 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
a7984101
KN
940 test_cmp expected actual
941'
942
1a34728e
KN
943cat >expected <<EOF
944refs
945refs/heads
946EOF
947
948test_expect_success 'Verify usage of %(symref:rstrip) atom' '
949 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
950 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
951 test_cmp expected actual
952'
953
1b586867
JS
954test_expect_success ':remotename and :remoteref' '
955 git init remote-tests &&
956 (
957 cd remote-tests &&
958 test_commit initial &&
959 git remote add from fifth.coffee:blub &&
960 git config branch.master.remote from &&
961 git config branch.master.merge refs/heads/stable &&
962 git remote add to southridge.audio:repo &&
963 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
964 git config branch.master.pushRemote to &&
965 for pair in "%(upstream)=refs/remotes/from/stable" \
966 "%(upstream:remotename)=from" \
967 "%(upstream:remoteref)=refs/heads/stable" \
968 "%(push)=refs/remotes/to/pushed/master" \
969 "%(push:remotename)=to" \
970 "%(push:remoteref)=refs/heads/pushed/master"
971 do
972 echo "${pair#*=}" >expect &&
973 git for-each-ref --format="${pair%=*}" \
974 refs/heads/master >actual &&
975 test_cmp expect actual
976 done &&
977 git branch push-simple &&
978 git config branch.push-simple.pushRemote from &&
979 actual="$(git for-each-ref \
980 --format="%(push:remotename),%(push:remoteref)" \
981 refs/heads/push-simple)" &&
982 test from, = "$actual"
983 )
984'
985
e674eb25 986test_expect_success 'for-each-ref --ignore-case ignores case' '
ee0f3e22 987 git for-each-ref --format="%(refname)" refs/heads/MASTER >actual &&
d3c6751b 988 test_must_be_empty actual &&
ee0f3e22
JK
989
990 echo refs/heads/master >expect &&
991 git for-each-ref --format="%(refname)" --ignore-case \
992 refs/heads/MASTER >actual &&
993 test_cmp expect actual
994'
995
76f9e569
JK
996test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
997 # name refs numerically to avoid case-insensitive filesystem conflicts
998 nr=0 &&
999 for email in a A b B
1000 do
1001 for subject in a A b B
1002 do
1003 GIT_COMMITTER_EMAIL="$email@example.com" \
1004 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1005 nr=$((nr+1))||
1006 return 1
1007 done
1008 done &&
1009 git for-each-ref --ignore-case \
1010 --format="%(taggeremail) %(subject) %(refname)" \
1011 --sort=refname \
1012 --sort=subject \
1013 --sort=taggeremail \
1014 refs/tags/icase-* >actual &&
1015 cat >expect <<-\EOF &&
1016 <a@example.com> tag a refs/tags/icase-00
1017 <a@example.com> tag A refs/tags/icase-01
1018 <A@example.com> tag a refs/tags/icase-04
1019 <A@example.com> tag A refs/tags/icase-05
1020 <a@example.com> tag b refs/tags/icase-02
1021 <a@example.com> tag B refs/tags/icase-03
1022 <A@example.com> tag b refs/tags/icase-06
1023 <A@example.com> tag B refs/tags/icase-07
1024 <b@example.com> tag a refs/tags/icase-08
1025 <b@example.com> tag A refs/tags/icase-09
1026 <B@example.com> tag a refs/tags/icase-12
1027 <B@example.com> tag A refs/tags/icase-13
1028 <b@example.com> tag b refs/tags/icase-10
1029 <b@example.com> tag B refs/tags/icase-11
1030 <B@example.com> tag b refs/tags/icase-14
1031 <B@example.com> tag B refs/tags/icase-15
1032 EOF
1033 test_cmp expect actual
1034'
1035
96b2d4fa 1036test_done