builtin-notes: Add "append" subcommand for appending to note objects
[git/mingw.git] / t / t3301-notes.sh
blob290ed63d4e3048cdaae77d10ea13434beaea6c7f
1 #!/bin/sh
3 # Copyright (c) 2007 Johannes E. Schindelin
6 test_description='Test commit notes'
8 . ./test-lib.sh
10 cat > fake_editor.sh << \EOF
11 echo "$MSG" > "$1"
12 echo "$MSG" >& 2
13 EOF
14 chmod a+x fake_editor.sh
15 GIT_EDITOR=./fake_editor.sh
16 export GIT_EDITOR
18 test_expect_success 'cannot annotate non-existing HEAD' '
19 (MSG=3 && export MSG && test_must_fail git notes add)
22 test_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
33 test_expect_success 'need valid notes ref' '
34 (MSG=1 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
35 test_must_fail git notes add) &&
36 (MSG=2 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
37 test_must_fail git notes show)
40 test_expect_success 'refusing to add notes in refs/heads/' '
41 (MSG=1 GIT_NOTES_REF=refs/heads/bogus &&
42 export MSG GIT_NOTES_REF &&
43 test_must_fail git notes add)
46 test_expect_success 'refusing to edit notes in refs/remotes/' '
47 (MSG=1 GIT_NOTES_REF=refs/remotes/bogus &&
48 export MSG GIT_NOTES_REF &&
49 test_must_fail git notes edit)
52 # 1 indicates caught gracefully by die, 128 means git-show barked
53 test_expect_success 'handle empty notes gracefully' '
54 git notes show ; test 1 = $?
57 test_expect_success 'create notes' '
58 git config core.notesRef refs/notes/commits &&
59 MSG=b4 git notes add &&
60 test ! -f .git/NOTES_EDITMSG &&
61 test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
62 test b4 = $(git notes show) &&
63 git show HEAD^ &&
64 test_must_fail git notes show HEAD^
67 test_expect_success 'edit existing notes' '
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^
76 test_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^
85 test_expect_success 'can overwrite existing note with "git notes add -f"' '
86 MSG=b1 git notes add -f &&
87 test ! -f .git/NOTES_EDITMSG &&
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^
94 cat > expect << EOF
95 commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
96 Author: A U Thor <author@example.com>
97 Date: Thu Apr 7 15:14:13 2005 -0700
99 2nd
101 Notes:
105 test_expect_success 'show notes' '
106 ! (git cat-file commit HEAD | grep b1) &&
107 git log -1 > output &&
108 test_cmp expect output
111 test_expect_success 'create multi-line notes (setup)' '
112 : > a3 &&
113 git add a3 &&
114 test_tick &&
115 git commit -m 3rd &&
116 MSG="b3
117 c3c3c3c3
118 d3d3d3" git notes add
121 cat > expect-multiline << EOF
122 commit 1584215f1d29c65e99c6c6848626553fdd07fd75
123 Author: A U Thor <author@example.com>
124 Date: Thu Apr 7 15:15:13 2005 -0700
128 Notes:
130 c3c3c3c3
131 d3d3d3
134 printf "\n" >> expect-multiline
135 cat expect >> expect-multiline
137 test_expect_success 'show multi-line notes' '
138 git log -2 > output &&
139 test_cmp expect-multiline output
141 test_expect_success 'create -F notes (setup)' '
142 : > a4 &&
143 git add a4 &&
144 test_tick &&
145 git commit -m 4th &&
146 echo "xyzzy" > note5 &&
147 git notes add -F note5
150 cat > expect-F << EOF
151 commit 15023535574ded8b1a89052b32673f84cf9582b8
152 Author: A U Thor <author@example.com>
153 Date: Thu Apr 7 15:16:13 2005 -0700
157 Notes:
158 xyzzy
161 printf "\n" >> expect-F
162 cat expect-multiline >> expect-F
164 test_expect_success 'show -F notes' '
165 git log -3 > output &&
166 test_cmp expect-F output
169 cat >expect << EOF
170 commit 15023535574ded8b1a89052b32673f84cf9582b8
171 tree e070e3af51011e47b183c33adf9736736a525709
172 parent 1584215f1d29c65e99c6c6848626553fdd07fd75
173 author A U Thor <author@example.com> 1112912173 -0700
174 committer C O Mitter <committer@example.com> 1112912173 -0700
178 test_expect_success 'git log --pretty=raw does not show notes' '
179 git log -1 --pretty=raw >output &&
180 test_cmp expect output
183 cat >>expect <<EOF
185 Notes:
186 xyzzy
188 test_expect_success 'git log --show-notes' '
189 git log -1 --pretty=raw --show-notes >output &&
190 test_cmp expect output
193 test_expect_success 'git log --no-notes' '
194 git log -1 --no-notes >output &&
195 ! grep xyzzy output
198 test_expect_success 'git format-patch does not show notes' '
199 git format-patch -1 --stdout >output &&
200 ! grep xyzzy output
203 test_expect_success 'git format-patch --show-notes does show notes' '
204 git format-patch --show-notes -1 --stdout >output &&
205 grep xyzzy output
208 for pretty in \
209 "" --pretty --pretty=raw --pretty=short --pretty=medium \
210 --pretty=full --pretty=fuller --pretty=format:%s --oneline
212 case "$pretty" in
213 "") p= not= negate="" ;;
214 ?*) p="$pretty" not=" not" negate="!" ;;
215 esac
216 test_expect_success "git show $pretty does$not show notes" '
217 git show $p >output &&
218 eval "$negate grep xyzzy output"
220 done
222 test_expect_success 'create -m notes (setup)' '
223 : > a5 &&
224 git add a5 &&
225 test_tick &&
226 git commit -m 5th &&
227 git notes add -m spam -m "foo
229 baz"
232 whitespace=" "
233 cat > expect-m << EOF
234 commit bd1753200303d0a0344be813e504253b3d98e74d
235 Author: A U Thor <author@example.com>
236 Date: Thu Apr 7 15:17:13 2005 -0700
240 Notes:
241 spam
242 $whitespace
248 printf "\n" >> expect-m
249 cat expect-F >> expect-m
251 test_expect_success 'show -m notes' '
252 git log -4 > output &&
253 test_cmp expect-m output
256 test_expect_success 'remove note with add -f -F /dev/null (setup)' '
257 git notes add -f -F /dev/null
260 cat > expect-rm-F << EOF
261 commit bd1753200303d0a0344be813e504253b3d98e74d
262 Author: A U Thor <author@example.com>
263 Date: Thu Apr 7 15:17:13 2005 -0700
268 printf "\n" >> expect-rm-F
269 cat expect-F >> expect-rm-F
271 test_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
277 test_expect_success 'do not create empty note with -m "" (setup)' '
278 git notes add -m ""
281 test_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
287 test_expect_success 'remove note with "git notes remove" (setup)' '
288 git notes remove HEAD^
291 cat > expect-rm-remove << EOF
292 commit bd1753200303d0a0344be813e504253b3d98e74d
293 Author: A U Thor <author@example.com>
294 Date: Thu Apr 7 15:17:13 2005 -0700
298 commit 15023535574ded8b1a89052b32673f84cf9582b8
299 Author: A U Thor <author@example.com>
300 Date: Thu Apr 7 15:16:13 2005 -0700
305 printf "\n" >> expect-rm-remove
306 cat expect-multiline >> expect-rm-remove
308 test_expect_success 'verify note removal with "git notes remove"' '
309 git log -4 > output &&
310 test_cmp expect-rm-remove output &&
311 ! git notes show HEAD^
314 cat > expect << EOF
315 c18dc024e14f08d18d14eea0d747ff692d66d6a3 1584215f1d29c65e99c6c6848626553fdd07fd75
316 c9c6af7f78bc47490dbf3e822cf2f3c24d4b9061 268048bfb8a1fb38e703baceb8ab235421bf80c5
319 test_expect_success 'list notes with "git notes list"' '
320 git notes list > output &&
321 test_cmp expect output
324 test_expect_success 'list notes with "git notes"' '
325 git notes > output &&
326 test_cmp expect output
329 cat > expect << EOF
330 c18dc024e14f08d18d14eea0d747ff692d66d6a3
333 test_expect_success 'list specific note with "git notes list <object>"' '
334 git notes list HEAD^^ > output &&
335 test_cmp expect output
338 cat > expect << EOF
341 test_expect_success 'listing non-existing notes fails' '
342 test_must_fail git notes list HEAD > output &&
343 test_cmp expect output
346 cat > expect << EOF
347 Initial set of notes
349 More notes appended with git notes append
352 test_expect_success 'append to existing note with "git notes append"' '
353 git notes add -m "Initial set of notes" &&
354 git notes append -m "More notes appended with git notes append" &&
355 git notes show > output &&
356 test_cmp expect output
359 test_expect_success 'appending empty string does not change existing note' '
360 git notes append -m "" &&
361 git notes show > output &&
362 test_cmp expect output
365 test_expect_success 'git notes append == add when there is no existing note' '
366 git notes remove HEAD &&
367 test_must_fail git notes list HEAD &&
368 git notes append -m "Initial set of notes
370 More notes appended with git notes append" &&
371 git notes show > output &&
372 test_cmp expect output
375 test_expect_success 'appending empty string to non-existing note does not create note' '
376 git notes remove HEAD &&
377 test_must_fail git notes list HEAD &&
378 git notes append -m "" &&
379 test_must_fail git notes list HEAD
382 test_expect_success 'create other note on a different notes ref (setup)' '
383 : > a6 &&
384 git add a6 &&
385 test_tick &&
386 git commit -m 6th &&
387 GIT_NOTES_REF="refs/notes/other" git notes add -m "other note"
390 cat > expect-other << EOF
391 commit 387a89921c73d7ed72cd94d179c1c7048ca47756
392 Author: A U Thor <author@example.com>
393 Date: Thu Apr 7 15:18:13 2005 -0700
397 Notes:
398 other note
401 cat > expect-not-other << EOF
402 commit 387a89921c73d7ed72cd94d179c1c7048ca47756
403 Author: A U Thor <author@example.com>
404 Date: Thu Apr 7 15:18:13 2005 -0700
409 test_expect_success 'Do not show note on other ref by default' '
410 git log -1 > output &&
411 test_cmp expect-not-other output
414 test_expect_success 'Do show note when ref is given in GIT_NOTES_REF' '
415 GIT_NOTES_REF="refs/notes/other" git log -1 > output &&
416 test_cmp expect-other output
419 test_expect_success 'Do show note when ref is given in core.notesRef config' '
420 git config core.notesRef "refs/notes/other" &&
421 git log -1 > output &&
422 test_cmp expect-other output
425 test_expect_success 'Do not show note when core.notesRef is overridden' '
426 GIT_NOTES_REF="refs/notes/wrong" git log -1 > output &&
427 test_cmp expect-not-other output
430 test_expect_success 'Allow notes on non-commits (trees, blobs, tags)' '
431 echo "Note on a tree" > expect
432 git notes add -m "Note on a tree" HEAD: &&
433 git notes show HEAD: > actual &&
434 test_cmp expect actual &&
435 echo "Note on a blob" > expect
436 filename=$(git ls-tree --name-only HEAD | head -n1) &&
437 git notes add -m "Note on a blob" HEAD:$filename &&
438 git notes show HEAD:$filename > actual &&
439 test_cmp expect actual &&
440 echo "Note on a tag" > expect
441 git tag -a -m "This is an annotated tag" foobar HEAD^ &&
442 git notes add -m "Note on a tag" foobar &&
443 git notes show foobar > actual &&
444 test_cmp expect actual
447 test_done