Merge branch 'jc/maint-refs-dangling'
[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         git notes show ; test 1 = $?
56 '
57
58 test_expect_success 'create notes' '
59         git config core.notesRef refs/notes/commits &&
60         MSG=b4 git notes add &&
61         test ! -f .git/NOTES_EDITMSG &&
62         test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
63         test b4 = $(git notes show) &&
64         git show HEAD^ &&
65         test_must_fail git notes show HEAD^
66 '
67
68 test_expect_success 'edit existing notes' '
69         MSG=b3 git notes edit &&
70         test ! -f .git/NOTES_EDITMSG &&
71         test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
72         test b3 = $(git notes show) &&
73         git show HEAD^ &&
74         test_must_fail git notes show HEAD^
75 '
76
77 test_expect_success 'cannot add note where one exists' '
78         ! MSG=b2 git notes add &&
79         test ! -f .git/NOTES_EDITMSG &&
80         test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
81         test b3 = $(git notes show) &&
82         git show HEAD^ &&
83         test_must_fail git notes show HEAD^
84 '
85
86 test_expect_success 'can overwrite existing note with "git notes add -f"' '
87         MSG=b1 git notes add -f &&
88         test ! -f .git/NOTES_EDITMSG &&
89         test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
90         test b1 = $(git notes show) &&
91         git show HEAD^ &&
92         test_must_fail git notes show HEAD^
93 '
94
95 cat > expect << EOF
96 commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
97 Author: A U Thor <author@example.com>
98 Date:   Thu Apr 7 15:14:13 2005 -0700
99
100     2nd
101
102 Notes:
103     b1
104 EOF
105
106 test_expect_success 'show notes' '
107         ! (git cat-file commit HEAD | grep b1) &&
108         git log -1 > output &&
109         test_cmp expect output
110 '
111
112 test_expect_success 'create multi-line notes (setup)' '
113         : > a3 &&
114         git add a3 &&
115         test_tick &&
116         git commit -m 3rd &&
117         MSG="b3
118 c3c3c3c3
119 d3d3d3" git notes add
120 '
121
122 cat > expect-multiline << EOF
123 commit 1584215f1d29c65e99c6c6848626553fdd07fd75
124 Author: A U Thor <author@example.com>
125 Date:   Thu Apr 7 15:15:13 2005 -0700
126
127     3rd
128
129 Notes:
130     b3
131     c3c3c3c3
132     d3d3d3
133 EOF
134
135 printf "\n" >> expect-multiline
136 cat expect >> expect-multiline
137
138 test_expect_success 'show multi-line notes' '
139         git log -2 > output &&
140         test_cmp expect-multiline output
141 '
142 test_expect_success 'create -F notes (setup)' '
143         : > a4 &&
144         git add a4 &&
145         test_tick &&
146         git commit -m 4th &&
147         echo "xyzzy" > note5 &&
148         git notes add -F note5
149 '
150
151 cat > expect-F << EOF
152 commit 15023535574ded8b1a89052b32673f84cf9582b8
153 Author: A U Thor <author@example.com>
154 Date:   Thu Apr 7 15:16:13 2005 -0700
155
156     4th
157
158 Notes:
159     xyzzy
160 EOF
161
162 printf "\n" >> expect-F
163 cat expect-multiline >> expect-F
164
165 test_expect_success 'show -F notes' '
166         git log -3 > output &&
167         test_cmp expect-F output
168 '
169
170 cat >expect << EOF
171 commit 15023535574ded8b1a89052b32673f84cf9582b8
172 tree e070e3af51011e47b183c33adf9736736a525709
173 parent 1584215f1d29c65e99c6c6848626553fdd07fd75
174 author A U Thor <author@example.com> 1112912173 -0700
175 committer C O Mitter <committer@example.com> 1112912173 -0700
176
177     4th
178 EOF
179 test_expect_success 'git log --pretty=raw does not show notes' '
180         git log -1 --pretty=raw >output &&
181         test_cmp expect output
182 '
183
184 cat >>expect <<EOF
185
186 Notes:
187     xyzzy
188 EOF
189 test_expect_success 'git log --show-notes' '
190         git log -1 --pretty=raw --show-notes >output &&
191         test_cmp expect output
192 '
193
194 test_expect_success 'git log --no-notes' '
195         git log -1 --no-notes >output &&
196         ! grep xyzzy output
197 '
198
199 test_expect_success 'git format-patch does not show notes' '
200         git format-patch -1 --stdout >output &&
201         ! grep xyzzy output
202 '
203
204 test_expect_success 'git format-patch --show-notes does show notes' '
205         git format-patch --show-notes -1 --stdout >output &&
206         grep xyzzy output
207 '
208
209 for pretty in \
210         "" --pretty --pretty=raw --pretty=short --pretty=medium \
211         --pretty=full --pretty=fuller --pretty=format:%s --oneline
212 do
213         case "$pretty" in
214         "") p= not= negate="" ;;
215         ?*) p="$pretty" not=" not" negate="!" ;;
216         esac
217         test_expect_success "git show $pretty does$not show notes" '
218                 git show $p >output &&
219                 eval "$negate grep xyzzy output"
220         '
221 done
222
223 test_expect_success 'create -m notes (setup)' '
224         : > a5 &&
225         git add a5 &&
226         test_tick &&
227         git commit -m 5th &&
228         git notes add -m spam -m "foo
229 bar
230 baz"
231 '
232
233 whitespace="    "
234 cat > expect-m << EOF
235 commit bd1753200303d0a0344be813e504253b3d98e74d
236 Author: A U Thor <author@example.com>
237 Date:   Thu Apr 7 15:17:13 2005 -0700
238
239     5th
240
241 Notes:
242     spam
243 $whitespace
244     foo
245     bar
246     baz
247 EOF
248
249 printf "\n" >> expect-m
250 cat expect-F >> expect-m
251
252 test_expect_success 'show -m notes' '
253         git log -4 > output &&
254         test_cmp expect-m output
255 '
256
257 test_expect_success 'remove note with add -f -F /dev/null (setup)' '
258         git notes add -f -F /dev/null
259 '
260
261 cat > expect-rm-F << EOF
262 commit bd1753200303d0a0344be813e504253b3d98e74d
263 Author: A U Thor <author@example.com>
264 Date:   Thu Apr 7 15:17:13 2005 -0700
265
266     5th
267 EOF
268
269 printf "\n" >> expect-rm-F
270 cat expect-F >> expect-rm-F
271
272 test_expect_success 'verify note removal with -F /dev/null' '
273         git log -4 > output &&
274         test_cmp expect-rm-F output &&
275         ! git notes show
276 '
277
278 test_expect_success 'do not create empty note with -m "" (setup)' '
279         git notes add -m ""
280 '
281
282 test_expect_success 'verify non-creation of note with -m ""' '
283         git log -4 > output &&
284         test_cmp expect-rm-F output &&
285         ! git notes show
286 '
287
288 cat > expect-combine_m_and_F << EOF
289 foo
290
291 xyzzy
292
293 bar
294
295 zyxxy
296
297 baz
298 EOF
299
300 test_expect_success 'create note with combination of -m and -F' '
301         echo "xyzzy" > note_a &&
302         echo "zyxxy" > note_b &&
303         git notes add -m "foo" -F note_a -m "bar" -F note_b -m "baz" &&
304         git notes show > output &&
305         test_cmp expect-combine_m_and_F output
306 '
307
308 test_expect_success 'remove note with "git notes remove" (setup)' '
309         git notes remove HEAD^ &&
310         git notes remove
311 '
312
313 cat > expect-rm-remove << EOF
314 commit bd1753200303d0a0344be813e504253b3d98e74d
315 Author: A U Thor <author@example.com>
316 Date:   Thu Apr 7 15:17:13 2005 -0700
317
318     5th
319
320 commit 15023535574ded8b1a89052b32673f84cf9582b8
321 Author: A U Thor <author@example.com>
322 Date:   Thu Apr 7 15:16:13 2005 -0700
323
324     4th
325 EOF
326
327 printf "\n" >> expect-rm-remove
328 cat expect-multiline >> expect-rm-remove
329
330 test_expect_success 'verify note removal with "git notes remove"' '
331         git log -4 > output &&
332         test_cmp expect-rm-remove output &&
333         ! git notes show HEAD^
334 '
335
336 cat > expect << EOF
337 c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd75
338 c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5
339 EOF
340
341 test_expect_success 'list notes with "git notes list"' '
342         git notes list > output &&
343         test_cmp expect output
344 '
345
346 test_expect_success 'list notes with "git notes"' '
347         git notes > output &&
348         test_cmp expect output
349 '
350
351 cat > expect << EOF
352 c18dc024e14f08d18d14eea0d747ff692d66d6a3
353 EOF
354
355 test_expect_success 'list specific note with "git notes list <object>"' '
356         git notes list HEAD^^ > output &&
357         test_cmp expect output
358 '
359
360 cat > expect << EOF
361 EOF
362
363 test_expect_success 'listing non-existing notes fails' '
364         test_must_fail git notes list HEAD > output &&
365         test_cmp expect output
366 '
367
368 cat > expect << EOF
369 Initial set of notes
370
371 More notes appended with git notes append
372 EOF
373
374 test_expect_success 'append to existing note with "git notes append"' '
375         git notes add -m "Initial set of notes" &&
376         git notes append -m "More notes appended with git notes append" &&
377         git notes show > output &&
378         test_cmp expect output
379 '
380
381 test_expect_success 'appending empty string does not change existing note' '
382         git notes append -m "" &&
383         git notes show > output &&
384         test_cmp expect output
385 '
386
387 test_expect_success 'git notes append == add when there is no existing note' '
388         git notes remove HEAD &&
389         test_must_fail git notes list HEAD &&
390         git notes append -m "Initial set of notes
391
392 More notes appended with git notes append" &&
393         git notes show > output &&
394         test_cmp expect output
395 '
396
397 test_expect_success 'appending empty string to non-existing note does not create note' '
398         git notes remove HEAD &&
399         test_must_fail git notes list HEAD &&
400         git notes append -m "" &&
401         test_must_fail git notes list HEAD
402 '
403
404 test_expect_success 'create other note on a different notes ref (setup)' '
405         : > a6 &&
406         git add a6 &&
407         test_tick &&
408         git commit -m 6th &&
409         GIT_NOTES_REF="refs/notes/other" git notes add -m "other note"
410 '
411
412 cat > expect-other << EOF
413 commit 387a89921c73d7ed72cd94d179c1c7048ca47756
414 Author: A U Thor <author@example.com>
415 Date:   Thu Apr 7 15:18:13 2005 -0700
416
417     6th
418
419 Notes:
420     other note
421 EOF
422
423 cat > expect-not-other << EOF
424 commit 387a89921c73d7ed72cd94d179c1c7048ca47756
425 Author: A U Thor <author@example.com>
426 Date:   Thu Apr 7 15:18:13 2005 -0700
427
428     6th
429 EOF
430
431 test_expect_success 'Do not show note on other ref by default' '
432         git log -1 > output &&
433         test_cmp expect-not-other output
434 '
435
436 test_expect_success 'Do show note when ref is given in GIT_NOTES_REF' '
437         GIT_NOTES_REF="refs/notes/other" git log -1 > output &&
438         test_cmp expect-other output
439 '
440
441 test_expect_success 'Do show note when ref is given in core.notesRef config' '
442         git config core.notesRef "refs/notes/other" &&
443         git log -1 > output &&
444         test_cmp expect-other output
445 '
446
447 test_expect_success 'Do not show note when core.notesRef is overridden' '
448         GIT_NOTES_REF="refs/notes/wrong" git log -1 > output &&
449         test_cmp expect-not-other output
450 '
451
452 test_expect_success 'Allow notes on non-commits (trees, blobs, tags)' '
453         echo "Note on a tree" > expect
454         git notes add -m "Note on a tree" HEAD: &&
455         git notes show HEAD: > actual &&
456         test_cmp expect actual &&
457         echo "Note on a blob" > expect
458         filename=$(git ls-tree --name-only HEAD | head -n1) &&
459         git notes add -m "Note on a blob" HEAD:$filename &&
460         git notes show HEAD:$filename > actual &&
461         test_cmp expect actual &&
462         echo "Note on a tag" > expect
463         git tag -a -m "This is an annotated tag" foobar HEAD^ &&
464         git notes add -m "Note on a tag" foobar &&
465         git notes show foobar > actual &&
466         test_cmp expect actual
467 '
468
469 cat > expect << EOF
470 commit 2ede89468182a62d0bde2583c736089bcf7d7e92
471 Author: A U Thor <author@example.com>
472 Date:   Thu Apr 7 15:19:13 2005 -0700
473
474     7th
475
476 Notes:
477     other note
478 EOF
479
480 test_expect_success 'create note from other note with "git notes add -C"' '
481         : > a7 &&
482         git add a7 &&
483         test_tick &&
484         git commit -m 7th &&
485         git notes add -C $(git notes list HEAD^) &&
486         git log -1 > actual &&
487         test_cmp expect actual &&
488         test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
489 '
490
491 test_expect_success 'create note from non-existing note with "git notes add -C" fails' '
492         : > a8 &&
493         git add a8 &&
494         test_tick &&
495         git commit -m 8th &&
496         test_must_fail git notes add -C deadbeef &&
497         test_must_fail git notes list HEAD
498 '
499
500 cat > expect << EOF
501 commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
502 Author: A U Thor <author@example.com>
503 Date:   Thu Apr 7 15:21:13 2005 -0700
504
505     9th
506
507 Notes:
508     yet another note
509 EOF
510
511 test_expect_success 'create note from other note with "git notes add -c"' '
512         : > a9 &&
513         git add a9 &&
514         test_tick &&
515         git commit -m 9th &&
516         MSG="yet another note" git notes add -c $(git notes list HEAD^^) &&
517         git log -1 > actual &&
518         test_cmp expect actual
519 '
520
521 test_expect_success 'create note from non-existing note with "git notes add -c" fails' '
522         : > a10 &&
523         git add a10 &&
524         test_tick &&
525         git commit -m 10th &&
526         test_must_fail MSG="yet another note" git notes add -c deadbeef &&
527         test_must_fail git notes list HEAD
528 '
529
530 cat > expect << EOF
531 commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
532 Author: A U Thor <author@example.com>
533 Date:   Thu Apr 7 15:21:13 2005 -0700
534
535     9th
536
537 Notes:
538     yet another note
539 $whitespace
540     yet another note
541 EOF
542
543 test_expect_success 'append to note from other note with "git notes append -C"' '
544         git notes append -C $(git notes list HEAD^) HEAD^ &&
545         git log -1 HEAD^ > actual &&
546         test_cmp expect actual
547 '
548
549 cat > expect << EOF
550 commit ffed603236bfa3891c49644257a83598afe8ae5a
551 Author: A U Thor <author@example.com>
552 Date:   Thu Apr 7 15:22:13 2005 -0700
553
554     10th
555
556 Notes:
557     other note
558 EOF
559
560 test_expect_success 'create note from other note with "git notes append -c"' '
561         MSG="other note" git notes append -c $(git notes list HEAD^) &&
562         git log -1 > actual &&
563         test_cmp expect actual
564 '
565
566 cat > expect << EOF
567 commit ffed603236bfa3891c49644257a83598afe8ae5a
568 Author: A U Thor <author@example.com>
569 Date:   Thu Apr 7 15:22:13 2005 -0700
570
571     10th
572
573 Notes:
574     other note
575 $whitespace
576     yet another note
577 EOF
578
579 test_expect_success 'append to note from other note with "git notes append -c"' '
580         MSG="yet another note" git notes append -c $(git notes list HEAD) &&
581         git log -1 > actual &&
582         test_cmp expect actual
583 '
584
585 cat > expect << EOF
586 commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
587 Author: A U Thor <author@example.com>
588 Date:   Thu Apr 7 15:23:13 2005 -0700
589
590     11th
591
592 Notes:
593     other note
594 $whitespace
595     yet another note
596 EOF
597
598 test_expect_success 'copy note with "git notes copy"' '
599         : > a11 &&
600         git add a11 &&
601         test_tick &&
602         git commit -m 11th &&
603         git notes copy HEAD^ HEAD &&
604         git log -1 > actual &&
605         test_cmp expect actual &&
606         test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
607 '
608
609 test_expect_success 'prevent overwrite with "git notes copy"' '
610         test_must_fail git notes copy HEAD~2 HEAD &&
611         git log -1 > actual &&
612         test_cmp expect actual &&
613         test "$(git notes list HEAD)" = "$(git notes list HEAD^)"
614 '
615
616 cat > expect << EOF
617 commit 6352c5e33dbcab725fe0579be16aa2ba8eb369be
618 Author: A U Thor <author@example.com>
619 Date:   Thu Apr 7 15:23:13 2005 -0700
620
621     11th
622
623 Notes:
624     yet another note
625 $whitespace
626     yet another note
627 EOF
628
629 test_expect_success 'allow overwrite with "git notes copy -f"' '
630         git notes copy -f HEAD~2 HEAD &&
631         git log -1 > actual &&
632         test_cmp expect actual &&
633         test "$(git notes list HEAD)" = "$(git notes list HEAD~2)"
634 '
635
636 test_expect_success 'cannot copy note from object without notes' '
637         : > a12 &&
638         git add a12 &&
639         test_tick &&
640         git commit -m 12th &&
641         : > a13 &&
642         git add a13 &&
643         test_tick &&
644         git commit -m 13th &&
645         test_must_fail git notes copy HEAD^ HEAD
646 '
647
648 test_done