3 # Copyright (c) 2007 Steven Grimm
6 test_description
='git commit
8 Tests for selected commit options.'
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' '
20 git commit -m "initial commit"
23 test_expect_success
'nonexistent template file should return error' '
24 echo changes >> foo &&
27 GIT_EDITOR="echo hello >\"\$1\"" &&
29 test_must_fail git commit --template "$PWD"/notexist
33 test_expect_success
'nonexistent template file in config should return error' '
34 git config commit.template "$PWD"/notexist &&
35 test_when_finished "git config --unset commit.template" &&
37 GIT_EDITOR="echo hello >\"\$1\"" &&
39 test_must_fail git commit
43 # From now on we'll use a template file that exists.
44 TEMPLATE
="$PWD"/template
46 test_expect_success
'unedited template should not commit' '
47 echo "template line" > "$TEMPLATE" &&
48 test_must_fail git commit --template "$TEMPLATE"
51 test_expect_success
'unedited template with comments should not commit' '
52 echo "# comment in template" >> "$TEMPLATE" &&
53 test_must_fail git commit --template "$TEMPLATE"
56 test_expect_success
'a Signed-off-by line by itself should not commit' '
58 test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off &&
59 test_must_fail git commit --template "$TEMPLATE"
63 test_expect_success
'adding comments to a template should not commit' '
65 test_set_editor "$TEST_DIRECTORY"/t7500/add-comments &&
66 test_must_fail git commit --template "$TEMPLATE"
70 test_expect_success
'adding real content to a template should commit' '
72 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
73 git commit --template "$TEMPLATE"
75 commit_msg_is "template linecommit message"
78 test_expect_success
'-t option should be short for --template' '
79 echo "short template" > "$TEMPLATE" &&
80 echo "new content" >> foo &&
83 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
84 git commit -t "$TEMPLATE"
86 commit_msg_is "short templatecommit message"
89 test_expect_success
'config-specified template should commit' '
90 echo "new template" > "$TEMPLATE" &&
91 git config commit.template "$TEMPLATE" &&
92 echo "more content" >> foo &&
95 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
98 git config --unset commit.template &&
99 commit_msg_is "new templatecommit message"
102 test_expect_success
'explicit commit message should override template' '
103 echo "still more content" >> foo &&
105 GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \
106 -m "command line msg" &&
107 commit_msg_is "command line msg"
110 test_expect_success
'commit message from file should override template' '
111 echo "content galore" >> foo &&
113 echo "standard input msg" |
115 test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
116 git commit --template "$TEMPLATE" --file -
118 commit_msg_is "standard input msg"
121 test_expect_success
'using alternate GIT_INDEX_FILE (1)' '
123 cp .git/index saved-index &&
125 echo some new content >file &&
126 GIT_INDEX_FILE=.git/another_index &&
127 export GIT_INDEX_FILE &&
129 git commit -m "commit using another index" &&
130 git diff-index --exit-code HEAD &&
131 git diff-files --exit-code
133 cmp .git/index saved-index >/dev/null
137 test_expect_success
'using alternate GIT_INDEX_FILE (2)' '
139 cp .git/index saved-index &&
141 rm -f .git/no-such-index &&
142 GIT_INDEX_FILE=.git/no-such-index &&
143 export GIT_INDEX_FILE &&
144 git commit -m "commit using nonexistent index" &&
145 test -z "$(git ls-files)" &&
146 test -z "$(git ls-tree HEAD)"
149 cmp .git/index saved-index >/dev/null
155 Signed-off-by: C O Mitter <committer@example.com>
158 test_expect_success
'--signoff' '
159 echo "yet another content *narf*" >> foo &&
160 echo "zort" | git commit -s -F - foo &&
161 git cat-file commit HEAD | sed "1,/^\$/d" > output &&
162 test_cmp expect output
165 test_expect_success
'commit message from file (1)' '
167 echo "Log in top directory" >log &&
168 echo "Log in sub directory" >subdir/log &&
171 git commit --allow-empty -F log
173 commit_msg_is "Log in sub directory"
176 test_expect_success
'commit message from file (2)' '
178 echo "Log in sub directory" >subdir/log &&
181 git commit --allow-empty -F log
183 commit_msg_is "Log in sub directory"
186 test_expect_success
'commit message from stdin' '
189 echo "Log with foo word" | git commit --allow-empty -F -
191 commit_msg_is "Log with foo word"
194 test_expect_success
'commit -F overrides -t' '
197 echo "-F log" > f.log &&
198 echo "-t template" > t.template &&
199 git commit --allow-empty -F f.log -t t.template
201 commit_msg_is "-F log"
204 test_expect_success
'Commit without message is allowed with --allow-empty-message' '
205 echo "more content" >>foo &&
208 git commit --allow-empty-message <empty &&
212 test_expect_success
'Commit without message is no-no without --allow-empty-message' '
213 echo "more content" >>foo &&
216 test_must_fail git commit <empty
219 test_expect_success
'Commit a message with --allow-empty-message' '
220 echo "even more content" >>foo &&
222 git commit --allow-empty-message -m"hello there" &&
223 commit_msg_is "hello there"