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