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