Skip to content

Commit c9b5fde

Browse files
avargitster
authored andcommitted
Add option to git-commit to allow empty log messages
Change git-commit(1) to accept the --allow-empty-message option to allow a commit with an empty message. This is analogous to the existing --allow-empty option which allows a commit that records no changes. As these are mainly for interoperating with foreign SCM systems, and are not meant for normal use, ensure that "git commit -h" does not talk about them. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 537f6c7 commit c9b5fde

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

Documentation/git-commit.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
[verse]
1111
'git commit' [-a | --interactive] [-s] [-v] [-u<mode>] [--amend] [--dry-run]
1212
[(-c | -C) <commit>] [-F <file> | -m <msg>] [--reset-author]
13-
[--allow-empty] [--no-verify] [-e] [--author=<author>]
13+
[--allow-empty] [--allow-empty-message] [--no-verify] [-e] [--author=<author>]
1414
[--date=<date>] [--cleanup=<mode>] [--status | --no-status] [--]
1515
[[-i | -o ]<file>...]
1616

@@ -131,6 +131,12 @@ OPTIONS
131131
from making such a commit. This option bypasses the safety, and
132132
is primarily for use by foreign scm interface scripts.
133133

134+
--allow-empty-message::
135+
Like --allow-empty this command is primarily for use by foreign
136+
scm interface scripts. It allows you to create a commit with an
137+
empty commit message without using plumbing commands like
138+
linkgit:git-commit-tree[1].
139+
134140
--cleanup=<mode>::
135141
This option sets how the commit message is cleaned up.
136142
The '<mode>' can be one of 'verbatim', 'whitespace', 'strip',

builtin/commit.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static char *edit_message, *use_message;
6666
static char *author_name, *author_email, *author_date;
6767
static int all, edit_flag, also, interactive, only, amend, signoff;
6868
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
69-
static int no_post_rewrite;
69+
static int no_post_rewrite, allow_empty_message;
7070
static char *untracked_files_arg, *force_date;
7171
/*
7272
* The default commit message cleanup mode will remove the lines
@@ -140,9 +140,15 @@ static struct option builtin_commit_options[] = {
140140
OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
141141
OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"),
142142
{ OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
143-
OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
144143
/* end commit contents options */
145144

145+
{ OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
146+
"ok to record an empty change",
147+
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
148+
{ OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
149+
"ok to record a change with an empty message",
150+
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
151+
146152
OPT_END()
147153
};
148154

@@ -1293,7 +1299,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
12931299

12941300
if (cleanup_mode != CLEANUP_NONE)
12951301
stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1296-
if (message_is_empty(&sb)) {
1302+
if (message_is_empty(&sb) && !allow_empty_message) {
12971303
rollback_index_files();
12981304
fprintf(stderr, "Aborting commit due to empty commit message.\n");
12991305
exit(1);

t/t7500-commit.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,26 @@ test_expect_success 'commit -F overrides -t' '
193193
commit_msg_is "-F log"
194194
'
195195

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 ""
202+
'
203+
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
209+
'
210+
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"
216+
'
217+
196218
test_done

0 commit comments

Comments
 (0)