]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6300-for-each-ref.sh
Merge branch 'jk/ci-retire-allow-ref'
[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
26c9c03f 9GNUPGHOME_NOT_USED=$GNUPGHOME
e2b23972 10. "$TEST_DIRECTORY"/lib-gpg.sh
11b087ad 11. "$TEST_DIRECTORY"/lib-terminal.sh
96b2d4fa 12
db7bae25
JK
13# Mon Jul 3 23:18:43 2006 +0000
14datestamp=1151968723
96b2d4fa
AP
15setdate_and_increment () {
16 GIT_COMMITTER_DATE="$datestamp +0200"
17 datestamp=$(expr "$datestamp" + 1)
18 GIT_AUTHOR_DATE="$datestamp +0200"
19 datestamp=$(expr "$datestamp" + 1)
20 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
21}
22
bc147968 23test_expect_success setup '
1f5f8f3e 24 test_oid_cache <<-EOF &&
25 disklen sha1:138
26 disklen sha256:154
27 EOF
96b2d4fa
AP
28 setdate_and_increment &&
29 echo "Using $datestamp" > one &&
30 git add one &&
31 git commit -m "Initial" &&
8f19c9fd 32 git branch -M main &&
96b2d4fa 33 setdate_and_increment &&
bc147968 34 git tag -a -m "Tagging at $datestamp" testtag &&
8f19c9fd 35 git update-ref refs/remotes/origin/main main &&
8cae19d9 36 git remote add origin nowhere &&
8f19c9fd
JS
37 git config branch.main.remote origin &&
38 git config branch.main.merge refs/heads/main &&
29bc8850
JK
39 git remote add myfork elsewhere &&
40 git config remote.pushdefault myfork &&
41 git config push.default current
8cae19d9
JK
42'
43
d2bf48d2
JK
44test_atom() {
45 case "$1" in
8f19c9fd 46 head) ref=refs/heads/main ;;
d2bf48d2 47 tag) ref=refs/tags/testtag ;;
01f95825 48 sym) ref=refs/heads/sym ;;
7140c22c 49 *) ref=$1 ;;
d2bf48d2
JK
50 esac
51 printf '%s\n' "$3" >expected
e2b23972 52 test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
d2bf48d2 53 git for-each-ref --format='%($2)' $ref >actual &&
e2b23972
MG
54 sanitize_pgp <actual >actual.clean &&
55 test_cmp expected actual.clean
d2bf48d2 56 "
b6839fda
CC
57 # Automatically test "contents:size" atom after testing "contents"
58 if test "$2" = "contents"
59 then
597fa8cb
ĐTCD
60 # for commit leg, $3 is changed there
61 expect=$(printf '%s' "$3" | wc -c)
3db796c1 62 test_expect_${4:-success} $PREREQ "basic atom: $1 contents:size" '
597fa8cb
ĐTCD
63 type=$(git cat-file -t "$ref") &&
64 case $type in
65 tag)
66 # We cannot use $3 as it expects sanitize_pgp to run
15495773
ĐTCD
67 git cat-file tag $ref >out &&
68 expect=$(tail -n +6 out | wc -c) &&
69 rm -f out ;;
597fa8cb
ĐTCD
70 tree | blob)
71 expect="" ;;
72 commit)
73 : "use the calculated expect" ;;
74 *)
75 BUG "unknown object type" ;;
76 esac &&
77 # Leave $expect unquoted to lose possible leading whitespaces
78 echo $expect >expected &&
b6839fda 79 git for-each-ref --format="%(contents:size)" "$ref" >actual &&
3db796c1 80 test_cmp expected actual
b6839fda
CC
81 '
82 fi
d2bf48d2
JK
83}
84
1f5f8f3e 85hexlen=$(test_oid hexsz)
86disklen=$(test_oid disklen)
87
8f19c9fd
JS
88test_atom head refname refs/heads/main
89test_atom head refname: refs/heads/main
90test_atom head refname:short main
91test_atom head refname:lstrip=1 heads/main
92test_atom head refname:lstrip=2 main
93test_atom head refname:lstrip=-1 main
94test_atom head refname:lstrip=-2 heads/main
1a34728e
KN
95test_atom head refname:rstrip=1 refs/heads
96test_atom head refname:rstrip=2 refs
97test_atom head refname:rstrip=-1 refs
98test_atom head refname:rstrip=-2 refs/heads
8f19c9fd
JS
99test_atom head refname:strip=1 heads/main
100test_atom head refname:strip=2 main
101test_atom head refname:strip=-1 main
102test_atom head refname:strip=-2 heads/main
103test_atom head upstream refs/remotes/origin/main
104test_atom head upstream:short origin/main
105test_atom head upstream:lstrip=2 origin/main
106test_atom head upstream:lstrip=-2 origin/main
1a34728e
KN
107test_atom head upstream:rstrip=2 refs/remotes
108test_atom head upstream:rstrip=-2 refs/remotes
8f19c9fd
JS
109test_atom head upstream:strip=2 origin/main
110test_atom head upstream:strip=-2 origin/main
111test_atom head push refs/remotes/myfork/main
112test_atom head push:short myfork/main
113test_atom head push:lstrip=1 remotes/myfork/main
114test_atom head push:lstrip=-1 main
1a34728e
KN
115test_atom head push:rstrip=1 refs/remotes/myfork
116test_atom head push:rstrip=-1 refs
8f19c9fd
JS
117test_atom head push:strip=1 remotes/myfork/main
118test_atom head push:strip=-1 main
d2bf48d2 119test_atom head objecttype commit
1f5f8f3e 120test_atom head objectsize $((131 + hexlen))
121test_atom head objectsize:disk $disklen
122test_atom head deltabase $ZERO_OID
8f19c9fd
JS
123test_atom head objectname $(git rev-parse refs/heads/main)
124test_atom head objectname:short $(git rev-parse --short refs/heads/main)
125test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
126test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
127test_atom head tree $(git rev-parse refs/heads/main^{tree})
128test_atom head tree:short $(git rev-parse --short refs/heads/main^{tree})
129test_atom head tree:short=1 $(git rev-parse --short=1 refs/heads/main^{tree})
130test_atom head tree:short=10 $(git rev-parse --short=10 refs/heads/main^{tree})
d2bf48d2 131test_atom head parent ''
26bc0aaf
HV
132test_atom head parent:short ''
133test_atom head parent:short=1 ''
134test_atom head parent:short=10 ''
d2bf48d2
JK
135test_atom head numparent 0
136test_atom head object ''
137test_atom head type ''
bd0708c7
ZH
138test_atom head raw "$(git cat-file commit refs/heads/main)
139"
b74cf648
JK
140test_atom head '*objectname' ''
141test_atom head '*objecttype' ''
db7bae25 142test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
d2bf48d2
JK
143test_atom head authorname 'A U Thor'
144test_atom head authoremail '<author@example.com>'
b82445dc
HV
145test_atom head authoremail:trim 'author@example.com'
146test_atom head authoremail:localpart 'author'
db7bae25
JK
147test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
148test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
d2bf48d2
JK
149test_atom head committername 'C O Mitter'
150test_atom head committeremail '<committer@example.com>'
b82445dc
HV
151test_atom head committeremail:trim 'committer@example.com'
152test_atom head committeremail:localpart 'committer'
db7bae25 153test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
d2bf48d2
JK
154test_atom head tag ''
155test_atom head tagger ''
156test_atom head taggername ''
157test_atom head taggeremail ''
b82445dc
HV
158test_atom head taggeremail:trim ''
159test_atom head taggeremail:localpart ''
d2bf48d2 160test_atom head taggerdate ''
db7bae25
JK
161test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
162test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
d2bf48d2 163test_atom head subject 'Initial'
905f0a4e 164test_atom head subject:sanitize 'Initial'
e2b23972 165test_atom head contents:subject 'Initial'
d2bf48d2 166test_atom head body ''
e2b23972
MG
167test_atom head contents:body ''
168test_atom head contents:signature ''
d2bf48d2
JK
169test_atom head contents 'Initial
170'
7a48b832 171test_atom head HEAD '*'
d2bf48d2
JK
172
173test_atom tag refname refs/tags/testtag
1d094db9 174test_atom tag refname:short testtag
8cae19d9 175test_atom tag upstream ''
29bc8850 176test_atom tag push ''
d2bf48d2 177test_atom tag objecttype tag
1f5f8f3e 178test_atom tag objectsize $((114 + hexlen))
179test_atom tag objectsize:disk $disklen
180test_atom tag '*objectsize:disk' $disklen
181test_atom tag deltabase $ZERO_OID
182test_atom tag '*deltabase' $ZERO_OID
189a5467 183test_atom tag objectname $(git rev-parse refs/tags/testtag)
1d094db9 184test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
8f19c9fd
JS
185test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
186test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
d2bf48d2 187test_atom tag tree ''
837adb10
HV
188test_atom tag tree:short ''
189test_atom tag tree:short=1 ''
190test_atom tag tree:short=10 ''
d2bf48d2 191test_atom tag parent ''
26bc0aaf
HV
192test_atom tag parent:short ''
193test_atom tag parent:short=1 ''
194test_atom tag parent:short=10 ''
d2bf48d2 195test_atom tag numparent ''
189a5467 196test_atom tag object $(git rev-parse refs/tags/testtag^0)
d2bf48d2 197test_atom tag type 'commit'
1f5f8f3e 198test_atom tag '*objectname' $(git rev-parse refs/tags/testtag^{})
b74cf648 199test_atom tag '*objecttype' 'commit'
d2bf48d2
JK
200test_atom tag author ''
201test_atom tag authorname ''
202test_atom tag authoremail ''
b82445dc
HV
203test_atom tag authoremail:trim ''
204test_atom tag authoremail:localpart ''
d2bf48d2
JK
205test_atom tag authordate ''
206test_atom tag committer ''
207test_atom tag committername ''
208test_atom tag committeremail ''
b82445dc
HV
209test_atom tag committeremail:trim ''
210test_atom tag committeremail:localpart ''
d2bf48d2
JK
211test_atom tag committerdate ''
212test_atom tag tag 'testtag'
db7bae25 213test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
d2bf48d2
JK
214test_atom tag taggername 'C O Mitter'
215test_atom tag taggeremail '<committer@example.com>'
b82445dc
HV
216test_atom tag taggeremail:trim 'committer@example.com'
217test_atom tag taggeremail:localpart 'committer'
db7bae25
JK
218test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
219test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
220test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
221test_atom tag subject 'Tagging at 1151968727'
905f0a4e 222test_atom tag subject:sanitize 'Tagging-at-1151968727'
db7bae25 223test_atom tag contents:subject 'Tagging at 1151968727'
d2bf48d2 224test_atom tag body ''
e2b23972
MG
225test_atom tag contents:body ''
226test_atom tag contents:signature ''
db7bae25 227test_atom tag contents 'Tagging at 1151968727
96b2d4fa 228'
7a48b832 229test_atom tag HEAD ' '
96b2d4fa 230
bd0708c7
ZH
231test_expect_success 'basic atom: refs/tags/testtag *raw' '
232 git cat-file commit refs/tags/testtag^{} >expected &&
233 git for-each-ref --format="%(*raw)" refs/tags/testtag >actual &&
234 sanitize_pgp <expected >expected.clean &&
235 echo >>expected.clean &&
236 sanitize_pgp <actual >actual.clean &&
237 test_cmp expected.clean actual.clean
238'
239
41ac414e 240test_expect_success 'Check invalid atoms names are errors' '
3604e7c5 241 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
96b2d4fa
AP
242'
243
244test_expect_success 'Check format specifiers are ignored in naming date atoms' '
3604e7c5
NS
245 git for-each-ref --format="%(authordate)" refs/heads &&
246 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
247 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
248 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
96b2d4fa
AP
249'
250
251test_expect_success 'Check valid format specifiers for date fields' '
3604e7c5
NS
252 git for-each-ref --format="%(authordate:default)" refs/heads &&
253 git for-each-ref --format="%(authordate:relative)" refs/heads &&
254 git for-each-ref --format="%(authordate:short)" refs/heads &&
255 git for-each-ref --format="%(authordate:local)" refs/heads &&
256 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
257 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
96b2d4fa
AP
258'
259
41ac414e 260test_expect_success 'Check invalid format specifiers are errors' '
3604e7c5 261 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
96b2d4fa
AP
262'
263
42d0eb05
KN
264test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
265 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
266 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
267 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
268'
269
f95cecf4
JK
270test_date () {
271 f=$1 &&
272 committer_date=$2 &&
273 author_date=$3 &&
274 tagger_date=$4 &&
275 cat >expected <<-EOF &&
8f19c9fd 276 'refs/heads/main' '$committer_date' '$author_date'
f95cecf4
JK
277 'refs/tags/testtag' '$tagger_date'
278 EOF
279 (
280 git for-each-ref --shell \
281 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
282 refs/heads &&
283 git for-each-ref --shell \
284 --format="%(refname) %(taggerdate${f:+:$f})" \
285 refs/tags
286 ) >actual &&
287 test_cmp expected actual
288}
96b2d4fa
AP
289
290test_expect_success 'Check unformatted date fields output' '
f95cecf4 291 test_date "" \
db7bae25
JK
292 "Tue Jul 4 01:18:43 2006 +0200" \
293 "Tue Jul 4 01:18:44 2006 +0200" \
294 "Tue Jul 4 01:18:45 2006 +0200"
96b2d4fa
AP
295'
296
297test_expect_success 'Check format "default" formatted date fields output' '
f95cecf4 298 test_date default \
db7bae25
JK
299 "Tue Jul 4 01:18:43 2006 +0200" \
300 "Tue Jul 4 01:18:44 2006 +0200" \
301 "Tue Jul 4 01:18:45 2006 +0200"
96b2d4fa
AP
302'
303
99264e93
JK
304test_expect_success 'Check format "default-local" date fields output' '
305 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
306'
307
308# Don't know how to do relative check because I can't know when this script
309# is going to be run and can't fake the current time to git, and hence can't
310# provide expected output. Instead, I'll just make sure that "relative"
311# doesn't exit in error
96b2d4fa
AP
312test_expect_success 'Check format "relative" date fields output' '
313 f=relative &&
314 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
315 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
316'
317
99264e93
JK
318# We just check that this is the same as "relative" for now.
319test_expect_success 'Check format "relative-local" date fields output' '
320 test_date relative-local \
321 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
322 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
323 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
324'
96b2d4fa
AP
325
326test_expect_success 'Check format "short" date fields output' '
db7bae25 327 test_date short 2006-07-04 2006-07-04 2006-07-04
96b2d4fa
AP
328'
329
99264e93
JK
330test_expect_success 'Check format "short-local" date fields output' '
331 test_date short-local 2006-07-03 2006-07-03 2006-07-03
332'
96b2d4fa
AP
333
334test_expect_success 'Check format "local" date fields output' '
f95cecf4 335 test_date local \
db7bae25
JK
336 "Mon Jul 3 23:18:43 2006" \
337 "Mon Jul 3 23:18:44 2006" \
338 "Mon Jul 3 23:18:45 2006"
96b2d4fa
AP
339'
340
96b2d4fa 341test_expect_success 'Check format "iso8601" date fields output' '
f95cecf4 342 test_date iso8601 \
db7bae25
JK
343 "2006-07-04 01:18:43 +0200" \
344 "2006-07-04 01:18:44 +0200" \
345 "2006-07-04 01:18:45 +0200"
96b2d4fa
AP
346'
347
99264e93
JK
348test_expect_success 'Check format "iso8601-local" date fields output' '
349 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"
350'
96b2d4fa
AP
351
352test_expect_success 'Check format "rfc2822" date fields output' '
f95cecf4 353 test_date rfc2822 \
db7bae25
JK
354 "Tue, 4 Jul 2006 01:18:43 +0200" \
355 "Tue, 4 Jul 2006 01:18:44 +0200" \
356 "Tue, 4 Jul 2006 01:18:45 +0200"
96b2d4fa
AP
357'
358
99264e93
JK
359test_expect_success 'Check format "rfc2822-local" date fields output' '
360 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"
361'
362
f3c1ba50 363test_expect_success 'Check format "raw" date fields output' '
db7bae25 364 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
f3c1ba50
JK
365'
366
99264e93
JK
367test_expect_success 'Check format "raw-local" date fields output' '
368 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
96b2d4fa
AP
369'
370
aa1462cc 371test_expect_success 'Check format of strftime date fields' '
db7bae25 372 echo "my date is 2006-07-04" >expected &&
aa1462cc
JK
373 git for-each-ref \
374 --format="%(authordate:format:my date is %Y-%m-%d)" \
375 refs/heads >actual &&
376 test_cmp expected actual
377'
378
99264e93
JK
379test_expect_success 'Check format of strftime-local date fields' '
380 echo "my date is 2006-07-03" >expected &&
381 git for-each-ref \
382 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
383 refs/heads >actual &&
384 test_cmp expected actual
385'
386
e4f031e3
JK
387test_expect_success 'exercise strftime with odd fields' '
388 echo >expected &&
389 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
390 test_cmp expected actual &&
8125a58b 391 long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
e4f031e3
JK
392 echo $long >expected &&
393 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
394 test_cmp expected actual
395'
396
c899a57c 397cat >expected <<\EOF
8f19c9fd
JS
398refs/heads/main
399refs/remotes/origin/main
c899a57c
LH
400refs/tags/testtag
401EOF
402
403test_expect_success 'Verify ascending sort' '
3604e7c5 404 git for-each-ref --format="%(refname)" --sort=refname >actual &&
3af82863 405 test_cmp expected actual
c899a57c
LH
406'
407
408
409cat >expected <<\EOF
410refs/tags/testtag
8f19c9fd
JS
411refs/remotes/origin/main
412refs/heads/main
c899a57c
LH
413EOF
414
415test_expect_success 'Verify descending sort' '
3604e7c5 416 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
3af82863 417 test_cmp expected actual
b31e2680
TB
418'
419
98e7ab6d
JH
420test_expect_success 'Give help even with invalid sort atoms' '
421 test_expect_code 129 git for-each-ref --sort=bogus -h >actual 2>&1 &&
422 grep "^usage: git for-each-ref" actual
423'
424
b31e2680
TB
425cat >expected <<\EOF
426refs/tags/testtag
427refs/tags/testtag-2
428EOF
429
430test_expect_success 'exercise patterns with prefixes' '
431 git tag testtag-2 &&
432 test_when_finished "git tag -d testtag-2" &&
433 git for-each-ref --format="%(refname)" \
434 refs/tags/testtag refs/tags/testtag-2 >actual &&
435 test_cmp expected actual
436'
437
438cat >expected <<\EOF
439refs/tags/testtag
440refs/tags/testtag-2
441EOF
442
443test_expect_success 'exercise glob patterns with prefixes' '
444 git tag testtag-2 &&
445 test_when_finished "git tag -d testtag-2" &&
446 git for-each-ref --format="%(refname)" \
447 refs/tags/testtag "refs/tags/testtag-*" >actual &&
448 test_cmp expected actual
c899a57c
LH
449'
450
8255dd8a
TB
451cat >expected <<\EOF
452refs/tags/bar
453refs/tags/baz
454refs/tags/testtag
455EOF
456
457test_expect_success 'exercise patterns with prefix exclusions' '
458 for tag in foo/one foo/two foo/three bar baz
459 do
460 git tag "$tag" || return 1
461 done &&
462 test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
463 git for-each-ref --format="%(refname)" \
464 refs/tags/ --exclude=refs/tags/foo >actual &&
465 test_cmp expected actual
466'
467
468cat >expected <<\EOF
469refs/tags/bar
470refs/tags/baz
471refs/tags/foo/one
472refs/tags/testtag
473EOF
474
475test_expect_success 'exercise patterns with pattern exclusions' '
476 for tag in foo/one foo/two foo/three bar baz
477 do
478 git tag "$tag" || return 1
479 done &&
480 test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
481 git for-each-ref --format="%(refname)" \
482 refs/tags/ --exclude="refs/tags/foo/t*" >actual &&
483 test_cmp expected actual
484'
485
c9ecf4f1 486cat >expected <<\EOF
8f19c9fd
JS
487'refs/heads/main'
488'refs/remotes/origin/main'
c9ecf4f1
JS
489'refs/tags/testtag'
490EOF
491
492test_expect_success 'Quoting style: shell' '
493 git for-each-ref --shell --format="%(refname)" >actual &&
3af82863 494 test_cmp expected actual
c9ecf4f1
JS
495'
496
497test_expect_success 'Quoting style: perl' '
498 git for-each-ref --perl --format="%(refname)" >actual &&
3af82863 499 test_cmp expected actual
c9ecf4f1
JS
500'
501
502test_expect_success 'Quoting style: python' '
503 git for-each-ref --python --format="%(refname)" >actual &&
3af82863 504 test_cmp expected actual
c9ecf4f1
JS
505'
506
507cat >expected <<\EOF
8f19c9fd
JS
508"refs/heads/main"
509"refs/remotes/origin/main"
c9ecf4f1
JS
510"refs/tags/testtag"
511EOF
512
513test_expect_success 'Quoting style: tcl' '
514 git for-each-ref --tcl --format="%(refname)" >actual &&
3af82863 515 test_cmp expected actual
c9ecf4f1
JS
516'
517
518for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
519 test_expect_success "more than one quoting style: $i" "
2708ef4a
SG
520 test_must_fail git for-each-ref $i 2>err &&
521 grep '^error: more than one quoting style' err
c9ecf4f1
JS
522 "
523done
c899a57c 524
b28061ce
RR
525test_expect_success 'setup for upstream:track[short]' '
526 test_commit two
527'
528
1d094db9
JK
529test_atom head upstream:track '[ahead 1]'
530test_atom head upstream:trackshort '>'
7743fcca
KN
531test_atom head upstream:track,nobracket 'ahead 1'
532test_atom head upstream:nobracket,track 'ahead 1'
c646d093
DR
533
534test_expect_success 'setup for push:track[short]' '
535 test_commit third &&
8f19c9fd
JS
536 git update-ref refs/remotes/myfork/main main &&
537 git reset main~1
c646d093
DR
538'
539
540test_atom head push:track '[behind 1]'
541test_atom head push:trackshort '<'
b28061ce
RR
542
543test_expect_success 'Check that :track[short] cannot be used with other atoms' '
544 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
545 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
546'
547
b6160d95
RKC
548test_expect_success 'Check that :track[short] works when upstream is invalid' '
549 cat >expected <<-\EOF &&
ffd921d3 550 [gone]
b6160d95
RKC
551
552 EOF
8f19c9fd
JS
553 test_when_finished "git config branch.main.merge refs/heads/main" &&
554 git config branch.main.merge refs/heads/does-not-exist &&
b6160d95
RKC
555 git for-each-ref \
556 --format="%(upstream:track)$LF%(upstream:trackshort)" \
557 refs/heads >actual &&
558 test_cmp expected actual
29bc8850
JK
559'
560
7d66f21a
BW
561test_expect_success 'Check for invalid refname format' '
562 test_must_fail git for-each-ref --format="%(refname:INVALID)"
563'
564
11b087ad
JK
565test_expect_success 'set up color tests' '
566 cat >expected.color <<-EOF &&
8f19c9fd
JS
567 $(git rev-parse --short refs/heads/main) <GREEN>main<RESET>
568 $(git rev-parse --short refs/remotes/myfork/main) <GREEN>myfork/main<RESET>
569 $(git rev-parse --short refs/remotes/origin/main) <GREEN>origin/main<RESET>
11b087ad 570 $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
c646d093 571 $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
11b087ad
JK
572 $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
573 EOF
574 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
575 color_format="%(objectname:short) %(color:green)%(refname:short)"
576'
fddb74c9 577
11b087ad 578test_expect_success TTY '%(color) shows color with a tty' '
e433749d 579 test_terminal git for-each-ref --format="$color_format" >actual.raw &&
097b681b 580 test_decode_color <actual.raw >actual &&
11b087ad
JK
581 test_cmp expected.color actual
582'
583
584test_expect_success '%(color) does not show color without tty' '
585 TERM=vt100 git for-each-ref --format="$color_format" >actual &&
586 test_cmp expected.bare actual
587'
588
0c88bf50
JK
589test_expect_success '--color can override tty check' '
590 git for-each-ref --color --format="$color_format" >actual.raw &&
11b087ad
JK
591 test_decode_color <actual.raw >actual &&
592 test_cmp expected.color actual
fddb74c9
RR
593'
594
b521fd12
JK
595test_expect_success 'color.ui=always does not override tty check' '
596 git -c color.ui=always for-each-ref --format="$color_format" >actual &&
597 test_cmp expected.bare actual
598'
599
f5d18f8c
KS
600test_expect_success 'setup for describe atom tests' '
601 git init -b master describe-repo &&
602 (
603 cd describe-repo &&
604
605 test_commit --no-tag one &&
606 git tag tagone &&
607
608 test_commit --no-tag two &&
609 git tag -a -m "tag two" tagtwo
610 )
611'
612
613test_expect_success 'describe atom vs git describe' '
614 (
615 cd describe-repo &&
616
617 git for-each-ref --format="%(objectname)" \
618 refs/tags/ >obj &&
619 while read hash
620 do
621 if desc=$(git describe $hash)
622 then
623 : >expect-contains-good
624 else
625 : >expect-contains-bad
626 fi &&
627 echo "$hash $desc" || return 1
628 done <obj >expect &&
629 test_path_exists expect-contains-good &&
630 test_path_exists expect-contains-bad &&
631
632 git for-each-ref --format="%(objectname) %(describe)" \
633 refs/tags/ >actual 2>err &&
634 test_cmp expect actual &&
635 test_must_be_empty err
636 )
637'
638
639test_expect_success 'describe:tags vs describe --tags' '
640 (
641 cd describe-repo &&
642 git describe --tags >expect &&
643 git for-each-ref --format="%(describe:tags)" \
644 refs/heads/master >actual &&
645 test_cmp expect actual
646 )
647'
648
649test_expect_success 'describe:abbrev=... vs describe --abbrev=...' '
650 (
651 cd describe-repo &&
652
653 # Case 1: We have commits between HEAD and the most
654 # recent tag reachable from it
655 test_commit --no-tag file &&
656 git describe --abbrev=14 >expect &&
657 git for-each-ref --format="%(describe:abbrev=14)" \
658 refs/heads/master >actual &&
659 test_cmp expect actual &&
660
661 # Make sure the hash used is atleast 14 digits long
662 sed -e "s/^.*-g\([0-9a-f]*\)$/\1/" <actual >hexpart &&
663 test 15 -le $(wc -c <hexpart) &&
664
665 # Case 2: We have a tag at HEAD, describe directly gives
666 # the name of the tag
667 git tag -a -m tagged tagname &&
668 git describe --abbrev=14 >expect &&
669 git for-each-ref --format="%(describe:abbrev=14)" \
670 refs/heads/master >actual &&
671 test_cmp expect actual &&
672 test tagname = $(cat actual)
673 )
674'
675
676test_expect_success 'describe:match=... vs describe --match ...' '
677 (
678 cd describe-repo &&
679 git tag -a -m "tag foo" tag-foo &&
680 git describe --match "*-foo" >expect &&
681 git for-each-ref --format="%(describe:match="*-foo")" \
682 refs/heads/master >actual &&
683 test_cmp expect actual
684 )
685'
686
687test_expect_success 'describe:exclude:... vs describe --exclude ...' '
688 (
689 cd describe-repo &&
690 git tag -a -m "tag bar" tag-bar &&
691 git describe --exclude "*-bar" >expect &&
692 git for-each-ref --format="%(describe:exclude="*-bar")" \
693 refs/heads/master >actual &&
694 test_cmp expect actual
695 )
696'
697
698test_expect_success 'deref with describe atom' '
699 (
700 cd describe-repo &&
701 cat >expect <<-\EOF &&
702
703 tagname
704 tagname
705 tagname
706
707 tagtwo
708 EOF
709 git for-each-ref --format="%(*describe)" >actual &&
710 test_cmp expect actual
711 )
712'
713
714test_expect_success 'err on bad describe atom arg' '
715 (
716 cd describe-repo &&
717
718 # The bad arg is the only arg passed to describe atom
719 cat >expect <<-\EOF &&
720 fatal: unrecognized %(describe) argument: baz
721 EOF
722 test_must_fail git for-each-ref --format="%(describe:baz)" \
723 refs/heads/master 2>actual &&
724 test_cmp expect actual &&
725
726 # The bad arg is in the middle of the option string
727 # passed to the describe atom
728 cat >expect <<-\EOF &&
729 fatal: unrecognized %(describe) argument: qux=1,abbrev=14
730 EOF
731 test_must_fail git for-each-ref \
732 --format="%(describe:tags,qux=1,abbrev=14)" \
733 ref/heads/master 2>actual &&
734 test_cmp expect actual
735 )
736'
737
7d66f21a 738cat >expected <<\EOF
8f19c9fd
JS
739heads/main
740tags/main
7d66f21a
BW
741EOF
742
2bb98169
BW
743test_expect_success 'Check ambiguous head and tag refs (strict)' '
744 git config --bool core.warnambiguousrefs true &&
7d66f21a
BW
745 git checkout -b newtag &&
746 echo "Using $datestamp" > one &&
747 git add one &&
748 git commit -m "Branch" &&
749 setdate_and_increment &&
8f19c9fd
JS
750 git tag -m "Tagging at $datestamp" main &&
751 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
7d66f21a
BW
752 test_cmp expected actual
753'
754
2bb98169 755cat >expected <<\EOF
8f19c9fd
JS
756heads/main
757main
2bb98169
BW
758EOF
759
760test_expect_success 'Check ambiguous head and tag refs (loose)' '
761 git config --bool core.warnambiguousrefs false &&
8f19c9fd 762 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
2bb98169
BW
763 test_cmp expected actual
764'
765
7d66f21a
BW
766cat >expected <<\EOF
767heads/ambiguous
768ambiguous
769EOF
770
2bb98169 771test_expect_success 'Check ambiguous head and tag refs II (loose)' '
8f19c9fd 772 git checkout main &&
7d66f21a
BW
773 git tag ambiguous testtag^0 &&
774 git branch ambiguous testtag^0 &&
775 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
776 test_cmp expected actual
777'
778
8b3f33ef
MP
779test_expect_success 'create tag without tagger' '
780 git tag -a -m "Broken tag" taggerless &&
781 git tag -f taggerless $(git cat-file tag taggerless |
782 sed -e "/^tagger /d" |
34959d80 783 git hash-object --literally --stdin -w -t tag)
8b3f33ef
MP
784'
785
786test_atom refs/tags/taggerless type 'commit'
787test_atom refs/tags/taggerless tag 'taggerless'
788test_atom refs/tags/taggerless tagger ''
789test_atom refs/tags/taggerless taggername ''
790test_atom refs/tags/taggerless taggeremail ''
b82445dc
HV
791test_atom refs/tags/taggerless taggeremail:trim ''
792test_atom refs/tags/taggerless taggeremail:localpart ''
8b3f33ef
MP
793test_atom refs/tags/taggerless taggerdate ''
794test_atom refs/tags/taggerless committer ''
795test_atom refs/tags/taggerless committername ''
796test_atom refs/tags/taggerless committeremail ''
b82445dc
HV
797test_atom refs/tags/taggerless committeremail:trim ''
798test_atom refs/tags/taggerless committeremail:localpart ''
8b3f33ef
MP
799test_atom refs/tags/taggerless committerdate ''
800test_atom refs/tags/taggerless subject 'Broken tag'
801
e276c26b
JH
802test_expect_success 'an unusual tag with an incomplete line' '
803
804 git tag -m "bogo" bogo &&
805 bogo=$(git cat-file tag bogo) &&
806 bogo=$(printf "%s" "$bogo" | git mktag) &&
807 git tag -f bogo "$bogo" &&
808 git for-each-ref --format "%(body)" refs/tags/bogo
809
810'
811
7140c22c
JK
812test_expect_success 'create tag with subject and body content' '
813 cat >>msg <<-\EOF &&
814 the subject line
815
816 first body line
817 second body line
818 EOF
819 git tag -F msg subject-body
820'
821test_atom refs/tags/subject-body subject 'the subject line'
905f0a4e 822test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
7140c22c
JK
823test_atom refs/tags/subject-body body 'first body line
824second body line
825'
826test_atom refs/tags/subject-body contents 'the subject line
827
828first body line
829second body line
830'
831
7f6e275b
JK
832test_expect_success 'create tag with multiline subject' '
833 cat >msg <<-\EOF &&
834 first subject line
835 second subject line
836
837 first body line
838 second body line
839 EOF
840 git tag -F msg multiline
841'
842test_atom refs/tags/multiline subject 'first subject line second subject line'
905f0a4e 843test_atom refs/tags/multiline subject:sanitize 'first-subject-line-second-subject-line'
e2b23972 844test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
7f6e275b
JK
845test_atom refs/tags/multiline body 'first body line
846second body line
847'
e2b23972
MG
848test_atom refs/tags/multiline contents:body 'first body line
849second body line
850'
851test_atom refs/tags/multiline contents:signature ''
7f6e275b
JK
852test_atom refs/tags/multiline contents 'first subject line
853second subject line
854
855first body line
856second body line
857'
858
e2b23972
MG
859test_expect_success GPG 'create signed tags' '
860 git tag -s -m "" signed-empty &&
861 git tag -s -m "subject line" signed-short &&
862 cat >msg <<-\EOF &&
863 subject line
864
865 body contents
866 EOF
867 git tag -s -F msg signed-long
868'
869
870sig='-----BEGIN PGP SIGNATURE-----
871-----END PGP SIGNATURE-----
872'
873
874PREREQ=GPG
875test_atom refs/tags/signed-empty subject ''
905f0a4e 876test_atom refs/tags/signed-empty subject:sanitize ''
e2b23972
MG
877test_atom refs/tags/signed-empty contents:subject ''
878test_atom refs/tags/signed-empty body "$sig"
879test_atom refs/tags/signed-empty contents:body ''
880test_atom refs/tags/signed-empty contents:signature "$sig"
881test_atom refs/tags/signed-empty contents "$sig"
882
bd0708c7
ZH
883test_expect_success GPG 'basic atom: refs/tags/signed-empty raw' '
884 git cat-file tag refs/tags/signed-empty >expected &&
885 git for-each-ref --format="%(raw)" refs/tags/signed-empty >actual &&
886 sanitize_pgp <expected >expected.clean &&
887 echo >>expected.clean &&
888 sanitize_pgp <actual >actual.clean &&
889 test_cmp expected.clean actual.clean
890'
891
e2b23972 892test_atom refs/tags/signed-short subject 'subject line'
905f0a4e 893test_atom refs/tags/signed-short subject:sanitize 'subject-line'
e2b23972
MG
894test_atom refs/tags/signed-short contents:subject 'subject line'
895test_atom refs/tags/signed-short body "$sig"
896test_atom refs/tags/signed-short contents:body ''
897test_atom refs/tags/signed-short contents:signature "$sig"
898test_atom refs/tags/signed-short contents "subject line
899$sig"
900
bd0708c7
ZH
901test_expect_success GPG 'basic atom: refs/tags/signed-short raw' '
902 git cat-file tag refs/tags/signed-short >expected &&
903 git for-each-ref --format="%(raw)" refs/tags/signed-short >actual &&
904 sanitize_pgp <expected >expected.clean &&
905 echo >>expected.clean &&
906 sanitize_pgp <actual >actual.clean &&
907 test_cmp expected.clean actual.clean
908'
909
e2b23972 910test_atom refs/tags/signed-long subject 'subject line'
905f0a4e 911test_atom refs/tags/signed-long subject:sanitize 'subject-line'
e2b23972
MG
912test_atom refs/tags/signed-long contents:subject 'subject line'
913test_atom refs/tags/signed-long body "body contents
914$sig"
915test_atom refs/tags/signed-long contents:body 'body contents
916'
917test_atom refs/tags/signed-long contents:signature "$sig"
918test_atom refs/tags/signed-long contents "subject line
919
920body contents
921$sig"
922
bd0708c7
ZH
923test_expect_success GPG 'basic atom: refs/tags/signed-long raw' '
924 git cat-file tag refs/tags/signed-long >expected &&
925 git for-each-ref --format="%(raw)" refs/tags/signed-long >actual &&
926 sanitize_pgp <expected >expected.clean &&
927 echo >>expected.clean &&
928 sanitize_pgp <actual >actual.clean &&
929 test_cmp expected.clean actual.clean
930'
931
6e2ef8eb 932test_expect_success 'set up refs pointing to tree and blob' '
8f19c9fd
JS
933 git update-ref refs/mytrees/first refs/heads/main^{tree} &&
934 git update-ref refs/myblobs/first refs/heads/main:one
6e2ef8eb
CC
935'
936
937test_atom refs/mytrees/first subject ""
938test_atom refs/mytrees/first contents:subject ""
939test_atom refs/mytrees/first body ""
940test_atom refs/mytrees/first contents:body ""
941test_atom refs/mytrees/first contents:signature ""
942test_atom refs/mytrees/first contents ""
943
bd0708c7
ZH
944test_expect_success 'basic atom: refs/mytrees/first raw' '
945 git cat-file tree refs/mytrees/first >expected &&
946 echo >>expected &&
947 git for-each-ref --format="%(raw)" refs/mytrees/first >actual &&
948 test_cmp expected actual &&
949 git cat-file -s refs/mytrees/first >expected &&
950 git for-each-ref --format="%(raw:size)" refs/mytrees/first >actual &&
951 test_cmp expected actual
952'
953
6e2ef8eb
CC
954test_atom refs/myblobs/first subject ""
955test_atom refs/myblobs/first contents:subject ""
956test_atom refs/myblobs/first body ""
957test_atom refs/myblobs/first contents:body ""
958test_atom refs/myblobs/first contents:signature ""
959test_atom refs/myblobs/first contents ""
960
bd0708c7
ZH
961test_expect_success 'basic atom: refs/myblobs/first raw' '
962 git cat-file blob refs/myblobs/first >expected &&
963 echo >>expected &&
964 git for-each-ref --format="%(raw)" refs/myblobs/first >actual &&
965 test_cmp expected actual &&
966 git cat-file -s refs/myblobs/first >expected &&
967 git for-each-ref --format="%(raw:size)" refs/myblobs/first >actual &&
968 test_cmp expected actual
969'
970
971test_expect_success 'set up refs pointing to binary blob' '
972 printf "a\0b\0c" >blob1 &&
973 printf "a\0c\0b" >blob2 &&
974 printf "\0a\0b\0c" >blob3 &&
975 printf "abc" >blob4 &&
976 printf "\0 \0 \0 " >blob5 &&
977 printf "\0 \0a\0 " >blob6 &&
978 printf " " >blob7 &&
979 >blob8 &&
980 obj=$(git hash-object -w blob1) &&
981 git update-ref refs/myblobs/blob1 "$obj" &&
982 obj=$(git hash-object -w blob2) &&
983 git update-ref refs/myblobs/blob2 "$obj" &&
984 obj=$(git hash-object -w blob3) &&
985 git update-ref refs/myblobs/blob3 "$obj" &&
986 obj=$(git hash-object -w blob4) &&
987 git update-ref refs/myblobs/blob4 "$obj" &&
988 obj=$(git hash-object -w blob5) &&
989 git update-ref refs/myblobs/blob5 "$obj" &&
990 obj=$(git hash-object -w blob6) &&
991 git update-ref refs/myblobs/blob6 "$obj" &&
992 obj=$(git hash-object -w blob7) &&
993 git update-ref refs/myblobs/blob7 "$obj" &&
994 obj=$(git hash-object -w blob8) &&
995 git update-ref refs/myblobs/blob8 "$obj"
996'
997
998test_expect_success 'Verify sorts with raw' '
999 cat >expected <<-EOF &&
1000 refs/myblobs/blob8
1001 refs/myblobs/blob5
1002 refs/myblobs/blob6
1003 refs/myblobs/blob3
1004 refs/myblobs/blob7
1005 refs/mytrees/first
1006 refs/myblobs/first
1007 refs/myblobs/blob1
1008 refs/myblobs/blob2
1009 refs/myblobs/blob4
1010 refs/heads/main
1011 EOF
1012 git for-each-ref --format="%(refname)" --sort=raw \
1013 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
1014 test_cmp expected actual
1015'
1016
1017test_expect_success 'Verify sorts with raw:size' '
1018 cat >expected <<-EOF &&
1019 refs/myblobs/blob8
1020 refs/myblobs/first
1021 refs/myblobs/blob7
1022 refs/heads/main
1023 refs/myblobs/blob4
1024 refs/myblobs/blob1
1025 refs/myblobs/blob2
1026 refs/myblobs/blob3
1027 refs/myblobs/blob5
1028 refs/myblobs/blob6
1029 refs/mytrees/first
1030 EOF
1031 git for-each-ref --format="%(refname)" --sort=raw:size \
1032 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
1033 test_cmp expected actual
1034'
1035
1036test_expect_success 'validate raw atom with %(if:equals)' '
1037 cat >expected <<-EOF &&
1038 not equals
1039 not equals
1040 not equals
1041 not equals
1042 not equals
1043 not equals
1044 refs/myblobs/blob4
1045 not equals
1046 not equals
1047 not equals
1048 not equals
1049 not equals
1050 EOF
1051 git for-each-ref --format="%(if:equals=abc)%(raw)%(then)%(refname)%(else)not equals%(end)" \
1052 refs/myblobs/ refs/heads/ >actual &&
1053 test_cmp expected actual
1054'
1055
1056test_expect_success 'validate raw atom with %(if:notequals)' '
1057 cat >expected <<-EOF &&
1058 refs/heads/ambiguous
1059 refs/heads/main
1060 refs/heads/newtag
1061 refs/myblobs/blob1
1062 refs/myblobs/blob2
1063 refs/myblobs/blob3
1064 equals
1065 refs/myblobs/blob5
1066 refs/myblobs/blob6
1067 refs/myblobs/blob7
1068 refs/myblobs/blob8
1069 refs/myblobs/first
1070 EOF
1071 git for-each-ref --format="%(if:notequals=abc)%(raw)%(then)%(refname)%(else)equals%(end)" \
1072 refs/myblobs/ refs/heads/ >actual &&
1073 test_cmp expected actual
1074'
1075
1076test_expect_success 'empty raw refs with %(if)' '
1077 cat >expected <<-EOF &&
1078 refs/myblobs/blob1 not empty
1079 refs/myblobs/blob2 not empty
1080 refs/myblobs/blob3 not empty
1081 refs/myblobs/blob4 not empty
1082 refs/myblobs/blob5 not empty
1083 refs/myblobs/blob6 not empty
1084 refs/myblobs/blob7 empty
1085 refs/myblobs/blob8 empty
1086 refs/myblobs/first not empty
1087 EOF
1088 git for-each-ref --format="%(refname) %(if)%(raw)%(then)not empty%(else)empty%(end)" \
1089 refs/myblobs/ >actual &&
1090 test_cmp expected actual
1091'
1092
1093test_expect_success '%(raw) with --python must fail' '
1094 test_must_fail git for-each-ref --format="%(raw)" --python
1095'
1096
1097test_expect_success '%(raw) with --tcl must fail' '
1098 test_must_fail git for-each-ref --format="%(raw)" --tcl
1099'
1100
7121c4d4
ZH
1101test_expect_success '%(raw) with --perl' '
1102 git for-each-ref --format="\$name= %(raw);
1103print \"\$name\"" refs/myblobs/blob1 --perl | perl >actual &&
1104 cmp blob1 actual &&
1105 git for-each-ref --format="\$name= %(raw);
1106print \"\$name\"" refs/myblobs/blob3 --perl | perl >actual &&
1107 cmp blob3 actual &&
1108 git for-each-ref --format="\$name= %(raw);
1109print \"\$name\"" refs/myblobs/blob8 --perl | perl >actual &&
1110 cmp blob8 actual &&
1111 git for-each-ref --format="\$name= %(raw);
1112print \"\$name\"" refs/myblobs/first --perl | perl >actual &&
1113 cmp one actual &&
1114 git cat-file tree refs/mytrees/first > expected &&
1115 git for-each-ref --format="\$name= %(raw);
1116print \"\$name\"" refs/mytrees/first --perl | perl >actual &&
1117 cmp expected actual
bd0708c7
ZH
1118'
1119
1120test_expect_success '%(raw) with --shell must fail' '
1121 test_must_fail git for-each-ref --format="%(raw)" --shell
1122'
1123
1124test_expect_success '%(raw) with --shell and --sort=raw must fail' '
1125 test_must_fail git for-each-ref --format="%(raw)" --sort=raw --shell
1126'
1127
1128test_expect_success '%(raw:size) with --shell' '
efe47c83 1129 git for-each-ref --format="%(raw:size)" | sed "s/^/$SQ/;s/$/$SQ/" >expect &&
bd0708c7
ZH
1130 git for-each-ref --format="%(raw:size)" --shell >actual &&
1131 test_cmp expect actual
1132'
1133
1134test_expect_success 'for-each-ref --format compare with cat-file --batch' '
1135 git rev-parse refs/mytrees/first | git cat-file --batch >expected &&
1136 git for-each-ref --format="%(objectname) %(objecttype) %(objectsize)
1137%(raw)" refs/mytrees/first >actual &&
1138 test_cmp expected actual
1139'
1140
7c5045fc
JK
1141test_expect_success 'set up multiple-sort tags' '
1142 for when in 100000 200000
1143 do
1144 for email in user1 user2
1145 do
1146 for ref in ref1 ref2
1147 do
1148 GIT_COMMITTER_DATE="@$when +0000" \
1149 GIT_COMMITTER_EMAIL="$email@example.com" \
1150 git tag -m "tag $ref-$when-$email" \
1151 multi-$ref-$when-$email || return 1
1152 done
1153 done
1154 done
1155'
912072d5 1156
3b51222c 1157test_expect_success 'Verify sort with multiple keys' '
7c5045fc
JK
1158 cat >expected <<-\EOF &&
1159 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1160 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1161 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1162 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1163 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1164 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1165 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1166 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1167 EOF
1168 git for-each-ref \
1169 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1170 --sort=-refname \
1171 --sort=taggeremail \
1172 --sort=taggerdate \
1173 "refs/tags/multi-*" >actual &&
912072d5
KK
1174 test_cmp expected actual
1175'
84679d47 1176
7c5045fc
JK
1177test_expect_success 'equivalent sorts fall back on refname' '
1178 cat >expected <<-\EOF &&
1179 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1180 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1181 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1182 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1183 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1184 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1185 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1186 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1187 EOF
1188 git for-each-ref \
1189 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1190 --sort=taggerdate \
1191 "refs/tags/multi-*" >actual &&
1192 test_cmp expected actual
1193'
01f95825 1194
98e7ab6d
JH
1195test_expect_success '--no-sort cancels the previous sort keys' '
1196 cat >expected <<-\EOF &&
1197 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1198 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1199 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1200 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1201 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1202 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1203 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1204 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1205 EOF
1206 git for-each-ref \
1207 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1208 --sort=-refname \
1209 --sort=taggeremail \
1210 --no-sort \
1211 --sort=taggerdate \
1212 "refs/tags/multi-*" >actual &&
1213 test_cmp expected actual
1214'
1215
84679d47 1216test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
8f19c9fd 1217 test_when_finished "git checkout main" &&
84679d47
JH
1218 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1219 sed -e "s/^\* / /" actual >expect &&
f0252ca2 1220 git checkout --orphan orphaned-branch &&
84679d47
JH
1221 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1222 test_cmp expect actual
1223'
1224
b1d31c89
JK
1225cat >trailers <<EOF
1226Reviewed-by: A U Thor <author@example.com>
1227Signed-off-by: A U Thor <author@example.com>
624b44d3
TB
1228[ v2 updated patch description ]
1229Acked-by: A U Thor
1230 <author@example.com>
b1d31c89
JK
1231EOF
1232
67a20a00
TB
1233unfold () {
1234 perl -0pe 's/\n\s+/ /g'
1235}
624b44d3
TB
1236
1237test_expect_success 'set up trailers for next test' '
b1d31c89
JK
1238 echo "Some contents" > two &&
1239 git add two &&
624b44d3 1240 git commit -F - <<-EOF
b1d31c89
JK
1241 trailers: this commit message has trailers
1242
1243 Some message contents
1244
1245 $(cat trailers)
1246 EOF
624b44d3
TB
1247'
1248
727331dc
HV
1249test_trailer_option () {
1250 title=$1 option=$2
1251 cat >expect
1252 test_expect_success "$title" '
1253 git for-each-ref --format="%($option)" refs/heads/main >actual &&
1254 test_cmp expect actual &&
1255 git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
1256 test_cmp expect actual
1257 '
1258}
67a20a00 1259
727331dc
HV
1260test_trailer_option '%(trailers:unfold) unfolds trailers' \
1261 'trailers:unfold' <<-EOF
1262 $(unfold <trailers)
67a20a00 1263
727331dc 1264 EOF
7a5edbdb 1265
727331dc
HV
1266test_trailer_option '%(trailers:only) shows only "key: value" trailers' \
1267 'trailers:only' <<-EOF
1268 $(grep -v patch.description <trailers)
1269
1270 EOF
1271
ee82a487
HV
1272test_trailer_option '%(trailers:only=no,only=true) shows only "key: value" trailers' \
1273 'trailers:only=no,only=true' <<-EOF
1274 $(grep -v patch.description <trailers)
1275
1276 EOF
1277
1278test_trailer_option '%(trailers:only=yes) shows only "key: value" trailers' \
1279 'trailers:only=yes' <<-EOF
1280 $(grep -v patch.description <trailers)
1281
1282 EOF
1283
1284test_trailer_option '%(trailers:only=no) shows all trailers' \
1285 'trailers:only=no' <<-EOF
1286 $(cat trailers)
1287
1288 EOF
1289
727331dc
HV
1290test_trailer_option '%(trailers:only) and %(trailers:unfold) work together' \
1291 'trailers:only,unfold' <<-EOF
1292 $(grep -v patch.description <trailers | unfold)
1293
1294 EOF
1295
1296test_trailer_option '%(trailers:unfold) and %(trailers:only) work together' \
1297 'trailers:unfold,only' <<-EOF
1298 $(grep -v patch.description <trailers | unfold)
1299
1300 EOF
1301
ee82a487
HV
1302test_trailer_option '%(trailers:key=foo) shows that trailer' \
1303 'trailers:key=Signed-off-by' <<-EOF
1304 Signed-off-by: A U Thor <author@example.com>
1305
1306 EOF
1307
1308test_trailer_option '%(trailers:key=foo) is case insensitive' \
1309 'trailers:key=SiGned-oFf-bY' <<-EOF
1310 Signed-off-by: A U Thor <author@example.com>
1311
1312 EOF
1313
1314test_trailer_option '%(trailers:key=foo:) trailing colon also works' \
1315 'trailers:key=Signed-off-by:' <<-EOF
1316 Signed-off-by: A U Thor <author@example.com>
1317
1318 EOF
1319
1320test_trailer_option '%(trailers:key=foo) multiple keys' \
1321 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF
1322 Reviewed-by: A U Thor <author@example.com>
1323 Signed-off-by: A U Thor <author@example.com>
1324
1325 EOF
1326
1327test_trailer_option '%(trailers:key=nonexistent) becomes empty' \
1328 'trailers:key=Shined-off-by:' <<-EOF
1329
1330 EOF
1331
1332test_trailer_option '%(trailers:key=foo) handles multiple lines even if folded' \
1333 'trailers:key=Acked-by' <<-EOF
1334 $(grep -v patch.description <trailers | grep -v Signed-off-by | grep -v Reviewed-by)
1335
1336 EOF
1337
1338test_trailer_option '%(trailers:key=foo,unfold) properly unfolds' \
1339 'trailers:key=Signed-Off-by,unfold' <<-EOF
1340 $(unfold <trailers | grep Signed-off-by)
1341
1342 EOF
1343
1344test_trailer_option '%(trailers:key=foo,only=no) also includes nontrailer lines' \
1345 'trailers:key=Signed-off-by,only=no' <<-EOF
1346 Signed-off-by: A U Thor <author@example.com>
1347 $(grep patch.description <trailers)
1348
1349 EOF
1350
1351test_trailer_option '%(trailers:key=foo,valueonly) shows only value' \
1352 'trailers:key=Signed-off-by,valueonly' <<-EOF
1353 A U Thor <author@example.com>
1354
1355 EOF
1356
1357test_trailer_option '%(trailers:separator) changes separator' \
1358 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1359 Reviewed-by: A U Thor <author@example.com>,Signed-off-by: A U Thor <author@example.com>
1360 EOF
1361
1362test_trailer_option '%(trailers:key_value_separator) changes key-value separator' \
1363 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1364 Reviewed-by,A U Thor <author@example.com>
1365 Signed-off-by,A U Thor <author@example.com>
1366
1367 EOF
1368
1369test_trailer_option '%(trailers:separator,key_value_separator) changes both separators' \
1370 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1371 Reviewed-by,A U Thor <author@example.com>,Signed-off-by,A U Thor <author@example.com>
1372 EOF
1373
727331dc
HV
1374test_failing_trailer_option () {
1375 title=$1 option=$2
1376 cat >expect
1377 test_expect_success "$title" '
1378 # error message cannot be checked under i18n
1379 test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
feeb03bc 1380 test_cmp expect actual &&
727331dc 1381 test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
feeb03bc 1382 test_cmp expect actual
727331dc
HV
1383 '
1384}
1385
1386test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \
1387 'trailers:unsupported' <<-\EOF
67a20a00
TB
1388 fatal: unknown %(trailers) argument: unsupported
1389 EOF
67a20a00 1390
ee82a487
HV
1391test_failing_trailer_option '%(trailers:key) without value is error' \
1392 'trailers:key' <<-\EOF
1393 fatal: expected %(trailers:key=<value>)
1394 EOF
1395
2c22e102 1396test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
7a5edbdb 1397 cat >expect <<-EOF &&
2c22e102 1398 fatal: unrecognized %(contents) argument: trailersonly
7a5edbdb 1399 EOF
2c22e102 1400 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
feeb03bc 1401 test_cmp expect actual
7a5edbdb
TB
1402'
1403
624b44d3 1404test_expect_success 'basic atom: head contents:trailers' '
8f19c9fd 1405 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
b1d31c89
JK
1406 sanitize_pgp <actual >actual.clean &&
1407 # git for-each-ref ends with a blank line
1408 cat >expect <<-EOF &&
1409 $(cat trailers)
1410
1411 EOF
1412 test_cmp expect actual.clean
1413'
1414
b9dee075
ZH
1415test_expect_success 'basic atom: rest must fail' '
1416 test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
1417'
1418
a33d0fae
JK
1419test_expect_success 'HEAD atom does not take arguments' '
1420 test_must_fail git for-each-ref --format="%(HEAD:foo)" 2>err &&
1421 echo "fatal: %(HEAD) does not take arguments" >expect &&
1422 test_cmp expect err
1423'
1424
dda4fc1a
JK
1425test_expect_success 'subject atom rejects unknown arguments' '
1426 test_must_fail git for-each-ref --format="%(subject:foo)" 2>err &&
1427 echo "fatal: unrecognized %(subject) argument: foo" >expect &&
1428 test_cmp expect err
1429'
1430
1955ef10
JK
1431test_expect_success 'refname atom rejects unknown arguments' '
1432 test_must_fail git for-each-ref --format="%(refname:foo)" 2>err &&
1433 echo "fatal: unrecognized %(refname) argument: foo" >expect &&
1434 test_cmp expect err
1435'
1436
e5fba5d5
JK
1437test_expect_success 'trailer parsing not fooled by --- line' '
1438 git commit --allow-empty -F - <<-\EOF &&
1439 this is the subject
1440
1441 This is the body. The message has a "---" line which would confuse a
1442 message+patch parser. But here we know we have only a commit message,
1443 so we get it right.
1444
1445 trailer: wrong
1446 ---
1447 This is more body.
1448
1449 trailer: right
1450 EOF
1451
1452 {
1453 echo "trailer: right" &&
1454 echo
1455 } >expect &&
8f19c9fd 1456 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
e5fba5d5
JK
1457 test_cmp expect actual
1458'
1459
01f95825 1460test_expect_success 'Add symbolic ref for the following tests' '
8f19c9fd 1461 git symbolic-ref refs/heads/sym refs/heads/main
01f95825
KN
1462'
1463
1464cat >expected <<EOF
8f19c9fd 1465refs/heads/main
01f95825
KN
1466EOF
1467
1468test_expect_success 'Verify usage of %(symref) atom' '
1469 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
1470 test_cmp expected actual
1471'
1472
1473cat >expected <<EOF
8f19c9fd 1474heads/main
01f95825
KN
1475EOF
1476
1477test_expect_success 'Verify usage of %(symref:short) atom' '
1478 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
1479 test_cmp expected actual
1480'
1481
a7984101 1482cat >expected <<EOF
8f19c9fd
JS
1483main
1484heads/main
a7984101
KN
1485EOF
1486
17938f17
KN
1487test_expect_success 'Verify usage of %(symref:lstrip) atom' '
1488 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
1a0ca5e3 1489 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
44a6b6ce
JH
1490 test_cmp expected actual &&
1491
1492 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
1493 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
a7984101
KN
1494 test_cmp expected actual
1495'
1496
1a34728e
KN
1497cat >expected <<EOF
1498refs
1499refs/heads
1500EOF
1501
1502test_expect_success 'Verify usage of %(symref:rstrip) atom' '
1503 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
1504 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
1505 test_cmp expected actual
1506'
1507
1b586867
JS
1508test_expect_success ':remotename and :remoteref' '
1509 git init remote-tests &&
1510 (
1511 cd remote-tests &&
1512 test_commit initial &&
8f19c9fd 1513 git branch -M main &&
1b586867 1514 git remote add from fifth.coffee:blub &&
8f19c9fd
JS
1515 git config branch.main.remote from &&
1516 git config branch.main.merge refs/heads/stable &&
1b586867
JS
1517 git remote add to southridge.audio:repo &&
1518 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
8f19c9fd 1519 git config branch.main.pushRemote to &&
1b586867
JS
1520 for pair in "%(upstream)=refs/remotes/from/stable" \
1521 "%(upstream:remotename)=from" \
1522 "%(upstream:remoteref)=refs/heads/stable" \
8f19c9fd 1523 "%(push)=refs/remotes/to/pushed/main" \
1b586867 1524 "%(push:remotename)=to" \
8f19c9fd 1525 "%(push:remoteref)=refs/heads/pushed/main"
1b586867
JS
1526 do
1527 echo "${pair#*=}" >expect &&
1528 git for-each-ref --format="${pair%=*}" \
8f19c9fd 1529 refs/heads/main >actual &&
0c51d6b4 1530 test_cmp expect actual || exit 1
1b586867
JS
1531 done &&
1532 git branch push-simple &&
1533 git config branch.push-simple.pushRemote from &&
1534 actual="$(git for-each-ref \
1535 --format="%(push:remotename),%(push:remoteref)" \
1536 refs/heads/push-simple)" &&
1537 test from, = "$actual"
1538 )
1539'
1540
e674eb25 1541test_expect_success 'for-each-ref --ignore-case ignores case' '
8f19c9fd 1542 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
d3c6751b 1543 test_must_be_empty actual &&
ee0f3e22 1544
8f19c9fd 1545 echo refs/heads/main >expect &&
ee0f3e22 1546 git for-each-ref --format="%(refname)" --ignore-case \
8f19c9fd 1547 refs/heads/MAIN >actual &&
ee0f3e22
JK
1548 test_cmp expect actual
1549'
1550
aabfdc95
ØW
1551test_expect_success 'for-each-ref --omit-empty works' '
1552 git for-each-ref --format="%(refname)" >actual &&
1553 test_line_count -gt 1 actual &&
1554 git for-each-ref --format="%(if:equals=refs/heads/main)%(refname)%(then)%(refname)%(end)" --omit-empty >actual &&
1555 echo refs/heads/main >expect &&
1556 test_cmp expect actual
1557'
1558
76f9e569
JK
1559test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
1560 # name refs numerically to avoid case-insensitive filesystem conflicts
1561 nr=0 &&
1562 for email in a A b B
1563 do
1564 for subject in a A b B
1565 do
1566 GIT_COMMITTER_EMAIL="$email@example.com" \
1567 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1568 nr=$((nr+1))||
1569 return 1
1570 done
1571 done &&
1572 git for-each-ref --ignore-case \
1573 --format="%(taggeremail) %(subject) %(refname)" \
1574 --sort=refname \
1575 --sort=subject \
1576 --sort=taggeremail \
1577 refs/tags/icase-* >actual &&
1578 cat >expect <<-\EOF &&
1579 <a@example.com> tag a refs/tags/icase-00
1580 <a@example.com> tag A refs/tags/icase-01
1581 <A@example.com> tag a refs/tags/icase-04
1582 <A@example.com> tag A refs/tags/icase-05
1583 <a@example.com> tag b refs/tags/icase-02
1584 <a@example.com> tag B refs/tags/icase-03
1585 <A@example.com> tag b refs/tags/icase-06
1586 <A@example.com> tag B refs/tags/icase-07
1587 <b@example.com> tag a refs/tags/icase-08
1588 <b@example.com> tag A refs/tags/icase-09
1589 <B@example.com> tag a refs/tags/icase-12
1590 <B@example.com> tag A refs/tags/icase-13
1591 <b@example.com> tag b refs/tags/icase-10
1592 <b@example.com> tag B refs/tags/icase-11
1593 <B@example.com> tag b refs/tags/icase-14
1594 <B@example.com> tag B refs/tags/icase-15
1595 EOF
1596 test_cmp expect actual
1597'
1598
c6854508
JK
1599test_expect_success 'for-each-ref reports broken tags' '
1600 git tag -m "good tag" broken-tag-good HEAD &&
1601 git cat-file tag broken-tag-good >good &&
1602 sed s/commit/blob/ <good >bad &&
1603 bad=$(git hash-object -w -t tag bad) &&
1604 git update-ref refs/tags/broken-tag-bad $bad &&
1605 test_must_fail git for-each-ref --format="%(*objectname)" \
1606 refs/tags/broken-tag-*
1607'
1608
b01e1c7e
JK
1609test_expect_success 'set up tag with signature and no blank lines' '
1610 git tag -F - fake-sig-no-blanks <<-\EOF
1611 this is the subject
1612 -----BEGIN PGP SIGNATURE-----
1613 not a real signature, but we just care about the
1614 subject/body parsing. It is important here that
1615 there are no blank lines in the signature.
1616 -----END PGP SIGNATURE-----
1617 EOF
1618'
1619
1620test_atom refs/tags/fake-sig-no-blanks contents:subject 'this is the subject'
1621test_atom refs/tags/fake-sig-no-blanks contents:body ''
1622test_atom refs/tags/fake-sig-no-blanks contents:signature "$sig"
1623
8e1c5fcf
JK
1624test_expect_success 'set up tag with CRLF signature' '
1625 append_cr <<-\EOF |
1626 this is the subject
1627 -----BEGIN PGP SIGNATURE-----
1628
1629 not a real signature, but we just care about
1630 the subject/body parsing. It is important here
1631 that there is a blank line separating this
1632 from the signature header.
1633 -----END PGP SIGNATURE-----
1634 EOF
1635 git tag -F - --cleanup=verbatim fake-sig-crlf
1636'
1637
1638test_atom refs/tags/fake-sig-crlf contents:subject 'this is the subject'
1639test_atom refs/tags/fake-sig-crlf contents:body ''
1640
1641# CRLF is retained in the signature, so we have to pass our expected value
1642# through append_cr. But test_atom requires a shell string, which means command
1643# substitution, and the shell will strip trailing newlines from the output of
1644# the substitution. Hack around it by adding and then removing a dummy line.
1645sig_crlf="$(printf "%s" "$sig" | append_cr; echo dummy)"
1646sig_crlf=${sig_crlf%dummy}
1647test_atom refs/tags/fake-sig-crlf contents:signature "$sig_crlf"
1648
b73dec55
DS
1649test_expect_success 'git for-each-ref --stdin: empty' '
1650 >in &&
1651 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1652 git for-each-ref --format="%(refname)" >expect &&
1653 test_cmp expect actual
1654'
1655
1656test_expect_success 'git for-each-ref --stdin: fails if extra args' '
1657 >in &&
1658 test_must_fail git for-each-ref --format="%(refname)" \
1659 --stdin refs/heads/extra <in 2>err &&
1660 grep "unknown arguments supplied with --stdin" err
1661'
1662
1663test_expect_success 'git for-each-ref --stdin: matches' '
1664 cat >in <<-EOF &&
1665 refs/tags/multi*
1666 refs/heads/amb*
1667 EOF
1668
1669 cat >expect <<-EOF &&
1670 refs/heads/ambiguous
1671 refs/tags/multi-ref1-100000-user1
1672 refs/tags/multi-ref1-100000-user2
1673 refs/tags/multi-ref1-200000-user1
1674 refs/tags/multi-ref1-200000-user2
1675 refs/tags/multi-ref2-100000-user1
1676 refs/tags/multi-ref2-100000-user2
1677 refs/tags/multi-ref2-200000-user1
1678 refs/tags/multi-ref2-200000-user2
1679 refs/tags/multiline
1680 EOF
1681
1682 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1683 test_cmp expect actual
1684'
1685
b2c51b75
DS
1686test_expect_success 'git for-each-ref with non-existing refs' '
1687 cat >in <<-EOF &&
1688 refs/heads/this-ref-does-not-exist
1689 refs/tags/bogus
1690 EOF
1691
1692 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1693 test_must_be_empty actual &&
1694
1695 xargs git for-each-ref --format="%(refname)" <in >actual &&
1696 test_must_be_empty actual
1697'
1698
26c9c03f
KS
1699GRADE_FORMAT="%(signature:grade)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1700TRUSTLEVEL_FORMAT="%(signature:trustlevel)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1701
1702test_expect_success GPG 'setup for signature atom using gpg' '
1703 git checkout -b signed &&
1704
1705 test_when_finished "test_unconfig commit.gpgSign" &&
1706
1707 echo "1" >file &&
1708 git add file &&
1709 test_tick &&
1710 git commit -S -m "file: 1" &&
1711 git tag first-signed &&
1712
1713 echo "2" >file &&
1714 test_tick &&
1715 git commit -a -m "file: 2" &&
1716 git tag second-unsigned &&
1717
1718 git config commit.gpgSign 1 &&
1719 echo "3" >file &&
1720 test_tick &&
1721 git commit -a --no-gpg-sign -m "file: 3" &&
1722 git tag third-unsigned &&
1723
1724 test_tick &&
1725 git rebase -f HEAD^^ && git tag second-signed HEAD^ &&
1726 git tag third-signed &&
1727
1728 echo "4" >file &&
1729 test_tick &&
1730 git commit -a -SB7227189 -m "file: 4" &&
1731 git tag fourth-signed &&
1732
1733 echo "5" >file &&
1734 test_tick &&
1735 git commit -a --no-gpg-sign -m "file: 5" &&
1736 git tag fifth-unsigned &&
1737
1738 echo "6" >file &&
1739 test_tick &&
1740 git commit -a --no-gpg-sign -m "file: 6" &&
1741
1742 test_tick &&
1743 git rebase -f HEAD^^ &&
1744 git tag fifth-signed HEAD^ &&
1745 git tag sixth-signed &&
1746
1747 echo "7" >file &&
1748 test_tick &&
1749 git commit -a --no-gpg-sign -m "file: 7" &&
1750 git tag seventh-unsigned
1751'
1752
1753test_expect_success GPGSSH 'setup for signature atom using ssh' '
1754 test_when_finished "test_unconfig gpg.format user.signingkey" &&
1755
1756 test_config gpg.format ssh &&
1757 test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
1758 echo "8" >file &&
1759 test_tick &&
065135fc
RS
1760 git add file &&
1761 git commit -S -m "file: 8" &&
26c9c03f
KS
1762 git tag eighth-signed-ssh
1763'
1764
1765test_expect_success GPG2 'bare signature atom' '
d0fc552b 1766 git verify-commit first-signed 2>expect &&
26c9c03f
KS
1767 echo >>expect &&
1768 git for-each-ref refs/tags/first-signed \
1769 --format="%(signature)" >actual &&
1770 test_cmp expect actual
1771'
1772
1773test_expect_success GPG 'show good signature with custom format' '
1774 git verify-commit first-signed &&
1775 cat >expect <<-\EOF &&
1776 G
1777 13B6F51ECDDE430D
1778 C O Mitter <committer@example.com>
1779 73D758744BE721698EC54E8713B6F51ECDDE430D
1780 73D758744BE721698EC54E8713B6F51ECDDE430D
1781 EOF
1782 git for-each-ref refs/tags/first-signed \
1783 --format="$GRADE_FORMAT" >actual &&
1784 test_cmp expect actual
1785'
1786test_expect_success GPGSSH 'show good signature with custom format
1787 with ssh' '
1788 test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
1789 FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
1790 cat >expect.tmpl <<-\EOF &&
1791 G
1792 FINGERPRINT
1793 principal with number 1
1794 FINGERPRINT
1795
1796 EOF
1797 sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
1798 git for-each-ref refs/tags/eighth-signed-ssh \
1799 --format="$GRADE_FORMAT" >actual &&
1800 test_cmp expect actual
1801'
1802
1803test_expect_success GPG 'signature atom with grade option and bad signature' '
1804 git cat-file commit third-signed >raw &&
1805 sed -e "s/^file: 3/file: 3 forged/" raw >forged1 &&
1806 FORGED1=$(git hash-object -w -t commit forged1) &&
1807 git update-ref refs/tags/third-signed "$FORGED1" &&
1808 test_must_fail git verify-commit "$FORGED1" &&
1809
1810 cat >expect <<-\EOF &&
1811 B
1812 13B6F51ECDDE430D
1813 C O Mitter <committer@example.com>
1814
1815
1816 EOF
1817 git for-each-ref refs/tags/third-signed \
1818 --format="$GRADE_FORMAT" >actual &&
1819 test_cmp expect actual
1820'
1821
1822test_expect_success GPG 'show untrusted signature with custom format' '
1823 cat >expect <<-\EOF &&
1824 U
1825 65A0EEA02E30CAD7
1826 Eris Discordia <discord@example.net>
1827 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
1828 D4BE22311AD3131E5EDA29A461092E85B7227189
1829 EOF
1830 git for-each-ref refs/tags/fourth-signed \
1831 --format="$GRADE_FORMAT" >actual &&
1832 test_cmp expect actual
1833'
1834
1835test_expect_success GPG 'show untrusted signature with undefined trust level' '
1836 cat >expect <<-\EOF &&
1837 undefined
1838 65A0EEA02E30CAD7
1839 Eris Discordia <discord@example.net>
1840 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
1841 D4BE22311AD3131E5EDA29A461092E85B7227189
1842 EOF
1843 git for-each-ref refs/tags/fourth-signed \
1844 --format="$TRUSTLEVEL_FORMAT" >actual &&
1845 test_cmp expect actual
1846'
1847
1848test_expect_success GPG 'show untrusted signature with ultimate trust level' '
1849 cat >expect <<-\EOF &&
1850 ultimate
1851 13B6F51ECDDE430D
1852 C O Mitter <committer@example.com>
1853 73D758744BE721698EC54E8713B6F51ECDDE430D
1854 73D758744BE721698EC54E8713B6F51ECDDE430D
1855 EOF
1856 git for-each-ref refs/tags/sixth-signed \
1857 --format="$TRUSTLEVEL_FORMAT" >actual &&
1858 test_cmp expect actual
1859'
1860
1861test_expect_success GPG 'show unknown signature with custom format' '
1862 cat >expect <<-\EOF &&
1863 E
1864 13B6F51ECDDE430D
1865
1866
1867
1868 EOF
1869 GNUPGHOME="$GNUPGHOME_NOT_USED" git for-each-ref \
1870 refs/tags/sixth-signed --format="$GRADE_FORMAT" >actual &&
1871 test_cmp expect actual
1872'
1873
1874test_expect_success GPG 'show lack of signature with custom format' '
1875 cat >expect <<-\EOF &&
1876 N
1877
1878
1879
1880
1881 EOF
1882 git for-each-ref refs/tags/seventh-unsigned \
1883 --format="$GRADE_FORMAT" >actual &&
1884 test_cmp expect actual
1885'
1886
96b2d4fa 1887test_done