Merge branch 'rs/commit-m-no-edit'
authorJunio C Hamano <gitster@pobox.com>
Wed, 5 Jun 2013 21:59:53 +0000 (14:59 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 5 Jun 2013 21:59:53 +0000 (14:59 -0700)
"git commit --allow-empty-message -m ''" should not start an
editor.

* rs/commit-m-no-edit:
commit: don't start editor if empty message is given with -m

1  2 
builtin/commit.c
t/t7502-commit.sh
diff --combined builtin/commit.c
index d2f30d960a71e41da88d6cc4201cbdca168b8648,8e26a422a195196e084dc2bb49a65edff0aae8b4..1621dfcd4008fc5853fc078a32bf9eefb6c52023
@@@ -107,7 -107,7 +107,7 @@@ static const char *cleanup_arg
  
  static enum commit_whence whence;
  static int use_editor = 1, include_status = 1;
- static int show_ignored_in_status;
+ static int show_ignored_in_status, have_option_m;
  static const char *only_include_assumed;
  static struct strbuf message = STRBUF_INIT;
  
@@@ -121,9 -121,11 +121,11 @@@ static enum 
  static int opt_parse_m(const struct option *opt, const char *arg, int unset)
  {
        struct strbuf *buf = opt->value;
-       if (unset)
+       if (unset) {
+               have_option_m = 0;
                strbuf_setlen(buf, 0);
-       else {
+       } else {
+               have_option_m = 1;
                if (buf->len)
                        strbuf_addch(buf, '\n');
                strbuf_addstr(buf, arg);
@@@ -702,7 -704,7 +704,7 @@@ static int prepare_to_commit(const cha
                        previous = eol;
                }
  
 -              append_signoff(&sb, ignore_footer);
 +              append_signoff(&sb, ignore_footer, 0);
        }
  
        if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
@@@ -955,7 -957,7 +957,7 @@@ static const char *read_commit_message(
        if (!commit)
                die(_("could not lookup commit %s"), name);
        out_enc = get_commit_output_encoding();
 -      return logmsg_reencode(commit, out_enc);
 +      return logmsg_reencode(commit, NULL, out_enc);
  }
  
  static int parse_and_validate_options(int argc, const char *argv[],
        if (force_author && renew_authorship)
                die(_("Using both --reset-author and --author does not make sense"));
  
-       if (logfile || message.len || use_message || fixup_message)
+       if (logfile || have_option_m || use_message || fixup_message)
                use_editor = 0;
        if (0 <= edit_flag)
                use_editor = edit_flag;
diff --combined t/t7502-commit.sh
index a4938b1e4549d5082362ec3f5513ffe91f210d39,91354d28bf94dcd66ba126d614338811a599bdbb..6313da2cdd979c6264e7a255711c6e444b768b96
@@@ -171,9 -171,10 +171,9 @@@ test_expect_success 'verbose' 
  
  test_expect_success 'verbose respects diff config' '
  
 -      git config color.diff always &&
 +      test_config color.diff always &&
        git status -v >actual &&
 -      grep "\[1mdiff --git" actual &&
 -      git config --unset color.diff
 +      grep "\[1mdiff --git" actual
  '
  
  mesg_with_comment_and_newlines='
@@@ -262,40 -263,32 +262,40 @@@ test_expect_success 'cleanup commit mes
  test_expect_success 'cleanup commit message (no config and no option uses default)' '
        echo content >>file &&
        git add file &&
 -      test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 -      git commit --no-status &&
 +      (
 +        test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 +        git commit --no-status
 +      ) &&
        commit_msg_is "commit message"
  '
  
  test_expect_success 'cleanup commit message (option overrides default)' '
        echo content >>file &&
        git add file &&
 -      test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 -      git commit --cleanup=whitespace --no-status &&
 +      (
 +        test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 +        git commit --cleanup=whitespace --no-status
 +      ) &&
        commit_msg_is "commit message # comment"
  '
  
  test_expect_success 'cleanup commit message (config overrides default)' '
        echo content >>file &&
        git add file &&
 -      test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 -      git -c commit.cleanup=whitespace commit --no-status &&
 +      (
 +        test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 +        git -c commit.cleanup=whitespace commit --no-status
 +      ) &&
        commit_msg_is "commit message # comment"
  '
  
  test_expect_success 'cleanup commit message (option overrides config)' '
        echo content >>file &&
        git add file &&
 -      test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 -      git -c commit.cleanup=whitespace commit --cleanup=default &&
 +      (
 +        test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
 +        git -c commit.cleanup=whitespace commit --cleanup=default
 +      ) &&
        commit_msg_is "commit message"
  '
  
@@@ -361,6 -354,23 +361,23 @@@ test_expect_success !AUTOIDENT 'do not 
        test_cmp expect .git/result
  '
  
+ test_expect_success 'do not fire editor if -m <msg> was given' '
+       echo tick >file &&
+       git add file &&
+       echo "editor not started" >.git/result &&
+       (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
+       test "$(cat .git/result)" = "editor not started"
+ '
+ test_expect_success 'do not fire editor if -m "" was given' '
+       echo tock >file &&
+       git add file &&
+       echo "editor not started" >.git/result &&
+       (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
+        git commit -m "" --allow-empty-message) &&
+       test "$(cat .git/result)" = "editor not started"
+ '
  test_expect_success 'do not fire editor in the presence of conflicts' '
  
        git clean -f &&
@@@ -424,18 -434,6 +441,18 @@@ test_expect_success 'A single-liner sub
  
  '
  
 +test_expect_success 'commit -s places sob on third line after two empty lines' '
 +      git commit -s --allow-empty --allow-empty-message &&
 +      cat <<-EOF >expect &&
 +
 +
 +      Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
 +
 +      EOF
 +      sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
 +      test_cmp expect actual
 +'
 +
  write_script .git/FAKE_EDITOR <<\EOF
  mv "$1" "$1.orig"
  (
@@@ -446,6 -444,16 +463,6 @@@ EO
  
  echo '## Custom template' >template
  
 -clear_config () {
 -      (
 -              git config --unset-all "$1"
 -              case $? in
 -              0|5)    exit 0 ;;
 -              *)      exit 1 ;;
 -              esac
 -      )
 -}
 -
  try_commit () {
        git reset --hard &&
        echo >>negative &&
  try_commit_status_combo () {
  
        test_expect_success 'commit' '
 -              clear_config commit.status &&
                try_commit "" &&
                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
        '
  
        test_expect_success 'commit' '
 -              clear_config commit.status &&
                try_commit "" &&
                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
        '
  
        test_expect_success 'commit --status' '
 -              clear_config commit.status &&
                try_commit --status &&
                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
        '
  
        test_expect_success 'commit --no-status' '
 -              clear_config commit.status &&
                try_commit --no-status &&
                test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
        '
  
        test_expect_success 'commit with commit.status = yes' '
 -              clear_config commit.status &&
 -              git config commit.status yes &&
 +              test_config commit.status yes &&
                try_commit "" &&
                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
        '
  
        test_expect_success 'commit with commit.status = no' '
 -              clear_config commit.status &&
 -              git config commit.status no &&
 +              test_config commit.status no &&
                try_commit "" &&
                test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
        '
  
        test_expect_success 'commit --status with commit.status = yes' '
 -              clear_config commit.status &&
 -              git config commit.status yes &&
 +              test_config commit.status yes &&
                try_commit --status &&
                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
        '
  
        test_expect_success 'commit --no-status with commit.status = yes' '
 -              clear_config commit.status &&
 -              git config commit.status yes &&
 +              test_config commit.status yes &&
                try_commit --no-status &&
                test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
        '
  
        test_expect_success 'commit --status with commit.status = no' '
 -              clear_config commit.status &&
 -              git config commit.status no &&
 +              test_config commit.status no &&
                try_commit --status &&
                test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
        '
  
        test_expect_success 'commit --no-status with commit.status = no' '
 -              clear_config commit.status &&
 -              git config commit.status no &&
 +              test_config commit.status no &&
                try_commit --no-status &&
                test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
        '
@@@ -525,7 -543,8 +542,7 @@@ use_template="-t template
  try_commit_status_combo
  
  test_expect_success 'commit --status with custom comment character' '
 -      test_when_finished "git config --unset core.commentchar" &&
 -      git config core.commentchar ";" &&
 +      test_config core.commentchar ";" &&
        try_commit --status &&
        test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
  '