archive: handle commits with an empty tree
[git/jnareb-git.git] / t / t7500-commit.sh
blob1c908f4d3966cb9a2769465652981bef831f312d
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_i18ncmp $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 &&
32 GIT_EDITOR="echo hello >\"\$1\"" &&
33 export GIT_EDITOR &&
34 test_must_fail git commit --template "$PWD"/notexist
38 test_expect_success 'nonexistent template file in config should return error' '
39 git config commit.template "$PWD"/notexist &&
40 test_when_finished "git config --unset commit.template" &&
42 GIT_EDITOR="echo hello >\"\$1\"" &&
43 export GIT_EDITOR &&
44 test_must_fail git commit
48 # From now on we'll use a template file that exists.
49 TEMPLATE="$PWD"/template
51 test_expect_success 'unedited template should not commit' '
52 echo "template line" > "$TEMPLATE" &&
53 test_must_fail git commit --template "$TEMPLATE"
56 test_expect_success 'unedited template with comments should not commit' '
57 echo "# comment in template" >> "$TEMPLATE" &&
58 test_must_fail git commit --template "$TEMPLATE"
61 test_expect_success 'a Signed-off-by line by itself should not commit' '
63 test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off &&
64 test_must_fail git commit --template "$TEMPLATE"
68 test_expect_success 'adding comments to a template should not commit' '
70 test_set_editor "$TEST_DIRECTORY"/t7500/add-comments &&
71 test_must_fail git commit --template "$TEMPLATE"
75 test_expect_success 'adding real content to a template should commit' '
77 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
78 git commit --template "$TEMPLATE"
79 ) &&
80 commit_msg_is "template linecommit message"
83 test_expect_success '-t option should be short for --template' '
84 echo "short template" > "$TEMPLATE" &&
85 echo "new content" >> foo &&
86 git add foo &&
88 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
89 git commit -t "$TEMPLATE"
90 ) &&
91 commit_msg_is "short templatecommit message"
94 test_expect_success 'config-specified template should commit' '
95 echo "new template" > "$TEMPLATE" &&
96 git config commit.template "$TEMPLATE" &&
97 echo "more content" >> foo &&
98 git add foo &&
100 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
101 git commit
102 ) &&
103 git config --unset commit.template &&
104 commit_msg_is "new templatecommit message"
107 test_expect_success 'explicit commit message should override template' '
108 echo "still more content" >> foo &&
109 git add foo &&
110 GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \
111 -m "command line msg" &&
112 commit_msg_is "command line msg"
115 test_expect_success 'commit message from file should override template' '
116 echo "content galore" >> foo &&
117 git add foo &&
118 echo "standard input msg" |
120 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
121 git commit --template "$TEMPLATE" --file -
122 ) &&
123 commit_msg_is "standard input msg"
126 cat >"$TEMPLATE" <<\EOF
129 ### template
132 test_expect_success 'commit message from template with whitespace issue' '
133 echo "content galore" >>foo &&
134 git add foo &&
135 GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-whitespaced-content git commit \
136 --template "$TEMPLATE" &&
137 commit_msg_is "commit message"
140 test_expect_success 'using alternate GIT_INDEX_FILE (1)' '
142 cp .git/index saved-index &&
144 echo some new content >file &&
145 GIT_INDEX_FILE=.git/another_index &&
146 export GIT_INDEX_FILE &&
147 git add file &&
148 git commit -m "commit using another index" &&
149 git diff-index --exit-code HEAD &&
150 git diff-files --exit-code
151 ) &&
152 cmp .git/index saved-index >/dev/null
156 test_expect_success 'using alternate GIT_INDEX_FILE (2)' '
158 cp .git/index saved-index &&
160 rm -f .git/no-such-index &&
161 GIT_INDEX_FILE=.git/no-such-index &&
162 export GIT_INDEX_FILE &&
163 git commit -m "commit using nonexistent index" &&
164 test -z "$(git ls-files)" &&
165 test -z "$(git ls-tree HEAD)"
167 ) &&
168 cmp .git/index saved-index >/dev/null
171 cat > expect << EOF
172 zort
174 Signed-off-by: C O Mitter <committer@example.com>
177 test_expect_success '--signoff' '
178 echo "yet another content *narf*" >> foo &&
179 echo "zort" | git commit -s -F - foo &&
180 git cat-file commit HEAD | sed "1,/^\$/d" > output &&
181 test_cmp expect output
184 test_expect_success 'commit message from file (1)' '
185 mkdir subdir &&
186 echo "Log in top directory" >log &&
187 echo "Log in sub directory" >subdir/log &&
189 cd subdir &&
190 git commit --allow-empty -F log
191 ) &&
192 commit_msg_is "Log in sub directory"
195 test_expect_success 'commit message from file (2)' '
196 rm -f log &&
197 echo "Log in sub directory" >subdir/log &&
199 cd subdir &&
200 git commit --allow-empty -F log
201 ) &&
202 commit_msg_is "Log in sub directory"
205 test_expect_success 'commit message from stdin' '
207 cd subdir &&
208 echo "Log with foo word" | git commit --allow-empty -F -
209 ) &&
210 commit_msg_is "Log with foo word"
213 test_expect_success 'commit -F overrides -t' '
215 cd subdir &&
216 echo "-F log" > f.log &&
217 echo "-t template" > t.template &&
218 git commit --allow-empty -F f.log -t t.template
219 ) &&
220 commit_msg_is "-F log"
223 test_expect_success 'Commit without message is allowed with --allow-empty-message' '
224 echo "more content" >>foo &&
225 git add foo &&
226 >empty &&
227 git commit --allow-empty-message <empty &&
228 commit_msg_is ""
231 test_expect_success 'Commit without message is no-no without --allow-empty-message' '
232 echo "more content" >>foo &&
233 git add foo &&
234 >empty &&
235 test_must_fail git commit <empty
238 test_expect_success 'Commit a message with --allow-empty-message' '
239 echo "even more content" >>foo &&
240 git add foo &&
241 git commit --allow-empty-message -m"hello there" &&
242 commit_msg_is "hello there"
245 commit_for_rebase_autosquash_setup () {
246 echo "first content line" >>foo &&
247 git add foo &&
248 cat >log <<EOF &&
249 target message subject line
251 target message body line 1
252 target message body line 2
254 git commit -F log &&
255 echo "second content line" >>foo &&
256 git add foo &&
257 git commit -m "intermediate commit" &&
258 echo "third content line" >>foo &&
259 git add foo
262 test_expect_success 'commit --fixup provides correct one-line commit message' '
263 commit_for_rebase_autosquash_setup &&
264 git commit --fixup HEAD~1 &&
265 commit_msg_is "fixup! target message subject line"
268 test_expect_success 'commit --squash works with -F' '
269 commit_for_rebase_autosquash_setup &&
270 echo "log message from file" >msgfile &&
271 git commit --squash HEAD~1 -F msgfile &&
272 commit_msg_is "squash! target message subject linelog message from file"
275 test_expect_success 'commit --squash works with -m' '
276 commit_for_rebase_autosquash_setup &&
277 git commit --squash HEAD~1 -m "foo bar\nbaz" &&
278 commit_msg_is "squash! target message subject linefoo bar\nbaz"
281 test_expect_success 'commit --squash works with -C' '
282 commit_for_rebase_autosquash_setup &&
283 git commit --squash HEAD~1 -C HEAD &&
284 commit_msg_is "squash! target message subject lineintermediate commit"
287 test_expect_success 'commit --squash works with -c' '
288 commit_for_rebase_autosquash_setup &&
289 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
290 git commit --squash HEAD~1 -c HEAD &&
291 commit_msg_is "squash! target message subject lineedited commit"
294 test_expect_success 'commit --squash works with -C for same commit' '
295 commit_for_rebase_autosquash_setup &&
296 git commit --squash HEAD -C HEAD &&
297 commit_msg_is "squash! intermediate commit"
300 test_expect_success 'commit --squash works with -c for same commit' '
301 commit_for_rebase_autosquash_setup &&
302 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
303 git commit --squash HEAD -c HEAD &&
304 commit_msg_is "squash! edited commit"
307 test_expect_success 'commit --squash works with editor' '
308 commit_for_rebase_autosquash_setup &&
309 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
310 git commit --squash HEAD~1 &&
311 commit_msg_is "squash! target message subject linecommit message"
314 test_expect_success 'invalid message options when using --fixup' '
315 echo changes >>foo &&
316 echo "message" >log &&
317 git add foo &&
318 test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 &&
319 test_must_fail git commit --fixup HEAD~1 -C HEAD~2 &&
320 test_must_fail git commit --fixup HEAD~1 -c HEAD~2 &&
321 test_must_fail git commit --fixup HEAD~1 -m "cmdline message" &&
322 test_must_fail git commit --fixup HEAD~1 -F log
325 test_done