Merge branch 'jn/gitweb-per-request-config'
[git/mingw.git] / t / t7500-commit.sh
blob162527c2114955df2c55db9bf37db59d05fe75f8
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 test "`git log --pretty=format:%s%b -1`" = "$1"
16 # A sanity check to see if commit is working at all.
17 test_expect_success 'a basic commit in an empty tree should succeed' '
18 echo content > foo &&
19 git add foo &&
20 git commit -m "initial commit"
23 test_expect_success 'nonexistent template file should return error' '
24 echo changes >> foo &&
25 git add foo &&
26 test_must_fail git commit --template "$PWD"/notexist
29 test_expect_success 'nonexistent template file in config should return error' '
30 git config commit.template "$PWD"/notexist &&
31 test_must_fail git commit &&
32 git config --unset commit.template
35 # From now on we'll use a template file that exists.
36 TEMPLATE="$PWD"/template
38 test_expect_success 'unedited template should not commit' '
39 echo "template line" > "$TEMPLATE" &&
40 test_must_fail git commit --template "$TEMPLATE"
43 test_expect_success 'unedited template with comments should not commit' '
44 echo "# comment in template" >> "$TEMPLATE" &&
45 test_must_fail git commit --template "$TEMPLATE"
48 test_expect_success 'a Signed-off-by line by itself should not commit' '
50 test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off &&
51 test_must_fail git commit --template "$TEMPLATE"
55 test_expect_success 'adding comments to a template should not commit' '
57 test_set_editor "$TEST_DIRECTORY"/t7500/add-comments &&
58 test_must_fail git commit --template "$TEMPLATE"
62 test_expect_success 'adding real content to a template should commit' '
64 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
65 git commit --template "$TEMPLATE"
66 ) &&
67 commit_msg_is "template linecommit message"
70 test_expect_success '-t option should be short for --template' '
71 echo "short template" > "$TEMPLATE" &&
72 echo "new content" >> foo &&
73 git add foo &&
75 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
76 git commit -t "$TEMPLATE"
77 ) &&
78 commit_msg_is "short templatecommit message"
81 test_expect_success 'config-specified template should commit' '
82 echo "new template" > "$TEMPLATE" &&
83 git config commit.template "$TEMPLATE" &&
84 echo "more content" >> foo &&
85 git add foo &&
87 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
88 git commit
89 ) &&
90 git config --unset commit.template &&
91 commit_msg_is "new templatecommit message"
94 test_expect_success 'explicit commit message should override template' '
95 echo "still more content" >> foo &&
96 git add foo &&
97 GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \
98 -m "command line msg" &&
99 commit_msg_is "command line msg"
102 test_expect_success 'commit message from file should override template' '
103 echo "content galore" >> foo &&
104 git add foo &&
105 echo "standard input msg" |
107 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
108 git commit --template "$TEMPLATE" --file -
109 ) &&
110 commit_msg_is "standard input msg"
113 test_expect_success 'using alternate GIT_INDEX_FILE (1)' '
115 cp .git/index saved-index &&
117 echo some new content >file &&
118 GIT_INDEX_FILE=.git/another_index &&
119 export GIT_INDEX_FILE &&
120 git add file &&
121 git commit -m "commit using another index" &&
122 git diff-index --exit-code HEAD &&
123 git diff-files --exit-code
124 ) &&
125 cmp .git/index saved-index >/dev/null
129 test_expect_success 'using alternate GIT_INDEX_FILE (2)' '
131 cp .git/index saved-index &&
133 rm -f .git/no-such-index &&
134 GIT_INDEX_FILE=.git/no-such-index &&
135 export GIT_INDEX_FILE &&
136 git commit -m "commit using nonexistent index" &&
137 test -z "$(git ls-files)" &&
138 test -z "$(git ls-tree HEAD)"
140 ) &&
141 cmp .git/index saved-index >/dev/null
144 cat > expect << EOF
145 zort
147 Signed-off-by: C O Mitter <committer@example.com>
150 test_expect_success '--signoff' '
151 echo "yet another content *narf*" >> foo &&
152 echo "zort" | git commit -s -F - foo &&
153 git cat-file commit HEAD | sed "1,/^\$/d" > output &&
154 test_cmp expect output
157 test_expect_success 'commit message from file (1)' '
158 mkdir subdir &&
159 echo "Log in top directory" >log &&
160 echo "Log in sub directory" >subdir/log &&
162 cd subdir &&
163 git commit --allow-empty -F log
164 ) &&
165 commit_msg_is "Log in sub directory"
168 test_expect_success 'commit message from file (2)' '
169 rm -f log &&
170 echo "Log in sub directory" >subdir/log &&
172 cd subdir &&
173 git commit --allow-empty -F log
174 ) &&
175 commit_msg_is "Log in sub directory"
178 test_expect_success 'commit message from stdin' '
180 cd subdir &&
181 echo "Log with foo word" | git commit --allow-empty -F -
182 ) &&
183 commit_msg_is "Log with foo word"
186 test_expect_success 'commit -F overrides -t' '
188 cd subdir &&
189 echo "-F log" > f.log &&
190 echo "-t template" > t.template &&
191 git commit --allow-empty -F f.log -t t.template
192 ) &&
193 commit_msg_is "-F log"
196 test_expect_success 'Commit without message is allowed with --allow-empty-message' '
197 echo "more content" >>foo &&
198 git add foo &&
199 >empty &&
200 git commit --allow-empty-message <empty &&
201 commit_msg_is ""
204 test_expect_success 'Commit without message is no-no without --allow-empty-message' '
205 echo "more content" >>foo &&
206 git add foo &&
207 >empty &&
208 test_must_fail git commit <empty
211 test_expect_success 'Commit a message with --allow-empty-message' '
212 echo "even more content" >>foo &&
213 git add foo &&
214 git commit --allow-empty-message -m"hello there" &&
215 commit_msg_is "hello there"
218 commit_for_rebase_autosquash_setup () {
219 echo "first content line" >>foo &&
220 git add foo &&
221 cat >log <<EOF &&
222 target message subject line
224 target message body line 1
225 target message body line 2
227 git commit -F log &&
228 echo "second content line" >>foo &&
229 git add foo &&
230 git commit -m "intermediate commit" &&
231 echo "third content line" >>foo &&
232 git add foo
235 test_expect_success 'commit --fixup provides correct one-line commit message' '
236 commit_for_rebase_autosquash_setup &&
237 git commit --fixup HEAD~1 &&
238 commit_msg_is "fixup! target message subject line"
241 test_expect_success 'commit --squash works with -F' '
242 commit_for_rebase_autosquash_setup &&
243 echo "log message from file" >msgfile &&
244 git commit --squash HEAD~1 -F msgfile &&
245 commit_msg_is "squash! target message subject linelog message from file"
248 test_expect_success 'commit --squash works with -m' '
249 commit_for_rebase_autosquash_setup &&
250 git commit --squash HEAD~1 -m "foo bar\nbaz" &&
251 commit_msg_is "squash! target message subject linefoo bar\nbaz"
254 test_expect_success 'commit --squash works with -C' '
255 commit_for_rebase_autosquash_setup &&
256 git commit --squash HEAD~1 -C HEAD &&
257 commit_msg_is "squash! target message subject lineintermediate commit"
260 test_expect_success 'commit --squash works with -c' '
261 commit_for_rebase_autosquash_setup &&
262 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
263 git commit --squash HEAD~1 -c HEAD &&
264 commit_msg_is "squash! target message subject lineedited commit"
267 test_expect_success 'commit --squash works with -C for same commit' '
268 commit_for_rebase_autosquash_setup &&
269 git commit --squash HEAD -C HEAD &&
270 commit_msg_is "squash! intermediate commit"
273 test_expect_success 'commit --squash works with -c for same commit' '
274 commit_for_rebase_autosquash_setup &&
275 test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
276 git commit --squash HEAD -c HEAD &&
277 commit_msg_is "squash! edited commit"
280 test_expect_success 'commit --squash works with editor' '
281 commit_for_rebase_autosquash_setup &&
282 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
283 git commit --squash HEAD~1 &&
284 commit_msg_is "squash! target message subject linecommit message"
287 test_expect_success 'invalid message options when using --fixup' '
288 echo changes >>foo &&
289 echo "message" >log &&
290 git add foo &&
291 test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 &&
292 test_must_fail git commit --fixup HEAD~1 -C HEAD~2 &&
293 test_must_fail git commit --fixup HEAD~1 -c HEAD~2 &&
294 test_must_fail git commit --fixup HEAD~1 -m "cmdline message" &&
295 test_must_fail git commit --fixup HEAD~1 -F log
298 test_done