stash: learn to parse -m/--message like commit does
[git] / t / t7513-interpret-trailers.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2013, 2014 Christian Couder
4 #
5
6 test_description='git interpret-trailers'
7
8 . ./test-lib.sh
9
10 # When we want one trailing space at the end of each line, let's use sed
11 # to make sure that these spaces are not removed by any automatic tool.
12
13 test_expect_success 'setup' '
14         : >empty &&
15         cat >basic_message <<-\EOF &&
16                 subject
17
18                 body
19         EOF
20         cat >complex_message_body <<-\EOF &&
21                 my subject
22
23                 my body which is long
24                 and contains some special
25                 chars like : = ? !
26
27         EOF
28         sed -e "s/ Z\$/ /" >complex_message_trailers <<-\EOF &&
29                 Fixes: Z
30                 Acked-by: Z
31                 Reviewed-by: Z
32                 Signed-off-by: Z
33         EOF
34         cat >basic_patch <<-\EOF
35                 ---
36                  foo.txt | 2 +-
37                  1 file changed, 1 insertion(+), 1 deletion(-)
38
39                 diff --git a/foo.txt b/foo.txt
40                 index 0353767..1d91aa1 100644
41                 --- a/foo.txt
42                 +++ b/foo.txt
43                 @@ -1,3 +1,3 @@
44
45                 -bar
46                 +baz
47
48                 --
49                 1.9.rc0.11.ga562ddc
50
51         EOF
52 '
53
54 test_expect_success 'without config' '
55         sed -e "s/ Z\$/ /" >expected <<-\EOF &&
56
57                 ack: Peff
58                 Reviewed-by: Z
59                 Acked-by: Johan
60         EOF
61         git interpret-trailers --trailer "ack = Peff" --trailer "Reviewed-by" \
62                 --trailer "Acked-by: Johan" empty >actual &&
63         test_cmp expected actual
64 '
65
66 test_expect_success 'without config in another order' '
67         sed -e "s/ Z\$/ /" >expected <<-\EOF &&
68
69                 Acked-by: Johan
70                 Reviewed-by: Z
71                 ack: Peff
72         EOF
73         git interpret-trailers --trailer "Acked-by: Johan" --trailer "Reviewed-by" \
74                 --trailer "ack = Peff" empty >actual &&
75         test_cmp expected actual
76 '
77
78 test_expect_success '--trim-empty without config' '
79         cat >expected <<-\EOF &&
80
81                 ack: Peff
82                 Acked-by: Johan
83         EOF
84         git interpret-trailers --trim-empty --trailer ack=Peff \
85                 --trailer "Reviewed-by" --trailer "Acked-by: Johan" \
86                 --trailer "sob:" empty >actual &&
87         test_cmp expected actual
88 '
89
90 test_expect_success 'with config option on the command line' '
91         cat >expected <<-\EOF &&
92
93                 Acked-by: Johan
94                 Reviewed-by: Peff
95         EOF
96         { echo; echo "Acked-by: Johan"; } |
97         git -c "trailer.Acked-by.ifexists=addifdifferent" interpret-trailers \
98                 --trailer "Reviewed-by: Peff" --trailer "Acked-by: Johan" >actual &&
99         test_cmp expected actual
100 '
101
102 test_expect_success 'with only a title in the message' '
103         cat >expected <<-\EOF &&
104                 area: change
105
106                 Reviewed-by: Peff
107                 Acked-by: Johan
108         EOF
109         echo "area: change" |
110         git interpret-trailers --trailer "Reviewed-by: Peff" \
111                 --trailer "Acked-by: Johan" >actual &&
112         test_cmp expected actual
113 '
114
115 test_expect_success 'with multiline title in the message' '
116         cat >expected <<-\EOF &&
117                 place of
118                 code: change
119
120                 Reviewed-by: Peff
121                 Acked-by: Johan
122         EOF
123         printf "%s\n" "place of" "code: change" |
124         git interpret-trailers --trailer "Reviewed-by: Peff" \
125                 --trailer "Acked-by: Johan" >actual &&
126         test_cmp expected actual
127 '
128
129 test_expect_success 'with non-trailer lines mixed with Signed-off-by' '
130         cat >patch <<-\EOF &&
131
132                 this is not a trailer
133                 this is not a trailer
134                 Signed-off-by: a <a@example.com>
135                 this is not a trailer
136         EOF
137         cat >expected <<-\EOF &&
138
139                 this is not a trailer
140                 this is not a trailer
141                 Signed-off-by: a <a@example.com>
142                 this is not a trailer
143                 token: value
144         EOF
145         git interpret-trailers --trailer "token: value" patch >actual &&
146         test_cmp expected actual
147 '
148
149 test_expect_success 'with non-trailer lines mixed with cherry picked from' '
150         cat >patch <<-\EOF &&
151
152                 this is not a trailer
153                 this is not a trailer
154                 (cherry picked from commit x)
155                 this is not a trailer
156         EOF
157         cat >expected <<-\EOF &&
158
159                 this is not a trailer
160                 this is not a trailer
161                 (cherry picked from commit x)
162                 this is not a trailer
163                 token: value
164         EOF
165         git interpret-trailers --trailer "token: value" patch >actual &&
166         test_cmp expected actual
167 '
168
169 test_expect_success 'with non-trailer lines mixed with a configured trailer' '
170         cat >patch <<-\EOF &&
171
172                 this is not a trailer
173                 this is not a trailer
174                 My-trailer: x
175                 this is not a trailer
176         EOF
177         cat >expected <<-\EOF &&
178
179                 this is not a trailer
180                 this is not a trailer
181                 My-trailer: x
182                 this is not a trailer
183                 token: value
184         EOF
185         test_config trailer.my.key "My-trailer: " &&
186         git interpret-trailers --trailer "token: value" patch >actual &&
187         test_cmp expected actual
188 '
189
190 test_expect_success 'with non-trailer lines mixed with a non-configured trailer' '
191         cat >patch <<-\EOF &&
192
193                 this is not a trailer
194                 this is not a trailer
195                 I-am-not-configured: x
196                 this is not a trailer
197         EOF
198         cat >expected <<-\EOF &&
199
200                 this is not a trailer
201                 this is not a trailer
202                 I-am-not-configured: x
203                 this is not a trailer
204
205                 token: value
206         EOF
207         test_config trailer.my.key "My-trailer: " &&
208         git interpret-trailers --trailer "token: value" patch >actual &&
209         test_cmp expected actual
210 '
211
212 test_expect_success 'with all non-configured trailers' '
213         cat >patch <<-\EOF &&
214
215                 I-am-not-configured: x
216                 I-am-also-not-configured: x
217         EOF
218         cat >expected <<-\EOF &&
219
220                 I-am-not-configured: x
221                 I-am-also-not-configured: x
222                 token: value
223         EOF
224         test_config trailer.my.key "My-trailer: " &&
225         git interpret-trailers --trailer "token: value" patch >actual &&
226         test_cmp expected actual
227 '
228
229 test_expect_success 'with non-trailer lines only' '
230         cat >patch <<-\EOF &&
231
232                 this is not a trailer
233         EOF
234         cat >expected <<-\EOF &&
235
236                 this is not a trailer
237
238                 token: value
239         EOF
240         git interpret-trailers --trailer "token: value" patch >actual &&
241         test_cmp expected actual
242 '
243
244 test_expect_success 'line with leading whitespace is not trailer' '
245         q_to_tab >patch <<-\EOF &&
246
247                 Qtoken: value
248         EOF
249         q_to_tab >expected <<-\EOF &&
250
251                 Qtoken: value
252
253                 token: value
254         EOF
255         git interpret-trailers --trailer "token: value" patch >actual &&
256         test_cmp expected actual
257 '
258
259 test_expect_success 'multiline field treated as one trailer for 25% check' '
260         q_to_tab >patch <<-\EOF &&
261
262                 Signed-off-by: a <a@example.com>
263                 name: value on
264                 Qmultiple lines
265                 this is not a trailer
266                 this is not a trailer
267                 this is not a trailer
268                 this is not a trailer
269                 this is not a trailer
270                 this is not a trailer
271         EOF
272         q_to_tab >expected <<-\EOF &&
273
274                 Signed-off-by: a <a@example.com>
275                 name: value on
276                 Qmultiple lines
277                 this is not a trailer
278                 this is not a trailer
279                 this is not a trailer
280                 this is not a trailer
281                 this is not a trailer
282                 this is not a trailer
283                 name: value
284         EOF
285         git interpret-trailers --trailer "name: value" patch >actual &&
286         test_cmp expected actual
287 '
288
289 test_expect_success 'multiline field treated as atomic for placement' '
290         q_to_tab >patch <<-\EOF &&
291
292                 another: trailer
293                 name: value on
294                 Qmultiple lines
295                 another: trailer
296         EOF
297         q_to_tab >expected <<-\EOF &&
298
299                 another: trailer
300                 name: value on
301                 Qmultiple lines
302                 name: value
303                 another: trailer
304         EOF
305         test_config trailer.name.where after &&
306         git interpret-trailers --trailer "name: value" patch >actual &&
307         test_cmp expected actual
308 '
309
310 test_expect_success 'multiline field treated as atomic for replacement' '
311         q_to_tab >patch <<-\EOF &&
312
313                 another: trailer
314                 name: value on
315                 Qmultiple lines
316                 another: trailer
317         EOF
318         q_to_tab >expected <<-\EOF &&
319
320                 another: trailer
321                 another: trailer
322                 name: value
323         EOF
324         test_config trailer.name.ifexists replace &&
325         git interpret-trailers --trailer "name: value" patch >actual &&
326         test_cmp expected actual
327 '
328
329 test_expect_success 'multiline field treated as atomic for difference check' '
330         q_to_tab >patch <<-\EOF &&
331
332                 another: trailer
333                 name: first line
334                 Qsecond line
335                 another: trailer
336         EOF
337         test_config trailer.name.ifexists addIfDifferent &&
338
339         q_to_tab >trailer <<-\EOF &&
340                 name: first line
341                 Qsecond line
342         EOF
343         q_to_tab >expected <<-\EOF &&
344
345                 another: trailer
346                 name: first line
347                 Qsecond line
348                 another: trailer
349         EOF
350         git interpret-trailers --trailer "$(cat trailer)" patch >actual &&
351         test_cmp expected actual &&
352
353         q_to_tab >trailer <<-\EOF &&
354                 name: first line
355                 QQQQQsecond line
356         EOF
357         q_to_tab >expected <<-\EOF &&
358
359                 another: trailer
360                 name: first line
361                 Qsecond line
362                 another: trailer
363                 name: first line
364                 QQQQQsecond line
365         EOF
366         git interpret-trailers --trailer "$(cat trailer)" patch >actual &&
367         test_cmp expected actual &&
368
369         q_to_tab >trailer <<-\EOF &&
370                 name: first line *DIFFERENT*
371                 Qsecond line
372         EOF
373         q_to_tab >expected <<-\EOF &&
374
375                 another: trailer
376                 name: first line
377                 Qsecond line
378                 another: trailer
379                 name: first line *DIFFERENT*
380                 Qsecond line
381         EOF
382         git interpret-trailers --trailer "$(cat trailer)" patch >actual &&
383         test_cmp expected actual
384 '
385
386 test_expect_success 'multiline field treated as atomic for neighbor check' '
387         q_to_tab >patch <<-\EOF &&
388
389                 another: trailer
390                 name: first line
391                 Qsecond line
392                 another: trailer
393         EOF
394         test_config trailer.name.where after &&
395         test_config trailer.name.ifexists addIfDifferentNeighbor &&
396
397         q_to_tab >trailer <<-\EOF &&
398                 name: first line
399                 Qsecond line
400         EOF
401         q_to_tab >expected <<-\EOF &&
402
403                 another: trailer
404                 name: first line
405                 Qsecond line
406                 another: trailer
407         EOF
408         git interpret-trailers --trailer "$(cat trailer)" patch >actual &&
409         test_cmp expected actual &&
410
411         q_to_tab >trailer <<-\EOF &&
412                 name: first line
413                 QQQQQsecond line
414         EOF
415         q_to_tab >expected <<-\EOF &&
416
417                 another: trailer
418                 name: first line
419                 Qsecond line
420                 name: first line
421                 QQQQQsecond line
422                 another: trailer
423         EOF
424         git interpret-trailers --trailer "$(cat trailer)" patch >actual &&
425         test_cmp expected actual
426 '
427
428 test_expect_success 'with config setup' '
429         git config trailer.ack.key "Acked-by: " &&
430         cat >expected <<-\EOF &&
431
432                 Acked-by: Peff
433         EOF
434         git interpret-trailers --trim-empty --trailer "ack = Peff" empty >actual &&
435         test_cmp expected actual &&
436         git interpret-trailers --trim-empty --trailer "Acked-by = Peff" empty >actual &&
437         test_cmp expected actual &&
438         git interpret-trailers --trim-empty --trailer "Acked-by :Peff" empty >actual &&
439         test_cmp expected actual
440 '
441
442 test_expect_success 'with config setup and ":=" as separators' '
443         git config trailer.separators ":=" &&
444         git config trailer.ack.key "Acked-by= " &&
445         cat >expected <<-\EOF &&
446
447                 Acked-by= Peff
448         EOF
449         git interpret-trailers --trim-empty --trailer "ack = Peff" empty >actual &&
450         test_cmp expected actual &&
451         git interpret-trailers --trim-empty --trailer "Acked-by= Peff" empty >actual &&
452         test_cmp expected actual &&
453         git interpret-trailers --trim-empty --trailer "Acked-by : Peff" empty >actual &&
454         test_cmp expected actual
455 '
456
457 test_expect_success 'with config setup and "%" as separators' '
458         git config trailer.separators "%" &&
459         cat >expected <<-\EOF &&
460
461                 bug% 42
462                 count% 10
463                 bug% 422
464         EOF
465         git interpret-trailers --trim-empty --trailer "bug = 42" \
466                 --trailer count%10 --trailer "test: stuff" \
467                 --trailer "bug % 422" empty >actual &&
468         test_cmp expected actual
469 '
470
471 test_expect_success 'with "%" as separators and a message with trailers' '
472         cat >special_message <<-\EOF &&
473                 Special Message
474
475                 bug% 42
476                 count% 10
477                 bug% 422
478         EOF
479         cat >expected <<-\EOF &&
480                 Special Message
481
482                 bug% 42
483                 count% 10
484                 bug% 422
485                 count% 100
486         EOF
487         git interpret-trailers --trailer count%100 \
488                 special_message >actual &&
489         test_cmp expected actual
490 '
491
492 test_expect_success 'with config setup and ":=#" as separators' '
493         git config trailer.separators ":=#" &&
494         git config trailer.bug.key "Bug #" &&
495         cat >expected <<-\EOF &&
496
497                 Bug #42
498         EOF
499         git interpret-trailers --trim-empty --trailer "bug = 42" empty >actual &&
500         test_cmp expected actual
501 '
502
503 test_expect_success 'with commit basic message' '
504         cat basic_message >expected &&
505         echo >>expected &&
506         git interpret-trailers <basic_message >actual &&
507         test_cmp expected actual
508 '
509
510 test_expect_success 'with basic patch' '
511         cat basic_message >input &&
512         cat basic_patch >>input &&
513         cat basic_message >expected &&
514         echo >>expected &&
515         cat basic_patch >>expected &&
516         git interpret-trailers <input >actual &&
517         test_cmp expected actual
518 '
519
520 test_expect_success 'with commit complex message as argument' '
521         cat complex_message_body complex_message_trailers >complex_message &&
522         cat complex_message_body >expected &&
523         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
524                 Fixes: Z
525                 Acked-by= Z
526                 Reviewed-by: Z
527                 Signed-off-by: Z
528         EOF
529         git interpret-trailers complex_message >actual &&
530         test_cmp expected actual
531 '
532
533 test_expect_success 'with 2 files arguments' '
534         cat basic_message >>expected &&
535         echo >>expected &&
536         cat basic_patch >>expected &&
537         git interpret-trailers complex_message input >actual &&
538         test_cmp expected actual
539 '
540
541 test_expect_success 'with message that has comments' '
542         cat basic_message >message_with_comments &&
543         sed -e "s/ Z\$/ /" >>message_with_comments <<-\EOF &&
544                 # comment
545
546                 # other comment
547                 Cc: Z
548                 # yet another comment
549                 Reviewed-by: Johan
550                 Reviewed-by: Z
551                 # last comment
552
553         EOF
554         cat basic_patch >>message_with_comments &&
555         cat basic_message >expected &&
556         cat >>expected <<-\EOF &&
557                 # comment
558
559                 Reviewed-by: Johan
560                 Cc: Peff
561                 # last comment
562
563         EOF
564         cat basic_patch >>expected &&
565         git interpret-trailers --trim-empty --trailer "Cc: Peff" message_with_comments >actual &&
566         test_cmp expected actual
567 '
568
569 test_expect_success 'with message that has an old style conflict block' '
570         cat basic_message >message_with_comments &&
571         sed -e "s/ Z\$/ /" >>message_with_comments <<-\EOF &&
572                 # comment
573
574                 # other comment
575                 Cc: Z
576                 # yet another comment
577                 Reviewed-by: Johan
578                 Reviewed-by: Z
579                 # last comment
580
581                 Conflicts:
582
583         EOF
584         cat basic_message >expected &&
585         cat >>expected <<-\EOF &&
586                 # comment
587
588                 Reviewed-by: Johan
589                 Cc: Peff
590                 # last comment
591
592                 Conflicts:
593
594         EOF
595         git interpret-trailers --trim-empty --trailer "Cc: Peff" message_with_comments >actual &&
596         test_cmp expected actual
597 '
598
599 test_expect_success 'with commit complex message and trailer args' '
600         cat complex_message_body >expected &&
601         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
602                 Fixes: Z
603                 Acked-by= Z
604                 Reviewed-by: Z
605                 Signed-off-by: Z
606                 Acked-by= Peff
607                 Bug #42
608         EOF
609         git interpret-trailers --trailer "ack: Peff" \
610                 --trailer "bug: 42" <complex_message >actual &&
611         test_cmp expected actual
612 '
613
614 test_expect_success 'with complex patch, args and --trim-empty' '
615         cat complex_message >complex_patch &&
616         cat basic_patch >>complex_patch &&
617         cat complex_message_body >expected &&
618         cat >>expected <<-\EOF &&
619                 Acked-by= Peff
620                 Bug #42
621         EOF
622         cat basic_patch >>expected &&
623         git interpret-trailers --trim-empty --trailer "ack: Peff" \
624                 --trailer "bug: 42" <complex_patch >actual &&
625         test_cmp expected actual
626 '
627
628 test_expect_success 'in-place editing with basic patch' '
629         cat basic_message >message &&
630         cat basic_patch >>message &&
631         cat basic_message >expected &&
632         echo >>expected &&
633         cat basic_patch >>expected &&
634         git interpret-trailers --in-place message &&
635         test_cmp expected message
636 '
637
638 test_expect_success 'in-place editing with additional trailer' '
639         cat basic_message >message &&
640         cat basic_patch >>message &&
641         cat basic_message >expected &&
642         echo >>expected &&
643         cat >>expected <<-\EOF &&
644                 Reviewed-by: Alice
645         EOF
646         cat basic_patch >>expected &&
647         git interpret-trailers --trailer "Reviewed-by: Alice" --in-place message &&
648         test_cmp expected message
649 '
650
651 test_expect_success 'in-place editing on stdin disallowed' '
652         test_must_fail git interpret-trailers --trailer "Reviewed-by: Alice" --in-place < basic_message
653 '
654
655 test_expect_success 'in-place editing on non-existing file' '
656         test_must_fail git interpret-trailers --trailer "Reviewed-by: Alice" --in-place nonexisting &&
657         test_path_is_missing nonexisting
658 '
659
660 test_expect_success POSIXPERM,SANITY "in-place editing doesn't clobber original file on error" '
661         cat basic_message >message &&
662         chmod -r message &&
663         test_must_fail git interpret-trailers --trailer "Reviewed-by: Alice" --in-place message &&
664         chmod +r message &&
665         test_cmp message basic_message
666 '
667
668 test_expect_success 'using "where = before"' '
669         git config trailer.bug.where "before" &&
670         cat complex_message_body >expected &&
671         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
672                 Bug #42
673                 Fixes: Z
674                 Acked-by= Z
675                 Reviewed-by: Z
676                 Signed-off-by: Z
677                 Acked-by= Peff
678         EOF
679         git interpret-trailers --trailer "ack: Peff" \
680                 --trailer "bug: 42" complex_message >actual &&
681         test_cmp expected actual
682 '
683
684 test_expect_success 'using "where = after"' '
685         git config trailer.ack.where "after" &&
686         cat complex_message_body >expected &&
687         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
688                 Bug #42
689                 Fixes: Z
690                 Acked-by= Z
691                 Acked-by= Peff
692                 Reviewed-by: Z
693                 Signed-off-by: Z
694         EOF
695         git interpret-trailers --trailer "ack: Peff" \
696                 --trailer "bug: 42" complex_message >actual &&
697         test_cmp expected actual
698 '
699
700 test_expect_success 'using "where = end"' '
701         git config trailer.review.key "Reviewed-by" &&
702         git config trailer.review.where "end" &&
703         cat complex_message_body >expected &&
704         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
705                 Fixes: Z
706                 Acked-by= Z
707                 Acked-by= Peff
708                 Reviewed-by: Z
709                 Signed-off-by: Z
710                 Reviewed-by: Junio
711                 Reviewed-by: Johannes
712         EOF
713         git interpret-trailers --trailer "ack: Peff" \
714                 --trailer "Reviewed-by: Junio" --trailer "Reviewed-by: Johannes" \
715                 complex_message >actual &&
716         test_cmp expected actual
717 '
718
719 test_expect_success 'using "where = start"' '
720         git config trailer.review.key "Reviewed-by" &&
721         git config trailer.review.where "start" &&
722         cat complex_message_body >expected &&
723         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
724                 Reviewed-by: Johannes
725                 Reviewed-by: Junio
726                 Fixes: Z
727                 Acked-by= Z
728                 Acked-by= Peff
729                 Reviewed-by: Z
730                 Signed-off-by: Z
731         EOF
732         git interpret-trailers --trailer "ack: Peff" \
733                 --trailer "Reviewed-by: Junio" --trailer "Reviewed-by: Johannes" \
734                 complex_message >actual &&
735         test_cmp expected actual
736 '
737
738 test_expect_success 'using "where = before" for a token in the middle of the message' '
739         git config trailer.review.key "Reviewed-by:" &&
740         git config trailer.review.where "before" &&
741         cat complex_message_body >expected &&
742         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
743                 Bug #42
744                 Fixes: Z
745                 Acked-by= Z
746                 Acked-by= Peff
747                 Reviewed-by:Johan
748                 Reviewed-by:
749                 Signed-off-by: Z
750         EOF
751         git interpret-trailers --trailer "ack: Peff" --trailer "bug: 42" \
752                 --trailer "review: Johan" <complex_message >actual &&
753         test_cmp expected actual
754 '
755
756 test_expect_success 'using "where = before" and --trim-empty' '
757         cat complex_message_body >expected &&
758         cat >>expected <<-\EOF &&
759                 Bug #46
760                 Bug #42
761                 Acked-by= Peff
762                 Reviewed-by:Johan
763         EOF
764         git interpret-trailers --trim-empty --trailer "ack: Peff" \
765                 --trailer "bug: 42" --trailer "review: Johan" \
766                 --trailer "Bug: 46" <complex_message >actual &&
767         test_cmp expected actual
768 '
769
770 test_expect_success 'the default is "ifExists = addIfDifferentNeighbor"' '
771         cat complex_message_body >expected &&
772         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
773                 Bug #42
774                 Fixes: Z
775                 Acked-by= Z
776                 Acked-by= Peff
777                 Acked-by= Junio
778                 Acked-by= Peff
779                 Reviewed-by:
780                 Signed-off-by: Z
781         EOF
782         git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
783                 --trailer "ack: Junio" --trailer "bug: 42" --trailer "ack: Peff" \
784                 --trailer "ack: Peff" <complex_message >actual &&
785         test_cmp expected actual
786 '
787
788 test_expect_success 'default "ifExists" is now "addIfDifferent"' '
789         git config trailer.ifexists "addIfDifferent" &&
790         cat complex_message_body >expected &&
791         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
792                 Bug #42
793                 Fixes: Z
794                 Acked-by= Z
795                 Acked-by= Peff
796                 Acked-by= Junio
797                 Reviewed-by:
798                 Signed-off-by: Z
799         EOF
800         git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
801                 --trailer "ack: Junio" --trailer "bug: 42" --trailer "ack: Peff" \
802                 --trailer "ack: Peff" <complex_message >actual &&
803         test_cmp expected actual
804 '
805
806 test_expect_success 'using "ifExists = addIfDifferent" with "where = end"' '
807         git config trailer.ack.ifExists "addIfDifferent" &&
808         git config trailer.ack.where "end" &&
809         cat complex_message_body >expected &&
810         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
811                 Bug #42
812                 Fixes: Z
813                 Acked-by= Z
814                 Reviewed-by:
815                 Signed-off-by: Z
816                 Acked-by= Peff
817         EOF
818         git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
819                 --trailer "bug: 42" --trailer "ack: Peff" \
820                 <complex_message >actual &&
821         test_cmp expected actual
822 '
823
824 test_expect_success 'using "ifExists = addIfDifferent" with "where = before"' '
825         git config trailer.ack.ifExists "addIfDifferent" &&
826         git config trailer.ack.where "before" &&
827         cat complex_message_body >expected &&
828         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
829                 Bug #42
830                 Fixes: Z
831                 Acked-by= Peff
832                 Acked-by= Z
833                 Reviewed-by:
834                 Signed-off-by: Z
835         EOF
836         git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
837                 --trailer "bug: 42" --trailer "ack: Peff" \
838                 <complex_message >actual &&
839         test_cmp expected actual
840 '
841
842 test_expect_success 'using "ifExists = addIfDifferentNeighbor" with "where = end"' '
843         git config trailer.ack.ifExists "addIfDifferentNeighbor" &&
844         git config trailer.ack.where "end" &&
845         cat complex_message_body >expected &&
846         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
847                 Bug #42
848                 Fixes: Z
849                 Acked-by= Z
850                 Reviewed-by:
851                 Signed-off-by: Z
852                 Acked-by= Peff
853                 Acked-by= Junio
854                 Tested-by: Jakub
855                 Acked-by= Junio
856                 Acked-by= Peff
857         EOF
858         git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
859                 --trailer "ack: Junio" --trailer "bug: 42" \
860                 --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
861                 --trailer "ack: Junio" --trailer "ack: Peff" <complex_message >actual &&
862         test_cmp expected actual
863 '
864
865 test_expect_success 'using "ifExists = addIfDifferentNeighbor"  with "where = after"' '
866         git config trailer.ack.ifExists "addIfDifferentNeighbor" &&
867         git config trailer.ack.where "after" &&
868         cat complex_message_body >expected &&
869         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
870                 Bug #42
871                 Fixes: Z
872                 Acked-by= Z
873                 Acked-by= Peff
874                 Acked-by= Junio
875                 Acked-by= Peff
876                 Reviewed-by:
877                 Signed-off-by: Z
878                 Tested-by: Jakub
879         EOF
880         git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
881                 --trailer "ack: Junio" --trailer "bug: 42" \
882                 --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
883                 --trailer "ack: Junio" --trailer "ack: Peff" <complex_message >actual &&
884         test_cmp expected actual
885 '
886
887 test_expect_success 'using "ifExists = addIfDifferentNeighbor" and --trim-empty' '
888         git config trailer.ack.ifExists "addIfDifferentNeighbor" &&
889         cat complex_message_body >expected &&
890         cat >>expected <<-\EOF &&
891                 Bug #42
892                 Acked-by= Peff
893                 Acked-by= Junio
894                 Acked-by= Peff
895         EOF
896         git interpret-trailers --trim-empty --trailer "ack: Peff" \
897                 --trailer "Acked-by= Peff" --trailer "review:" \
898                 --trailer "ack: Junio" --trailer "bug: 42" \
899                 --trailer "ack: Peff" <complex_message >actual &&
900         test_cmp expected actual
901 '
902
903 test_expect_success 'using "ifExists = add" with "where = end"' '
904         git config trailer.ack.ifExists "add" &&
905         git config trailer.ack.where "end" &&
906         cat complex_message_body >expected &&
907         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
908                 Bug #42
909                 Fixes: Z
910                 Acked-by= Z
911                 Reviewed-by:
912                 Signed-off-by: Z
913                 Acked-by= Peff
914                 Acked-by= Peff
915                 Tested-by: Jakub
916                 Acked-by= Junio
917                 Tested-by: Johannes
918                 Acked-by= Peff
919         EOF
920         git interpret-trailers --trailer "ack: Peff" \
921                 --trailer "Acked-by= Peff" --trailer "review:" \
922                 --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
923                 --trailer "bug: 42" --trailer "Tested-by: Johannes" \
924                 --trailer "ack: Peff" <complex_message >actual &&
925         test_cmp expected actual
926 '
927
928 test_expect_success 'using "ifExists = add" with "where = after"' '
929         git config trailer.ack.ifExists "add" &&
930         git config trailer.ack.where "after" &&
931         cat complex_message_body >expected &&
932         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
933                 Bug #42
934                 Fixes: Z
935                 Acked-by= Z
936                 Acked-by= Peff
937                 Acked-by= Peff
938                 Acked-by= Junio
939                 Acked-by= Peff
940                 Reviewed-by:
941                 Signed-off-by: Z
942         EOF
943         git interpret-trailers --trailer "ack: Peff" \
944                 --trailer "Acked-by= Peff" --trailer "review:" \
945                 --trailer "ack: Junio" --trailer "bug: 42" \
946                 --trailer "ack: Peff" <complex_message >actual &&
947         test_cmp expected actual
948 '
949
950 test_expect_success 'using "ifExists = replace"' '
951         git config trailer.fix.key "Fixes: " &&
952         git config trailer.fix.ifExists "replace" &&
953         cat complex_message_body >expected &&
954         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
955                 Bug #42
956                 Acked-by= Z
957                 Acked-by= Junio
958                 Acked-by= Peff
959                 Reviewed-by:
960                 Signed-off-by: Z
961                 Fixes: 22
962         EOF
963         git interpret-trailers --trailer "review:" \
964                 --trailer "fix=53" --trailer "ack: Junio" --trailer "fix=22" \
965                 --trailer "bug: 42" --trailer "ack: Peff" \
966                 <complex_message >actual &&
967         test_cmp expected actual
968 '
969
970 test_expect_success 'using "ifExists = replace" with "where = after"' '
971         git config trailer.fix.where "after" &&
972         cat complex_message_body >expected &&
973         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
974                 Bug #42
975                 Fixes: 22
976                 Acked-by= Z
977                 Acked-by= Junio
978                 Acked-by= Peff
979                 Reviewed-by:
980                 Signed-off-by: Z
981         EOF
982         git interpret-trailers --trailer "review:" \
983                 --trailer "fix=53" --trailer "ack: Junio" --trailer "fix=22" \
984                 --trailer "bug: 42" --trailer "ack: Peff" \
985                 <complex_message >actual &&
986         test_cmp expected actual
987 '
988
989 test_expect_success 'using "ifExists = doNothing"' '
990         git config trailer.fix.ifExists "doNothing" &&
991         cat complex_message_body >expected &&
992         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
993                 Bug #42
994                 Fixes: Z
995                 Acked-by= Z
996                 Acked-by= Junio
997                 Acked-by= Peff
998                 Reviewed-by:
999                 Signed-off-by: Z
1000         EOF
1001         git interpret-trailers --trailer "review:" --trailer "fix=53" \
1002                 --trailer "ack: Junio" --trailer "fix=22" \
1003                 --trailer "bug: 42" --trailer "ack: Peff" \
1004                 <complex_message >actual &&
1005         test_cmp expected actual
1006 '
1007
1008 test_expect_success 'the default is "ifMissing = add"' '
1009         git config trailer.cc.key "Cc: " &&
1010         git config trailer.cc.where "before" &&
1011         cat complex_message_body >expected &&
1012         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
1013                 Bug #42
1014                 Cc: Linus
1015                 Fixes: Z
1016                 Acked-by= Z
1017                 Acked-by= Junio
1018                 Acked-by= Peff
1019                 Reviewed-by:
1020                 Signed-off-by: Z
1021         EOF
1022         git interpret-trailers --trailer "review:" --trailer "fix=53" \
1023                 --trailer "cc=Linus" --trailer "ack: Junio" \
1024                 --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
1025                 <complex_message >actual &&
1026         test_cmp expected actual
1027 '
1028
1029 test_expect_success 'when default "ifMissing" is "doNothing"' '
1030         git config trailer.ifmissing "doNothing" &&
1031         cat complex_message_body >expected &&
1032         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
1033                 Fixes: Z
1034                 Acked-by= Z
1035                 Acked-by= Junio
1036                 Acked-by= Peff
1037                 Reviewed-by:
1038                 Signed-off-by: Z
1039         EOF
1040         git interpret-trailers --trailer "review:" --trailer "fix=53" \
1041                 --trailer "cc=Linus" --trailer "ack: Junio" \
1042                 --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
1043                 <complex_message >actual &&
1044         test_cmp expected actual &&
1045         git config trailer.ifmissing "add"
1046 '
1047
1048 test_expect_success 'using "ifMissing = add" with "where = end"' '
1049         git config trailer.cc.key "Cc: " &&
1050         git config trailer.cc.where "end" &&
1051         git config trailer.cc.ifMissing "add" &&
1052         cat complex_message_body >expected &&
1053         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
1054                 Bug #42
1055                 Fixes: Z
1056                 Acked-by= Z
1057                 Acked-by= Junio
1058                 Acked-by= Peff
1059                 Reviewed-by:
1060                 Signed-off-by: Z
1061                 Cc: Linus
1062         EOF
1063         git interpret-trailers --trailer "review:" --trailer "fix=53" \
1064                 --trailer "ack: Junio" --trailer "fix=22" \
1065                 --trailer "bug: 42" --trailer "cc=Linus" --trailer "ack: Peff" \
1066                 <complex_message >actual &&
1067         test_cmp expected actual
1068 '
1069
1070 test_expect_success 'using "ifMissing = add" with "where = before"' '
1071         git config trailer.cc.key "Cc: " &&
1072         git config trailer.cc.where "before" &&
1073         git config trailer.cc.ifMissing "add" &&
1074         cat complex_message_body >expected &&
1075         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
1076                 Cc: Linus
1077                 Bug #42
1078                 Fixes: Z
1079                 Acked-by= Z
1080                 Acked-by= Junio
1081                 Acked-by= Peff
1082                 Reviewed-by:
1083                 Signed-off-by: Z
1084         EOF
1085         git interpret-trailers --trailer "review:" --trailer "fix=53" \
1086                 --trailer "ack: Junio" --trailer "fix=22" \
1087                 --trailer "bug: 42" --trailer "cc=Linus" --trailer "ack: Peff" \
1088                 <complex_message >actual &&
1089         test_cmp expected actual
1090 '
1091
1092 test_expect_success 'using "ifMissing = doNothing"' '
1093         git config trailer.cc.ifMissing "doNothing" &&
1094         cat complex_message_body >expected &&
1095         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
1096                 Bug #42
1097                 Fixes: Z
1098                 Acked-by= Z
1099                 Acked-by= Junio
1100                 Acked-by= Peff
1101                 Reviewed-by:
1102                 Signed-off-by: Z
1103         EOF
1104         git interpret-trailers --trailer "review:" --trailer "fix=53" \
1105                 --trailer "cc=Linus" --trailer "ack: Junio" \
1106                 --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
1107                 <complex_message >actual &&
1108         test_cmp expected actual
1109 '
1110
1111 test_expect_success 'default "where" is now "after"' '
1112         git config trailer.where "after" &&
1113         git config --unset trailer.ack.where &&
1114         cat complex_message_body >expected &&
1115         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
1116                 Bug #42
1117                 Fixes: Z
1118                 Acked-by= Z
1119                 Acked-by= Peff
1120                 Acked-by= Peff
1121                 Acked-by= Junio
1122                 Acked-by= Peff
1123                 Reviewed-by:
1124                 Signed-off-by: Z
1125                 Tested-by: Jakub
1126                 Tested-by: Johannes
1127         EOF
1128         git interpret-trailers --trailer "ack: Peff" \
1129                 --trailer "Acked-by= Peff" --trailer "review:" \
1130                 --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
1131                 --trailer "bug: 42" --trailer "Tested-by: Johannes" \
1132                 --trailer "ack: Peff" <complex_message >actual &&
1133         test_cmp expected actual
1134 '
1135
1136 test_expect_success 'with simple command' '
1137         git config trailer.sign.key "Signed-off-by: " &&
1138         git config trailer.sign.where "after" &&
1139         git config trailer.sign.ifExists "addIfDifferentNeighbor" &&
1140         git config trailer.sign.command "echo \"A U Thor <author@example.com>\"" &&
1141         cat complex_message_body >expected &&
1142         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
1143                 Fixes: Z
1144                 Acked-by= Z
1145                 Reviewed-by:
1146                 Signed-off-by: Z
1147                 Signed-off-by: A U Thor <author@example.com>
1148         EOF
1149         git interpret-trailers --trailer "review:" --trailer "fix=22" \
1150                 <complex_message >actual &&
1151         test_cmp expected actual
1152 '
1153
1154 test_expect_success 'with command using commiter information' '
1155         git config trailer.sign.ifExists "addIfDifferent" &&
1156         git config trailer.sign.command "echo \"\$GIT_COMMITTER_NAME <\$GIT_COMMITTER_EMAIL>\"" &&
1157         cat complex_message_body >expected &&
1158         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
1159                 Fixes: Z
1160                 Acked-by= Z
1161                 Reviewed-by:
1162                 Signed-off-by: Z
1163                 Signed-off-by: C O Mitter <committer@example.com>
1164         EOF
1165         git interpret-trailers --trailer "review:" --trailer "fix=22" \
1166                 <complex_message >actual &&
1167         test_cmp expected actual
1168 '
1169
1170 test_expect_success 'with command using author information' '
1171         git config trailer.sign.key "Signed-off-by: " &&
1172         git config trailer.sign.where "after" &&
1173         git config trailer.sign.ifExists "addIfDifferentNeighbor" &&
1174         git config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
1175         cat complex_message_body >expected &&
1176         sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
1177                 Fixes: Z
1178                 Acked-by= Z
1179                 Reviewed-by:
1180                 Signed-off-by: Z
1181                 Signed-off-by: A U Thor <author@example.com>
1182         EOF
1183         git interpret-trailers --trailer "review:" --trailer "fix=22" \
1184                 <complex_message >actual &&
1185         test_cmp expected actual
1186 '
1187
1188 test_expect_success 'setup a commit' '
1189         echo "Content of the first commit." > a.txt &&
1190         git add a.txt &&
1191         git commit -m "Add file a.txt"
1192 '
1193
1194 test_expect_success 'with command using $ARG' '
1195         git config trailer.fix.ifExists "replace" &&
1196         git config trailer.fix.command "git log -1 --oneline --format=\"%h (%s)\" --abbrev-commit --abbrev=14 \$ARG" &&
1197         FIXED=$(git log -1 --oneline --format="%h (%s)" --abbrev-commit --abbrev=14 HEAD) &&
1198         cat complex_message_body >expected &&
1199         sed -e "s/ Z\$/ /" >>expected <<-EOF &&
1200                 Fixes: $FIXED
1201                 Acked-by= Z
1202                 Reviewed-by:
1203                 Signed-off-by: Z
1204                 Signed-off-by: A U Thor <author@example.com>
1205         EOF
1206         git interpret-trailers --trailer "review:" --trailer "fix=HEAD" \
1207                 <complex_message >actual &&
1208         test_cmp expected actual
1209 '
1210
1211 test_expect_success 'with failing command using $ARG' '
1212         git config trailer.fix.ifExists "replace" &&
1213         git config trailer.fix.command "false \$ARG" &&
1214         cat complex_message_body >expected &&
1215         sed -e "s/ Z\$/ /" >>expected <<-EOF &&
1216                 Fixes: Z
1217                 Acked-by= Z
1218                 Reviewed-by:
1219                 Signed-off-by: Z
1220                 Signed-off-by: A U Thor <author@example.com>
1221         EOF
1222         git interpret-trailers --trailer "review:" --trailer "fix=HEAD" \
1223                 <complex_message >actual &&
1224         test_cmp expected actual
1225 '
1226
1227 test_expect_success 'with empty tokens' '
1228         git config --unset trailer.fix.command &&
1229         cat >expected <<-EOF &&
1230
1231                 Signed-off-by: A U Thor <author@example.com>
1232         EOF
1233         git interpret-trailers --trailer ":" --trailer ":test" >actual <<-EOF &&
1234         EOF
1235         test_cmp expected actual
1236 '
1237
1238 test_expect_success 'with command but no key' '
1239         git config --unset trailer.sign.key &&
1240         cat >expected <<-EOF &&
1241
1242                 sign: A U Thor <author@example.com>
1243         EOF
1244         git interpret-trailers >actual <<-EOF &&
1245         EOF
1246         test_cmp expected actual
1247 '
1248
1249 test_expect_success 'with no command and no key' '
1250         git config --unset trailer.review.key &&
1251         cat >expected <<-EOF &&
1252
1253                 review: Junio
1254                 sign: A U Thor <author@example.com>
1255         EOF
1256         git interpret-trailers --trailer "review:Junio" >actual <<-EOF &&
1257         EOF
1258         test_cmp expected actual
1259 '
1260
1261 test_expect_success 'with cut line' '
1262         cat >expected <<-\EOF &&
1263                 my subject
1264
1265                 review: Brian
1266                 sign: A U Thor <author@example.com>
1267                 # ------------------------ >8 ------------------------
1268                 ignore this
1269         EOF
1270         git interpret-trailers --trailer review:Brian >actual <<-\EOF &&
1271                 my subject
1272                 # ------------------------ >8 ------------------------
1273                 ignore this
1274         EOF
1275         test_cmp expected actual
1276 '
1277
1278 test_done