commit: make default of "cleanup" option configurable

The default of the "cleanup" option in "git commit" is not configurable. Users who don't want to use the default have to pass this option on every commit since there's no way to configure it. This commit introduces a new config option "commit.cleanup" which can be used to change the default of the "cleanup" option in "git commit". Signed-off-by: Ralf Thielow <ralf.thielow@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ralf Thielow committed Jan 10, 2013 at 18:45 UTC 51fb3a3dfa4ffeeb210fb0e7d6f631503a5a1dfb
5 files changed +97 -9
Documentation/config.txt
+9
@@ -923,6 +923,15 @@ column.tag::
923 Specify whether to output tag listing in `git tag` in columns.
924 See `column.ui` for details.
925
926 +commit.cleanup::
927 + This setting overrides the default of the `--cleanup` option in
928 + `git commit`. See linkgit:git-commit[1] for details. Changing the
929 + default can be useful when you always want to keep lines that begin
930 + with comment character `#` in your log message, in which case you
931 + would do `git config commit.cleanup whitespace` (note that you will
932 + have to remove the help lines that begin with `#` in the commit log
933 + template yourself, if you do this).
934 +
935 commit.status::
936 A boolean to enable/disable inclusion of status information in the
937 commit message template when using an editor to prepare the commit
Documentation/git-commit.txt
+3 -1
@@ -179,7 +179,9 @@ OPTIONS
179 only if the message is to be edited. Otherwise only whitespace
180 removed. The 'verbatim' mode does not change message at all,
181 'whitespace' removes just leading/trailing whitespace lines
182 - and 'strip' removes both whitespace and commentary.
182 + and 'strip' removes both whitespace and commentary. The default
183 + can be changed by the 'commit.cleanup' configuration variable
184 + (see linkgit:git-config[1]).
185
186 -e::
187 --edit::
builtin/commit.c
+3 -1
@@ -103,7 +103,7 @@ static enum {
103 CLEANUP_NONE,
104 CLEANUP_ALL
105 } cleanup_mode;
106 -static char *cleanup_arg;
106 +static const char *cleanup_arg;
107
108 static enum commit_whence whence;
109 static int use_editor = 1, include_status = 1;
@@ -1320,6 +1320,8 @@ static int git_commit_config(const char *k, const char *v, void *cb)
1320 include_status = git_config_bool(k, v);
1321 return 0;
1322 }
1323 + if (!strcmp(k, "commit.cleanup"))
1324 + return git_config_string(&cleanup_arg, k, v);
1325
1326 status = git_gpg_config(k, v, NULL);
1327 if (status)
t/t7500/add-content-and-comment new
+5
@@ -0,0 +1,5 @@
1 +#!/bin/sh
2 +echo "commit message" >> "$1"
3 +echo "# comment" >> "$1"
4 +exit 0
5 +
t/t7502-commit.sh
+77 -7
@@ -4,6 +4,15 @@ test_description='git commit porcelain-ish'
4
5 . ./test-lib.sh
6
7 +commit_msg_is () {
8 + expect=commit_msg_is.expect
9 + actual=commit_msg_is.actual
10 +
11 + printf "%s" "$(git log --pretty=format:%s%b -1)" >$actual &&
12 + printf "%s" "$1" >$expect &&
13 + test_i18ncmp $expect $actual
14 +}
15 +
16 # Arguments: [<prefix] [<commit message>] [<commit options>]
17 check_summary_oneline() {
18 test_tick &&
@@ -168,7 +177,7 @@ test_expect_success 'verbose respects diff config' '
177 git config --unset color.diff
178 '
179
171 -test_expect_success 'cleanup commit messages (verbatim,-t)' '
180 +test_expect_success 'cleanup commit messages (verbatim option,-t)' '
181
182 echo >>negative &&
183 { echo;echo "# text";echo; } >expect &&
@@ -178,7 +187,7 @@ test_expect_success 'cleanup commit messages (verbatim,-t)' '
187
188 '
189
181 -test_expect_success 'cleanup commit messages (verbatim,-F)' '
190 +test_expect_success 'cleanup commit messages (verbatim option,-F)' '
191
192 echo >>negative &&
193 git commit --cleanup=verbatim -F expect -a &&
@@ -187,7 +196,7 @@ test_expect_success 'cleanup commit messages (verbatim,-F)' '
196
197 '
198
190 -test_expect_success 'cleanup commit messages (verbatim,-m)' '
199 +test_expect_success 'cleanup commit messages (verbatim option,-m)' '
200
201 echo >>negative &&
202 git commit --cleanup=verbatim -m "$(cat expect)" -a &&
@@ -196,7 +205,7 @@ test_expect_success 'cleanup commit messages (verbatim,-m)' '
205
206 '
207
199 -test_expect_success 'cleanup commit messages (whitespace,-F)' '
208 +test_expect_success 'cleanup commit messages (whitespace option,-F)' '
209
210 echo >>negative &&
211 { echo;echo "# text";echo; } >text &&
@@ -207,7 +216,7 @@ test_expect_success 'cleanup commit messages (whitespace,-F)' '
216
217 '
218
210 -test_expect_success 'cleanup commit messages (strip,-F)' '
219 +test_expect_success 'cleanup commit messages (strip option,-F)' '
220
221 echo >>negative &&
222 { echo;echo "# text";echo sample;echo; } >text &&
@@ -218,7 +227,7 @@ test_expect_success 'cleanup commit messages (strip,-F)' '
227
228 '
229
221 -test_expect_success 'cleanup commit messages (strip,-F,-e)' '
230 +test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
231
232 echo >>negative &&
233 { echo;echo sample;echo; } >text &&
@@ -231,10 +240,71 @@ echo "sample
240 # Please enter the commit message for your changes. Lines starting
241 # with '#' will be ignored, and an empty message aborts the commit." >expect
242
234 -test_expect_success 'cleanup commit messages (strip,-F,-e): output' '
243 +test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
244 test_i18ncmp expect actual
245 '
246
247 +test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
248 + test_must_fail git commit --cleanup=non-existent
249 +'
250 +
251 +test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
252 + test_must_fail git -c commit.cleanup=non-existent commit
253 +'
254 +
255 +test_expect_success 'cleanup commit message (no config and no option uses default)' '
256 + echo content >>file &&
257 + git add file &&
258 + test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
259 + git commit --no-status &&
260 + commit_msg_is "commit message"
261 +'
262 +
263 +test_expect_success 'cleanup commit message (option overrides default)' '
264 + echo content >>file &&
265 + git add file &&
266 + test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
267 + git commit --cleanup=whitespace --no-status &&
268 + commit_msg_is "commit message # comment"
269 +'
270 +
271 +test_expect_success 'cleanup commit message (config overrides default)' '
272 + echo content >>file &&
273 + git add file &&
274 + test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
275 + git -c commit.cleanup=whitespace commit --no-status &&
276 + commit_msg_is "commit message # comment"
277 +'
278 +
279 +test_expect_success 'cleanup commit message (option overrides config)' '
280 + echo content >>file &&
281 + git add file &&
282 + test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
283 + git -c commit.cleanup=whitespace commit --cleanup=default &&
284 + commit_msg_is "commit message"
285 +'
286 +
287 +test_expect_success 'cleanup commit message (default, -m)' '
288 + echo content >>file &&
289 + git add file &&
290 + git commit -m "message #comment " &&
291 + commit_msg_is "message #comment"
292 +'
293 +
294 +test_expect_success 'cleanup commit message (whitespace option, -m)' '
295 + echo content >>file &&
296 + git add file &&
297 + git commit --cleanup=whitespace --no-status -m "message #comment " &&
298 + commit_msg_is "message #comment"
299 +'
300 +
301 +test_expect_success 'cleanup commit message (whitespace config, -m)' '
302 + echo content >>file &&
303 + git add file &&
304 + git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
305 + commit_msg_is "message #comment"
306 +'
307 +
308 test_expect_success 'message shows author when it is not equal to committer' '
309 echo >>negative &&
310 git commit -e -m "sample" -a &&