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