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
11#!/bin/sh
12echo "$MSG" > "$1"
13echo "$MSG" >& 2
14EOF
15chmod a+x fake_editor.sh
16VISUAL=./fake_editor.sh
17export VISUAL
18
19test_expect_success 'cannot annotate non-existing HEAD' '
20 (MSG=3 && export MSG && test_must_fail git notes edit)
21'
22
23test_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
34test_expect_success 'need valid notes ref' '
35 (MSG=1 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
36 test_must_fail git notes edit) &&
37 (MSG=2 GIT_NOTES_REF=/ && export MSG GIT_NOTES_REF &&
38 test_must_fail git notes show)
39'
40
41test_expect_success 'refusing to edit in refs/heads/' '
42 (MSG=1 GIT_NOTES_REF=refs/heads/bogus &&
43 export MSG GIT_NOTES_REF &&
44 test_must_fail git notes edit)
45'
46
47test_expect_success 'refusing to edit 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
54test_expect_success 'handle empty notes gracefully' '
55 git notes show ; test 1 = $?
56'
57
58test_expect_success 'create notes' '
59 git config core.notesRef refs/notes/commits &&
60 MSG=b1 git notes edit &&
61 test ! -f .git/new-notes &&
62 test 1 = $(git ls-tree refs/notes/commits | wc -l) &&
63 test b1 = $(git notes show) &&
64 git show HEAD^ &&
65 test_must_fail git notes show HEAD^
66'
67
68cat > expect << EOF
69commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
70Author: A U Thor <author@example.com>
71Date: Thu Apr 7 15:14:13 2005 -0700
72
73 2nd
74
75Notes:
76 b1
77EOF
78
79test_expect_success 'show notes' '
80 ! (git cat-file commit HEAD | grep b1) &&
81 git log -1 > output &&
82 test_cmp expect output
83'
84test_expect_success 'create multi-line notes (setup)' '
85 : > a3 &&
86 git add a3 &&
87 test_tick &&
88 git commit -m 3rd &&
89 MSG="b3
90c3c3c3c3
91d3d3d3" git notes edit
92'
93
94cat > expect-multiline << EOF
95commit 1584215f1d29c65e99c6c6848626553fdd07fd75
96Author: A U Thor <author@example.com>
97Date: Thu Apr 7 15:15:13 2005 -0700
98
99 3rd
100
101Notes:
102 b3
103 c3c3c3c3
104 d3d3d3
105EOF
106
107printf "\n" >> expect-multiline
108cat expect >> expect-multiline
109
110test_expect_success 'show multi-line notes' '
111 git log -2 > output &&
112 test_cmp expect-multiline output
113'
114test_expect_success 'create -m and -F notes (setup)' '
115 : > a4 &&
116 git add a4 &&
117 test_tick &&
118 git commit -m 4th &&
119 echo "xyzzy" > note5 &&
120 git notes edit -m spam -F note5 -m "foo
121bar
122baz"
123'
124
125whitespace=" "
126cat > expect-m-and-F << EOF
127commit 15023535574ded8b1a89052b32673f84cf9582b8
128Author: A U Thor <author@example.com>
129Date: Thu Apr 7 15:16:13 2005 -0700
130
131 4th
132
133Notes:
134 spam
135$whitespace
136 xyzzy
137$whitespace
138 foo
139 bar
140 baz
141EOF
142
143printf "\n" >> expect-m-and-F
144cat expect-multiline >> expect-m-and-F
145
146test_expect_success 'show -m and -F notes' '
147 git log -3 > output &&
148 test_cmp expect-m-and-F output
149'
150
151cat >expect << EOF
152commit 15023535574ded8b1a89052b32673f84cf9582b8
153tree e070e3af51011e47b183c33adf9736736a525709
154parent 1584215f1d29c65e99c6c6848626553fdd07fd75
155author A U Thor <author@example.com> 1112912173 -0700
156committer C O Mitter <committer@example.com> 1112912173 -0700
157
158 4th
159EOF
160test_expect_success 'git log --pretty=raw does not show notes' '
161 git log -1 --pretty=raw >output &&
162 test_cmp expect output
163'
164
165cat >>expect <<EOF
166
167Notes:
168 spam
169$whitespace
170 xyzzy
171$whitespace
172 foo
173 bar
174 baz
175EOF
176test_expect_success 'git log --show-notes' '
177 git log -1 --pretty=raw --show-notes >output &&
178 test_cmp expect output
179'
180
181test_expect_success 'git log --no-notes' '
182 git log -1 --no-notes >output &&
183 ! grep spam output
184'
185
186test_expect_success 'git format-patch does not show notes' '
187 git format-patch -1 --stdout >output &&
188 ! grep spam output
189'
190
191test_expect_success 'git format-patch --show-notes does show notes' '
192 git format-patch --show-notes -1 --stdout >output &&
193 grep spam output
194'
195
196for pretty in \
197 "" --pretty --pretty=raw --pretty=short --pretty=medium \
198 --pretty=full --pretty=fuller --pretty=format:%s --oneline
199do
200 case "$pretty" in
201 "") p= not= negate="" ;;
202 ?*) p="$pretty" not=" not" negate="!" ;;
203 esac
204 test_expect_success "git show $pretty does$not show notes" '
205 git show $p >output &&
206 eval "$negate grep spam output"
207 '
208done
209
210test_done