blob: eb6c8204e8bd0edd653d74102eb92db3da720202 [file] [log] [blame]
Andy Parkins96b2d4f2007-10-02 12:02:57 +01001#!/bin/sh
2#
3# Copyright (c) 2007 Andy Parkins
4#
5
6test_description='for-each-ref test'
7
8. ./test-lib.sh
Kousik Sanagavarapu26c9c032023-06-04 23:52:47 +05309GNUPGHOME_NOT_USED=$GNUPGHOME
Michał Górnye2b23972011-09-07 13:46:08 -040010. "$TEST_DIRECTORY"/lib-gpg.sh
Jeff King11b087a2017-07-13 11:09:32 -040011. "$TEST_DIRECTORY"/lib-terminal.sh
Andy Parkins96b2d4f2007-10-02 12:02:57 +010012
John Keepingdb7bae22015-09-03 22:49:00 +010013# Mon Jul 3 23:18:43 2006 +0000
14datestamp=1151968723
Andy Parkins96b2d4f2007-10-02 12:02:57 +010015setdate_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
René Scharfefbc65262023-12-12 18:04:55 +010023test_object_file_size () {
24 oid=$(git rev-parse "$1")
25 path=".git/objects/$(test_oid_to_path $oid)"
26 test_file_size "$path"
27}
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +053028
René Scharfefbc65262023-12-12 18:04:55 +010029test_expect_success setup '
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +053030 # setup .mailmap
31 cat >.mailmap <<-EOF &&
32 A Thor <athor@example.com> A U Thor <author@example.com>
33 C Mitter <cmitter@example.com> C O Mitter <committer@example.com>
34 EOF
35
Andy Parkins96b2d4f2007-10-02 12:02:57 +010036 setdate_and_increment &&
37 echo "Using $datestamp" > one &&
38 git add one &&
39 git commit -m "Initial" &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +000040 git branch -M main &&
Andy Parkins96b2d4f2007-10-02 12:02:57 +010041 setdate_and_increment &&
Ramkumar Ramachandrabc147962013-11-18 23:09:08 +053042 git tag -a -m "Tagging at $datestamp" testtag &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +000043 git update-ref refs/remotes/origin/main main &&
Jeff King8cae19d2009-04-07 03:09:39 -040044 git remote add origin nowhere &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +000045 git config branch.main.remote origin &&
46 git config branch.main.merge refs/heads/main &&
Jeff King29bc8852015-05-21 00:45:55 -040047 git remote add myfork elsewhere &&
48 git config remote.pushdefault myfork &&
49 git config push.default current
Jeff King8cae19d2009-04-07 03:09:39 -040050'
51
Kousik Sanagavarapu04830eb2023-09-25 23:13:08 +053052test_atom () {
Jeff Kingd2bf48d2008-06-26 04:08:41 -040053 case "$1" in
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +000054 head) ref=refs/heads/main ;;
Jeff Kingd2bf48d2008-06-26 04:08:41 -040055 tag) ref=refs/tags/testtag ;;
Karthik Nayak01f95822017-01-10 14:19:42 +053056 sym) ref=refs/heads/sym ;;
Jeff King7140c222011-09-07 13:43:28 -040057 *) ref=$1 ;;
Jeff Kingd2bf48d2008-06-26 04:08:41 -040058 esac
Kousik Sanagavarapu04830eb2023-09-25 23:13:08 +053059 format=$2
60 test_do=test_expect_${4:-success}
61
Jeff Kingd2bf48d2008-06-26 04:08:41 -040062 printf '%s\n' "$3" >expected
Kousik Sanagavarapu04830eb2023-09-25 23:13:08 +053063 $test_do $PREREQ "basic atom: $ref $format" '
64 git for-each-ref --format="%($format)" "$ref" >actual &&
Michał Górnye2b23972011-09-07 13:46:08 -040065 sanitize_pgp <actual >actual.clean &&
66 test_cmp expected actual.clean
Kousik Sanagavarapu04830eb2023-09-25 23:13:08 +053067 '
68
Christian Couderb6839fd2020-07-16 14:19:40 +020069 # Automatically test "contents:size" atom after testing "contents"
Kousik Sanagavarapu04830eb2023-09-25 23:13:08 +053070 if test "$format" = "contents"
Christian Couderb6839fd2020-07-16 14:19:40 +020071 then
Đoàn Trần Công Danh597fa8c2021-08-21 08:36:33 +070072 # for commit leg, $3 is changed there
73 expect=$(printf '%s' "$3" | wc -c)
Kousik Sanagavarapu04830eb2023-09-25 23:13:08 +053074 $test_do $PREREQ "basic atom: $ref contents:size" '
Đoàn Trần Công Danh597fa8c2021-08-21 08:36:33 +070075 type=$(git cat-file -t "$ref") &&
76 case $type in
77 tag)
78 # We cannot use $3 as it expects sanitize_pgp to run
Đoàn Trần Công Danh15495772021-08-21 08:36:34 +070079 git cat-file tag $ref >out &&
80 expect=$(tail -n +6 out | wc -c) &&
81 rm -f out ;;
Đoàn Trần Công Danh597fa8c2021-08-21 08:36:33 +070082 tree | blob)
83 expect="" ;;
84 commit)
85 : "use the calculated expect" ;;
86 *)
87 BUG "unknown object type" ;;
88 esac &&
89 # Leave $expect unquoted to lose possible leading whitespaces
90 echo $expect >expected &&
Christian Couderb6839fd2020-07-16 14:19:40 +020091 git for-each-ref --format="%(contents:size)" "$ref" >actual &&
Alban Gruin3db796c2020-07-31 20:26:07 +020092 test_cmp expected actual
Christian Couderb6839fd2020-07-16 14:19:40 +020093 '
94 fi
Jeff Kingd2bf48d2008-06-26 04:08:41 -040095}
96
brian m. carlson1f5f8f32020-02-22 20:17:32 +000097hexlen=$(test_oid hexsz)
brian m. carlson1f5f8f32020-02-22 20:17:32 +000098
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +000099test_atom head refname refs/heads/main
100test_atom head refname: refs/heads/main
101test_atom head refname:short main
102test_atom head refname:lstrip=1 heads/main
103test_atom head refname:lstrip=2 main
104test_atom head refname:lstrip=-1 main
105test_atom head refname:lstrip=-2 heads/main
Karthik Nayak1a347282017-01-10 14:19:49 +0530106test_atom head refname:rstrip=1 refs/heads
107test_atom head refname:rstrip=2 refs
108test_atom head refname:rstrip=-1 refs
109test_atom head refname:rstrip=-2 refs/heads
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000110test_atom head refname:strip=1 heads/main
111test_atom head refname:strip=2 main
112test_atom head refname:strip=-1 main
113test_atom head refname:strip=-2 heads/main
114test_atom head upstream refs/remotes/origin/main
115test_atom head upstream:short origin/main
116test_atom head upstream:lstrip=2 origin/main
117test_atom head upstream:lstrip=-2 origin/main
Karthik Nayak1a347282017-01-10 14:19:49 +0530118test_atom head upstream:rstrip=2 refs/remotes
119test_atom head upstream:rstrip=-2 refs/remotes
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000120test_atom head upstream:strip=2 origin/main
121test_atom head upstream:strip=-2 origin/main
122test_atom head push refs/remotes/myfork/main
123test_atom head push:short myfork/main
124test_atom head push:lstrip=1 remotes/myfork/main
125test_atom head push:lstrip=-1 main
Karthik Nayak1a347282017-01-10 14:19:49 +0530126test_atom head push:rstrip=1 refs/remotes/myfork
127test_atom head push:rstrip=-1 refs
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000128test_atom head push:strip=1 remotes/myfork/main
129test_atom head push:strip=-1 main
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400130test_atom head objecttype commit
brian m. carlson1f5f8f32020-02-22 20:17:32 +0000131test_atom head objectsize $((131 + hexlen))
René Scharfefbc65262023-12-12 18:04:55 +0100132test_atom head objectsize:disk $(test_object_file_size refs/heads/main)
brian m. carlson1f5f8f32020-02-22 20:17:32 +0000133test_atom head deltabase $ZERO_OID
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000134test_atom head objectname $(git rev-parse refs/heads/main)
135test_atom head objectname:short $(git rev-parse --short refs/heads/main)
136test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
137test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
138test_atom head tree $(git rev-parse refs/heads/main^{tree})
139test_atom head tree:short $(git rev-parse --short refs/heads/main^{tree})
140test_atom head tree:short=1 $(git rev-parse --short=1 refs/heads/main^{tree})
141test_atom head tree:short=10 $(git rev-parse --short=10 refs/heads/main^{tree})
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400142test_atom head parent ''
Hariom Verma26bc0aa2020-08-21 21:41:48 +0000143test_atom head parent:short ''
144test_atom head parent:short=1 ''
145test_atom head parent:short=10 ''
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400146test_atom head numparent 0
147test_atom head object ''
148test_atom head type ''
ZheNing Hubd0708c2021-07-26 03:26:47 +0000149test_atom head raw "$(git cat-file commit refs/heads/main)
150"
Jeff Kingb74cf642013-10-30 02:50:16 -0400151test_atom head '*objectname' ''
152test_atom head '*objecttype' ''
John Keepingdb7bae22015-09-03 22:49:00 +0100153test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400154test_atom head authorname 'A U Thor'
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530155test_atom head authorname:mailmap 'A Thor'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400156test_atom head authoremail '<author@example.com>'
Hariom Vermab82445d2020-08-21 21:41:43 +0000157test_atom head authoremail:trim 'author@example.com'
158test_atom head authoremail:localpart 'author'
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530159test_atom head authoremail:trim,localpart 'author'
160test_atom head authoremail:mailmap '<athor@example.com>'
161test_atom head authoremail:mailmap,trim 'athor@example.com'
162test_atom head authoremail:trim,mailmap 'athor@example.com'
163test_atom head authoremail:mailmap,localpart 'athor'
164test_atom head authoremail:localpart,mailmap 'athor'
165test_atom head authoremail:mailmap,trim,localpart,mailmap,trim 'athor'
John Keepingdb7bae22015-09-03 22:49:00 +0100166test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
167test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400168test_atom head committername 'C O Mitter'
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530169test_atom head committername:mailmap 'C Mitter'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400170test_atom head committeremail '<committer@example.com>'
Hariom Vermab82445d2020-08-21 21:41:43 +0000171test_atom head committeremail:trim 'committer@example.com'
172test_atom head committeremail:localpart 'committer'
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530173test_atom head committeremail:localpart,trim 'committer'
174test_atom head committeremail:mailmap '<cmitter@example.com>'
175test_atom head committeremail:mailmap,trim 'cmitter@example.com'
176test_atom head committeremail:trim,mailmap 'cmitter@example.com'
177test_atom head committeremail:mailmap,localpart 'cmitter'
178test_atom head committeremail:localpart,mailmap 'cmitter'
179test_atom head committeremail:trim,mailmap,trim,trim,localpart 'cmitter'
John Keepingdb7bae22015-09-03 22:49:00 +0100180test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400181test_atom head tag ''
182test_atom head tagger ''
183test_atom head taggername ''
184test_atom head taggeremail ''
Hariom Vermab82445d2020-08-21 21:41:43 +0000185test_atom head taggeremail:trim ''
186test_atom head taggeremail:localpart ''
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400187test_atom head taggerdate ''
John Keepingdb7bae22015-09-03 22:49:00 +0100188test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
189test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400190test_atom head subject 'Initial'
Hariom Verma905f0a42020-08-21 21:41:50 +0000191test_atom head subject:sanitize 'Initial'
Michał Górnye2b23972011-09-07 13:46:08 -0400192test_atom head contents:subject 'Initial'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400193test_atom head body ''
Michał Górnye2b23972011-09-07 13:46:08 -0400194test_atom head contents:body ''
195test_atom head contents:signature ''
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400196test_atom head contents 'Initial
197'
Ramkumar Ramachandra7a48b832013-11-18 23:09:10 +0530198test_atom head HEAD '*'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400199
200test_atom tag refname refs/tags/testtag
Jeff King1d094db2016-01-24 18:08:18 -0500201test_atom tag refname:short testtag
Jeff King8cae19d2009-04-07 03:09:39 -0400202test_atom tag upstream ''
Jeff King29bc8852015-05-21 00:45:55 -0400203test_atom tag push ''
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400204test_atom tag objecttype tag
brian m. carlson1f5f8f32020-02-22 20:17:32 +0000205test_atom tag objectsize $((114 + hexlen))
René Scharfefbc65262023-12-12 18:04:55 +0100206test_atom tag objectsize:disk $(test_object_file_size refs/tags/testtag)
207test_atom tag '*objectsize:disk' $(test_object_file_size refs/heads/main)
brian m. carlson1f5f8f32020-02-22 20:17:32 +0000208test_atom tag deltabase $ZERO_OID
209test_atom tag '*deltabase' $ZERO_OID
Ramkumar Ramachandra189a5462013-11-18 23:09:09 +0530210test_atom tag objectname $(git rev-parse refs/tags/testtag)
Jeff King1d094db2016-01-24 18:08:18 -0500211test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000212test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
213test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400214test_atom tag tree ''
Hariom Verma837adb12020-08-21 21:41:47 +0000215test_atom tag tree:short ''
216test_atom tag tree:short=1 ''
217test_atom tag tree:short=10 ''
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400218test_atom tag parent ''
Hariom Verma26bc0aa2020-08-21 21:41:48 +0000219test_atom tag parent:short ''
220test_atom tag parent:short=1 ''
221test_atom tag parent:short=10 ''
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400222test_atom tag numparent ''
Ramkumar Ramachandra189a5462013-11-18 23:09:09 +0530223test_atom tag object $(git rev-parse refs/tags/testtag^0)
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400224test_atom tag type 'commit'
brian m. carlson1f5f8f32020-02-22 20:17:32 +0000225test_atom tag '*objectname' $(git rev-parse refs/tags/testtag^{})
Jeff Kingb74cf642013-10-30 02:50:16 -0400226test_atom tag '*objecttype' 'commit'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400227test_atom tag author ''
228test_atom tag authorname ''
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530229test_atom tag authorname:mailmap ''
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400230test_atom tag authoremail ''
Hariom Vermab82445d2020-08-21 21:41:43 +0000231test_atom tag authoremail:trim ''
232test_atom tag authoremail:localpart ''
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530233test_atom tag authoremail:trim,localpart ''
234test_atom tag authoremail:mailmap ''
235test_atom tag authoremail:mailmap,trim ''
236test_atom tag authoremail:trim,mailmap ''
237test_atom tag authoremail:mailmap,localpart ''
238test_atom tag authoremail:localpart,mailmap ''
239test_atom tag authoremail:mailmap,trim,localpart,mailmap,trim ''
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400240test_atom tag authordate ''
241test_atom tag committer ''
242test_atom tag committername ''
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530243test_atom tag committername:mailmap ''
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400244test_atom tag committeremail ''
Hariom Vermab82445d2020-08-21 21:41:43 +0000245test_atom tag committeremail:trim ''
246test_atom tag committeremail:localpart ''
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530247test_atom tag committeremail:localpart,trim ''
248test_atom tag committeremail:mailmap ''
249test_atom tag committeremail:mailmap,trim ''
250test_atom tag committeremail:trim,mailmap ''
251test_atom tag committeremail:mailmap,localpart ''
252test_atom tag committeremail:localpart,mailmap ''
253test_atom tag committeremail:trim,mailmap,trim,trim,localpart ''
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400254test_atom tag committerdate ''
255test_atom tag tag 'testtag'
John Keepingdb7bae22015-09-03 22:49:00 +0100256test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400257test_atom tag taggername 'C O Mitter'
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530258test_atom tag taggername:mailmap 'C Mitter'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400259test_atom tag taggeremail '<committer@example.com>'
Hariom Vermab82445d2020-08-21 21:41:43 +0000260test_atom tag taggeremail:trim 'committer@example.com'
261test_atom tag taggeremail:localpart 'committer'
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530262test_atom tag taggeremail:trim,localpart 'committer'
263test_atom tag taggeremail:mailmap '<cmitter@example.com>'
264test_atom tag taggeremail:mailmap,trim 'cmitter@example.com'
265test_atom tag taggeremail:trim,mailmap 'cmitter@example.com'
266test_atom tag taggeremail:mailmap,localpart 'cmitter'
267test_atom tag taggeremail:localpart,mailmap 'cmitter'
268test_atom tag taggeremail:trim,mailmap,trim,localpart,localpart 'cmitter'
John Keepingdb7bae22015-09-03 22:49:00 +0100269test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
270test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
271test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
272test_atom tag subject 'Tagging at 1151968727'
Hariom Verma905f0a42020-08-21 21:41:50 +0000273test_atom tag subject:sanitize 'Tagging-at-1151968727'
John Keepingdb7bae22015-09-03 22:49:00 +0100274test_atom tag contents:subject 'Tagging at 1151968727'
Jeff Kingd2bf48d2008-06-26 04:08:41 -0400275test_atom tag body ''
Michał Górnye2b23972011-09-07 13:46:08 -0400276test_atom tag contents:body ''
277test_atom tag contents:signature ''
John Keepingdb7bae22015-09-03 22:49:00 +0100278test_atom tag contents 'Tagging at 1151968727
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100279'
Ramkumar Ramachandra7a48b832013-11-18 23:09:10 +0530280test_atom tag HEAD ' '
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100281
ZheNing Hubd0708c2021-07-26 03:26:47 +0000282test_expect_success 'basic atom: refs/tags/testtag *raw' '
283 git cat-file commit refs/tags/testtag^{} >expected &&
284 git for-each-ref --format="%(*raw)" refs/tags/testtag >actual &&
285 sanitize_pgp <expected >expected.clean &&
286 echo >>expected.clean &&
287 sanitize_pgp <actual >actual.clean &&
288 test_cmp expected.clean actual.clean
289'
290
Junio C Hamano41ac4142008-02-01 01:50:53 -0800291test_expect_success 'Check invalid atoms names are errors' '
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +0900292 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100293'
294
295test_expect_success 'Check format specifiers are ignored in naming date atoms' '
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +0900296 git for-each-ref --format="%(authordate)" refs/heads &&
297 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
298 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
299 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100300'
301
302test_expect_success 'Check valid format specifiers for date fields' '
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +0900303 git for-each-ref --format="%(authordate:default)" refs/heads &&
304 git for-each-ref --format="%(authordate:relative)" refs/heads &&
305 git for-each-ref --format="%(authordate:short)" refs/heads &&
306 git for-each-ref --format="%(authordate:local)" refs/heads &&
307 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
308 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100309'
310
Junio C Hamano41ac4142008-02-01 01:50:53 -0800311test_expect_success 'Check invalid format specifiers are errors' '
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +0900312 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100313'
314
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530315test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
316 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
317 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
318 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
319'
320
Kousik Sanagavarapu0144f0d2023-09-25 23:13:09 +0530321test_bad_atom () {
322 case "$1" in
323 head) ref=refs/heads/main ;;
324 tag) ref=refs/tags/testtag ;;
325 sym) ref=refs/heads/sym ;;
326 *) ref=$1 ;;
327 esac
328 format=$2
329 test_do=test_expect_${4:-success}
330
331 printf '%s\n' "$3" >expect
332 $test_do $PREREQ "err basic atom: $ref $format" '
333 test_must_fail git for-each-ref \
334 --format="%($format)" "$ref" 2>error &&
335 test_cmp expect error
336 '
337}
338
339test_bad_atom head 'authoremail:foo' \
340 'fatal: unrecognized %(authoremail) argument: foo'
341
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530342test_bad_atom head 'authoremail:mailmap,trim,bar' \
343 'fatal: unrecognized %(authoremail) argument: bar'
344
345test_bad_atom head 'authoremail:trim,' \
346 'fatal: unrecognized %(authoremail) argument: '
347
348test_bad_atom head 'authoremail:mailmaptrim' \
349 'fatal: unrecognized %(authoremail) argument: trim'
350
351test_bad_atom head 'committeremail: ' \
352 'fatal: unrecognized %(committeremail) argument: '
353
354test_bad_atom head 'committeremail: trim,foo' \
355 'fatal: unrecognized %(committeremail) argument: trim,foo'
356
357test_bad_atom head 'committeremail:mailmap,localpart ' \
358 'fatal: unrecognized %(committeremail) argument: '
359
360test_bad_atom head 'committeremail:trim_localpart' \
361 'fatal: unrecognized %(committeremail) argument: _localpart'
362
363test_bad_atom head 'committeremail:localpart,,,trim' \
364 'fatal: unrecognized %(committeremail) argument: ,,trim'
365
366test_bad_atom tag 'taggeremail:mailmap,trim, foo ' \
367 'fatal: unrecognized %(taggeremail) argument: foo '
368
369test_bad_atom tag 'taggeremail:trim,localpart,' \
370 'fatal: unrecognized %(taggeremail) argument: '
371
372test_bad_atom tag 'taggeremail:mailmap;localpart trim' \
373 'fatal: unrecognized %(taggeremail) argument: ;localpart trim'
374
Kousik Sanagavarapu0144f0d2023-09-25 23:13:09 +0530375test_bad_atom tag 'taggeremail:localpart trim' \
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530376 'fatal: unrecognized %(taggeremail) argument: trim'
377
378test_bad_atom tag 'taggeremail:mailmap,mailmap,trim,qux,localpart,trim' \
379 'fatal: unrecognized %(taggeremail) argument: qux,localpart,trim'
Kousik Sanagavarapu0144f0d2023-09-25 23:13:09 +0530380
John Keepingf95cecf2015-09-03 22:48:56 +0100381test_date () {
382 f=$1 &&
383 committer_date=$2 &&
384 author_date=$3 &&
385 tagger_date=$4 &&
386 cat >expected <<-EOF &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000387 'refs/heads/main' '$committer_date' '$author_date'
John Keepingf95cecf2015-09-03 22:48:56 +0100388 'refs/tags/testtag' '$tagger_date'
389 EOF
390 (
391 git for-each-ref --shell \
392 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
393 refs/heads &&
394 git for-each-ref --shell \
395 --format="%(refname) %(taggerdate${f:+:$f})" \
396 refs/tags
397 ) >actual &&
398 test_cmp expected actual
399}
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100400
401test_expect_success 'Check unformatted date fields output' '
John Keepingf95cecf2015-09-03 22:48:56 +0100402 test_date "" \
John Keepingdb7bae22015-09-03 22:49:00 +0100403 "Tue Jul 4 01:18:43 2006 +0200" \
404 "Tue Jul 4 01:18:44 2006 +0200" \
405 "Tue Jul 4 01:18:45 2006 +0200"
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100406'
407
408test_expect_success 'Check format "default" formatted date fields output' '
John Keepingf95cecf2015-09-03 22:48:56 +0100409 test_date default \
John Keepingdb7bae22015-09-03 22:49:00 +0100410 "Tue Jul 4 01:18:43 2006 +0200" \
411 "Tue Jul 4 01:18:44 2006 +0200" \
412 "Tue Jul 4 01:18:45 2006 +0200"
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100413'
414
John Keeping99264e92015-09-03 22:49:01 +0100415test_expect_success 'Check format "default-local" date fields output' '
416 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"
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100417'
418
419# Don't know how to do relative check because I can't know when this script
420# is going to be run and can't fake the current time to git, and hence can't
421# provide expected output. Instead, I'll just make sure that "relative"
422# doesn't exit in error
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100423test_expect_success 'Check format "relative" date fields output' '
424 f=relative &&
425 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
426 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
427'
428
John Keeping99264e92015-09-03 22:49:01 +0100429# We just check that this is the same as "relative" for now.
430test_expect_success 'Check format "relative-local" date fields output' '
431 test_date relative-local \
432 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
433 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
434 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
435'
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100436
437test_expect_success 'Check format "short" date fields output' '
John Keepingdb7bae22015-09-03 22:49:00 +0100438 test_date short 2006-07-04 2006-07-04 2006-07-04
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100439'
440
John Keeping99264e92015-09-03 22:49:01 +0100441test_expect_success 'Check format "short-local" date fields output' '
442 test_date short-local 2006-07-03 2006-07-03 2006-07-03
443'
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100444
445test_expect_success 'Check format "local" date fields output' '
John Keepingf95cecf2015-09-03 22:48:56 +0100446 test_date local \
John Keepingdb7bae22015-09-03 22:49:00 +0100447 "Mon Jul 3 23:18:43 2006" \
448 "Mon Jul 3 23:18:44 2006" \
449 "Mon Jul 3 23:18:45 2006"
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100450'
451
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100452test_expect_success 'Check format "iso8601" date fields output' '
John Keepingf95cecf2015-09-03 22:48:56 +0100453 test_date iso8601 \
John Keepingdb7bae22015-09-03 22:49:00 +0100454 "2006-07-04 01:18:43 +0200" \
455 "2006-07-04 01:18:44 +0200" \
456 "2006-07-04 01:18:45 +0200"
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100457'
458
John Keeping99264e92015-09-03 22:49:01 +0100459test_expect_success 'Check format "iso8601-local" date fields output' '
460 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"
461'
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100462
463test_expect_success 'Check format "rfc2822" date fields output' '
John Keepingf95cecf2015-09-03 22:48:56 +0100464 test_date rfc2822 \
John Keepingdb7bae22015-09-03 22:49:00 +0100465 "Tue, 4 Jul 2006 01:18:43 +0200" \
466 "Tue, 4 Jul 2006 01:18:44 +0200" \
467 "Tue, 4 Jul 2006 01:18:45 +0200"
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100468'
469
John Keeping99264e92015-09-03 22:49:01 +0100470test_expect_success 'Check format "rfc2822-local" date fields output' '
471 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"
472'
473
John Keepingf3c1ba52015-09-03 22:48:57 +0100474test_expect_success 'Check format "raw" date fields output' '
John Keepingdb7bae22015-09-03 22:49:00 +0100475 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
John Keepingf3c1ba52015-09-03 22:48:57 +0100476'
477
John Keeping99264e92015-09-03 22:49:01 +0100478test_expect_success 'Check format "raw-local" date fields output' '
479 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
Andy Parkins96b2d4f2007-10-02 12:02:57 +0100480'
481
Jeff Kingaa1462c2015-06-25 12:55:45 -0400482test_expect_success 'Check format of strftime date fields' '
John Keepingdb7bae22015-09-03 22:49:00 +0100483 echo "my date is 2006-07-04" >expected &&
Jeff Kingaa1462c2015-06-25 12:55:45 -0400484 git for-each-ref \
485 --format="%(authordate:format:my date is %Y-%m-%d)" \
486 refs/heads >actual &&
487 test_cmp expected actual
488'
489
John Keeping99264e92015-09-03 22:49:01 +0100490test_expect_success 'Check format of strftime-local date fields' '
491 echo "my date is 2006-07-03" >expected &&
492 git for-each-ref \
493 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
494 refs/heads >actual &&
495 test_cmp expected actual
496'
497
Jeff Kinge4f031e2015-06-30 09:26:53 -0400498test_expect_success 'exercise strftime with odd fields' '
499 echo >expected &&
500 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
501 test_cmp expected actual &&
brian m. carlson8125a582018-05-13 02:24:13 +0000502 long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
Jeff Kinge4f031e2015-06-30 09:26:53 -0400503 echo $long >expected &&
504 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
505 test_cmp expected actual
506'
507
Lars Hjemlic899a572007-11-10 17:47:54 +0100508cat >expected <<\EOF
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000509refs/heads/main
510refs/remotes/origin/main
Lars Hjemlic899a572007-11-10 17:47:54 +0100511refs/tags/testtag
512EOF
513
514test_expect_success 'Verify ascending sort' '
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +0900515 git for-each-ref --format="%(refname)" --sort=refname >actual &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700516 test_cmp expected actual
Lars Hjemlic899a572007-11-10 17:47:54 +0100517'
518
519
520cat >expected <<\EOF
521refs/tags/testtag
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000522refs/remotes/origin/main
523refs/heads/main
Lars Hjemlic899a572007-11-10 17:47:54 +0100524EOF
525
526test_expect_success 'Verify descending sort' '
Nanako Shiraishi3604e7c2008-09-03 17:59:29 +0900527 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700528 test_cmp expected actual
Lars Hjemlic899a572007-11-10 17:47:54 +0100529'
530
Junio C Hamano98e7ab62021-10-20 12:23:53 -0700531test_expect_success 'Give help even with invalid sort atoms' '
532 test_expect_code 129 git for-each-ref --sort=bogus -h >actual 2>&1 &&
533 grep "^usage: git for-each-ref" actual
534'
535
Johannes Sixtc9ecf4f2007-12-06 13:24:39 +0100536cat >expected <<\EOF
Taylor Blaub31e2682019-06-26 17:41:48 -0500537refs/tags/testtag
538refs/tags/testtag-2
539EOF
540
541test_expect_success 'exercise patterns with prefixes' '
542 git tag testtag-2 &&
543 test_when_finished "git tag -d testtag-2" &&
544 git for-each-ref --format="%(refname)" \
545 refs/tags/testtag refs/tags/testtag-2 >actual &&
546 test_cmp expected actual
547'
548
549cat >expected <<\EOF
550refs/tags/testtag
551refs/tags/testtag-2
552EOF
553
554test_expect_success 'exercise glob patterns with prefixes' '
555 git tag testtag-2 &&
556 test_when_finished "git tag -d testtag-2" &&
557 git for-each-ref --format="%(refname)" \
558 refs/tags/testtag "refs/tags/testtag-*" >actual &&
559 test_cmp expected actual
560'
561
562cat >expected <<\EOF
Taylor Blau8255dd82023-07-10 17:12:19 -0400563refs/tags/bar
564refs/tags/baz
565refs/tags/testtag
566EOF
567
568test_expect_success 'exercise patterns with prefix exclusions' '
569 for tag in foo/one foo/two foo/three bar baz
570 do
571 git tag "$tag" || return 1
572 done &&
573 test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
574 git for-each-ref --format="%(refname)" \
575 refs/tags/ --exclude=refs/tags/foo >actual &&
576 test_cmp expected actual
577'
578
579cat >expected <<\EOF
580refs/tags/bar
581refs/tags/baz
582refs/tags/foo/one
583refs/tags/testtag
584EOF
585
586test_expect_success 'exercise patterns with pattern exclusions' '
587 for tag in foo/one foo/two foo/three bar baz
588 do
589 git tag "$tag" || return 1
590 done &&
591 test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
592 git for-each-ref --format="%(refname)" \
593 refs/tags/ --exclude="refs/tags/foo/t*" >actual &&
594 test_cmp expected actual
595'
596
597cat >expected <<\EOF
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000598'refs/heads/main'
599'refs/remotes/origin/main'
Johannes Sixtc9ecf4f2007-12-06 13:24:39 +0100600'refs/tags/testtag'
601EOF
602
603test_expect_success 'Quoting style: shell' '
604 git for-each-ref --shell --format="%(refname)" >actual &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700605 test_cmp expected actual
Johannes Sixtc9ecf4f2007-12-06 13:24:39 +0100606'
607
608test_expect_success 'Quoting style: perl' '
609 git for-each-ref --perl --format="%(refname)" >actual &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700610 test_cmp expected actual
Johannes Sixtc9ecf4f2007-12-06 13:24:39 +0100611'
612
613test_expect_success 'Quoting style: python' '
614 git for-each-ref --python --format="%(refname)" >actual &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700615 test_cmp expected actual
Johannes Sixtc9ecf4f2007-12-06 13:24:39 +0100616'
617
618cat >expected <<\EOF
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000619"refs/heads/main"
620"refs/remotes/origin/main"
Johannes Sixtc9ecf4f2007-12-06 13:24:39 +0100621"refs/tags/testtag"
622EOF
623
624test_expect_success 'Quoting style: tcl' '
625 git for-each-ref --tcl --format="%(refname)" >actual &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700626 test_cmp expected actual
Johannes Sixtc9ecf4f2007-12-06 13:24:39 +0100627'
628
629for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
630 test_expect_success "more than one quoting style: $i" "
SZEDER Gábor2708ef42018-02-13 01:36:01 +0100631 test_must_fail git for-each-ref $i 2>err &&
632 grep '^error: more than one quoting style' err
Johannes Sixtc9ecf4f2007-12-06 13:24:39 +0100633 "
634done
Lars Hjemlic899a572007-11-10 17:47:54 +0100635
Ramkumar Ramachandrab28061c2013-11-18 23:09:11 +0530636test_expect_success 'setup for upstream:track[short]' '
637 test_commit two
638'
639
Jeff King1d094db2016-01-24 18:08:18 -0500640test_atom head upstream:track '[ahead 1]'
641test_atom head upstream:trackshort '>'
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530642test_atom head upstream:track,nobracket 'ahead 1'
643test_atom head upstream:nobracket,track 'ahead 1'
Damien Robertc646d092019-04-16 14:16:46 +0200644
645test_expect_success 'setup for push:track[short]' '
646 test_commit third &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000647 git update-ref refs/remotes/myfork/main main &&
648 git reset main~1
Damien Robertc646d092019-04-16 14:16:46 +0200649'
650
651test_atom head push:track '[behind 1]'
652test_atom head push:trackshort '<'
Ramkumar Ramachandrab28061c2013-11-18 23:09:11 +0530653
654test_expect_success 'Check that :track[short] cannot be used with other atoms' '
655 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
656 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
657'
658
Raphael Kubo da Costab6160d92015-01-05 11:58:55 +0200659test_expect_success 'Check that :track[short] works when upstream is invalid' '
660 cat >expected <<-\EOF &&
Karthik Nayakffd921d2017-01-10 14:19:40 +0530661 [gone]
Raphael Kubo da Costab6160d92015-01-05 11:58:55 +0200662
663 EOF
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000664 test_when_finished "git config branch.main.merge refs/heads/main" &&
665 git config branch.main.merge refs/heads/does-not-exist &&
Raphael Kubo da Costab6160d92015-01-05 11:58:55 +0200666 git for-each-ref \
667 --format="%(upstream:track)$LF%(upstream:trackshort)" \
668 refs/heads >actual &&
669 test_cmp expected actual
670'
671
Bert Wesarg7d66f212008-09-05 23:16:23 +0200672test_expect_success 'Check for invalid refname format' '
673 test_must_fail git for-each-ref --format="%(refname:INVALID)"
674'
675
Jeff King11b087a2017-07-13 11:09:32 -0400676test_expect_success 'set up color tests' '
677 cat >expected.color <<-EOF &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000678 $(git rev-parse --short refs/heads/main) <GREEN>main<RESET>
679 $(git rev-parse --short refs/remotes/myfork/main) <GREEN>myfork/main<RESET>
680 $(git rev-parse --short refs/remotes/origin/main) <GREEN>origin/main<RESET>
Jeff King11b087a2017-07-13 11:09:32 -0400681 $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
Damien Robertc646d092019-04-16 14:16:46 +0200682 $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
Jeff King11b087a2017-07-13 11:09:32 -0400683 $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
684 EOF
685 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
686 color_format="%(objectname:short) %(color:green)%(refname:short)"
687'
Ramkumar Ramachandrafddb74c2013-11-18 23:09:12 +0530688
Jeff King11b087a2017-07-13 11:09:32 -0400689test_expect_success TTY '%(color) shows color with a tty' '
Jeff Kinge4337492017-10-03 09:39:34 -0400690 test_terminal git for-each-ref --format="$color_format" >actual.raw &&
Jeff King097b6812017-07-13 10:58:41 -0400691 test_decode_color <actual.raw >actual &&
Jeff King11b087a2017-07-13 11:09:32 -0400692 test_cmp expected.color actual
693'
694
695test_expect_success '%(color) does not show color without tty' '
696 TERM=vt100 git for-each-ref --format="$color_format" >actual &&
697 test_cmp expected.bare actual
698'
699
Jeff King0c88bf52017-10-03 09:45:47 -0400700test_expect_success '--color can override tty check' '
701 git for-each-ref --color --format="$color_format" >actual.raw &&
Jeff King11b087a2017-07-13 11:09:32 -0400702 test_decode_color <actual.raw >actual &&
703 test_cmp expected.color actual
Ramkumar Ramachandrafddb74c2013-11-18 23:09:12 +0530704'
705
Jeff Kingb521fd12017-10-13 13:26:02 -0400706test_expect_success 'color.ui=always does not override tty check' '
707 git -c color.ui=always for-each-ref --format="$color_format" >actual &&
708 test_cmp expected.bare actual
709'
710
Kousik Sanagavarapuf5d18f82023-07-23 21:49:59 +0530711test_expect_success 'setup for describe atom tests' '
712 git init -b master describe-repo &&
713 (
714 cd describe-repo &&
715
716 test_commit --no-tag one &&
717 git tag tagone &&
718
719 test_commit --no-tag two &&
720 git tag -a -m "tag two" tagtwo
721 )
722'
723
724test_expect_success 'describe atom vs git describe' '
725 (
726 cd describe-repo &&
727
728 git for-each-ref --format="%(objectname)" \
729 refs/tags/ >obj &&
730 while read hash
731 do
732 if desc=$(git describe $hash)
733 then
734 : >expect-contains-good
735 else
736 : >expect-contains-bad
737 fi &&
738 echo "$hash $desc" || return 1
739 done <obj >expect &&
740 test_path_exists expect-contains-good &&
741 test_path_exists expect-contains-bad &&
742
743 git for-each-ref --format="%(objectname) %(describe)" \
744 refs/tags/ >actual 2>err &&
745 test_cmp expect actual &&
746 test_must_be_empty err
747 )
748'
749
750test_expect_success 'describe:tags vs describe --tags' '
751 (
752 cd describe-repo &&
753 git describe --tags >expect &&
754 git for-each-ref --format="%(describe:tags)" \
755 refs/heads/master >actual &&
756 test_cmp expect actual
757 )
758'
759
760test_expect_success 'describe:abbrev=... vs describe --abbrev=...' '
761 (
762 cd describe-repo &&
763
764 # Case 1: We have commits between HEAD and the most
765 # recent tag reachable from it
766 test_commit --no-tag file &&
767 git describe --abbrev=14 >expect &&
768 git for-each-ref --format="%(describe:abbrev=14)" \
769 refs/heads/master >actual &&
770 test_cmp expect actual &&
771
772 # Make sure the hash used is atleast 14 digits long
773 sed -e "s/^.*-g\([0-9a-f]*\)$/\1/" <actual >hexpart &&
774 test 15 -le $(wc -c <hexpart) &&
775
776 # Case 2: We have a tag at HEAD, describe directly gives
777 # the name of the tag
778 git tag -a -m tagged tagname &&
779 git describe --abbrev=14 >expect &&
780 git for-each-ref --format="%(describe:abbrev=14)" \
781 refs/heads/master >actual &&
782 test_cmp expect actual &&
783 test tagname = $(cat actual)
784 )
785'
786
787test_expect_success 'describe:match=... vs describe --match ...' '
788 (
789 cd describe-repo &&
790 git tag -a -m "tag foo" tag-foo &&
791 git describe --match "*-foo" >expect &&
792 git for-each-ref --format="%(describe:match="*-foo")" \
793 refs/heads/master >actual &&
794 test_cmp expect actual
795 )
796'
797
798test_expect_success 'describe:exclude:... vs describe --exclude ...' '
799 (
800 cd describe-repo &&
801 git tag -a -m "tag bar" tag-bar &&
802 git describe --exclude "*-bar" >expect &&
803 git for-each-ref --format="%(describe:exclude="*-bar")" \
804 refs/heads/master >actual &&
805 test_cmp expect actual
806 )
807'
808
809test_expect_success 'deref with describe atom' '
810 (
811 cd describe-repo &&
812 cat >expect <<-\EOF &&
813
814 tagname
815 tagname
816 tagname
817
818 tagtwo
819 EOF
820 git for-each-ref --format="%(*describe)" >actual &&
821 test_cmp expect actual
822 )
823'
824
825test_expect_success 'err on bad describe atom arg' '
826 (
827 cd describe-repo &&
828
829 # The bad arg is the only arg passed to describe atom
830 cat >expect <<-\EOF &&
831 fatal: unrecognized %(describe) argument: baz
832 EOF
833 test_must_fail git for-each-ref --format="%(describe:baz)" \
834 refs/heads/master 2>actual &&
835 test_cmp expect actual &&
836
837 # The bad arg is in the middle of the option string
838 # passed to the describe atom
839 cat >expect <<-\EOF &&
840 fatal: unrecognized %(describe) argument: qux=1,abbrev=14
841 EOF
842 test_must_fail git for-each-ref \
843 --format="%(describe:tags,qux=1,abbrev=14)" \
844 ref/heads/master 2>actual &&
845 test_cmp expect actual
846 )
847'
848
Bert Wesarg7d66f212008-09-05 23:16:23 +0200849cat >expected <<\EOF
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000850heads/main
851tags/main
Bert Wesarg7d66f212008-09-05 23:16:23 +0200852EOF
853
Bert Wesarg2bb98162009-04-13 12:25:47 +0200854test_expect_success 'Check ambiguous head and tag refs (strict)' '
855 git config --bool core.warnambiguousrefs true &&
Bert Wesarg7d66f212008-09-05 23:16:23 +0200856 git checkout -b newtag &&
857 echo "Using $datestamp" > one &&
858 git add one &&
859 git commit -m "Branch" &&
860 setdate_and_increment &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000861 git tag -m "Tagging at $datestamp" main &&
862 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
Bert Wesarg7d66f212008-09-05 23:16:23 +0200863 test_cmp expected actual
864'
865
866cat >expected <<\EOF
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000867heads/main
868main
Bert Wesarg2bb98162009-04-13 12:25:47 +0200869EOF
870
871test_expect_success 'Check ambiguous head and tag refs (loose)' '
872 git config --bool core.warnambiguousrefs false &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000873 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
Bert Wesarg2bb98162009-04-13 12:25:47 +0200874 test_cmp expected actual
875'
876
877cat >expected <<\EOF
Bert Wesarg7d66f212008-09-05 23:16:23 +0200878heads/ambiguous
879ambiguous
880EOF
881
Bert Wesarg2bb98162009-04-13 12:25:47 +0200882test_expect_success 'Check ambiguous head and tag refs II (loose)' '
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +0000883 git checkout main &&
Bert Wesarg7d66f212008-09-05 23:16:23 +0200884 git tag ambiguous testtag^0 &&
885 git branch ambiguous testtag^0 &&
886 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
887 test_cmp expected actual
888'
889
Mischa POSLAWSKY8b3f33e2019-08-17 23:51:07 +0200890test_expect_success 'create tag without tagger' '
891 git tag -a -m "Broken tag" taggerless &&
892 git tag -f taggerless $(git cat-file tag taggerless |
893 sed -e "/^tagger /d" |
Jeff King34959d82023-01-18 15:41:56 -0500894 git hash-object --literally --stdin -w -t tag)
Mischa POSLAWSKY8b3f33e2019-08-17 23:51:07 +0200895'
896
897test_atom refs/tags/taggerless type 'commit'
898test_atom refs/tags/taggerless tag 'taggerless'
899test_atom refs/tags/taggerless tagger ''
900test_atom refs/tags/taggerless taggername ''
901test_atom refs/tags/taggerless taggeremail ''
Hariom Vermab82445d2020-08-21 21:41:43 +0000902test_atom refs/tags/taggerless taggeremail:trim ''
903test_atom refs/tags/taggerless taggeremail:localpart ''
Mischa POSLAWSKY8b3f33e2019-08-17 23:51:07 +0200904test_atom refs/tags/taggerless taggerdate ''
905test_atom refs/tags/taggerless committer ''
906test_atom refs/tags/taggerless committername ''
907test_atom refs/tags/taggerless committeremail ''
Hariom Vermab82445d2020-08-21 21:41:43 +0000908test_atom refs/tags/taggerless committeremail:trim ''
909test_atom refs/tags/taggerless committeremail:localpart ''
Mischa POSLAWSKY8b3f33e2019-08-17 23:51:07 +0200910test_atom refs/tags/taggerless committerdate ''
911test_atom refs/tags/taggerless subject 'Broken tag'
912
Junio C Hamanoe276c262008-08-20 12:29:27 -0700913test_expect_success 'an unusual tag with an incomplete line' '
914
915 git tag -m "bogo" bogo &&
916 bogo=$(git cat-file tag bogo) &&
917 bogo=$(printf "%s" "$bogo" | git mktag) &&
918 git tag -f bogo "$bogo" &&
919 git for-each-ref --format "%(body)" refs/tags/bogo
920
921'
922
Jeff King7140c222011-09-07 13:43:28 -0400923test_expect_success 'create tag with subject and body content' '
924 cat >>msg <<-\EOF &&
925 the subject line
926
927 first body line
928 second body line
929 EOF
930 git tag -F msg subject-body
931'
932test_atom refs/tags/subject-body subject 'the subject line'
Hariom Verma905f0a42020-08-21 21:41:50 +0000933test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
Jeff King7140c222011-09-07 13:43:28 -0400934test_atom refs/tags/subject-body body 'first body line
935second body line
936'
937test_atom refs/tags/subject-body contents 'the subject line
938
939first body line
940second body line
941'
942
Jeff King7f6e2752011-09-07 13:44:56 -0400943test_expect_success 'create tag with multiline subject' '
944 cat >msg <<-\EOF &&
945 first subject line
946 second subject line
947
948 first body line
949 second body line
950 EOF
951 git tag -F msg multiline
952'
953test_atom refs/tags/multiline subject 'first subject line second subject line'
Hariom Verma905f0a42020-08-21 21:41:50 +0000954test_atom refs/tags/multiline subject:sanitize 'first-subject-line-second-subject-line'
Michał Górnye2b23972011-09-07 13:46:08 -0400955test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
Jeff King7f6e2752011-09-07 13:44:56 -0400956test_atom refs/tags/multiline body 'first body line
957second body line
958'
Michał Górnye2b23972011-09-07 13:46:08 -0400959test_atom refs/tags/multiline contents:body 'first body line
960second body line
961'
962test_atom refs/tags/multiline contents:signature ''
Jeff King7f6e2752011-09-07 13:44:56 -0400963test_atom refs/tags/multiline contents 'first subject line
964second subject line
965
966first body line
967second body line
968'
969
Michał Górnye2b23972011-09-07 13:46:08 -0400970test_expect_success GPG 'create signed tags' '
971 git tag -s -m "" signed-empty &&
972 git tag -s -m "subject line" signed-short &&
973 cat >msg <<-\EOF &&
974 subject line
975
976 body contents
977 EOF
978 git tag -s -F msg signed-long
979'
980
981sig='-----BEGIN PGP SIGNATURE-----
982-----END PGP SIGNATURE-----
983'
984
985PREREQ=GPG
986test_atom refs/tags/signed-empty subject ''
Hariom Verma905f0a42020-08-21 21:41:50 +0000987test_atom refs/tags/signed-empty subject:sanitize ''
Michał Górnye2b23972011-09-07 13:46:08 -0400988test_atom refs/tags/signed-empty contents:subject ''
989test_atom refs/tags/signed-empty body "$sig"
990test_atom refs/tags/signed-empty contents:body ''
991test_atom refs/tags/signed-empty contents:signature "$sig"
992test_atom refs/tags/signed-empty contents "$sig"
993
ZheNing Hubd0708c2021-07-26 03:26:47 +0000994test_expect_success GPG 'basic atom: refs/tags/signed-empty raw' '
995 git cat-file tag refs/tags/signed-empty >expected &&
996 git for-each-ref --format="%(raw)" refs/tags/signed-empty >actual &&
997 sanitize_pgp <expected >expected.clean &&
998 echo >>expected.clean &&
999 sanitize_pgp <actual >actual.clean &&
1000 test_cmp expected.clean actual.clean
1001'
1002
Michał Górnye2b23972011-09-07 13:46:08 -04001003test_atom refs/tags/signed-short subject 'subject line'
Hariom Verma905f0a42020-08-21 21:41:50 +00001004test_atom refs/tags/signed-short subject:sanitize 'subject-line'
Michał Górnye2b23972011-09-07 13:46:08 -04001005test_atom refs/tags/signed-short contents:subject 'subject line'
1006test_atom refs/tags/signed-short body "$sig"
1007test_atom refs/tags/signed-short contents:body ''
1008test_atom refs/tags/signed-short contents:signature "$sig"
1009test_atom refs/tags/signed-short contents "subject line
1010$sig"
1011
ZheNing Hubd0708c2021-07-26 03:26:47 +00001012test_expect_success GPG 'basic atom: refs/tags/signed-short raw' '
1013 git cat-file tag refs/tags/signed-short >expected &&
1014 git for-each-ref --format="%(raw)" refs/tags/signed-short >actual &&
1015 sanitize_pgp <expected >expected.clean &&
1016 echo >>expected.clean &&
1017 sanitize_pgp <actual >actual.clean &&
1018 test_cmp expected.clean actual.clean
1019'
1020
Michał Górnye2b23972011-09-07 13:46:08 -04001021test_atom refs/tags/signed-long subject 'subject line'
Hariom Verma905f0a42020-08-21 21:41:50 +00001022test_atom refs/tags/signed-long subject:sanitize 'subject-line'
Michał Górnye2b23972011-09-07 13:46:08 -04001023test_atom refs/tags/signed-long contents:subject 'subject line'
1024test_atom refs/tags/signed-long body "body contents
1025$sig"
1026test_atom refs/tags/signed-long contents:body 'body contents
1027'
1028test_atom refs/tags/signed-long contents:signature "$sig"
1029test_atom refs/tags/signed-long contents "subject line
1030
1031body contents
1032$sig"
1033
ZheNing Hubd0708c2021-07-26 03:26:47 +00001034test_expect_success GPG 'basic atom: refs/tags/signed-long raw' '
1035 git cat-file tag refs/tags/signed-long >expected &&
1036 git for-each-ref --format="%(raw)" refs/tags/signed-long >actual &&
1037 sanitize_pgp <expected >expected.clean &&
1038 echo >>expected.clean &&
1039 sanitize_pgp <actual >actual.clean &&
1040 test_cmp expected.clean actual.clean
1041'
1042
Christian Couder6e2ef8e2020-07-10 18:47:38 +02001043test_expect_success 'set up refs pointing to tree and blob' '
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001044 git update-ref refs/mytrees/first refs/heads/main^{tree} &&
1045 git update-ref refs/myblobs/first refs/heads/main:one
Christian Couder6e2ef8e2020-07-10 18:47:38 +02001046'
1047
1048test_atom refs/mytrees/first subject ""
1049test_atom refs/mytrees/first contents:subject ""
1050test_atom refs/mytrees/first body ""
1051test_atom refs/mytrees/first contents:body ""
1052test_atom refs/mytrees/first contents:signature ""
1053test_atom refs/mytrees/first contents ""
1054
ZheNing Hubd0708c2021-07-26 03:26:47 +00001055test_expect_success 'basic atom: refs/mytrees/first raw' '
1056 git cat-file tree refs/mytrees/first >expected &&
1057 echo >>expected &&
1058 git for-each-ref --format="%(raw)" refs/mytrees/first >actual &&
1059 test_cmp expected actual &&
1060 git cat-file -s refs/mytrees/first >expected &&
1061 git for-each-ref --format="%(raw:size)" refs/mytrees/first >actual &&
1062 test_cmp expected actual
1063'
1064
Christian Couder6e2ef8e2020-07-10 18:47:38 +02001065test_atom refs/myblobs/first subject ""
1066test_atom refs/myblobs/first contents:subject ""
1067test_atom refs/myblobs/first body ""
1068test_atom refs/myblobs/first contents:body ""
1069test_atom refs/myblobs/first contents:signature ""
1070test_atom refs/myblobs/first contents ""
1071
ZheNing Hubd0708c2021-07-26 03:26:47 +00001072test_expect_success 'basic atom: refs/myblobs/first raw' '
1073 git cat-file blob refs/myblobs/first >expected &&
1074 echo >>expected &&
1075 git for-each-ref --format="%(raw)" refs/myblobs/first >actual &&
1076 test_cmp expected actual &&
1077 git cat-file -s refs/myblobs/first >expected &&
1078 git for-each-ref --format="%(raw:size)" refs/myblobs/first >actual &&
1079 test_cmp expected actual
1080'
1081
1082test_expect_success 'set up refs pointing to binary blob' '
1083 printf "a\0b\0c" >blob1 &&
1084 printf "a\0c\0b" >blob2 &&
1085 printf "\0a\0b\0c" >blob3 &&
1086 printf "abc" >blob4 &&
1087 printf "\0 \0 \0 " >blob5 &&
1088 printf "\0 \0a\0 " >blob6 &&
1089 printf " " >blob7 &&
1090 >blob8 &&
1091 obj=$(git hash-object -w blob1) &&
1092 git update-ref refs/myblobs/blob1 "$obj" &&
1093 obj=$(git hash-object -w blob2) &&
1094 git update-ref refs/myblobs/blob2 "$obj" &&
1095 obj=$(git hash-object -w blob3) &&
1096 git update-ref refs/myblobs/blob3 "$obj" &&
1097 obj=$(git hash-object -w blob4) &&
1098 git update-ref refs/myblobs/blob4 "$obj" &&
1099 obj=$(git hash-object -w blob5) &&
1100 git update-ref refs/myblobs/blob5 "$obj" &&
1101 obj=$(git hash-object -w blob6) &&
1102 git update-ref refs/myblobs/blob6 "$obj" &&
1103 obj=$(git hash-object -w blob7) &&
1104 git update-ref refs/myblobs/blob7 "$obj" &&
1105 obj=$(git hash-object -w blob8) &&
1106 git update-ref refs/myblobs/blob8 "$obj"
1107'
1108
1109test_expect_success 'Verify sorts with raw' '
1110 cat >expected <<-EOF &&
1111 refs/myblobs/blob8
1112 refs/myblobs/blob5
1113 refs/myblobs/blob6
1114 refs/myblobs/blob3
1115 refs/myblobs/blob7
1116 refs/mytrees/first
1117 refs/myblobs/first
1118 refs/myblobs/blob1
1119 refs/myblobs/blob2
1120 refs/myblobs/blob4
1121 refs/heads/main
1122 EOF
1123 git for-each-ref --format="%(refname)" --sort=raw \
1124 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
1125 test_cmp expected actual
1126'
1127
1128test_expect_success 'Verify sorts with raw:size' '
1129 cat >expected <<-EOF &&
1130 refs/myblobs/blob8
ZheNing Hubd0708c2021-07-26 03:26:47 +00001131 refs/myblobs/blob7
ZheNing Hubd0708c2021-07-26 03:26:47 +00001132 refs/myblobs/blob4
1133 refs/myblobs/blob1
1134 refs/myblobs/blob2
1135 refs/myblobs/blob3
1136 refs/myblobs/blob5
1137 refs/myblobs/blob6
Kousik Sanagavarapu6d79cd82023-09-02 14:30:39 +05301138 refs/myblobs/first
ZheNing Hubd0708c2021-07-26 03:26:47 +00001139 refs/mytrees/first
Kousik Sanagavarapu6d79cd82023-09-02 14:30:39 +05301140 refs/heads/main
ZheNing Hubd0708c2021-07-26 03:26:47 +00001141 EOF
1142 git for-each-ref --format="%(refname)" --sort=raw:size \
1143 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
1144 test_cmp expected actual
1145'
1146
1147test_expect_success 'validate raw atom with %(if:equals)' '
1148 cat >expected <<-EOF &&
1149 not equals
1150 not equals
1151 not equals
1152 not equals
1153 not equals
1154 not equals
1155 refs/myblobs/blob4
1156 not equals
1157 not equals
1158 not equals
1159 not equals
1160 not equals
1161 EOF
1162 git for-each-ref --format="%(if:equals=abc)%(raw)%(then)%(refname)%(else)not equals%(end)" \
1163 refs/myblobs/ refs/heads/ >actual &&
1164 test_cmp expected actual
1165'
1166
1167test_expect_success 'validate raw atom with %(if:notequals)' '
1168 cat >expected <<-EOF &&
1169 refs/heads/ambiguous
1170 refs/heads/main
1171 refs/heads/newtag
1172 refs/myblobs/blob1
1173 refs/myblobs/blob2
1174 refs/myblobs/blob3
1175 equals
1176 refs/myblobs/blob5
1177 refs/myblobs/blob6
1178 refs/myblobs/blob7
1179 refs/myblobs/blob8
1180 refs/myblobs/first
1181 EOF
1182 git for-each-ref --format="%(if:notequals=abc)%(raw)%(then)%(refname)%(else)equals%(end)" \
1183 refs/myblobs/ refs/heads/ >actual &&
1184 test_cmp expected actual
1185'
1186
1187test_expect_success 'empty raw refs with %(if)' '
1188 cat >expected <<-EOF &&
1189 refs/myblobs/blob1 not empty
1190 refs/myblobs/blob2 not empty
1191 refs/myblobs/blob3 not empty
1192 refs/myblobs/blob4 not empty
1193 refs/myblobs/blob5 not empty
1194 refs/myblobs/blob6 not empty
1195 refs/myblobs/blob7 empty
1196 refs/myblobs/blob8 empty
1197 refs/myblobs/first not empty
1198 EOF
1199 git for-each-ref --format="%(refname) %(if)%(raw)%(then)not empty%(else)empty%(end)" \
1200 refs/myblobs/ >actual &&
1201 test_cmp expected actual
1202'
1203
1204test_expect_success '%(raw) with --python must fail' '
1205 test_must_fail git for-each-ref --format="%(raw)" --python
1206'
1207
1208test_expect_success '%(raw) with --tcl must fail' '
1209 test_must_fail git for-each-ref --format="%(raw)" --tcl
1210'
1211
ZheNing Hu7121c4d2021-07-26 03:26:48 +00001212test_expect_success '%(raw) with --perl' '
1213 git for-each-ref --format="\$name= %(raw);
1214print \"\$name\"" refs/myblobs/blob1 --perl | perl >actual &&
1215 cmp blob1 actual &&
1216 git for-each-ref --format="\$name= %(raw);
1217print \"\$name\"" refs/myblobs/blob3 --perl | perl >actual &&
1218 cmp blob3 actual &&
1219 git for-each-ref --format="\$name= %(raw);
1220print \"\$name\"" refs/myblobs/blob8 --perl | perl >actual &&
1221 cmp blob8 actual &&
1222 git for-each-ref --format="\$name= %(raw);
1223print \"\$name\"" refs/myblobs/first --perl | perl >actual &&
1224 cmp one actual &&
1225 git cat-file tree refs/mytrees/first > expected &&
1226 git for-each-ref --format="\$name= %(raw);
1227print \"\$name\"" refs/mytrees/first --perl | perl >actual &&
1228 cmp expected actual
ZheNing Hubd0708c2021-07-26 03:26:47 +00001229'
1230
1231test_expect_success '%(raw) with --shell must fail' '
1232 test_must_fail git for-each-ref --format="%(raw)" --shell
1233'
1234
1235test_expect_success '%(raw) with --shell and --sort=raw must fail' '
1236 test_must_fail git for-each-ref --format="%(raw)" --sort=raw --shell
1237'
1238
1239test_expect_success '%(raw:size) with --shell' '
Eric Sunshineefe47c82021-12-09 00:11:02 -05001240 git for-each-ref --format="%(raw:size)" | sed "s/^/$SQ/;s/$/$SQ/" >expect &&
ZheNing Hubd0708c2021-07-26 03:26:47 +00001241 git for-each-ref --format="%(raw:size)" --shell >actual &&
1242 test_cmp expect actual
1243'
1244
1245test_expect_success 'for-each-ref --format compare with cat-file --batch' '
1246 git rev-parse refs/mytrees/first | git cat-file --batch >expected &&
1247 git for-each-ref --format="%(objectname) %(objecttype) %(objectsize)
1248%(raw)" refs/mytrees/first >actual &&
1249 test_cmp expected actual
1250'
1251
Kousik Sanagavarapu6d79cd82023-09-02 14:30:39 +05301252test_expect_success 'verify sorts with contents:size' '
1253 cat >expect <<-\EOF &&
1254 refs/heads/main
1255 refs/heads/newtag
1256 refs/heads/ambiguous
1257 EOF
1258 git for-each-ref --format="%(refname)" \
1259 --sort=contents:size refs/heads/ >actual &&
1260 test_cmp expect actual
1261'
1262
Jeff King7c5045f2020-05-03 05:13:09 -04001263test_expect_success 'set up multiple-sort tags' '
1264 for when in 100000 200000
1265 do
1266 for email in user1 user2
1267 do
1268 for ref in ref1 ref2
1269 do
1270 GIT_COMMITTER_DATE="@$when +0000" \
1271 GIT_COMMITTER_EMAIL="$email@example.com" \
1272 git tag -m "tag $ref-$when-$email" \
1273 multi-$ref-$when-$email || return 1
1274 done
1275 done
1276 done
1277'
Kacper Kornet912072d2012-08-21 09:46:06 +02001278
Kacper Kornet3b512222012-08-21 09:47:26 +02001279test_expect_success 'Verify sort with multiple keys' '
Jeff King7c5045f2020-05-03 05:13:09 -04001280 cat >expected <<-\EOF &&
1281 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1282 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1283 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1284 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1285 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1286 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1287 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1288 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1289 EOF
1290 git for-each-ref \
1291 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1292 --sort=-refname \
1293 --sort=taggeremail \
1294 --sort=taggerdate \
1295 "refs/tags/multi-*" >actual &&
Kacper Kornet912072d2012-08-21 09:46:06 +02001296 test_cmp expected actual
1297'
Junio C Hamano84679d42016-11-18 15:21:12 -08001298
Jeff King7c5045f2020-05-03 05:13:09 -04001299test_expect_success 'equivalent sorts fall back on refname' '
1300 cat >expected <<-\EOF &&
1301 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1302 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1303 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1304 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1305 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1306 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1307 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1308 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1309 EOF
1310 git for-each-ref \
1311 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1312 --sort=taggerdate \
1313 "refs/tags/multi-*" >actual &&
1314 test_cmp expected actual
1315'
Karthik Nayak01f95822017-01-10 14:19:42 +05301316
Junio C Hamano98e7ab62021-10-20 12:23:53 -07001317test_expect_success '--no-sort cancels the previous sort keys' '
1318 cat >expected <<-\EOF &&
1319 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1320 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1321 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1322 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1323 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1324 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1325 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1326 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1327 EOF
1328 git for-each-ref \
1329 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1330 --sort=-refname \
1331 --sort=taggeremail \
1332 --no-sort \
1333 --sort=taggerdate \
1334 "refs/tags/multi-*" >actual &&
1335 test_cmp expected actual
1336'
1337
Victoria Dye56d26ad2023-11-14 19:53:49 +00001338test_expect_success '--no-sort without subsequent --sort prints expected refs' '
1339 cat >expected <<-\EOF &&
1340 refs/tags/multi-ref1-100000-user1
1341 refs/tags/multi-ref1-100000-user2
1342 refs/tags/multi-ref1-200000-user1
1343 refs/tags/multi-ref1-200000-user2
1344 refs/tags/multi-ref2-100000-user1
1345 refs/tags/multi-ref2-100000-user2
1346 refs/tags/multi-ref2-200000-user1
1347 refs/tags/multi-ref2-200000-user2
1348 EOF
1349
1350 # Sort the results with `sort` for a consistent comparison against
1351 # expected
1352 git for-each-ref \
1353 --format="%(refname)" \
1354 --no-sort \
1355 "refs/tags/multi-*" | sort >actual &&
1356 test_cmp expected actual
1357'
1358
Victoria Dye46176d72024-02-08 01:57:19 +00001359test_expect_success 'set up custom date sorting' '
1360 # Dates:
1361 # - Wed Feb 07 2024 21:34:20 +0000
1362 # - Tue Dec 14 1999 00:05:22 +0000
1363 # - Fri Jun 04 2021 11:26:51 +0000
1364 # - Mon Jan 22 2007 16:44:01 GMT+0000
1365 i=1 &&
1366 for when in 1707341660 945129922 1622806011 1169484241
1367 do
1368 GIT_COMMITTER_DATE="@$when +0000" \
1369 GIT_COMMITTER_EMAIL="user@example.com" \
1370 git tag -m "tag $when" custom-dates-$i &&
1371 i=$(($i+1)) || return 1
1372 done
1373'
1374
1375test_expect_success 'sort by date defaults to full timestamp' '
1376 cat >expected <<-\EOF &&
1377 945129922 refs/tags/custom-dates-2
1378 1169484241 refs/tags/custom-dates-4
1379 1622806011 refs/tags/custom-dates-3
1380 1707341660 refs/tags/custom-dates-1
1381 EOF
1382
1383 git for-each-ref \
1384 --format="%(creatordate:unix) %(refname)" \
1385 --sort=creatordate \
1386 "refs/tags/custom-dates-*" >actual &&
1387 test_cmp expected actual
1388'
1389
1390test_expect_success 'sort by custom date format' '
1391 cat >expected <<-\EOF &&
1392 00:05:22 refs/tags/custom-dates-2
1393 11:26:51 refs/tags/custom-dates-3
1394 16:44:01 refs/tags/custom-dates-4
1395 21:34:20 refs/tags/custom-dates-1
1396 EOF
1397
1398 git for-each-ref \
1399 --format="%(creatordate:format:%H:%M:%S) %(refname)" \
1400 --sort="creatordate:format:%H:%M:%S" \
1401 "refs/tags/custom-dates-*" >actual &&
1402 test_cmp expected actual
1403'
1404
Junio C Hamano84679d42016-11-18 15:21:12 -08001405test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001406 test_when_finished "git checkout main" &&
Junio C Hamano84679d42016-11-18 15:21:12 -08001407 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1408 sed -e "s/^\* / /" actual >expect &&
Jeff Kingf0252ca2017-02-27 04:29:31 -05001409 git checkout --orphan orphaned-branch &&
Junio C Hamano84679d42016-11-18 15:21:12 -08001410 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1411 test_cmp expect actual
1412'
1413
Jacob Kellerb1d31c82016-11-18 16:58:15 -08001414cat >trailers <<EOF
1415Reviewed-by: A U Thor <author@example.com>
1416Signed-off-by: A U Thor <author@example.com>
Taylor Blau624b44d2017-10-01 22:25:22 -07001417[ v2 updated patch description ]
1418Acked-by: A U Thor
1419 <author@example.com>
Jacob Kellerb1d31c82016-11-18 16:58:15 -08001420EOF
1421
Taylor Blau67a20a02017-10-01 22:25:23 -07001422unfold () {
1423 perl -0pe 's/\n\s+/ /g'
1424}
Taylor Blau624b44d2017-10-01 22:25:22 -07001425
1426test_expect_success 'set up trailers for next test' '
Jacob Kellerb1d31c82016-11-18 16:58:15 -08001427 echo "Some contents" > two &&
1428 git add two &&
Taylor Blau624b44d2017-10-01 22:25:22 -07001429 git commit -F - <<-EOF
Jacob Kellerb1d31c82016-11-18 16:58:15 -08001430 trailers: this commit message has trailers
1431
1432 Some message contents
1433
1434 $(cat trailers)
1435 EOF
Taylor Blau624b44d2017-10-01 22:25:22 -07001436'
1437
Hariom Verma727331d2021-02-13 01:52:40 +00001438test_trailer_option () {
1439 title=$1 option=$2
1440 cat >expect
1441 test_expect_success "$title" '
1442 git for-each-ref --format="%($option)" refs/heads/main >actual &&
1443 test_cmp expect actual &&
1444 git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
1445 test_cmp expect actual
1446 '
1447}
Taylor Blau67a20a02017-10-01 22:25:23 -07001448
Hariom Verma727331d2021-02-13 01:52:40 +00001449test_trailer_option '%(trailers:unfold) unfolds trailers' \
1450 'trailers:unfold' <<-EOF
1451 $(unfold <trailers)
Taylor Blau67a20a02017-10-01 22:25:23 -07001452
Hariom Verma727331d2021-02-13 01:52:40 +00001453 EOF
Taylor Blau7a5edbd2017-10-01 22:25:24 -07001454
Hariom Verma727331d2021-02-13 01:52:40 +00001455test_trailer_option '%(trailers:only) shows only "key: value" trailers' \
1456 'trailers:only' <<-EOF
1457 $(grep -v patch.description <trailers)
1458
1459 EOF
1460
Hariom Vermaee82a482021-02-13 01:52:43 +00001461test_trailer_option '%(trailers:only=no,only=true) shows only "key: value" trailers' \
1462 'trailers:only=no,only=true' <<-EOF
1463 $(grep -v patch.description <trailers)
1464
1465 EOF
1466
1467test_trailer_option '%(trailers:only=yes) shows only "key: value" trailers' \
1468 'trailers:only=yes' <<-EOF
1469 $(grep -v patch.description <trailers)
1470
1471 EOF
1472
1473test_trailer_option '%(trailers:only=no) shows all trailers' \
1474 'trailers:only=no' <<-EOF
1475 $(cat trailers)
1476
1477 EOF
1478
Hariom Verma727331d2021-02-13 01:52:40 +00001479test_trailer_option '%(trailers:only) and %(trailers:unfold) work together' \
1480 'trailers:only,unfold' <<-EOF
1481 $(grep -v patch.description <trailers | unfold)
1482
1483 EOF
1484
1485test_trailer_option '%(trailers:unfold) and %(trailers:only) work together' \
1486 'trailers:unfold,only' <<-EOF
1487 $(grep -v patch.description <trailers | unfold)
1488
1489 EOF
1490
Hariom Vermaee82a482021-02-13 01:52:43 +00001491test_trailer_option '%(trailers:key=foo) shows that trailer' \
1492 'trailers:key=Signed-off-by' <<-EOF
1493 Signed-off-by: A U Thor <author@example.com>
1494
1495 EOF
1496
1497test_trailer_option '%(trailers:key=foo) is case insensitive' \
1498 'trailers:key=SiGned-oFf-bY' <<-EOF
1499 Signed-off-by: A U Thor <author@example.com>
1500
1501 EOF
1502
1503test_trailer_option '%(trailers:key=foo:) trailing colon also works' \
1504 'trailers:key=Signed-off-by:' <<-EOF
1505 Signed-off-by: A U Thor <author@example.com>
1506
1507 EOF
1508
1509test_trailer_option '%(trailers:key=foo) multiple keys' \
1510 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF
1511 Reviewed-by: A U Thor <author@example.com>
1512 Signed-off-by: A U Thor <author@example.com>
1513
1514 EOF
1515
1516test_trailer_option '%(trailers:key=nonexistent) becomes empty' \
1517 'trailers:key=Shined-off-by:' <<-EOF
1518
1519 EOF
1520
1521test_trailer_option '%(trailers:key=foo) handles multiple lines even if folded' \
1522 'trailers:key=Acked-by' <<-EOF
1523 $(grep -v patch.description <trailers | grep -v Signed-off-by | grep -v Reviewed-by)
1524
1525 EOF
1526
1527test_trailer_option '%(trailers:key=foo,unfold) properly unfolds' \
1528 'trailers:key=Signed-Off-by,unfold' <<-EOF
1529 $(unfold <trailers | grep Signed-off-by)
1530
1531 EOF
1532
1533test_trailer_option '%(trailers:key=foo,only=no) also includes nontrailer lines' \
1534 'trailers:key=Signed-off-by,only=no' <<-EOF
1535 Signed-off-by: A U Thor <author@example.com>
1536 $(grep patch.description <trailers)
1537
1538 EOF
1539
1540test_trailer_option '%(trailers:key=foo,valueonly) shows only value' \
1541 'trailers:key=Signed-off-by,valueonly' <<-EOF
1542 A U Thor <author@example.com>
1543
1544 EOF
1545
1546test_trailer_option '%(trailers:separator) changes separator' \
1547 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1548 Reviewed-by: A U Thor <author@example.com>,Signed-off-by: A U Thor <author@example.com>
1549 EOF
1550
1551test_trailer_option '%(trailers:key_value_separator) changes key-value separator' \
1552 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1553 Reviewed-by,A U Thor <author@example.com>
1554 Signed-off-by,A U Thor <author@example.com>
1555
1556 EOF
1557
1558test_trailer_option '%(trailers:separator,key_value_separator) changes both separators' \
1559 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1560 Reviewed-by,A U Thor <author@example.com>,Signed-off-by,A U Thor <author@example.com>
1561 EOF
1562
Hariom Verma727331d2021-02-13 01:52:40 +00001563test_failing_trailer_option () {
1564 title=$1 option=$2
1565 cat >expect
1566 test_expect_success "$title" '
1567 # error message cannot be checked under i18n
1568 test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
Ævar Arnfjörð Bjarmasonfeeb03b2021-04-13 14:19:51 +02001569 test_cmp expect actual &&
Hariom Verma727331d2021-02-13 01:52:40 +00001570 test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
Ævar Arnfjörð Bjarmasonfeeb03b2021-04-13 14:19:51 +02001571 test_cmp expect actual
Hariom Verma727331d2021-02-13 01:52:40 +00001572 '
1573}
1574
1575test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \
1576 'trailers:unsupported' <<-\EOF
Taylor Blau67a20a02017-10-01 22:25:23 -07001577 fatal: unknown %(trailers) argument: unsupported
1578 EOF
Taylor Blau67a20a02017-10-01 22:25:23 -07001579
Hariom Vermaee82a482021-02-13 01:52:43 +00001580test_failing_trailer_option '%(trailers:key) without value is error' \
1581 'trailers:key' <<-\EOF
1582 fatal: expected %(trailers:key=<value>)
1583 EOF
1584
Hariom Verma2c22e102020-08-21 21:06:14 +00001585test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
Taylor Blau7a5edbd2017-10-01 22:25:24 -07001586 cat >expect <<-EOF &&
Hariom Verma2c22e102020-08-21 21:06:14 +00001587 fatal: unrecognized %(contents) argument: trailersonly
Taylor Blau7a5edbd2017-10-01 22:25:24 -07001588 EOF
Hariom Verma2c22e102020-08-21 21:06:14 +00001589 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
Ævar Arnfjörð Bjarmasonfeeb03b2021-04-13 14:19:51 +02001590 test_cmp expect actual
Taylor Blau7a5edbd2017-10-01 22:25:24 -07001591'
1592
Taylor Blau624b44d2017-10-01 22:25:22 -07001593test_expect_success 'basic atom: head contents:trailers' '
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001594 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
Jacob Kellerb1d31c82016-11-18 16:58:15 -08001595 sanitize_pgp <actual >actual.clean &&
1596 # git for-each-ref ends with a blank line
1597 cat >expect <<-EOF &&
1598 $(cat trailers)
1599
1600 EOF
1601 test_cmp expect actual.clean
1602'
1603
ZheNing Hub9dee072021-07-26 03:26:50 +00001604test_expect_success 'basic atom: rest must fail' '
1605 test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
1606'
1607
Jeff Kinga33d0fa2022-12-14 11:19:43 -05001608test_expect_success 'HEAD atom does not take arguments' '
1609 test_must_fail git for-each-ref --format="%(HEAD:foo)" 2>err &&
1610 echo "fatal: %(HEAD) does not take arguments" >expect &&
1611 test_cmp expect err
1612'
1613
Jeff Kingdda4fc12022-12-14 11:20:19 -05001614test_expect_success 'subject atom rejects unknown arguments' '
1615 test_must_fail git for-each-ref --format="%(subject:foo)" 2>err &&
1616 echo "fatal: unrecognized %(subject) argument: foo" >expect &&
1617 test_cmp expect err
1618'
1619
Jeff King1955ef12022-12-14 11:23:53 -05001620test_expect_success 'refname atom rejects unknown arguments' '
1621 test_must_fail git for-each-ref --format="%(refname:foo)" 2>err &&
1622 echo "fatal: unrecognized %(refname) argument: foo" >expect &&
1623 test_cmp expect err
1624'
1625
Jeff Kinge5fba5d2018-08-22 20:50:17 -04001626test_expect_success 'trailer parsing not fooled by --- line' '
1627 git commit --allow-empty -F - <<-\EOF &&
1628 this is the subject
1629
1630 This is the body. The message has a "---" line which would confuse a
1631 message+patch parser. But here we know we have only a commit message,
1632 so we get it right.
1633
1634 trailer: wrong
1635 ---
1636 This is more body.
1637
1638 trailer: right
1639 EOF
1640
1641 {
1642 echo "trailer: right" &&
1643 echo
1644 } >expect &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001645 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
Jeff Kinge5fba5d2018-08-22 20:50:17 -04001646 test_cmp expect actual
1647'
1648
Karthik Nayak01f95822017-01-10 14:19:42 +05301649test_expect_success 'Add symbolic ref for the following tests' '
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001650 git symbolic-ref refs/heads/sym refs/heads/main
Karthik Nayak01f95822017-01-10 14:19:42 +05301651'
1652
1653cat >expected <<EOF
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001654refs/heads/main
Karthik Nayak01f95822017-01-10 14:19:42 +05301655EOF
1656
1657test_expect_success 'Verify usage of %(symref) atom' '
1658 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
1659 test_cmp expected actual
1660'
1661
1662cat >expected <<EOF
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001663heads/main
Karthik Nayak01f95822017-01-10 14:19:42 +05301664EOF
1665
1666test_expect_success 'Verify usage of %(symref:short) atom' '
1667 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
1668 test_cmp expected actual
1669'
1670
Karthik Nayaka7984102017-01-10 14:19:44 +05301671cat >expected <<EOF
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001672main
1673heads/main
Karthik Nayaka7984102017-01-10 14:19:44 +05301674EOF
1675
Karthik Nayak17938f12017-01-10 14:19:46 +05301676test_expect_success 'Verify usage of %(symref:lstrip) atom' '
1677 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +05301678 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
Junio C Hamano44a6b6c2017-02-07 11:50:34 -08001679 test_cmp expected actual &&
1680
1681 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
1682 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
Karthik Nayaka7984102017-01-10 14:19:44 +05301683 test_cmp expected actual
1684'
1685
Karthik Nayak1a347282017-01-10 14:19:49 +05301686cat >expected <<EOF
1687refs
1688refs/heads
1689EOF
1690
1691test_expect_success 'Verify usage of %(symref:rstrip) atom' '
1692 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
1693 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
1694 test_cmp expected actual
1695'
1696
Johannes Schindelin1b586862017-11-07 17:31:14 +01001697test_expect_success ':remotename and :remoteref' '
1698 git init remote-tests &&
1699 (
1700 cd remote-tests &&
1701 test_commit initial &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001702 git branch -M main &&
Johannes Schindelin1b586862017-11-07 17:31:14 +01001703 git remote add from fifth.coffee:blub &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001704 git config branch.main.remote from &&
1705 git config branch.main.merge refs/heads/stable &&
Johannes Schindelin1b586862017-11-07 17:31:14 +01001706 git remote add to southridge.audio:repo &&
1707 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001708 git config branch.main.pushRemote to &&
Johannes Schindelin1b586862017-11-07 17:31:14 +01001709 for pair in "%(upstream)=refs/remotes/from/stable" \
1710 "%(upstream:remotename)=from" \
1711 "%(upstream:remoteref)=refs/heads/stable" \
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001712 "%(push)=refs/remotes/to/pushed/main" \
Johannes Schindelin1b586862017-11-07 17:31:14 +01001713 "%(push:remotename)=to" \
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001714 "%(push:remoteref)=refs/heads/pushed/main"
Johannes Schindelin1b586862017-11-07 17:31:14 +01001715 do
1716 echo "${pair#*=}" >expect &&
1717 git for-each-ref --format="${pair%=*}" \
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001718 refs/heads/main >actual &&
Eric Sunshine0c51d6b2021-12-09 00:11:15 -05001719 test_cmp expect actual || exit 1
Johannes Schindelin1b586862017-11-07 17:31:14 +01001720 done &&
1721 git branch push-simple &&
1722 git config branch.push-simple.pushRemote from &&
1723 actual="$(git for-each-ref \
1724 --format="%(push:remotename),%(push:remoteref)" \
1725 refs/heads/push-simple)" &&
1726 test from, = "$actual"
1727 )
1728'
1729
Jeff Kinge674eb22018-07-02 17:12:42 -04001730test_expect_success 'for-each-ref --ignore-case ignores case' '
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001731 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
Ævar Arnfjörð Bjarmasond3c67512018-07-27 17:48:11 +00001732 test_must_be_empty actual &&
Jeff Kingee0f3e22018-07-02 17:11:23 -04001733
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001734 echo refs/heads/main >expect &&
Jeff Kingee0f3e22018-07-02 17:11:23 -04001735 git for-each-ref --format="%(refname)" --ignore-case \
Johannes Schindelin8f19c9f2020-12-06 13:15:59 +00001736 refs/heads/MAIN >actual &&
Jeff Kingee0f3e22018-07-02 17:11:23 -04001737 test_cmp expect actual
1738'
1739
Øystein Walleaabfdc92023-04-07 19:53:16 +02001740test_expect_success 'for-each-ref --omit-empty works' '
1741 git for-each-ref --format="%(refname)" >actual &&
1742 test_line_count -gt 1 actual &&
1743 git for-each-ref --format="%(if:equals=refs/heads/main)%(refname)%(then)%(refname)%(end)" --omit-empty >actual &&
1744 echo refs/heads/main >expect &&
1745 test_cmp expect actual
1746'
1747
Jeff King76f9e562020-05-03 05:11:57 -04001748test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
1749 # name refs numerically to avoid case-insensitive filesystem conflicts
1750 nr=0 &&
1751 for email in a A b B
1752 do
1753 for subject in a A b B
1754 do
1755 GIT_COMMITTER_EMAIL="$email@example.com" \
1756 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1757 nr=$((nr+1))||
1758 return 1
1759 done
1760 done &&
1761 git for-each-ref --ignore-case \
1762 --format="%(taggeremail) %(subject) %(refname)" \
1763 --sort=refname \
1764 --sort=subject \
1765 --sort=taggeremail \
1766 refs/tags/icase-* >actual &&
1767 cat >expect <<-\EOF &&
1768 <a@example.com> tag a refs/tags/icase-00
1769 <a@example.com> tag A refs/tags/icase-01
1770 <A@example.com> tag a refs/tags/icase-04
1771 <A@example.com> tag A refs/tags/icase-05
1772 <a@example.com> tag b refs/tags/icase-02
1773 <a@example.com> tag B refs/tags/icase-03
1774 <A@example.com> tag b refs/tags/icase-06
1775 <A@example.com> tag B refs/tags/icase-07
1776 <b@example.com> tag a refs/tags/icase-08
1777 <b@example.com> tag A refs/tags/icase-09
1778 <B@example.com> tag a refs/tags/icase-12
1779 <B@example.com> tag A refs/tags/icase-13
1780 <b@example.com> tag b refs/tags/icase-10
1781 <b@example.com> tag B refs/tags/icase-11
1782 <B@example.com> tag b refs/tags/icase-14
1783 <B@example.com> tag B refs/tags/icase-15
1784 EOF
1785 test_cmp expect actual
1786'
1787
Jeff Kingc6854502021-04-01 04:32:24 -04001788test_expect_success 'for-each-ref reports broken tags' '
1789 git tag -m "good tag" broken-tag-good HEAD &&
1790 git cat-file tag broken-tag-good >good &&
1791 sed s/commit/blob/ <good >bad &&
1792 bad=$(git hash-object -w -t tag bad) &&
1793 git update-ref refs/tags/broken-tag-bad $bad &&
1794 test_must_fail git for-each-ref --format="%(*objectname)" \
1795 refs/tags/broken-tag-*
1796'
1797
Jeff Kingb01e1c72022-11-02 03:42:07 -04001798test_expect_success 'set up tag with signature and no blank lines' '
1799 git tag -F - fake-sig-no-blanks <<-\EOF
1800 this is the subject
1801 -----BEGIN PGP SIGNATURE-----
1802 not a real signature, but we just care about the
1803 subject/body parsing. It is important here that
1804 there are no blank lines in the signature.
1805 -----END PGP SIGNATURE-----
1806 EOF
1807'
1808
1809test_atom refs/tags/fake-sig-no-blanks contents:subject 'this is the subject'
1810test_atom refs/tags/fake-sig-no-blanks contents:body ''
1811test_atom refs/tags/fake-sig-no-blanks contents:signature "$sig"
1812
Jeff King8e1c5fc2022-11-02 03:44:00 -04001813test_expect_success 'set up tag with CRLF signature' '
1814 append_cr <<-\EOF |
1815 this is the subject
1816 -----BEGIN PGP SIGNATURE-----
1817
1818 not a real signature, but we just care about
1819 the subject/body parsing. It is important here
1820 that there is a blank line separating this
1821 from the signature header.
1822 -----END PGP SIGNATURE-----
1823 EOF
1824 git tag -F - --cleanup=verbatim fake-sig-crlf
1825'
1826
1827test_atom refs/tags/fake-sig-crlf contents:subject 'this is the subject'
1828test_atom refs/tags/fake-sig-crlf contents:body ''
1829
1830# CRLF is retained in the signature, so we have to pass our expected value
1831# through append_cr. But test_atom requires a shell string, which means command
1832# substitution, and the shell will strip trailing newlines from the output of
1833# the substitution. Hack around it by adding and then removing a dummy line.
1834sig_crlf="$(printf "%s" "$sig" | append_cr; echo dummy)"
1835sig_crlf=${sig_crlf%dummy}
1836test_atom refs/tags/fake-sig-crlf contents:signature "$sig_crlf"
1837
Derrick Stoleeb73dec52023-03-20 11:26:47 +00001838test_expect_success 'git for-each-ref --stdin: empty' '
1839 >in &&
1840 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1841 git for-each-ref --format="%(refname)" >expect &&
1842 test_cmp expect actual
1843'
1844
1845test_expect_success 'git for-each-ref --stdin: fails if extra args' '
1846 >in &&
1847 test_must_fail git for-each-ref --format="%(refname)" \
1848 --stdin refs/heads/extra <in 2>err &&
1849 grep "unknown arguments supplied with --stdin" err
1850'
1851
1852test_expect_success 'git for-each-ref --stdin: matches' '
1853 cat >in <<-EOF &&
1854 refs/tags/multi*
1855 refs/heads/amb*
1856 EOF
1857
1858 cat >expect <<-EOF &&
1859 refs/heads/ambiguous
1860 refs/tags/multi-ref1-100000-user1
1861 refs/tags/multi-ref1-100000-user2
1862 refs/tags/multi-ref1-200000-user1
1863 refs/tags/multi-ref1-200000-user2
1864 refs/tags/multi-ref2-100000-user1
1865 refs/tags/multi-ref2-100000-user2
1866 refs/tags/multi-ref2-200000-user1
1867 refs/tags/multi-ref2-200000-user2
1868 refs/tags/multiline
1869 EOF
1870
1871 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1872 test_cmp expect actual
1873'
1874
Derrick Stoleeb2c51b72023-03-20 11:26:48 +00001875test_expect_success 'git for-each-ref with non-existing refs' '
1876 cat >in <<-EOF &&
1877 refs/heads/this-ref-does-not-exist
1878 refs/tags/bogus
1879 EOF
1880
1881 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1882 test_must_be_empty actual &&
1883
1884 xargs git for-each-ref --format="%(refname)" <in >actual &&
1885 test_must_be_empty actual
1886'
1887
Victoria Dye188782e2023-11-14 19:53:57 +00001888test_expect_success 'git for-each-ref with nested tags' '
1889 git tag -am "Normal tag" nested/base HEAD &&
1890 git tag -am "Nested tag" nested/nest1 refs/tags/nested/base &&
1891 git tag -am "Double nested tag" nested/nest2 refs/tags/nested/nest1 &&
1892
1893 head_oid="$(git rev-parse HEAD)" &&
1894 base_tag_oid="$(git rev-parse refs/tags/nested/base)" &&
1895 nest1_tag_oid="$(git rev-parse refs/tags/nested/nest1)" &&
1896 nest2_tag_oid="$(git rev-parse refs/tags/nested/nest2)" &&
1897
1898 cat >expect <<-EOF &&
1899 refs/tags/nested/base $base_tag_oid tag $head_oid commit
1900 refs/tags/nested/nest1 $nest1_tag_oid tag $head_oid commit
1901 refs/tags/nested/nest2 $nest2_tag_oid tag $head_oid commit
1902 EOF
1903
1904 git for-each-ref \
1905 --format="%(refname) %(objectname) %(objecttype) %(*objectname) %(*objecttype)" \
1906 refs/tags/nested/ >actual &&
1907 test_cmp expect actual
1908'
1909
Kousik Sanagavarapu26c9c032023-06-04 23:52:47 +05301910GRADE_FORMAT="%(signature:grade)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1911TRUSTLEVEL_FORMAT="%(signature:trustlevel)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1912
1913test_expect_success GPG 'setup for signature atom using gpg' '
1914 git checkout -b signed &&
1915
1916 test_when_finished "test_unconfig commit.gpgSign" &&
1917
1918 echo "1" >file &&
1919 git add file &&
1920 test_tick &&
1921 git commit -S -m "file: 1" &&
1922 git tag first-signed &&
1923
1924 echo "2" >file &&
1925 test_tick &&
1926 git commit -a -m "file: 2" &&
1927 git tag second-unsigned &&
1928
1929 git config commit.gpgSign 1 &&
1930 echo "3" >file &&
1931 test_tick &&
1932 git commit -a --no-gpg-sign -m "file: 3" &&
1933 git tag third-unsigned &&
1934
1935 test_tick &&
1936 git rebase -f HEAD^^ && git tag second-signed HEAD^ &&
1937 git tag third-signed &&
1938
1939 echo "4" >file &&
1940 test_tick &&
1941 git commit -a -SB7227189 -m "file: 4" &&
1942 git tag fourth-signed &&
1943
1944 echo "5" >file &&
1945 test_tick &&
1946 git commit -a --no-gpg-sign -m "file: 5" &&
1947 git tag fifth-unsigned &&
1948
1949 echo "6" >file &&
1950 test_tick &&
1951 git commit -a --no-gpg-sign -m "file: 6" &&
1952
1953 test_tick &&
1954 git rebase -f HEAD^^ &&
1955 git tag fifth-signed HEAD^ &&
1956 git tag sixth-signed &&
1957
1958 echo "7" >file &&
1959 test_tick &&
1960 git commit -a --no-gpg-sign -m "file: 7" &&
1961 git tag seventh-unsigned
1962'
1963
1964test_expect_success GPGSSH 'setup for signature atom using ssh' '
1965 test_when_finished "test_unconfig gpg.format user.signingkey" &&
1966
1967 test_config gpg.format ssh &&
1968 test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
1969 echo "8" >file &&
1970 test_tick &&
René Scharfe065135f2023-07-16 10:17:35 +02001971 git add file &&
1972 git commit -S -m "file: 8" &&
Kousik Sanagavarapu26c9c032023-06-04 23:52:47 +05301973 git tag eighth-signed-ssh
1974'
1975
1976test_expect_success GPG2 'bare signature atom' '
Christian Hessed0fc5522023-08-22 15:03:15 +02001977 git verify-commit first-signed 2>expect &&
Kousik Sanagavarapu26c9c032023-06-04 23:52:47 +05301978 echo >>expect &&
1979 git for-each-ref refs/tags/first-signed \
1980 --format="%(signature)" >actual &&
1981 test_cmp expect actual
1982'
1983
1984test_expect_success GPG 'show good signature with custom format' '
1985 git verify-commit first-signed &&
1986 cat >expect <<-\EOF &&
1987 G
1988 13B6F51ECDDE430D
1989 C O Mitter <committer@example.com>
1990 73D758744BE721698EC54E8713B6F51ECDDE430D
1991 73D758744BE721698EC54E8713B6F51ECDDE430D
1992 EOF
1993 git for-each-ref refs/tags/first-signed \
1994 --format="$GRADE_FORMAT" >actual &&
1995 test_cmp expect actual
1996'
1997test_expect_success GPGSSH 'show good signature with custom format
1998 with ssh' '
1999 test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
2000 FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
2001 cat >expect.tmpl <<-\EOF &&
2002 G
2003 FINGERPRINT
2004 principal with number 1
2005 FINGERPRINT
2006
2007 EOF
2008 sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
2009 git for-each-ref refs/tags/eighth-signed-ssh \
2010 --format="$GRADE_FORMAT" >actual &&
2011 test_cmp expect actual
2012'
2013
2014test_expect_success GPG 'signature atom with grade option and bad signature' '
2015 git cat-file commit third-signed >raw &&
2016 sed -e "s/^file: 3/file: 3 forged/" raw >forged1 &&
2017 FORGED1=$(git hash-object -w -t commit forged1) &&
2018 git update-ref refs/tags/third-signed "$FORGED1" &&
2019 test_must_fail git verify-commit "$FORGED1" &&
2020
2021 cat >expect <<-\EOF &&
2022 B
2023 13B6F51ECDDE430D
2024 C O Mitter <committer@example.com>
2025
2026
2027 EOF
2028 git for-each-ref refs/tags/third-signed \
2029 --format="$GRADE_FORMAT" >actual &&
2030 test_cmp expect actual
2031'
2032
2033test_expect_success GPG 'show untrusted signature with custom format' '
2034 cat >expect <<-\EOF &&
2035 U
2036 65A0EEA02E30CAD7
2037 Eris Discordia <discord@example.net>
2038 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
2039 D4BE22311AD3131E5EDA29A461092E85B7227189
2040 EOF
2041 git for-each-ref refs/tags/fourth-signed \
2042 --format="$GRADE_FORMAT" >actual &&
2043 test_cmp expect actual
2044'
2045
2046test_expect_success GPG 'show untrusted signature with undefined trust level' '
2047 cat >expect <<-\EOF &&
2048 undefined
2049 65A0EEA02E30CAD7
2050 Eris Discordia <discord@example.net>
2051 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
2052 D4BE22311AD3131E5EDA29A461092E85B7227189
2053 EOF
2054 git for-each-ref refs/tags/fourth-signed \
2055 --format="$TRUSTLEVEL_FORMAT" >actual &&
2056 test_cmp expect actual
2057'
2058
2059test_expect_success GPG 'show untrusted signature with ultimate trust level' '
2060 cat >expect <<-\EOF &&
2061 ultimate
2062 13B6F51ECDDE430D
2063 C O Mitter <committer@example.com>
2064 73D758744BE721698EC54E8713B6F51ECDDE430D
2065 73D758744BE721698EC54E8713B6F51ECDDE430D
2066 EOF
2067 git for-each-ref refs/tags/sixth-signed \
2068 --format="$TRUSTLEVEL_FORMAT" >actual &&
2069 test_cmp expect actual
2070'
2071
2072test_expect_success GPG 'show unknown signature with custom format' '
2073 cat >expect <<-\EOF &&
2074 E
2075 13B6F51ECDDE430D
2076
2077
2078
2079 EOF
2080 GNUPGHOME="$GNUPGHOME_NOT_USED" git for-each-ref \
2081 refs/tags/sixth-signed --format="$GRADE_FORMAT" >actual &&
2082 test_cmp expect actual
2083'
2084
2085test_expect_success GPG 'show lack of signature with custom format' '
2086 cat >expect <<-\EOF &&
2087 N
2088
2089
2090
2091
2092 EOF
2093 git for-each-ref refs/tags/seventh-unsigned \
2094 --format="$GRADE_FORMAT" >actual &&
2095 test_cmp expect actual
2096'
2097
Andy Parkins96b2d4f2007-10-02 12:02:57 +01002098test_done