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ć <mislav.marohnic@gmail.com> Signed-off-by: René Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed May 25, 2013 at 23:43 UTC 25206778aac776fc6cc4887653fdae476c7a9b5a
2 files changed +23 -4
builtin/commit.c
+6 -4
@@ -107,7 +107,7 @@ static const char *cleanup_arg;
107
108 static enum commit_whence whence;
109 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;
111 static const char *only_include_assumed;
112 static struct strbuf message = STRBUF_INIT;
113
@@ -121,9 +121,11 @@ static enum {
121 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
122 {
123 struct strbuf *buf = opt->value;
124 - if (unset)
124 + if (unset) {
125 + have_option_m = 0;
126 strbuf_setlen(buf, 0);
126 - else {
127 + } else {
128 + have_option_m = 1;
129 if (buf->len)
130 strbuf_addch(buf, '\n');
131 strbuf_addstr(buf, arg);
@@ -975,7 +977,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
977 if (force_author && renew_authorship)
978 die(_("Using both --reset-author and --author does not make sense"));
979
978 - if (logfile || message.len || use_message || fixup_message)
980 + if (logfile || have_option_m || use_message || fixup_message)
981 use_editor = 0;
982 if (0 <= edit_flag)
983 use_editor = edit_flag;
t/t7502-commit.sh
+17
@@ -354,6 +354,23 @@ test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
354 test_cmp expect .git/result
355 '
356
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 +
374 test_expect_success 'do not fire editor in the presence of conflicts' '
375
376 git clean -f &&