Skip to content

Commit 51fb3a3

Browse files
ralfthgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 44fe835 commit 51fb3a3

File tree

5 files changed

+97
-9
lines changed

5 files changed

+97
-9
lines changed

Documentation/config.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,15 @@ column.tag::
923923
Specify whether to output tag listing in `git tag` in columns.
924924
See `column.ui` for details.
925925

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+
926935
commit.status::
927936
A boolean to enable/disable inclusion of status information in the
928937
commit message template when using an editor to prepare the commit

Documentation/git-commit.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ OPTIONS
179179
only if the message is to be edited. Otherwise only whitespace
180180
removed. The 'verbatim' mode does not change message at all,
181181
'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]).
183185

184186
-e::
185187
--edit::

builtin/commit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static enum {
103103
CLEANUP_NONE,
104104
CLEANUP_ALL
105105
} cleanup_mode;
106-
static char *cleanup_arg;
106+
static const char *cleanup_arg;
107107

108108
static enum commit_whence whence;
109109
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)
13201320
include_status = git_config_bool(k, v);
13211321
return 0;
13221322
}
1323+
if (!strcmp(k, "commit.cleanup"))
1324+
return git_config_string(&cleanup_arg, k, v);
13231325

13241326
status = git_gpg_config(k, v, NULL);
13251327
if (status)

t/t7500/add-content-and-comment

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
echo "commit message" >> "$1"
3+
echo "# comment" >> "$1"
4+
exit 0
5+

t/t7502-commit.sh

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ test_description='git commit porcelain-ish'
44

55
. ./test-lib.sh
66

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+
716
# Arguments: [<prefix] [<commit message>] [<commit options>]
817
check_summary_oneline() {
918
test_tick &&
@@ -168,7 +177,7 @@ test_expect_success 'verbose respects diff config' '
168177
git config --unset color.diff
169178
'
170179

171-
test_expect_success 'cleanup commit messages (verbatim,-t)' '
180+
test_expect_success 'cleanup commit messages (verbatim option,-t)' '
172181
173182
echo >>negative &&
174183
{ echo;echo "# text";echo; } >expect &&
@@ -178,7 +187,7 @@ test_expect_success 'cleanup commit messages (verbatim,-t)' '
178187
179188
'
180189

181-
test_expect_success 'cleanup commit messages (verbatim,-F)' '
190+
test_expect_success 'cleanup commit messages (verbatim option,-F)' '
182191
183192
echo >>negative &&
184193
git commit --cleanup=verbatim -F expect -a &&
@@ -187,7 +196,7 @@ test_expect_success 'cleanup commit messages (verbatim,-F)' '
187196
188197
'
189198

190-
test_expect_success 'cleanup commit messages (verbatim,-m)' '
199+
test_expect_success 'cleanup commit messages (verbatim option,-m)' '
191200
192201
echo >>negative &&
193202
git commit --cleanup=verbatim -m "$(cat expect)" -a &&
@@ -196,7 +205,7 @@ test_expect_success 'cleanup commit messages (verbatim,-m)' '
196205
197206
'
198207

199-
test_expect_success 'cleanup commit messages (whitespace,-F)' '
208+
test_expect_success 'cleanup commit messages (whitespace option,-F)' '
200209
201210
echo >>negative &&
202211
{ echo;echo "# text";echo; } >text &&
@@ -207,7 +216,7 @@ test_expect_success 'cleanup commit messages (whitespace,-F)' '
207216
208217
'
209218

210-
test_expect_success 'cleanup commit messages (strip,-F)' '
219+
test_expect_success 'cleanup commit messages (strip option,-F)' '
211220
212221
echo >>negative &&
213222
{ echo;echo "# text";echo sample;echo; } >text &&
@@ -218,7 +227,7 @@ test_expect_success 'cleanup commit messages (strip,-F)' '
218227
219228
'
220229

221-
test_expect_success 'cleanup commit messages (strip,-F,-e)' '
230+
test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
222231
223232
echo >>negative &&
224233
{ echo;echo sample;echo; } >text &&
@@ -231,10 +240,71 @@ echo "sample
231240
# Please enter the commit message for your changes. Lines starting
232241
# with '#' will be ignored, and an empty message aborts the commit." >expect
233242

234-
test_expect_success 'cleanup commit messages (strip,-F,-e): output' '
243+
test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
235244
test_i18ncmp expect actual
236245
'
237246

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+
238308
test_expect_success 'message shows author when it is not equal to committer' '
239309
echo >>negative &&
240310
git commit -e -m "sample" -a &&

0 commit comments

Comments
 (0)