t / t3301-notes.shon commit Merge branch 'maint' (f1ba1c9)
   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
 381test_expect_success 'appending empty string does not change existing note' '
 382        git notes append -m "" &&
 383        git notes show > output &&
 384        test_cmp expect output
 385'
 386
 387test_expect_success 'git notes append == add when there is no existing note' '
 388        git notes remove HEAD &&
 389        test_must_fail git notes list HEAD &&
 390        git notes append -m "Initial set of notes
 391
 392More notes appended with git notes append" &&
 393        git notes show > output &&
 394        test_cmp expect output
 395'
 396
 397test_expect_success 'appending empty string to non-existing note does not create note' '
 398        git notes remove HEAD &&
 399        test_must_fail git notes list HEAD &&
 400        git notes append -m "" &&
 401        test_must_fail git notes list HEAD
 402'
 403
 404test_expect_success 'create other note on a different notes ref (setup)' '
 405        : > a6 &&
 406        git add a6 &&
 407        test_tick &&
 408        git commit -m 6th &&
 409        GIT_NOTES_REF="refs/notes/other" git notes add -m "other note"
 410'
 411
 412cat > expect-other << EOF
 413commit 387a89921c73d7ed72cd94d179c1c7048ca47756
 414Author: A U Thor <author@example.com>
 415Date:   Thu Apr 7 15:18:13 2005 -0700
 416
 417    6th
 418
 419Notes:
 420    other note
 421EOF
 422
 423cat > expect-not-other << EOF
 424commit 387a89921c73d7ed72cd94d179c1c7048ca47756
 425Author: A U Thor <author@example.com>
 426Date:   Thu Apr 7 15:18:13 2005 -0700
 427
 428    6th
 429EOF
 430
 431test_expect_success 'Do not show note on other ref by default' '
 432        git log -1 > output &&
 433        test_cmp expect-not-other output
 434'
 435
 436test_expect_success 'Do show note when ref is given in GIT_NOTES_REF' '
 437        GIT_NOTES_REF="refs/notes/other" git log -1 > output &&
 438        test_cmp expect-other output
 439'
 440
 441test_expect_success 'Do show note when ref is given in core.notesRef config' '
 442        git config core.notesRef "refs/notes/other" &&
 443        git log -1 > output &&
 444        test_cmp expect-other output
 445'
 446
 447test_expect_success 'Do not show note when core.notesRef is overridden' '
 448        GIT_NOTES_REF="refs/notes/wrong" git log -1 > output &&
 449        test_cmp expect-not-other output
 450'
 451
 452test_expect_success 'Allow notes on non-commits (trees, blobs, tags)' '
 453        echo "Note on a tree" > expect
 454        git notes add -m "Note on a tree" HEAD: &&
 455        git notes show HEAD: > actual &&
 456        test_cmp expect actual &&
 457        echo "Note on a blob" > expect
 458        filename=$(git ls-tree --name-only HEAD | head -n1) &&
 459        git notes add -m "Note on a blob" HEAD:$filename &&
 460        git notes show HEAD:$filename > actual &&
 461        test_cmp expect actual &&
 462        echo "Note on a tag" > expect
 463        git tag -a -m "This is an annotated tag" foobar HEAD^ &&
 464        git notes add -m "Note on a tag" foobar &&
 465        git notes show foobar > actual &&
 466        test_cmp expect actual
 467'
 468
 469cat > expect << EOF
 470commit 2ede89468182a62d0bde2583c736089bcf7d7e92
 471Author: A U Thor <author@example.com>
 472Date:   Thu Apr 7 15:19:13 2005 -0700
 473
 474    7th
 475
 476Notes:
 477    other note
 478EOF
 479
 480test_expect_success 'create note from other note with "git notes add -C"' '
 481        : > a7 &&
 482        git add a7 &&
 483        test_tick &&
 484        git commit -m 7th &&
 485        git notes add -C $(git notes list HEAD^) &&
 486        git log -1 > actual &&
 487        test_cmp expect actual &&
 488        test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
 489'
 490
 491test_expect_success 'create note from non-existing note with "git notes add -C" fails' '
 492        : > a8 &&
 493        git add a8 &&
 494        test_tick &&
 495        git commit -m 8th &&
 496        test_must_fail git notes add -C deadbeef &&
 497        test_must_fail git notes list HEAD
 498'
 499
 500cat > expect << EOF
 501commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
 502Author: A U Thor <author@example.com>
 503Date:   Thu Apr 7 15:21:13 2005 -0700
 504
 505    9th
 506
 507Notes:
 508    yet another note
 509EOF
 510
 511test_expect_success 'create note from other note with "git notes add -c"' '
 512        : > a9 &&
 513        git add a9 &&
 514        test_tick &&
 515        git commit -m 9th &&
 516        MSG="yet another note" git notes add -c $(git notes list HEAD^^) &&
 517        git log -1 > actual &&
 518        test_cmp expect actual
 519'
 520
 521test_expect_success 'create note from non-existing note with "git notes add -c" fails' '
 522        : > a10 &&
 523        git add a10 &&
 524        test_tick &&
 525        git commit -m 10th &&
 526        test_must_fail MSG="yet another note" git notes add -c deadbeef &&
 527        test_must_fail git notes list HEAD
 528'
 529
 530cat > expect << EOF
 531commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
 532Author: A U Thor <author@example.com>
 533Date:   Thu Apr 7 15:21:13 2005 -0700
 534
 535    9th
 536
 537Notes:
 538    yet another note
 539$whitespace
 540    yet another note
 541EOF
 542
 543test_expect_success 'append to note from other note with "git notes append -C"' '
 544        git notes append -C $(git notes list HEAD^) HEAD^ &&
 545        git log -1 HEAD^ > actual &&
 546        test_cmp expect actual
 547'
 548
 549cat > expect << EOF
 550commit ffed603236bfa3891c49644257a83598afe8ae5a
 551Author: A U Thor <author@example.com>
 552Date:   Thu Apr 7 15:22:13 2005 -0700
 553
 554    10th
 555
 556Notes:
 557    other note
 558EOF
 559
 560test_expect_success 'create note from other note with "git notes append -c"' '
 561        MSG="other note" git notes append -c $(git notes list HEAD^) &&
 562        git log -1 > actual &&
 563        test_cmp expect actual
 564'
 565
 566cat > expect << EOF
 567commit ffed603236bfa3891c49644257a83598afe8ae5a
 568Author: A U Thor <author@example.com>
 569Date:   Thu Apr 7 15:22:13 2005 -0700
 570
 571    10th
 572
 573Notes:
 574    other note
 575$whitespace
 576    yet another note
 577EOF
 578
 579test_expect_success 'append to note from other note with "git notes append -c"' '
 580        MSG="yet another note" git notes append -c $(git notes list HEAD) &&
 581        git log -1 > actual &&
 582        test_cmp expect actual
 583'
 584
 585cat > expect << EOF
 586commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
 587Author: A U Thor <author@example.com>
 588Date:   Thu Apr 7 15:23:13 2005 -0700
 589
 590    11th
 591
 592Notes:
 593    other note
 594$whitespace
 595    yet another note
 596EOF
 597
 598test_expect_success 'copy note with "git notes copy"' '
 599        : > a11 &&
 600        git add a11 &&
 601        test_tick &&
 602        git commit -m 11th &&
 603        git notes copy HEAD^ HEAD &&
 604        git log -1 > actual &&
 605        test_cmp expect actual &&
 606        test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
 607'
 608
 609test_expect_success 'prevent overwrite with "git notes copy"' '
 610        test_must_fail git notes copy HEAD~2 HEAD &&
 611        git log -1 > actual &&
 612        test_cmp expect actual &&
 613        test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
 614'
 615
 616cat > expect << EOF
 617commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
 618Author: A U Thor <author@example.com>
 619Date:   Thu Apr 7 15:23:13 2005 -0700
 620
 621    11th
 622
 623Notes:
 624    yet another note
 625$whitespace
 626    yet another note
 627EOF
 628
 629test_expect_success 'allow overwrite with "git notes copy -f"' '
 630        git notes copy -f HEAD~2 HEAD &&
 631        git log -1 > actual &&
 632        test_cmp expect actual &&
 633        test "$(git notes list HEAD)" = "$(git notes list HEAD~2)"
 634'
 635
 636test_expect_success 'cannot copy note from object without notes' '
 637        : > a12 &&
 638        git add a12 &&
 639        test_tick &&
 640        git commit -m 12th &&
 641        : > a13 &&
 642        git add a13 &&
 643        test_tick &&
 644        git commit -m 13th &&
 645        test_must_fail git notes copy HEAD^ HEAD
 646'
 647
 648test_done