vcs-svn: use strbuf for revision log
[git/mingw.git] / t / t7500-commit.sh
blobd551b77ce6d94acce80069f2197f0169ee59e6d5
1 #!/bin/sh
3 # Copyright (c) 2007 Steven Grimm
6 test_description='git commit
8 Tests for selected commit options.'
10 . ./test-lib.sh
12 commit_msg_is () {
13 expect=commit_msg_is.expect
14 actual=commit_msg_is.actual
16 printf "%s" "$(git log --pretty=format:%s%b -1)" >$expect &&
17 printf "%s" "$1" >$actual &&
18 test_cmp $expect $actual
21 # A sanity check to see if commit is working at all.
22 test_expect_success 'a basic commit in an empty tree should succeed' '
23 echo content > foo &&
24 git add foo &&
25 git commit -m "initial commit"
28 test_expect_success 'nonexistent template file should return error' '
29 echo changes >> foo &&
30 git add foo &&
31 test_must_fail git commit --template "$PWD"/notexist
34 test_expect_success 'nonexistent template file in config should return error' '
35 git config commit.template "$PWD"/notexist &&
36 test_must_fail git commit &&
37 git config --unset commit.template
40 # From now on we'll use a template file that exists.
41 TEMPLATE="$PWD"/template
43 test_expect_success 'unedited template should not commit' '
44 echo "template line" > "$TEMPLATE" &&
45 test_must_fail git commit --template "$TEMPLATE"
48 test_expect_success 'unedited template with comments should not commit' '
49 echo "# comment in template" >> "$TEMPLATE" &&
50 test_must_fail git commit --template "$TEMPLATE"
53 test_expect_success 'a Signed-off-by line by itself should not commit' '
55 test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off &&
56 test_must_fail git commit --template "$TEMPLATE"
60 test_expect_success 'adding comments to a template should not commit' '
62 test_set_editor "$TEST_DIRECTORY"/t7500/add-comments &&
63 test_must_fail git commit --template "$TEMPLATE"
67 test_expect_success 'adding real content to a template should commit' '
69 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
70 git commit --template "$TEMPLATE"
71 ) &&
72 commit_msg_is "template linecommit message"
75 test_expect_success '-t option should be short for --template' '
76 echo "short template" > "$TEMPLATE" &&
77 echo "new content" >> foo &&
78 git add foo &&
80 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
81 git commit -t "$TEMPLATE"
82 ) &&
83 commit_msg_is "short templatecommit message"
86 test_expect_success 'config-specified template should commit' '
87 echo "new template" > "$TEMPLATE" &&
88 git config commit.template "$TEMPLATE" &&
89 echo "more content" >> foo &&
90 git add foo &&
92 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
93 git commit
94 ) &&
95 git config --unset commit.template &&
96 commit_msg_is "new templatecommit message"
99 test_expect_success 'explicit commit message should override template' '
100 echo "still more content" >> foo &&
101 git add foo &&
102 GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \
103 -m "command line msg" &&
104 commit_msg_is "command line msg"
107 test_expect_success 'commit message from file should override template' '
108 echo "content galore" >> foo &&
109 git add foo &&
110 echo "standard input msg" |
112 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
113 git commit --template "$TEMPLATE" --file -
114 ) &&
115 commit_msg_is "standard input msg"
118 test_expect_success 'using alternate GIT_INDEX_FILE (1)' '
120 cp .git/index saved-index &&
122 echo some new content >file &&
123 GIT_INDEX_FILE=.git/another_index &&
124 export GIT_INDEX_FILE &&
125 git add file &&
126 git commit -m "commit using another index" &&
127 git diff-index --exit-code HEAD &&
128 git diff-files --exit-code
129 ) &&
130 cmp .git/index saved-index >/dev/null
134 test_expect_success 'using alternate GIT_INDEX_FILE (2)' '
136 cp .git/index saved-index &&
138 rm -f .git/no-such-index &&
139 GIT_INDEX_FILE=.git/no-such-index &&
140 export GIT_INDEX_FILE &&
141 git commit -m "commit using nonexistent index" &&
142 test -z "$(git ls-files)" &&
143 test -z "$(git ls-tree HEAD)"
145 ) &&
146 cmp .git/index saved-index >/dev/null
149 cat > expect << EOF
150 zort
152 Signed-off-by: C O Mitter <committer@example.com>
155 test_expect_success '--signoff' '
156 echo "yet another content *narf*" >> foo &&
157 echo "zort" | git commit -s -F - foo &&
158 git cat-file commit HEAD | sed "1,/^\$/d" > output &&
159 test_cmp expect output
162 test_expect_success 'commit message from file (1)' '
163 mkdir subdir &&
164 echo "Log in top directory" >log &&
165 echo "Log in sub directory" >subdir/log &&
167 cd subdir &&
168 git commit --allow-empty -F log
169 ) &&
170 commit_msg_is "Log in sub directory"
173 test_expect_success 'commit message from file (2)' '
174 rm -f log &&
175 echo "Log in sub directory" >subdir/log &&
177 cd subdir &&
178 git commit --allow-empty -F log
179 ) &&
180 commit_msg_is "Log in sub directory"
183 test_expect_success 'commit message from stdin' '
185 cd subdir &&
186 echo "Log with foo word" | git commit --allow-empty -F -
187 ) &&
188 commit_msg_is "Log with foo word"
191 test_expect_success 'commit -F overrides -t' '
193 cd subdir &&
194 echo "-F log" > f.log &&
195 echo "-t template" > t.template &&
196 git commit --allow-empty -F f.log -t t.template
197 ) &&
198 commit_msg_is "-F log"
201 test_expect_success 'Commit without message is allowed with --allow-empty-message' '
202 echo "more content" >>foo &&
203 git add foo &&
204 >empty &&
205 git commit --allow-empty-message <empty &&
206 commit_msg_is ""
209 test_expect_success 'Commit without message is no-no without --allow-empty-message' '
210 echo "more content" >>foo &&
211 git add foo &&
212 >empty &&
213 test_must_fail git commit <empty
216 test_expect_success 'Commit a message with --allow-empty-message' '
217 echo "even more content" >>foo &&
218 git add foo &&
219 git commit --allow-empty-message -m"hello there" &&
220 commit_msg_is "hello there"
223 commit_for_rebase_autosquash_setup () {
224 echo "first content line" >>foo &&
225 git add foo &&
226 cat >log <<EOF &&
227 target message subject line
229 target message body line 1
230 target message body line 2
232 git commit -F log &&
233 echo "second content line" >>foo &&
234 git add foo &&
235 git commit -m "intermediate commit" &&
236 echo "third content line" >>foo &&
237 git add foo
240 test_expect_success 'commit --fixup provides correct one-line commit message' '
241 commit_for_rebase_autosquash_setup &&
242 git commit --fixup HEAD~1 &&
243 commit_msg_is "fixup! target message subject line"
246 test_expect_success 'commit --squash works with -F' '
247 commit_for_rebase_autosquash_setup &&
248 echo "log message from file" >msgfile &&
249 git commit --squash HEAD~1 -F msgfile &&
250 commit_msg_is "squash! target message subject linelog message from file"
253 test_expect_success 'commit --squash works with -m' '
254 commit_for_rebase_autosquash_setup &&
255 git commit --squash HEAD~1 -m "foo bar\nbaz" &&
256 commit_msg_is "squash! target message subject linefoo bar\nbaz"
259 test_expect_success 'commit --squash works with -C' '
260 commit_for_rebase_autosquash_setup &&
261 git commit --squash HEAD~1 -C HEAD &&
262 commit_msg_is "squash! target message subject lineintermediate commit"
265 test_expect_success 'commit --squash works with -c' '
266 commit_for_rebase_autosquash_setup &&
267 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
268 git commit --squash HEAD~1 -c HEAD &&
269 commit_msg_is "squash! target message subject lineedited commit"
272 test_expect_success 'commit --squash works with -C for same commit' '
273 commit_for_rebase_autosquash_setup &&
274 git commit --squash HEAD -C HEAD &&
275 commit_msg_is "squash! intermediate commit"
278 test_expect_success 'commit --squash works with -c for same commit' '
279 commit_for_rebase_autosquash_setup &&
280 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
281 git commit --squash HEAD -c HEAD &&
282 commit_msg_is "squash! edited commit"
285 test_expect_success 'commit --squash works with editor' '
286 commit_for_rebase_autosquash_setup &&
287 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
288 git commit --squash HEAD~1 &&
289 commit_msg_is "squash! target message subject linecommit message"
292 test_expect_success 'invalid message options when using --fixup' '
293 echo changes >>foo &&
294 echo "message" >log &&
295 git add foo &&
296 test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 &&
297 test_must_fail git commit --fixup HEAD~1 -C HEAD~2 &&
298 test_must_fail git commit --fixup HEAD~1 -c HEAD~2 &&
299 test_must_fail git commit --fixup HEAD~1 -m "cmdline message" &&
300 test_must_fail git commit --fixup HEAD~1 -F log
303 test_done