t / t3301-notes.shon commit diff: cache textconv output (d9bae1a)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Johannes E. Schindelin
   4#
   5
   6test_description='Test commit notes'
   7
   8. ./test-lib.sh
   9
  10cat > fake_editor.sh << \EOF
  11#!/bin/sh
  12echo "$MSG" > "$1"
  13echo "$MSG" >& 2
  14EOF
  15chmod a+x fake_editor.sh
  16GIT_EDITOR=./fake_editor.sh
  17export GIT_EDITOR
  18
  19test_expect_success 'cannot annotate non-existing HEAD' '
  20        (MSG=3 && export MSG && test_must_fail git notes add)
  21'
  22
  23test_expect_success setup '
  24        : > a1 &&
  25        git add a1 &&
  26        test_tick &&
  27        git commit -m 1st &&
  28        : > a2 &&
  29        git add a2 &&
  30        test_tick &&
  31        git commit -m 2nd
  32'
  33
  34test_expect_success 'need valid notes ref' '
  35        (MSG=1 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
  36         test_must_fail git notes add) &&
  37        (MSG=2 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
  38         test_must_fail git notes show)
  39'
  40
  41test_expect_success 'refusing to add notes in refs/heads/' '
  42        (MSG=1 GIT_NOTES_REF=refs/heads/bogus &&
  43         export MSG GIT_NOTES_REF &&
  44         test_must_fail git notes add)
  45'
  46
  47test_expect_success 'refusing to edit notes in refs/remotes/' '
  48        (MSG=1 GIT_NOTES_REF=refs/remotes/bogus &&
  49         export MSG GIT_NOTES_REF &&
  50         test_must_fail git notes edit)
  51'
  52
  53# 1 indicates caught gracefully by die, 128 means git-show barked
  54test_expect_success 'handle empty notes gracefully' '
  55        git notes show ; test 1 = $?
  56'
  57
  58test_expect_success 'create notes' '
  59        git config core.notesRef refs/notes/commits &&
  60        MSG=b4 git notes add &&
  61        test ! -f .git/NOTES_EDITMSG &&
  62        test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
  63        test b4 = $(git notes show) &&
  64        git show HEAD^ &&
  65        test_must_fail git notes show HEAD^
  66'
  67
  68test_expect_success 'edit existing notes' '
  69        MSG=b3 git notes edit &&
  70        test ! -f .git/NOTES_EDITMSG &&
  71        test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
  72        test b3 = $(git notes show) &&
  73        git show HEAD^ &&
  74        test_must_fail git notes show HEAD^
  75'
  76
  77test_expect_success 'cannot add note where one exists' '
  78        ! MSG=b2 git notes add &&
  79        test ! -f .git/NOTES_EDITMSG &&
  80        test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
  81        test b3 = $(git notes show) &&
  82        git show HEAD^ &&
  83        test_must_fail git notes show HEAD^
  84'
  85
  86test_expect_success 'can overwrite existing note with "git notes add -f"' '
  87        MSG=b1 git notes add -f &&
  88        test ! -f .git/NOTES_EDITMSG &&
  89        test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
  90        test b1 = $(git notes show) &&
  91        git show HEAD^ &&
  92        test_must_fail git notes show HEAD^
  93'
  94
  95cat > expect << EOF
  96commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
  97Author: A U Thor <author@example.com>
  98Date:   Thu Apr 7 15:14:13 2005 -0700
  99
 100    2nd
 101
 102Notes:
 103    b1
 104EOF
 105
 106test_expect_success 'show notes' '
 107        ! (git cat-file commit HEAD | grep b1) &&
 108        git log -1 > output &&
 109        test_cmp expect output
 110'
 111
 112test_expect_success 'create multi-line notes (setup)' '
 113        : > a3 &&
 114        git add a3 &&
 115        test_tick &&
 116        git commit -m 3rd &&
 117        MSG="b3
 118c3c3c3c3
 119d3d3d3" git notes add
 120'
 121
 122cat > expect-multiline << EOF
 123commit 1584215f1d29c65e99c6c6848626553fdd07fd75
 124Author: A U Thor <author@example.com>
 125Date:   Thu Apr 7 15:15:13 2005 -0700
 126
 127    3rd
 128
 129Notes:
 130    b3
 131    c3c3c3c3
 132    d3d3d3
 133EOF
 134
 135printf "\n" >> expect-multiline
 136cat expect >> expect-multiline
 137
 138test_expect_success 'show multi-line notes' '
 139        git log -2 > output &&
 140        test_cmp expect-multiline output
 141'
 142test_expect_success 'create -F notes (setup)' '
 143        : > a4 &&
 144        git add a4 &&
 145        test_tick &&
 146        git commit -m 4th &&
 147        echo "xyzzy" > note5 &&
 148        git notes add -F note5
 149'
 150
 151cat > expect-F << EOF
 152commit 15023535574ded8b1a89052b32673f84cf9582b8
 153Author: A U Thor <author@example.com>
 154Date:   Thu Apr 7 15:16:13 2005 -0700
 155
 156    4th
 157
 158Notes:
 159    xyzzy
 160EOF
 161
 162printf "\n" >> expect-F
 163cat expect-multiline >> expect-F
 164
 165test_expect_success 'show -F notes' '
 166        git log -3 > output &&
 167        test_cmp expect-F output
 168'
 169
 170cat >expect << EOF
 171commit 15023535574ded8b1a89052b32673f84cf9582b8
 172tree e070e3af51011e47b183c33adf9736736a525709
 173parent 1584215f1d29c65e99c6c6848626553fdd07fd75
 174author A U Thor <author@example.com> 1112912173 -0700
 175committer C O Mitter <committer@example.com> 1112912173 -0700
 176
 177    4th
 178EOF
 179test_expect_success 'git log --pretty=raw does not show notes' '
 180        git log -1 --pretty=raw >output &&
 181        test_cmp expect output
 182'
 183
 184cat >>expect <<EOF
 185
 186Notes:
 187    xyzzy
 188EOF
 189test_expect_success 'git log --show-notes' '
 190        git log -1 --pretty=raw --show-notes >output &&
 191        test_cmp expect output
 192'
 193
 194test_expect_success 'git log --no-notes' '
 195        git log -1 --no-notes >output &&
 196        ! grep xyzzy output
 197'
 198
 199test_expect_success 'git format-patch does not show notes' '
 200        git format-patch -1 --stdout >output &&
 201        ! grep xyzzy output
 202'
 203
 204test_expect_success 'git format-patch --show-notes does show notes' '
 205        git format-patch --show-notes -1 --stdout >output &&
 206        grep xyzzy output
 207'
 208
 209for pretty in \
 210        "" --pretty --pretty=raw --pretty=short --pretty=medium \
 211        --pretty=full --pretty=fuller --pretty=format:%s --oneline
 212do
 213        case "$pretty" in
 214        "") p= not= negate="" ;;
 215        ?*) p="$pretty" not=" not" negate="!" ;;
 216        esac
 217        test_expect_success "git show $pretty does$not show notes" '
 218                git show $p >output &&
 219                eval "$negate grep xyzzy output"
 220        '
 221done
 222
 223test_expect_success 'create -m notes (setup)' '
 224        : > a5 &&
 225        git add a5 &&
 226        test_tick &&
 227        git commit -m 5th &&
 228        git notes add -m spam -m "foo
 229bar
 230baz"
 231'
 232
 233whitespace="    "
 234cat > expect-m << EOF
 235commit bd1753200303d0a0344be813e504253b3d98e74d
 236Author: A U Thor <author@example.com>
 237Date:   Thu Apr 7 15:17:13 2005 -0700
 238
 239    5th
 240
 241Notes:
 242    spam
 243$whitespace
 244    foo
 245    bar
 246    baz
 247EOF
 248
 249printf "\n" >> expect-m
 250cat expect-F >> expect-m
 251
 252test_expect_success 'show -m notes' '
 253        git log -4 > output &&
 254        test_cmp expect-m output
 255'
 256
 257test_expect_success 'remove note with add -f -F /dev/null (setup)' '
 258        git notes add -f -F /dev/null
 259'
 260
 261cat > expect-rm-F << EOF
 262commit bd1753200303d0a0344be813e504253b3d98e74d
 263Author: A U Thor <author@example.com>
 264Date:   Thu Apr 7 15:17:13 2005 -0700
 265
 266    5th
 267EOF
 268
 269printf "\n" >> expect-rm-F
 270cat expect-F >> expect-rm-F
 271
 272test_expect_success 'verify note removal with -F /dev/null' '
 273        git log -4 > output &&
 274        test_cmp expect-rm-F output &&
 275        ! git notes show
 276'
 277
 278test_expect_success 'do not create empty note with -m "" (setup)' '
 279        git notes add -m ""
 280'
 281
 282test_expect_success 'verify non-creation of note with -m ""' '
 283        git log -4 > output &&
 284        test_cmp expect-rm-F output &&
 285        ! git notes show
 286'
 287
 288cat > expect-combine_m_and_F << EOF
 289foo
 290
 291xyzzy
 292
 293bar
 294
 295zyxxy
 296
 297baz
 298EOF
 299
 300test_expect_success 'create note with combination of -m and -F' '
 301        echo "xyzzy" > note_a &&
 302        echo "zyxxy" > note_b &&
 303        git notes add -m "foo" -F note_a -m "bar" -F note_b -m "baz" &&
 304        git notes show > output &&
 305        test_cmp expect-combine_m_and_F output
 306'
 307
 308test_expect_success 'remove note with "git notes remove" (setup)' '
 309        git notes remove HEAD^ &&
 310        git notes remove
 311'
 312
 313cat > expect-rm-remove << EOF
 314commit bd1753200303d0a0344be813e504253b3d98e74d
 315Author: A U Thor <author@example.com>
 316Date:   Thu Apr 7 15:17:13 2005 -0700
 317
 318    5th
 319
 320commit 15023535574ded8b1a89052b32673f84cf9582b8
 321Author: A U Thor <author@example.com>
 322Date:   Thu Apr 7 15:16:13 2005 -0700
 323
 324    4th
 325EOF
 326
 327printf "\n" >> expect-rm-remove
 328cat expect-multiline >> expect-rm-remove
 329
 330test_expect_success 'verify note removal with "git notes remove"' '
 331        git log -4 > output &&
 332        test_cmp expect-rm-remove output &&
 333        ! git notes show HEAD^
 334'
 335
 336cat > expect << EOF
 337c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd75
 338c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5
 339EOF
 340
 341test_expect_success 'list notes with "git notes list"' '
 342        git notes list > output &&
 343        test_cmp expect output
 344'
 345
 346test_expect_success 'list notes with "git notes"' '
 347        git notes > output &&
 348        test_cmp expect output
 349'
 350
 351cat > expect << EOF
 352c18dc024e14f08d18d14eea0d747ff692d66d6a3
 353EOF
 354
 355test_expect_success 'list specific note with "git notes list <object>"' '
 356        git notes list HEAD^^ > output &&
 357        test_cmp expect output
 358'
 359
 360cat > expect << EOF
 361EOF
 362
 363test_expect_success 'listing non-existing notes fails' '
 364        test_must_fail git notes list HEAD > output &&
 365        test_cmp expect output
 366'
 367
 368cat > expect << EOF
 369Initial set of notes
 370
 371More notes appended with git notes append
 372EOF
 373
 374test_expect_success 'append to existing note with "git notes append"' '
 375        git notes add -m "Initial set of notes" &&
 376        git notes append -m "More notes appended with git notes append" &&
 377        git notes show > output &&
 378        test_cmp expect output
 379'
 380
 381cat > expect_list << EOF
 382c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd75
 383c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5
 3844b6ad22357cc8a1296720574b8d2fbc22fab0671 bd1753200303d0a0344be813e504253b3d98e74d
 385EOF
 386
 387test_expect_success '"git notes list" does not expand to "git notes list HEAD"' '
 388        git notes list > output &&
 389        test_cmp expect_list output
 390'
 391
 392test_expect_success 'appending empty string does not change existing note' '
 393        git notes append -m "" &&
 394        git notes show > output &&
 395        test_cmp expect output
 396'
 397
 398test_expect_success 'git notes append == add when there is no existing note' '
 399        git notes remove HEAD &&
 400        test_must_fail git notes list HEAD &&
 401        git notes append -m "Initial set of notes
 402
 403More notes appended with git notes append" &&
 404        git notes show > output &&
 405        test_cmp expect output
 406'
 407
 408test_expect_success 'appending empty string to non-existing note does not create note' '
 409        git notes remove HEAD &&
 410        test_must_fail git notes list HEAD &&
 411        git notes append -m "" &&
 412        test_must_fail git notes list HEAD
 413'
 414
 415test_expect_success 'create other note on a different notes ref (setup)' '
 416        : > a6 &&
 417        git add a6 &&
 418        test_tick &&
 419        git commit -m 6th &&
 420        GIT_NOTES_REF="refs/notes/other" git notes add -m "other note"
 421'
 422
 423cat > expect-other << EOF
 424commit 387a89921c73d7ed72cd94d179c1c7048ca47756
 425Author: A U Thor <author@example.com>
 426Date:   Thu Apr 7 15:18:13 2005 -0700
 427
 428    6th
 429
 430Notes (other):
 431    other note
 432EOF
 433
 434cat > expect-not-other << EOF
 435commit 387a89921c73d7ed72cd94d179c1c7048ca47756
 436Author: A U Thor <author@example.com>
 437Date:   Thu Apr 7 15:18:13 2005 -0700
 438
 439    6th
 440EOF
 441
 442test_expect_success 'Do not show note on other ref by default' '
 443        git log -1 > output &&
 444        test_cmp expect-not-other output
 445'
 446
 447test_expect_success 'Do show note when ref is given in GIT_NOTES_REF' '
 448        GIT_NOTES_REF="refs/notes/other" git log -1 > output &&
 449        test_cmp expect-other output
 450'
 451
 452test_expect_success 'Do show note when ref is given in core.notesRef config' '
 453        git config core.notesRef "refs/notes/other" &&
 454        git log -1 > output &&
 455        test_cmp expect-other output
 456'
 457
 458test_expect_success 'Do not show note when core.notesRef is overridden' '
 459        GIT_NOTES_REF="refs/notes/wrong" git log -1 > output &&
 460        test_cmp expect-not-other output
 461'
 462
 463cat > expect-both << EOF
 464commit 387a89921c73d7ed72cd94d179c1c7048ca47756
 465Author: A U Thor <author@example.com>
 466Date:   Thu Apr 7 15:18:13 2005 -0700
 467
 468    6th
 469
 470Notes:
 471    order test
 472
 473Notes (other):
 474    other note
 475
 476commit bd1753200303d0a0344be813e504253b3d98e74d
 477Author: A U Thor <author@example.com>
 478Date:   Thu Apr 7 15:17:13 2005 -0700
 479
 480    5th
 481
 482Notes:
 483    replacement for deleted note
 484EOF
 485
 486test_expect_success 'Show all notes when notes.displayRef=refs/notes/*' '
 487        GIT_NOTES_REF=refs/notes/commits git notes add \
 488                -m"replacement for deleted note" HEAD^ &&
 489        GIT_NOTES_REF=refs/notes/commits git notes add -m"order test" &&
 490        git config --unset core.notesRef &&
 491        git config notes.displayRef "refs/notes/*" &&
 492        git log -2 > output &&
 493        test_cmp expect-both output
 494'
 495
 496test_expect_success 'core.notesRef is implicitly in notes.displayRef' '
 497        git config core.notesRef refs/notes/commits &&
 498        git config notes.displayRef refs/notes/other &&
 499        git log -2 > output &&
 500        test_cmp expect-both output
 501'
 502
 503test_expect_success 'notes.displayRef can be given more than once' '
 504        git config --unset core.notesRef &&
 505        git config notes.displayRef refs/notes/commits &&
 506        git config --add notes.displayRef refs/notes/other &&
 507        git log -2 > output &&
 508        test_cmp expect-both output
 509'
 510
 511cat > expect-both-reversed << EOF
 512commit 387a89921c73d7ed72cd94d179c1c7048ca47756
 513Author: A U Thor <author@example.com>
 514Date:   Thu Apr 7 15:18:13 2005 -0700
 515
 516    6th
 517
 518Notes (other):
 519    other note
 520
 521Notes:
 522    order test
 523EOF
 524
 525test_expect_success 'notes.displayRef respects order' '
 526        git config core.notesRef refs/notes/other &&
 527        git config --unset-all notes.displayRef &&
 528        git config notes.displayRef refs/notes/commits &&
 529        git log -1 > output &&
 530        test_cmp expect-both-reversed output
 531'
 532
 533test_expect_success 'GIT_NOTES_DISPLAY_REF works' '
 534        git config --unset-all core.notesRef &&
 535        git config --unset-all notes.displayRef &&
 536        GIT_NOTES_DISPLAY_REF=refs/notes/commits:refs/notes/other \
 537                git log -2 > output &&
 538        test_cmp expect-both output
 539'
 540
 541cat > expect-none << EOF
 542commit 387a89921c73d7ed72cd94d179c1c7048ca47756
 543Author: A U Thor <author@example.com>
 544Date:   Thu Apr 7 15:18:13 2005 -0700
 545
 546    6th
 547
 548commit bd1753200303d0a0344be813e504253b3d98e74d
 549Author: A U Thor <author@example.com>
 550Date:   Thu Apr 7 15:17:13 2005 -0700
 551
 552    5th
 553EOF
 554
 555test_expect_success 'GIT_NOTES_DISPLAY_REF overrides config' '
 556        git config notes.displayRef "refs/notes/*" &&
 557        GIT_NOTES_REF= GIT_NOTES_DISPLAY_REF= git log -2 > output &&
 558        test_cmp expect-none output
 559'
 560
 561test_expect_success '--show-notes=* adds to GIT_NOTES_DISPLAY_REF' '
 562        GIT_NOTES_REF= GIT_NOTES_DISPLAY_REF= git log --show-notes=* -2 > output &&
 563        test_cmp expect-both output
 564'
 565
 566cat > expect-commits << EOF
 567commit 387a89921c73d7ed72cd94d179c1c7048ca47756
 568Author: A U Thor <author@example.com>
 569Date:   Thu Apr 7 15:18:13 2005 -0700
 570
 571    6th
 572
 573Notes:
 574    order test
 575EOF
 576
 577test_expect_success '--no-standard-notes' '
 578        git log --no-standard-notes --show-notes=commits -1 > output &&
 579        test_cmp expect-commits output
 580'
 581
 582test_expect_success '--standard-notes' '
 583        git log --no-standard-notes --show-notes=commits \
 584                --standard-notes -2 > output &&
 585        test_cmp expect-both output
 586'
 587
 588test_expect_success '--show-notes=ref accumulates' '
 589        git log --show-notes=other --show-notes=commits \
 590                 --no-standard-notes -1 > output &&
 591        test_cmp expect-both-reversed output
 592'
 593
 594test_expect_success 'Allow notes on non-commits (trees, blobs, tags)' '
 595        git config core.notesRef refs/notes/other &&
 596        echo "Note on a tree" > expect
 597        git notes add -m "Note on a tree" HEAD: &&
 598        git notes show HEAD: > actual &&
 599        test_cmp expect actual &&
 600        echo "Note on a blob" > expect
 601        filename=$(git ls-tree --name-only HEAD | head -n1) &&
 602        git notes add -m "Note on a blob" HEAD:$filename &&
 603        git notes show HEAD:$filename > actual &&
 604        test_cmp expect actual &&
 605        echo "Note on a tag" > expect
 606        git tag -a -m "This is an annotated tag" foobar HEAD^ &&
 607        git notes add -m "Note on a tag" foobar &&
 608        git notes show foobar > actual &&
 609        test_cmp expect actual
 610'
 611
 612cat > expect << EOF
 613commit 2ede89468182a62d0bde2583c736089bcf7d7e92
 614Author: A U Thor <author@example.com>
 615Date:   Thu Apr 7 15:19:13 2005 -0700
 616
 617    7th
 618
 619Notes (other):
 620    other note
 621EOF
 622
 623test_expect_success 'create note from other note with "git notes add -C"' '
 624        : > a7 &&
 625        git add a7 &&
 626        test_tick &&
 627        git commit -m 7th &&
 628        git notes add -C $(git notes list HEAD^) &&
 629        git log -1 > actual &&
 630        test_cmp expect actual &&
 631        test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
 632'
 633
 634test_expect_success 'create note from non-existing note with "git notes add -C" fails' '
 635        : > a8 &&
 636        git add a8 &&
 637        test_tick &&
 638        git commit -m 8th &&
 639        test_must_fail git notes add -C deadbeef &&
 640        test_must_fail git notes list HEAD
 641'
 642
 643cat > expect << EOF
 644commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
 645Author: A U Thor <author@example.com>
 646Date:   Thu Apr 7 15:21:13 2005 -0700
 647
 648    9th
 649
 650Notes (other):
 651    yet another note
 652EOF
 653
 654test_expect_success 'create note from other note with "git notes add -c"' '
 655        : > a9 &&
 656        git add a9 &&
 657        test_tick &&
 658        git commit -m 9th &&
 659        MSG="yet another note" git notes add -c $(git notes list HEAD^^) &&
 660        git log -1 > actual &&
 661        test_cmp expect actual
 662'
 663
 664test_expect_success 'create note from non-existing note with "git notes add -c" fails' '
 665        : > a10 &&
 666        git add a10 &&
 667        test_tick &&
 668        git commit -m 10th &&
 669        test_must_fail MSG="yet another note" git notes add -c deadbeef &&
 670        test_must_fail git notes list HEAD
 671'
 672
 673cat > expect << EOF
 674commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
 675Author: A U Thor <author@example.com>
 676Date:   Thu Apr 7 15:21:13 2005 -0700
 677
 678    9th
 679
 680Notes (other):
 681    yet another note
 682$whitespace
 683    yet another note
 684EOF
 685
 686test_expect_success 'append to note from other note with "git notes append -C"' '
 687        git notes append -C $(git notes list HEAD^) HEAD^ &&
 688        git log -1 HEAD^ > actual &&
 689        test_cmp expect actual
 690'
 691
 692cat > expect << EOF
 693commit ffed603236bfa3891c49644257a83598afe8ae5a
 694Author: A U Thor <author@example.com>
 695Date:   Thu Apr 7 15:22:13 2005 -0700
 696
 697    10th
 698
 699Notes (other):
 700    other note
 701EOF
 702
 703test_expect_success 'create note from other note with "git notes append -c"' '
 704        MSG="other note" git notes append -c $(git notes list HEAD^) &&
 705        git log -1 > actual &&
 706        test_cmp expect actual
 707'
 708
 709cat > expect << EOF
 710commit ffed603236bfa3891c49644257a83598afe8ae5a
 711Author: A U Thor <author@example.com>
 712Date:   Thu Apr 7 15:22:13 2005 -0700
 713
 714    10th
 715
 716Notes (other):
 717    other note
 718$whitespace
 719    yet another note
 720EOF
 721
 722test_expect_success 'append to note from other note with "git notes append -c"' '
 723        MSG="yet another note" git notes append -c $(git notes list HEAD) &&
 724        git log -1 > actual &&
 725        test_cmp expect actual
 726'
 727
 728cat > expect << EOF
 729commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
 730Author: A U Thor <author@example.com>
 731Date:   Thu Apr 7 15:23:13 2005 -0700
 732
 733    11th
 734
 735Notes (other):
 736    other note
 737$whitespace
 738    yet another note
 739EOF
 740
 741test_expect_success 'copy note with "git notes copy"' '
 742        : > a11 &&
 743        git add a11 &&
 744        test_tick &&
 745        git commit -m 11th &&
 746        git notes copy HEAD^ HEAD &&
 747        git log -1 > actual &&
 748        test_cmp expect actual &&
 749        test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
 750'
 751
 752test_expect_success 'prevent overwrite with "git notes copy"' '
 753        test_must_fail git notes copy HEAD~2 HEAD &&
 754        git log -1 > actual &&
 755        test_cmp expect actual &&
 756        test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
 757'
 758
 759cat > expect << EOF
 760commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
 761Author: A U Thor <author@example.com>
 762Date:   Thu Apr 7 15:23:13 2005 -0700
 763
 764    11th
 765
 766Notes (other):
 767    yet another note
 768$whitespace
 769    yet another note
 770EOF
 771
 772test_expect_success 'allow overwrite with "git notes copy -f"' '
 773        git notes copy -f HEAD~2 HEAD &&
 774        git log -1 > actual &&
 775        test_cmp expect actual &&
 776        test "$(git notes list HEAD)" = "$(git notes list HEAD~2)"
 777'
 778
 779test_expect_success 'cannot copy note from object without notes' '
 780        : > a12 &&
 781        git add a12 &&
 782        test_tick &&
 783        git commit -m 12th &&
 784        : > a13 &&
 785        git add a13 &&
 786        test_tick &&
 787        git commit -m 13th &&
 788        test_must_fail git notes copy HEAD^ HEAD
 789'
 790
 791cat > expect << EOF
 792commit e5d4fb5698d564ab8c73551538ecaf2b0c666185
 793Author: A U Thor <author@example.com>
 794Date:   Thu Apr 7 15:25:13 2005 -0700
 795
 796    13th
 797
 798Notes (other):
 799    yet another note
 800$whitespace
 801    yet another note
 802
 803commit 7038787dfe22a14c3867ce816dbba39845359719
 804Author: A U Thor <author@example.com>
 805Date:   Thu Apr 7 15:24:13 2005 -0700
 806
 807    12th
 808
 809Notes (other):
 810    other note
 811$whitespace
 812    yet another note
 813EOF
 814
 815test_expect_success 'git notes copy --stdin' '
 816        (echo $(git rev-parse HEAD~3) $(git rev-parse HEAD^); \
 817        echo $(git rev-parse HEAD~2) $(git rev-parse HEAD)) |
 818        git notes copy --stdin &&
 819        git log -2 > output &&
 820        test_cmp expect output &&
 821        test "$(git notes list HEAD)" = "$(git notes list HEAD~2)" &&
 822        test "$(git notes list HEAD^)" = "$(git notes list HEAD~3)"
 823'
 824
 825cat > expect << EOF
 826commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
 827Author: A U Thor <author@example.com>
 828Date:   Thu Apr 7 15:27:13 2005 -0700
 829
 830    15th
 831
 832commit be28d8b4d9951ad940d229ee3b0b9ee3b1ec273d
 833Author: A U Thor <author@example.com>
 834Date:   Thu Apr 7 15:26:13 2005 -0700
 835
 836    14th
 837EOF
 838
 839test_expect_success 'git notes copy --for-rewrite (unconfigured)' '
 840        test_commit 14th &&
 841        test_commit 15th &&
 842        (echo $(git rev-parse HEAD~3) $(git rev-parse HEAD^); \
 843        echo $(git rev-parse HEAD~2) $(git rev-parse HEAD)) |
 844        git notes copy --for-rewrite=foo &&
 845        git log -2 > output &&
 846        test_cmp expect output
 847'
 848
 849cat > expect << EOF
 850commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
 851Author: A U Thor <author@example.com>
 852Date:   Thu Apr 7 15:27:13 2005 -0700
 853
 854    15th
 855
 856Notes (other):
 857    yet another note
 858$whitespace
 859    yet another note
 860
 861commit be28d8b4d9951ad940d229ee3b0b9ee3b1ec273d
 862Author: A U Thor <author@example.com>
 863Date:   Thu Apr 7 15:26:13 2005 -0700
 864
 865    14th
 866
 867Notes (other):
 868    other note
 869$whitespace
 870    yet another note
 871EOF
 872
 873test_expect_success 'git notes copy --for-rewrite (enabled)' '
 874        git config notes.rewriteMode overwrite &&
 875        git config notes.rewriteRef "refs/notes/*" &&
 876        (echo $(git rev-parse HEAD~3) $(git rev-parse HEAD^); \
 877        echo $(git rev-parse HEAD~2) $(git rev-parse HEAD)) |
 878        git notes copy --for-rewrite=foo &&
 879        git log -2 > output &&
 880        test_cmp expect output
 881'
 882
 883test_expect_success 'git notes copy --for-rewrite (disabled)' '
 884        git config notes.rewrite.bar false &&
 885        echo $(git rev-parse HEAD~3) $(git rev-parse HEAD) |
 886        git notes copy --for-rewrite=bar &&
 887        git log -2 > output &&
 888        test_cmp expect output
 889'
 890
 891cat > expect << EOF
 892commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
 893Author: A U Thor <author@example.com>
 894Date:   Thu Apr 7 15:27:13 2005 -0700
 895
 896    15th
 897
 898Notes (other):
 899    a fresh note
 900EOF
 901
 902test_expect_success 'git notes copy --for-rewrite (overwrite)' '
 903        git notes add -f -m"a fresh note" HEAD^ &&
 904        echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
 905        git notes copy --for-rewrite=foo &&
 906        git log -1 > output &&
 907        test_cmp expect output
 908'
 909
 910test_expect_success 'git notes copy --for-rewrite (ignore)' '
 911        git config notes.rewriteMode ignore &&
 912        echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
 913        git notes copy --for-rewrite=foo &&
 914        git log -1 > output &&
 915        test_cmp expect output
 916'
 917
 918cat > expect << EOF
 919commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
 920Author: A U Thor <author@example.com>
 921Date:   Thu Apr 7 15:27:13 2005 -0700
 922
 923    15th
 924
 925Notes (other):
 926    a fresh note
 927    another fresh note
 928EOF
 929
 930test_expect_success 'git notes copy --for-rewrite (append)' '
 931        git notes add -f -m"another fresh note" HEAD^ &&
 932        git config notes.rewriteMode concatenate &&
 933        echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
 934        git notes copy --for-rewrite=foo &&
 935        git log -1 > output &&
 936        test_cmp expect output
 937'
 938
 939cat > expect << EOF
 940commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
 941Author: A U Thor <author@example.com>
 942Date:   Thu Apr 7 15:27:13 2005 -0700
 943
 944    15th
 945
 946Notes (other):
 947    a fresh note
 948    another fresh note
 949    append 1
 950    append 2
 951EOF
 952
 953test_expect_success 'git notes copy --for-rewrite (append two to one)' '
 954        git notes add -f -m"append 1" HEAD^ &&
 955        git notes add -f -m"append 2" HEAD^^ &&
 956        (echo $(git rev-parse HEAD^) $(git rev-parse HEAD);
 957        echo $(git rev-parse HEAD^^) $(git rev-parse HEAD)) |
 958        git notes copy --for-rewrite=foo &&
 959        git log -1 > output &&
 960        test_cmp expect output
 961'
 962
 963test_expect_success 'git notes copy --for-rewrite (append empty)' '
 964        git notes remove HEAD^ &&
 965        echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
 966        git notes copy --for-rewrite=foo &&
 967        git log -1 > output &&
 968        test_cmp expect output
 969'
 970
 971cat > expect << EOF
 972commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
 973Author: A U Thor <author@example.com>
 974Date:   Thu Apr 7 15:27:13 2005 -0700
 975
 976    15th
 977
 978Notes (other):
 979    replacement note 1
 980EOF
 981
 982test_expect_success 'GIT_NOTES_REWRITE_MODE works' '
 983        git notes add -f -m"replacement note 1" HEAD^ &&
 984        echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
 985        GIT_NOTES_REWRITE_MODE=overwrite git notes copy --for-rewrite=foo &&
 986        git log -1 > output &&
 987        test_cmp expect output
 988'
 989
 990cat > expect << EOF
 991commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
 992Author: A U Thor <author@example.com>
 993Date:   Thu Apr 7 15:27:13 2005 -0700
 994
 995    15th
 996
 997Notes (other):
 998    replacement note 2
 999EOF
1000
1001test_expect_success 'GIT_NOTES_REWRITE_REF works' '
1002        git config notes.rewriteMode overwrite &&
1003        git notes add -f -m"replacement note 2" HEAD^ &&
1004        git config --unset-all notes.rewriteRef &&
1005        echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
1006        GIT_NOTES_REWRITE_REF=refs/notes/commits:refs/notes/other \
1007                git notes copy --for-rewrite=foo &&
1008        git log -1 > output &&
1009        test_cmp expect output
1010'
1011
1012test_expect_success 'GIT_NOTES_REWRITE_REF overrides config' '
1013        git config notes.rewriteRef refs/notes/other &&
1014        git notes add -f -m"replacement note 3" HEAD^ &&
1015        echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
1016        GIT_NOTES_REWRITE_REF= git notes copy --for-rewrite=foo &&
1017        git log -1 > output &&
1018        test_cmp expect output
1019'
1020test_done