fed053a7ecb99c6cb4f18e8336e1d97154c624c4
   1#!/bin/sh
   2#
   3# Copyright (c) 2013, 2014 Christian Couder
   4#
   5
   6test_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
  13test_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
  54test_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
  66test_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
  78test_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
  90test_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 "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
 102test_expect_success 'with config setup' '
 103        git config trailer.ack.key "Acked-by: " &&
 104        cat >expected <<-\EOF &&
 105
 106                Acked-by: Peff
 107        EOF
 108        git interpret-trailers --trim-empty --trailer "ack = Peff" empty >actual &&
 109        test_cmp expected actual &&
 110        git interpret-trailers --trim-empty --trailer "Acked-by = Peff" empty >actual &&
 111        test_cmp expected actual &&
 112        git interpret-trailers --trim-empty --trailer "Acked-by :Peff" empty >actual &&
 113        test_cmp expected actual
 114'
 115
 116test_expect_success 'with config setup and ":=" as separators' '
 117        git config trailer.separators ":=" &&
 118        git config trailer.ack.key "Acked-by= " &&
 119        cat >expected <<-\EOF &&
 120
 121                Acked-by= Peff
 122        EOF
 123        git interpret-trailers --trim-empty --trailer "ack = Peff" empty >actual &&
 124        test_cmp expected actual &&
 125        git interpret-trailers --trim-empty --trailer "Acked-by= Peff" empty >actual &&
 126        test_cmp expected actual &&
 127        git interpret-trailers --trim-empty --trailer "Acked-by : Peff" empty >actual &&
 128        test_cmp expected actual
 129'
 130
 131test_expect_success 'with config setup and "%" as separators' '
 132        git config trailer.separators "%" &&
 133        cat >expected <<-\EOF &&
 134
 135                bug% 42
 136                count% 10
 137                bug% 422
 138        EOF
 139        git interpret-trailers --trim-empty --trailer "bug = 42" \
 140                --trailer count%10 --trailer "test: stuff" \
 141                --trailer "bug % 422" empty >actual &&
 142        test_cmp expected actual
 143'
 144
 145test_expect_success 'with "%" as separators and a message with trailers' '
 146        cat >special_message <<-\EOF &&
 147                Special Message
 148
 149                bug% 42
 150                count% 10
 151                bug% 422
 152        EOF
 153        cat >expected <<-\EOF &&
 154                Special Message
 155
 156                bug% 42
 157                count% 10
 158                bug% 422
 159                count% 100
 160        EOF
 161        git interpret-trailers --trailer count%100 \
 162                special_message >actual &&
 163        test_cmp expected actual
 164'
 165
 166test_expect_success 'with config setup and ":=#" as separators' '
 167        git config trailer.separators ":=#" &&
 168        git config trailer.bug.key "Bug #" &&
 169        cat >expected <<-\EOF &&
 170
 171                Bug #42
 172        EOF
 173        git interpret-trailers --trim-empty --trailer "bug = 42" empty >actual &&
 174        test_cmp expected actual
 175'
 176
 177test_expect_success 'with commit basic message' '
 178        cat basic_message >expected &&
 179        echo >>expected &&
 180        git interpret-trailers <basic_message >actual &&
 181        test_cmp expected actual
 182'
 183
 184test_expect_success 'with basic patch' '
 185        cat basic_message >input &&
 186        cat basic_patch >>input &&
 187        cat basic_message >expected &&
 188        echo >>expected &&
 189        cat basic_patch >>expected &&
 190        git interpret-trailers <input >actual &&
 191        test_cmp expected actual
 192'
 193
 194test_expect_success 'with commit complex message as argument' '
 195        cat complex_message_body complex_message_trailers >complex_message &&
 196        cat complex_message_body >expected &&
 197        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 198                Fixes: Z
 199                Acked-by= Z
 200                Reviewed-by: Z
 201                Signed-off-by: Z
 202        EOF
 203        git interpret-trailers complex_message >actual &&
 204        test_cmp expected actual
 205'
 206
 207test_expect_success 'with 2 files arguments' '
 208        cat basic_message >>expected &&
 209        echo >>expected &&
 210        cat basic_patch >>expected &&
 211        git interpret-trailers complex_message input >actual &&
 212        test_cmp expected actual
 213'
 214
 215test_expect_success 'with message that has comments' '
 216        cat basic_message >>message_with_comments &&
 217        sed -e "s/ Z\$/ /" >>message_with_comments <<-\EOF &&
 218                # comment
 219
 220                # other comment
 221                Cc: Z
 222                # yet another comment
 223                Reviewed-by: Johan
 224                Reviewed-by: Z
 225                # last comment
 226
 227        EOF
 228        cat basic_patch >>message_with_comments &&
 229        cat basic_message >expected &&
 230        cat >>expected <<-\EOF &&
 231                # comment
 232
 233                Reviewed-by: Johan
 234                Cc: Peff
 235                # last comment
 236
 237        EOF
 238        cat basic_patch >>expected &&
 239        git interpret-trailers --trim-empty --trailer "Cc: Peff" message_with_comments >actual &&
 240        test_cmp expected actual
 241'
 242
 243test_expect_success 'with commit complex message and trailer args' '
 244        cat complex_message_body >expected &&
 245        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 246                Fixes: Z
 247                Acked-by= Z
 248                Reviewed-by: Z
 249                Signed-off-by: Z
 250                Acked-by= Peff
 251                Bug #42
 252        EOF
 253        git interpret-trailers --trailer "ack: Peff" \
 254                --trailer "bug: 42" <complex_message >actual &&
 255        test_cmp expected actual
 256'
 257
 258test_expect_success 'with complex patch, args and --trim-empty' '
 259        cat complex_message >complex_patch &&
 260        cat basic_patch >>complex_patch &&
 261        cat complex_message_body >expected &&
 262        cat >>expected <<-\EOF &&
 263                Acked-by= Peff
 264                Bug #42
 265        EOF
 266        cat basic_patch >>expected &&
 267        git interpret-trailers --trim-empty --trailer "ack: Peff" \
 268                --trailer "bug: 42" <complex_patch >actual &&
 269        test_cmp expected actual
 270'
 271
 272test_expect_success 'using "where = before"' '
 273        git config trailer.bug.where "before" &&
 274        cat complex_message_body >expected &&
 275        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 276                Bug #42
 277                Fixes: Z
 278                Acked-by= Z
 279                Reviewed-by: Z
 280                Signed-off-by: Z
 281                Acked-by= Peff
 282        EOF
 283        git interpret-trailers --trailer "ack: Peff" \
 284                --trailer "bug: 42" complex_message >actual &&
 285        test_cmp expected actual
 286'
 287
 288test_expect_success 'using "where = after"' '
 289        git config trailer.ack.where "after" &&
 290        cat complex_message_body >expected &&
 291        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 292                Bug #42
 293                Fixes: Z
 294                Acked-by= Z
 295                Acked-by= Peff
 296                Reviewed-by: Z
 297                Signed-off-by: Z
 298        EOF
 299        git interpret-trailers --trailer "ack: Peff" \
 300                --trailer "bug: 42" complex_message >actual &&
 301        test_cmp expected actual
 302'
 303
 304test_expect_success 'using "where = end"' '
 305        git config trailer.review.key "Reviewed-by" &&
 306        git config trailer.review.where "end" &&
 307        cat complex_message_body >expected &&
 308        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 309                Fixes: Z
 310                Acked-by= Z
 311                Acked-by= Peff
 312                Reviewed-by: Z
 313                Signed-off-by: Z
 314                Reviewed-by: Junio
 315                Reviewed-by: Johannes
 316        EOF
 317        git interpret-trailers --trailer "ack: Peff" \
 318                --trailer "Reviewed-by: Junio" --trailer "Reviewed-by: Johannes" \
 319                complex_message >actual &&
 320        test_cmp expected actual
 321'
 322
 323test_expect_success 'using "where = start"' '
 324        git config trailer.review.key "Reviewed-by" &&
 325        git config trailer.review.where "start" &&
 326        cat complex_message_body >expected &&
 327        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 328                Reviewed-by: Johannes
 329                Reviewed-by: Junio
 330                Fixes: Z
 331                Acked-by= Z
 332                Acked-by= Peff
 333                Reviewed-by: Z
 334                Signed-off-by: Z
 335        EOF
 336        git interpret-trailers --trailer "ack: Peff" \
 337                --trailer "Reviewed-by: Junio" --trailer "Reviewed-by: Johannes" \
 338                complex_message >actual &&
 339        test_cmp expected actual
 340'
 341
 342test_expect_success 'using "where = before" for a token in the middle of the message' '
 343        git config trailer.review.key "Reviewed-by:" &&
 344        git config trailer.review.where "before" &&
 345        cat complex_message_body >expected &&
 346        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 347                Bug #42
 348                Fixes: Z
 349                Acked-by= Z
 350                Acked-by= Peff
 351                Reviewed-by:Johan
 352                Reviewed-by:
 353                Signed-off-by: Z
 354        EOF
 355        git interpret-trailers --trailer "ack: Peff" --trailer "bug: 42" \
 356                --trailer "review: Johan" <complex_message >actual &&
 357        test_cmp expected actual
 358'
 359
 360test_expect_success 'using "where = before" and --trim-empty' '
 361        cat complex_message_body >expected &&
 362        cat >>expected <<-\EOF &&
 363                Bug #46
 364                Bug #42
 365                Acked-by= Peff
 366                Reviewed-by:Johan
 367        EOF
 368        git interpret-trailers --trim-empty --trailer "ack: Peff" \
 369                --trailer "bug: 42" --trailer "review: Johan" \
 370                --trailer "Bug: 46" <complex_message >actual &&
 371        test_cmp expected actual
 372'
 373
 374test_expect_success 'the default is "ifExists = addIfDifferentNeighbor"' '
 375        cat complex_message_body >expected &&
 376        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 377                Bug #42
 378                Fixes: Z
 379                Acked-by= Z
 380                Acked-by= Peff
 381                Acked-by= Junio
 382                Acked-by= Peff
 383                Reviewed-by:
 384                Signed-off-by: Z
 385        EOF
 386        git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
 387                --trailer "ack: Junio" --trailer "bug: 42" --trailer "ack: Peff" \
 388                --trailer "ack: Peff" <complex_message >actual &&
 389        test_cmp expected actual
 390'
 391
 392test_expect_success 'default "ifExists" is now "addIfDifferent"' '
 393        git config trailer.ifexists "addIfDifferent" &&
 394        cat complex_message_body >expected &&
 395        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 396                Bug #42
 397                Fixes: Z
 398                Acked-by= Z
 399                Acked-by= Peff
 400                Acked-by= Junio
 401                Reviewed-by:
 402                Signed-off-by: Z
 403        EOF
 404        git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
 405                --trailer "ack: Junio" --trailer "bug: 42" --trailer "ack: Peff" \
 406                --trailer "ack: Peff" <complex_message >actual &&
 407        test_cmp expected actual
 408'
 409
 410test_expect_success 'using "ifExists = addIfDifferent" with "where = end"' '
 411        git config trailer.ack.ifExists "addIfDifferent" &&
 412        git config trailer.ack.where "end" &&
 413        cat complex_message_body >expected &&
 414        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 415                Bug #42
 416                Fixes: Z
 417                Acked-by= Z
 418                Reviewed-by:
 419                Signed-off-by: Z
 420                Acked-by= Peff
 421        EOF
 422        git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
 423                --trailer "bug: 42" --trailer "ack: Peff" \
 424                <complex_message >actual &&
 425        test_cmp expected actual
 426'
 427
 428test_expect_success 'using "ifExists = addIfDifferent" with "where = before"' '
 429        git config trailer.ack.ifExists "addIfDifferent" &&
 430        git config trailer.ack.where "before" &&
 431        cat complex_message_body >expected &&
 432        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 433                Bug #42
 434                Fixes: Z
 435                Acked-by= Peff
 436                Acked-by= Z
 437                Reviewed-by:
 438                Signed-off-by: Z
 439        EOF
 440        git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
 441                --trailer "bug: 42" --trailer "ack: Peff" \
 442                <complex_message >actual &&
 443        test_cmp expected actual
 444'
 445
 446test_expect_success 'using "ifExists = addIfDifferentNeighbor" with "where = end"' '
 447        git config trailer.ack.ifExists "addIfDifferentNeighbor" &&
 448        git config trailer.ack.where "end" &&
 449        cat complex_message_body >expected &&
 450        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 451                Bug #42
 452                Fixes: Z
 453                Acked-by= Z
 454                Reviewed-by:
 455                Signed-off-by: Z
 456                Acked-by= Peff
 457                Acked-by= Junio
 458                Tested-by: Jakub
 459                Acked-by= Junio
 460                Acked-by= Peff
 461        EOF
 462        git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
 463                --trailer "ack: Junio" --trailer "bug: 42" \
 464                --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
 465                --trailer "ack: Junio" --trailer "ack: Peff" <complex_message >actual &&
 466        test_cmp expected actual
 467'
 468
 469test_expect_success 'using "ifExists = addIfDifferentNeighbor"  with "where = after"' '
 470        git config trailer.ack.ifExists "addIfDifferentNeighbor" &&
 471        git config trailer.ack.where "after" &&
 472        cat complex_message_body >expected &&
 473        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 474                Bug #42
 475                Fixes: Z
 476                Acked-by= Z
 477                Acked-by= Peff
 478                Acked-by= Junio
 479                Acked-by= Peff
 480                Reviewed-by:
 481                Signed-off-by: Z
 482                Tested-by: Jakub
 483        EOF
 484        git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
 485                --trailer "ack: Junio" --trailer "bug: 42" \
 486                --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
 487                --trailer "ack: Junio" --trailer "ack: Peff" <complex_message >actual &&
 488        test_cmp expected actual
 489'
 490
 491test_expect_success 'using "ifExists = addIfDifferentNeighbor" and --trim-empty' '
 492        git config trailer.ack.ifExists "addIfDifferentNeighbor" &&
 493        cat complex_message_body >expected &&
 494        cat >>expected <<-\EOF &&
 495                Bug #42
 496                Acked-by= Peff
 497                Acked-by= Junio
 498                Acked-by= Peff
 499        EOF
 500        git interpret-trailers --trim-empty --trailer "ack: Peff" \
 501                --trailer "Acked-by= Peff" --trailer "review:" \
 502                --trailer "ack: Junio" --trailer "bug: 42" \
 503                --trailer "ack: Peff" <complex_message >actual &&
 504        test_cmp expected actual
 505'
 506
 507test_expect_success 'using "ifExists = add" with "where = end"' '
 508        git config trailer.ack.ifExists "add" &&
 509        git config trailer.ack.where "end" &&
 510        cat complex_message_body >expected &&
 511        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 512                Bug #42
 513                Fixes: Z
 514                Acked-by= Z
 515                Reviewed-by:
 516                Signed-off-by: Z
 517                Acked-by= Peff
 518                Acked-by= Peff
 519                Tested-by: Jakub
 520                Acked-by= Junio
 521                Tested-by: Johannes
 522                Acked-by= Peff
 523        EOF
 524        git interpret-trailers --trailer "ack: Peff" \
 525                --trailer "Acked-by= Peff" --trailer "review:" \
 526                --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
 527                --trailer "bug: 42" --trailer "Tested-by: Johannes" \
 528                --trailer "ack: Peff" <complex_message >actual &&
 529        test_cmp expected actual
 530'
 531
 532test_expect_success 'using "ifExists = add" with "where = after"' '
 533        git config trailer.ack.ifExists "add" &&
 534        git config trailer.ack.where "after" &&
 535        cat complex_message_body >expected &&
 536        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 537                Bug #42
 538                Fixes: Z
 539                Acked-by= Z
 540                Acked-by= Peff
 541                Acked-by= Peff
 542                Acked-by= Junio
 543                Acked-by= Peff
 544                Reviewed-by:
 545                Signed-off-by: Z
 546        EOF
 547        git interpret-trailers --trailer "ack: Peff" \
 548                --trailer "Acked-by= Peff" --trailer "review:" \
 549                --trailer "ack: Junio" --trailer "bug: 42" \
 550                --trailer "ack: Peff" <complex_message >actual &&
 551        test_cmp expected actual
 552'
 553
 554test_expect_success 'using "ifExists = replace"' '
 555        git config trailer.fix.key "Fixes: " &&
 556        git config trailer.fix.ifExists "replace" &&
 557        cat complex_message_body >expected &&
 558        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 559                Bug #42
 560                Acked-by= Z
 561                Acked-by= Junio
 562                Acked-by= Peff
 563                Reviewed-by:
 564                Signed-off-by: Z
 565                Fixes: 22
 566        EOF
 567        git interpret-trailers --trailer "review:" \
 568                --trailer "fix=53" --trailer "ack: Junio" --trailer "fix=22" \
 569                --trailer "bug: 42" --trailer "ack: Peff" \
 570                <complex_message >actual &&
 571        test_cmp expected actual
 572'
 573
 574test_expect_success 'using "ifExists = replace" with "where = after"' '
 575        git config trailer.fix.where "after" &&
 576        cat complex_message_body >expected &&
 577        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 578                Bug #42
 579                Fixes: 22
 580                Acked-by= Z
 581                Acked-by= Junio
 582                Acked-by= Peff
 583                Reviewed-by:
 584                Signed-off-by: Z
 585        EOF
 586        git interpret-trailers --trailer "review:" \
 587                --trailer "fix=53" --trailer "ack: Junio" --trailer "fix=22" \
 588                --trailer "bug: 42" --trailer "ack: Peff" \
 589                <complex_message >actual &&
 590        test_cmp expected actual
 591'
 592
 593test_expect_success 'using "ifExists = doNothing"' '
 594        git config trailer.fix.ifExists "doNothing" &&
 595        cat complex_message_body >expected &&
 596        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 597                Bug #42
 598                Fixes: Z
 599                Acked-by= Z
 600                Acked-by= Junio
 601                Acked-by= Peff
 602                Reviewed-by:
 603                Signed-off-by: Z
 604        EOF
 605        git interpret-trailers --trailer "review:" --trailer "fix=53" \
 606                --trailer "ack: Junio" --trailer "fix=22" \
 607                --trailer "bug: 42" --trailer "ack: Peff" \
 608                <complex_message >actual &&
 609        test_cmp expected actual
 610'
 611
 612test_expect_success 'the default is "ifMissing = add"' '
 613        git config trailer.cc.key "Cc: " &&
 614        git config trailer.cc.where "before" &&
 615        cat complex_message_body >expected &&
 616        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 617                Bug #42
 618                Cc: Linus
 619                Fixes: Z
 620                Acked-by= Z
 621                Acked-by= Junio
 622                Acked-by= Peff
 623                Reviewed-by:
 624                Signed-off-by: Z
 625        EOF
 626        git interpret-trailers --trailer "review:" --trailer "fix=53" \
 627                --trailer "cc=Linus" --trailer "ack: Junio" \
 628                --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
 629                <complex_message >actual &&
 630        test_cmp expected actual
 631'
 632
 633test_expect_success 'when default "ifMissing" is "doNothing"' '
 634        git config trailer.ifmissing "doNothing" &&
 635        cat complex_message_body >expected &&
 636        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 637                Fixes: Z
 638                Acked-by= Z
 639                Acked-by= Junio
 640                Acked-by= Peff
 641                Reviewed-by:
 642                Signed-off-by: Z
 643        EOF
 644        git interpret-trailers --trailer "review:" --trailer "fix=53" \
 645                --trailer "cc=Linus" --trailer "ack: Junio" \
 646                --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
 647                <complex_message >actual &&
 648        test_cmp expected actual &&
 649        git config trailer.ifmissing "add"
 650'
 651
 652test_expect_success 'using "ifMissing = add" with "where = end"' '
 653        git config trailer.cc.key "Cc: " &&
 654        git config trailer.cc.where "end" &&
 655        git config trailer.cc.ifMissing "add" &&
 656        cat complex_message_body >expected &&
 657        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 658                Bug #42
 659                Fixes: Z
 660                Acked-by= Z
 661                Acked-by= Junio
 662                Acked-by= Peff
 663                Reviewed-by:
 664                Signed-off-by: Z
 665                Cc: Linus
 666        EOF
 667        git interpret-trailers --trailer "review:" --trailer "fix=53" \
 668                --trailer "ack: Junio" --trailer "fix=22" \
 669                --trailer "bug: 42" --trailer "cc=Linus" --trailer "ack: Peff" \
 670                <complex_message >actual &&
 671        test_cmp expected actual
 672'
 673
 674test_expect_success 'using "ifMissing = add" with "where = before"' '
 675        git config trailer.cc.key "Cc: " &&
 676        git config trailer.cc.where "before" &&
 677        git config trailer.cc.ifMissing "add" &&
 678        cat complex_message_body >expected &&
 679        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 680                Cc: Linus
 681                Bug #42
 682                Fixes: Z
 683                Acked-by= Z
 684                Acked-by= Junio
 685                Acked-by= Peff
 686                Reviewed-by:
 687                Signed-off-by: Z
 688        EOF
 689        git interpret-trailers --trailer "review:" --trailer "fix=53" \
 690                --trailer "ack: Junio" --trailer "fix=22" \
 691                --trailer "bug: 42" --trailer "cc=Linus" --trailer "ack: Peff" \
 692                <complex_message >actual &&
 693        test_cmp expected actual
 694'
 695
 696test_expect_success 'using "ifMissing = doNothing"' '
 697        git config trailer.cc.ifMissing "doNothing" &&
 698        cat complex_message_body >expected &&
 699        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 700                Bug #42
 701                Fixes: Z
 702                Acked-by= Z
 703                Acked-by= Junio
 704                Acked-by= Peff
 705                Reviewed-by:
 706                Signed-off-by: Z
 707        EOF
 708        git interpret-trailers --trailer "review:" --trailer "fix=53" \
 709                --trailer "cc=Linus" --trailer "ack: Junio" \
 710                --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
 711                <complex_message >actual &&
 712        test_cmp expected actual
 713'
 714
 715test_expect_success 'default "where" is now "after"' '
 716        git config trailer.where "after" &&
 717        git config --unset trailer.ack.where &&
 718        cat complex_message_body >expected &&
 719        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 720                Bug #42
 721                Fixes: Z
 722                Acked-by= Z
 723                Acked-by= Peff
 724                Acked-by= Peff
 725                Acked-by= Junio
 726                Acked-by= Peff
 727                Reviewed-by:
 728                Signed-off-by: Z
 729                Tested-by: Jakub
 730                Tested-by: Johannes
 731        EOF
 732        git interpret-trailers --trailer "ack: Peff" \
 733                --trailer "Acked-by= Peff" --trailer "review:" \
 734                --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
 735                --trailer "bug: 42" --trailer "Tested-by: Johannes" \
 736                --trailer "ack: Peff" <complex_message >actual &&
 737        test_cmp expected actual
 738'
 739
 740test_expect_success 'with simple command' '
 741        git config trailer.sign.key "Signed-off-by: " &&
 742        git config trailer.sign.where "after" &&
 743        git config trailer.sign.ifExists "addIfDifferentNeighbor" &&
 744        git config trailer.sign.command "echo \"A U Thor <author@example.com>\"" &&
 745        cat complex_message_body >expected &&
 746        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 747                Fixes: Z
 748                Acked-by= Z
 749                Reviewed-by:
 750                Signed-off-by: Z
 751                Signed-off-by: A U Thor <author@example.com>
 752        EOF
 753        git interpret-trailers --trailer "review:" --trailer "fix=22" \
 754                <complex_message >actual &&
 755        test_cmp expected actual
 756'
 757
 758test_expect_success 'with command using commiter information' '
 759        git config trailer.sign.ifExists "addIfDifferent" &&
 760        git config trailer.sign.command "echo \"\$GIT_COMMITTER_NAME <\$GIT_COMMITTER_EMAIL>\"" &&
 761        cat complex_message_body >expected &&
 762        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 763                Fixes: Z
 764                Acked-by= Z
 765                Reviewed-by:
 766                Signed-off-by: Z
 767                Signed-off-by: C O Mitter <committer@example.com>
 768        EOF
 769        git interpret-trailers --trailer "review:" --trailer "fix=22" \
 770                <complex_message >actual &&
 771        test_cmp expected actual
 772'
 773
 774test_expect_success 'with command using author information' '
 775        git config trailer.sign.key "Signed-off-by: " &&
 776        git config trailer.sign.where "after" &&
 777        git config trailer.sign.ifExists "addIfDifferentNeighbor" &&
 778        git config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
 779        cat complex_message_body >expected &&
 780        sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
 781                Fixes: Z
 782                Acked-by= Z
 783                Reviewed-by:
 784                Signed-off-by: Z
 785                Signed-off-by: A U Thor <author@example.com>
 786        EOF
 787        git interpret-trailers --trailer "review:" --trailer "fix=22" \
 788                <complex_message >actual &&
 789        test_cmp expected actual
 790'
 791
 792test_expect_success 'setup a commit' '
 793        echo "Content of the first commit." > a.txt &&
 794        git add a.txt &&
 795        git commit -m "Add file a.txt"
 796'
 797
 798test_expect_success 'with command using $ARG' '
 799        git config trailer.fix.ifExists "replace" &&
 800        git config trailer.fix.command "git log -1 --oneline --format=\"%h (%s)\" --abbrev-commit --abbrev=14 \$ARG" &&
 801        FIXED=$(git log -1 --oneline --format="%h (%s)" --abbrev-commit --abbrev=14 HEAD) &&
 802        cat complex_message_body >expected &&
 803        sed -e "s/ Z\$/ /" >>expected <<-EOF &&
 804                Fixes: $FIXED
 805                Acked-by= Z
 806                Reviewed-by:
 807                Signed-off-by: Z
 808                Signed-off-by: A U Thor <author@example.com>
 809        EOF
 810        git interpret-trailers --trailer "review:" --trailer "fix=HEAD" \
 811                <complex_message >actual &&
 812        test_cmp expected actual
 813'
 814
 815test_expect_success 'with failing command using $ARG' '
 816        git config trailer.fix.ifExists "replace" &&
 817        git config trailer.fix.command "false \$ARG" &&
 818        cat complex_message_body >expected &&
 819        sed -e "s/ Z\$/ /" >>expected <<-EOF &&
 820                Fixes: Z
 821                Acked-by= Z
 822                Reviewed-by:
 823                Signed-off-by: Z
 824                Signed-off-by: A U Thor <author@example.com>
 825        EOF
 826        git interpret-trailers --trailer "review:" --trailer "fix=HEAD" \
 827                <complex_message >actual &&
 828        test_cmp expected actual
 829'
 830
 831test_expect_success 'with empty tokens' '
 832        git config --unset trailer.fix.command &&
 833        cat >expected <<-EOF &&
 834
 835                Signed-off-by: A U Thor <author@example.com>
 836        EOF
 837        git interpret-trailers --trailer ":" --trailer ":test" >actual <<-EOF &&
 838        EOF
 839        test_cmp expected actual
 840'
 841
 842test_expect_success 'with command but no key' '
 843        git config --unset trailer.sign.key &&
 844        cat >expected <<-EOF &&
 845
 846                sign: A U Thor <author@example.com>
 847        EOF
 848        git interpret-trailers >actual <<-EOF &&
 849        EOF
 850        test_cmp expected actual
 851'
 852
 853test_expect_success 'with no command and no key' '
 854        git config --unset trailer.review.key &&
 855        cat >expected <<-EOF &&
 856
 857                review: Junio
 858                sign: A U Thor <author@example.com>
 859        EOF
 860        git interpret-trailers --trailer "review:Junio" >actual <<-EOF &&
 861        EOF
 862        test_cmp expected actual
 863'
 864
 865test_done