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