Skip to content

Commit 2520677

Browse files
René Scharfegitster
authored andcommitted
commit: don't start editor if empty message is given with -m
If an empty message is specified with the option -m of git commit then the editor is started. That's unexpected and unnecessary. Instead of using the length of the message string for checking if the user specified one, directly remember if the option -m was given. Reported-by: Mislav Marohnić <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 92758dd commit 2520677

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

builtin/commit.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static const char *cleanup_arg;
107107

108108
static enum commit_whence whence;
109109
static int use_editor = 1, include_status = 1;
110-
static int show_ignored_in_status;
110+
static int show_ignored_in_status, have_option_m;
111111
static const char *only_include_assumed;
112112
static struct strbuf message = STRBUF_INIT;
113113

@@ -121,9 +121,11 @@ static enum {
121121
static int opt_parse_m(const struct option *opt, const char *arg, int unset)
122122
{
123123
struct strbuf *buf = opt->value;
124-
if (unset)
124+
if (unset) {
125+
have_option_m = 0;
125126
strbuf_setlen(buf, 0);
126-
else {
127+
} else {
128+
have_option_m = 1;
127129
if (buf->len)
128130
strbuf_addch(buf, '\n');
129131
strbuf_addstr(buf, arg);
@@ -975,7 +977,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
975977
if (force_author && renew_authorship)
976978
die(_("Using both --reset-author and --author does not make sense"));
977979

978-
if (logfile || message.len || use_message || fixup_message)
980+
if (logfile || have_option_m || use_message || fixup_message)
979981
use_editor = 0;
980982
if (0 <= edit_flag)
981983
use_editor = edit_flag;

t/t7502-commit.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,23 @@ test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
354354
test_cmp expect .git/result
355355
'
356356

357+
test_expect_success 'do not fire editor if -m <msg> was given' '
358+
echo tick >file &&
359+
git add file &&
360+
echo "editor not started" >.git/result &&
361+
(GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
362+
test "$(cat .git/result)" = "editor not started"
363+
'
364+
365+
test_expect_success 'do not fire editor if -m "" was given' '
366+
echo tock >file &&
367+
git add file &&
368+
echo "editor not started" >.git/result &&
369+
(GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
370+
git commit -m "" --allow-empty-message) &&
371+
test "$(cat .git/result)" = "editor not started"
372+
'
373+
357374
test_expect_success 'do not fire editor in the presence of conflicts' '
358375
359376
git clean -f &&

0 commit comments

Comments
 (0)