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