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