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