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