revision.c: support --notes command-line option
[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 'create -m notes (setup)' '
273         : > a5 &&
274         git add a5 &&
275         test_tick &&
276         git commit -m 5th &&
277         git notes add -m spam -m "foo
278 bar
279 baz"
280 '
281
282 whitespace="    "
283 cat > expect-m << EOF
284 commit bd1753200303d0a0344be813e504253b3d98e74d
285 Author: A U Thor <author@example.com>
286 Date:   Thu Apr 7 15:17:13 2005 -0700
287
288     5th
289
290 Notes:
291     spam
292 $whitespace
293     foo
294     bar
295     baz
296 EOF
297
298 printf "\n" >> expect-m
299 cat expect-F >> expect-m
300
301 test_expect_success 'show -m notes' '
302         git log -4 > output &&
303         test_cmp expect-m output
304 '
305
306 test_expect_success 'remove note with add -f -F /dev/null (setup)' '
307         git notes add -f -F /dev/null
308 '
309
310 cat > expect-rm-F << EOF
311 commit bd1753200303d0a0344be813e504253b3d98e74d
312 Author: A U Thor <author@example.com>
313 Date:   Thu Apr 7 15:17:13 2005 -0700
314
315     5th
316 EOF
317
318 printf "\n" >> expect-rm-F
319 cat expect-F >> expect-rm-F
320
321 test_expect_success 'verify note removal with -F /dev/null' '
322         git log -4 > output &&
323         test_cmp expect-rm-F output &&
324         test_must_fail git notes show
325 '
326
327 test_expect_success 'do not create empty note with -m "" (setup)' '
328         git notes add -m ""
329 '
330
331 test_expect_success 'verify non-creation of note with -m ""' '
332         git log -4 > output &&
333         test_cmp expect-rm-F output &&
334         test_must_fail git notes show
335 '
336
337 cat > expect-combine_m_and_F << EOF
338 foo
339
340 xyzzy
341
342 bar
343
344 zyxxy
345
346 baz
347 EOF
348
349 test_expect_success 'create note with combination of -m and -F' '
350         echo "xyzzy" > note_a &&
351         echo "zyxxy" > note_b &&
352         git notes add -m "foo" -F note_a -m "bar" -F note_b -m "baz" &&
353         git notes show > output &&
354         test_cmp expect-combine_m_and_F output
355 '
356
357 test_expect_success 'remove note with "git notes remove" (setup)' '
358         git notes remove HEAD^ &&
359         git notes remove
360 '
361
362 cat > expect-rm-remove << EOF
363 commit bd1753200303d0a0344be813e504253b3d98e74d
364 Author: A U Thor <author@example.com>
365 Date:   Thu Apr 7 15:17:13 2005 -0700
366
367     5th
368
369 commit 15023535574ded8b1a89052b32673f84cf9582b8
370 Author: A U Thor <author@example.com>
371 Date:   Thu Apr 7 15:16:13 2005 -0700
372
373     4th
374 EOF
375
376 printf "\n" >> expect-rm-remove
377 cat expect-multiline >> expect-rm-remove
378
379 test_expect_success 'verify note removal with "git notes remove"' '
380         git log -4 > output &&
381         test_cmp expect-rm-remove output &&
382         test_must_fail git notes show HEAD^
383 '
384
385 cat > expect << EOF
386 c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd75
387 c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5
388 EOF
389
390 test_expect_success 'removing non-existing note should not create new commit' '
391         git rev-parse --verify refs/notes/commits > before_commit &&
392         test_must_fail git notes remove HEAD^ &&
393         git rev-parse --verify refs/notes/commits > after_commit &&
394         test_cmp before_commit after_commit
395 '
396
397 test_expect_success 'list notes with "git notes list"' '
398         git notes list > output &&
399         test_cmp expect output
400 '
401
402 test_expect_success 'list notes with "git notes"' '
403         git notes > output &&
404         test_cmp expect output
405 '
406
407 cat > expect << EOF
408 c18dc024e14f08d18d14eea0d747ff692d66d6a3
409 EOF
410
411 test_expect_success 'list specific note with "git notes list <object>"' '
412         git notes list HEAD^^ > output &&
413         test_cmp expect output
414 '
415
416 cat > expect << EOF
417 EOF
418
419 test_expect_success 'listing non-existing notes fails' '
420         test_must_fail git notes list HEAD > output &&
421         test_cmp expect output
422 '
423
424 cat > expect << EOF
425 Initial set of notes
426
427 More notes appended with git notes append
428 EOF
429
430 test_expect_success 'append to existing note with "git notes append"' '
431         git notes add -m "Initial set of notes" &&
432         git notes append -m "More notes appended with git notes append" &&
433         git notes show > output &&
434         test_cmp expect output
435 '
436
437 cat > expect_list << EOF
438 c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd75
439 c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5
440 4b6ad22357cc8a1296720574b8d2fbc22fab0671 bd1753200303d0a0344be813e504253b3d98e74d
441 EOF
442
443 test_expect_success '"git notes list" does not expand to "git notes list HEAD"' '
444         git notes list > output &&
445         test_cmp expect_list output
446 '
447
448 test_expect_success 'appending empty string does not change existing note' '
449         git notes append -m "" &&
450         git notes show > output &&
451         test_cmp expect output
452 '
453
454 test_expect_success 'git notes append == add when there is no existing note' '
455         git notes remove HEAD &&
456         test_must_fail git notes list HEAD &&
457         git notes append -m "Initial set of notes
458
459 More notes appended with git notes append" &&
460         git notes show > output &&
461         test_cmp expect output
462 '
463
464 test_expect_success 'appending empty string to non-existing note does not create note' '
465         git notes remove HEAD &&
466         test_must_fail git notes list HEAD &&
467         git notes append -m "" &&
468         test_must_fail git notes list HEAD
469 '
470
471 test_expect_success 'create other note on a different notes ref (setup)' '
472         : > a6 &&
473         git add a6 &&
474         test_tick &&
475         git commit -m 6th &&
476         GIT_NOTES_REF="refs/notes/other" git notes add -m "other note"
477 '
478
479 cat > expect-other << EOF
480 commit 387a89921c73d7ed72cd94d179c1c7048ca47756
481 Author: A U Thor <author@example.com>
482 Date:   Thu Apr 7 15:18:13 2005 -0700
483
484     6th
485
486 Notes (other):
487     other note
488 EOF
489
490 cat > expect-not-other << EOF
491 commit 387a89921c73d7ed72cd94d179c1c7048ca47756
492 Author: A U Thor <author@example.com>
493 Date:   Thu Apr 7 15:18:13 2005 -0700
494
495     6th
496 EOF
497
498 test_expect_success 'Do not show note on other ref by default' '
499         git log -1 > output &&
500         test_cmp expect-not-other output
501 '
502
503 test_expect_success 'Do show note when ref is given in GIT_NOTES_REF' '
504         GIT_NOTES_REF="refs/notes/other" git log -1 > output &&
505         test_cmp expect-other output
506 '
507
508 test_expect_success 'Do show note when ref is given in core.notesRef config' '
509         git config core.notesRef "refs/notes/other" &&
510         git log -1 > output &&
511         test_cmp expect-other output
512 '
513
514 test_expect_success 'Do not show note when core.notesRef is overridden' '
515         GIT_NOTES_REF="refs/notes/wrong" git log -1 > output &&
516         test_cmp expect-not-other output
517 '
518
519 cat > expect-both << EOF
520 commit 387a89921c73d7ed72cd94d179c1c7048ca47756
521 Author: A U Thor <author@example.com>
522 Date:   Thu Apr 7 15:18:13 2005 -0700
523
524     6th
525
526 Notes:
527     order test
528
529 Notes (other):
530     other note
531
532 commit bd1753200303d0a0344be813e504253b3d98e74d
533 Author: A U Thor <author@example.com>
534 Date:   Thu Apr 7 15:17:13 2005 -0700
535
536     5th
537
538 Notes:
539     replacement for deleted note
540 EOF
541
542 test_expect_success 'Show all notes when notes.displayRef=refs/notes/*' '
543         GIT_NOTES_REF=refs/notes/commits git notes add \
544                 -m"replacement for deleted note" HEAD^ &&
545         GIT_NOTES_REF=refs/notes/commits git notes add -m"order test" &&
546         git config --unset core.notesRef &&
547         git config notes.displayRef "refs/notes/*" &&
548         git log -2 > output &&
549         test_cmp expect-both output
550 '
551
552 test_expect_success 'core.notesRef is implicitly in notes.displayRef' '
553         git config core.notesRef refs/notes/commits &&
554         git config notes.displayRef refs/notes/other &&
555         git log -2 > output &&
556         test_cmp expect-both output
557 '
558
559 test_expect_success 'notes.displayRef can be given more than once' '
560         git config --unset core.notesRef &&
561         git config notes.displayRef refs/notes/commits &&
562         git config --add notes.displayRef refs/notes/other &&
563         git log -2 > output &&
564         test_cmp expect-both output
565 '
566
567 cat > expect-both-reversed << EOF
568 commit 387a89921c73d7ed72cd94d179c1c7048ca47756
569 Author: A U Thor <author@example.com>
570 Date:   Thu Apr 7 15:18:13 2005 -0700
571
572     6th
573
574 Notes (other):
575     other note
576
577 Notes:
578     order test
579 EOF
580
581 test_expect_success 'notes.displayRef respects order' '
582         git config core.notesRef refs/notes/other &&
583         git config --unset-all notes.displayRef &&
584         git config notes.displayRef refs/notes/commits &&
585         git log -1 > output &&
586         test_cmp expect-both-reversed output
587 '
588
589 test_expect_success 'GIT_NOTES_DISPLAY_REF works' '
590         git config --unset-all core.notesRef &&
591         git config --unset-all notes.displayRef &&
592         GIT_NOTES_DISPLAY_REF=refs/notes/commits:refs/notes/other \
593                 git log -2 > output &&
594         test_cmp expect-both output
595 '
596
597 cat > expect-none << EOF
598 commit 387a89921c73d7ed72cd94d179c1c7048ca47756
599 Author: A U Thor <author@example.com>
600 Date:   Thu Apr 7 15:18:13 2005 -0700
601
602     6th
603
604 commit bd1753200303d0a0344be813e504253b3d98e74d
605 Author: A U Thor <author@example.com>
606 Date:   Thu Apr 7 15:17:13 2005 -0700
607
608     5th
609 EOF
610
611 test_expect_success 'GIT_NOTES_DISPLAY_REF overrides config' '
612         git config notes.displayRef "refs/notes/*" &&
613         GIT_NOTES_REF= GIT_NOTES_DISPLAY_REF= git log -2 > output &&
614         test_cmp expect-none output
615 '
616
617 test_expect_success '--show-notes=* adds to GIT_NOTES_DISPLAY_REF' '
618         GIT_NOTES_REF= GIT_NOTES_DISPLAY_REF= git log --show-notes=* -2 > output &&
619         test_cmp expect-both output
620 '
621
622 cat > expect-commits << EOF
623 commit 387a89921c73d7ed72cd94d179c1c7048ca47756
624 Author: A U Thor <author@example.com>
625 Date:   Thu Apr 7 15:18:13 2005 -0700
626
627     6th
628
629 Notes:
630     order test
631 EOF
632
633 test_expect_success '--no-standard-notes' '
634         git log --no-standard-notes --show-notes=commits -1 > output &&
635         test_cmp expect-commits output
636 '
637
638 test_expect_success '--standard-notes' '
639         git log --no-standard-notes --show-notes=commits \
640                 --standard-notes -2 > output &&
641         test_cmp expect-both output
642 '
643
644 test_expect_success '--show-notes=ref accumulates' '
645         git log --show-notes=other --show-notes=commits \
646                  --no-standard-notes -1 > output &&
647         test_cmp expect-both-reversed output
648 '
649
650 test_expect_success 'Allow notes on non-commits (trees, blobs, tags)' '
651         git config core.notesRef refs/notes/other &&
652         echo "Note on a tree" > expect &&
653         git notes add -m "Note on a tree" HEAD: &&
654         git notes show HEAD: > actual &&
655         test_cmp expect actual &&
656         echo "Note on a blob" > expect &&
657         filename=$(git ls-tree --name-only HEAD | head -n1) &&
658         git notes add -m "Note on a blob" HEAD:$filename &&
659         git notes show HEAD:$filename > actual &&
660         test_cmp expect actual &&
661         echo "Note on a tag" > expect &&
662         git tag -a -m "This is an annotated tag" foobar HEAD^ &&
663         git notes add -m "Note on a tag" foobar &&
664         git notes show foobar > actual &&
665         test_cmp expect actual
666 '
667
668 cat > expect << EOF
669 commit 2ede89468182a62d0bde2583c736089bcf7d7e92
670 Author: A U Thor <author@example.com>
671 Date:   Thu Apr 7 15:19:13 2005 -0700
672
673     7th
674
675 Notes (other):
676     other note
677 EOF
678
679 test_expect_success 'create note from other note with "git notes add -C"' '
680         : > a7 &&
681         git add a7 &&
682         test_tick &&
683         git commit -m 7th &&
684         git notes add -C $(git notes list HEAD^) &&
685         git log -1 > actual &&
686         test_cmp expect actual &&
687         test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
688 '
689
690 test_expect_success 'create note from non-existing note with "git notes add -C" fails' '
691         : > a8 &&
692         git add a8 &&
693         test_tick &&
694         git commit -m 8th &&
695         test_must_fail git notes add -C deadbeef &&
696         test_must_fail git notes list HEAD
697 '
698
699 cat > expect << EOF
700 commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
701 Author: A U Thor <author@example.com>
702 Date:   Thu Apr 7 15:21:13 2005 -0700
703
704     9th
705
706 Notes (other):
707     yet another note
708 EOF
709
710 test_expect_success 'create note from other note with "git notes add -c"' '
711         : > a9 &&
712         git add a9 &&
713         test_tick &&
714         git commit -m 9th &&
715         MSG="yet another note" git notes add -c $(git notes list HEAD^^) &&
716         git log -1 > actual &&
717         test_cmp expect actual
718 '
719
720 test_expect_success 'create note from non-existing note with "git notes add -c" fails' '
721         : > a10 &&
722         git add a10 &&
723         test_tick &&
724         git commit -m 10th &&
725         (
726                 MSG="yet another note" &&
727                 export MSG &&
728                 test_must_fail git notes add -c deadbeef
729         ) &&
730         test_must_fail git notes list HEAD
731 '
732
733 cat > expect << EOF
734 commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
735 Author: A U Thor <author@example.com>
736 Date:   Thu Apr 7 15:21:13 2005 -0700
737
738     9th
739
740 Notes (other):
741     yet another note
742 $whitespace
743     yet another note
744 EOF
745
746 test_expect_success 'append to note from other note with "git notes append -C"' '
747         git notes append -C $(git notes list HEAD^) HEAD^ &&
748         git log -1 HEAD^ > actual &&
749         test_cmp expect actual
750 '
751
752 cat > expect << EOF
753 commit ffed603236bfa3891c49644257a83598afe8ae5a
754 Author: A U Thor <author@example.com>
755 Date:   Thu Apr 7 15:22:13 2005 -0700
756
757     10th
758
759 Notes (other):
760     other note
761 EOF
762
763 test_expect_success 'create note from other note with "git notes append -c"' '
764         MSG="other note" git notes append -c $(git notes list HEAD^) &&
765         git log -1 > actual &&
766         test_cmp expect actual
767 '
768
769 cat > expect << EOF
770 commit ffed603236bfa3891c49644257a83598afe8ae5a
771 Author: A U Thor <author@example.com>
772 Date:   Thu Apr 7 15:22:13 2005 -0700
773
774     10th
775
776 Notes (other):
777     other note
778 $whitespace
779     yet another note
780 EOF
781
782 test_expect_success 'append to note from other note with "git notes append -c"' '
783         MSG="yet another note" git notes append -c $(git notes list HEAD) &&
784         git log -1 > actual &&
785         test_cmp expect actual
786 '
787
788 cat > expect << EOF
789 commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
790 Author: A U Thor <author@example.com>
791 Date:   Thu Apr 7 15:23:13 2005 -0700
792
793     11th
794
795 Notes (other):
796     other note
797 $whitespace
798     yet another note
799 EOF
800
801 test_expect_success 'copy note with "git notes copy"' '
802         : > a11 &&
803         git add a11 &&
804         test_tick &&
805         git commit -m 11th &&
806         git notes copy HEAD^ HEAD &&
807         git log -1 > actual &&
808         test_cmp expect actual &&
809         test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
810 '
811
812 test_expect_success 'prevent overwrite with "git notes copy"' '
813         test_must_fail git notes copy HEAD~2 HEAD &&
814         git log -1 > actual &&
815         test_cmp expect actual &&
816         test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
817 '
818
819 cat > expect << EOF
820 commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
821 Author: A U Thor <author@example.com>
822 Date:   Thu Apr 7 15:23:13 2005 -0700
823
824     11th
825
826 Notes (other):
827     yet another note
828 $whitespace
829     yet another note
830 EOF
831
832 test_expect_success 'allow overwrite with "git notes copy -f"' '
833         git notes copy -f HEAD~2 HEAD &&
834         git log -1 > actual &&
835         test_cmp expect actual &&
836         test "$(git notes list HEAD)" = "$(git notes list HEAD~2)"
837 '
838
839 test_expect_success 'cannot copy note from object without notes' '
840         : > a12 &&
841         git add a12 &&
842         test_tick &&
843         git commit -m 12th &&
844         : > a13 &&
845         git add a13 &&
846         test_tick &&
847         git commit -m 13th &&
848         test_must_fail git notes copy HEAD^ HEAD
849 '
850
851 cat > expect << EOF
852 commit e5d4fb5698d564ab8c73551538ecaf2b0c666185
853 Author: A U Thor <author@example.com>
854 Date:   Thu Apr 7 15:25:13 2005 -0700
855
856     13th
857
858 Notes (other):
859     yet another note
860 $whitespace
861     yet another note
862
863 commit 7038787dfe22a14c3867ce816dbba39845359719
864 Author: A U Thor <author@example.com>
865 Date:   Thu Apr 7 15:24:13 2005 -0700
866
867     12th
868
869 Notes (other):
870     other note
871 $whitespace
872     yet another note
873 EOF
874
875 test_expect_success 'git notes copy --stdin' '
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 --stdin &&
879         git log -2 > output &&
880         test_cmp expect output &&
881         test "$(git notes list HEAD)" = "$(git notes list HEAD~2)" &&
882         test "$(git notes list HEAD^)" = "$(git notes list HEAD~3)"
883 '
884
885 cat > expect << EOF
886 commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
887 Author: A U Thor <author@example.com>
888 Date:   Thu Apr 7 15:27:13 2005 -0700
889
890     15th
891
892 commit be28d8b4d9951ad940d229ee3b0b9ee3b1ec273d
893 Author: A U Thor <author@example.com>
894 Date:   Thu Apr 7 15:26:13 2005 -0700
895
896     14th
897 EOF
898
899 test_expect_success 'git notes copy --for-rewrite (unconfigured)' '
900         test_commit 14th &&
901         test_commit 15th &&
902         (echo $(git rev-parse HEAD~3) $(git rev-parse HEAD^); \
903         echo $(git rev-parse HEAD~2) $(git rev-parse HEAD)) |
904         git notes copy --for-rewrite=foo &&
905         git log -2 > output &&
906         test_cmp expect output
907 '
908
909 cat > expect << EOF
910 commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
911 Author: A U Thor <author@example.com>
912 Date:   Thu Apr 7 15:27:13 2005 -0700
913
914     15th
915
916 Notes (other):
917     yet another note
918 $whitespace
919     yet another note
920
921 commit be28d8b4d9951ad940d229ee3b0b9ee3b1ec273d
922 Author: A U Thor <author@example.com>
923 Date:   Thu Apr 7 15:26:13 2005 -0700
924
925     14th
926
927 Notes (other):
928     other note
929 $whitespace
930     yet another note
931 EOF
932
933 test_expect_success 'git notes copy --for-rewrite (enabled)' '
934         git config notes.rewriteMode overwrite &&
935         git config notes.rewriteRef "refs/notes/*" &&
936         (echo $(git rev-parse HEAD~3) $(git rev-parse HEAD^); \
937         echo $(git rev-parse HEAD~2) $(git rev-parse HEAD)) |
938         git notes copy --for-rewrite=foo &&
939         git log -2 > output &&
940         test_cmp expect output
941 '
942
943 test_expect_success 'git notes copy --for-rewrite (disabled)' '
944         git config notes.rewrite.bar false &&
945         echo $(git rev-parse HEAD~3) $(git rev-parse HEAD) |
946         git notes copy --for-rewrite=bar &&
947         git log -2 > output &&
948         test_cmp expect output
949 '
950
951 cat > expect << EOF
952 commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
953 Author: A U Thor <author@example.com>
954 Date:   Thu Apr 7 15:27:13 2005 -0700
955
956     15th
957
958 Notes (other):
959     a fresh note
960 EOF
961
962 test_expect_success 'git notes copy --for-rewrite (overwrite)' '
963         git notes add -f -m"a fresh note" HEAD^ &&
964         echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
965         git notes copy --for-rewrite=foo &&
966         git log -1 > output &&
967         test_cmp expect output
968 '
969
970 test_expect_success 'git notes copy --for-rewrite (ignore)' '
971         git config notes.rewriteMode ignore &&
972         echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
973         git notes copy --for-rewrite=foo &&
974         git log -1 > output &&
975         test_cmp expect output
976 '
977
978 cat > expect << EOF
979 commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
980 Author: A U Thor <author@example.com>
981 Date:   Thu Apr 7 15:27:13 2005 -0700
982
983     15th
984
985 Notes (other):
986     a fresh note
987 $whitespace
988     another fresh note
989 EOF
990
991 test_expect_success 'git notes copy --for-rewrite (append)' '
992         git notes add -f -m"another fresh note" HEAD^ &&
993         git config notes.rewriteMode concatenate &&
994         echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
995         git notes copy --for-rewrite=foo &&
996         git log -1 > output &&
997         test_cmp expect output
998 '
999
1000 cat > expect << EOF
1001 commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
1002 Author: A U Thor <author@example.com>
1003 Date:   Thu Apr 7 15:27:13 2005 -0700
1004
1005     15th
1006
1007 Notes (other):
1008     a fresh note
1009 $whitespace
1010     another fresh note
1011 $whitespace
1012     append 1
1013 $whitespace
1014     append 2
1015 EOF
1016
1017 test_expect_success 'git notes copy --for-rewrite (append two to one)' '
1018         git notes add -f -m"append 1" HEAD^ &&
1019         git notes add -f -m"append 2" HEAD^^ &&
1020         (echo $(git rev-parse HEAD^) $(git rev-parse HEAD);
1021         echo $(git rev-parse HEAD^^) $(git rev-parse HEAD)) |
1022         git notes copy --for-rewrite=foo &&
1023         git log -1 > output &&
1024         test_cmp expect output
1025 '
1026
1027 test_expect_success 'git notes copy --for-rewrite (append empty)' '
1028         git notes remove HEAD^ &&
1029         echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
1030         git notes copy --for-rewrite=foo &&
1031         git log -1 > output &&
1032         test_cmp expect output
1033 '
1034
1035 cat > expect << EOF
1036 commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
1037 Author: A U Thor <author@example.com>
1038 Date:   Thu Apr 7 15:27:13 2005 -0700
1039
1040     15th
1041
1042 Notes (other):
1043     replacement note 1
1044 EOF
1045
1046 test_expect_success 'GIT_NOTES_REWRITE_MODE works' '
1047         git notes add -f -m"replacement note 1" HEAD^ &&
1048         echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
1049         GIT_NOTES_REWRITE_MODE=overwrite git notes copy --for-rewrite=foo &&
1050         git log -1 > output &&
1051         test_cmp expect output
1052 '
1053
1054 cat > expect << EOF
1055 commit 37a0d4cba38afef96ba54a3ea567e6dac575700b
1056 Author: A U Thor <author@example.com>
1057 Date:   Thu Apr 7 15:27:13 2005 -0700
1058
1059     15th
1060
1061 Notes (other):
1062     replacement note 2
1063 EOF
1064
1065 test_expect_success 'GIT_NOTES_REWRITE_REF works' '
1066         git config notes.rewriteMode overwrite &&
1067         git notes add -f -m"replacement note 2" HEAD^ &&
1068         git config --unset-all notes.rewriteRef &&
1069         echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
1070         GIT_NOTES_REWRITE_REF=refs/notes/commits:refs/notes/other \
1071                 git notes copy --for-rewrite=foo &&
1072         git log -1 > output &&
1073         test_cmp expect output
1074 '
1075
1076 test_expect_success 'GIT_NOTES_REWRITE_REF overrides config' '
1077         git config notes.rewriteRef refs/notes/other &&
1078         git notes add -f -m"replacement note 3" HEAD^ &&
1079         echo $(git rev-parse HEAD^) $(git rev-parse HEAD) |
1080         GIT_NOTES_REWRITE_REF= git notes copy --for-rewrite=foo &&
1081         git log -1 > output &&
1082         test_cmp expect output
1083 '
1084
1085 test_expect_success 'git notes copy diagnoses too many or too few parameters' '
1086         test_must_fail git notes copy &&
1087         test_must_fail git notes copy one two three
1088 '
1089
1090 test_expect_success 'git notes get-ref (no overrides)' '
1091         git config --unset core.notesRef &&
1092         sane_unset GIT_NOTES_REF &&
1093         test "$(git notes get-ref)" = "refs/notes/commits"
1094 '
1095
1096 test_expect_success 'git notes get-ref (core.notesRef)' '
1097         git config core.notesRef refs/notes/foo &&
1098         test "$(git notes get-ref)" = "refs/notes/foo"
1099 '
1100
1101 test_expect_success 'git notes get-ref (GIT_NOTES_REF)' '
1102         test "$(GIT_NOTES_REF=refs/notes/bar git notes get-ref)" = "refs/notes/bar"
1103 '
1104
1105 test_expect_success 'git notes get-ref (--ref)' '
1106         test "$(GIT_NOTES_REF=refs/notes/bar git notes --ref=baz get-ref)" = "refs/notes/baz"
1107 '
1108
1109 test_done