commit: fix patch hunk editing with "commit -p -m"

Don't change git environment: move the GIT_EDITOR=":" override to the hook command subprocess, like it's already done for GIT_INDEX_FILE. Signed-off-by: Benoit Pierre <benoit.pierre@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Benoit Pierre committed Mar 18, 2014 at 11:00 UTC 15048f8a9ace23df67161746ca76b4f46114deee
9 files changed +80 -32
builtin/checkout.c
+4 -4
@@ -53,10 +53,10 @@ struct checkout_opts {
53 static int post_checkout_hook(struct commit *old, struct commit *new,
54 int changed)
55 {
56 - return run_hook(NULL, "post-checkout",
57 - sha1_to_hex(old ? old->object.sha1 : null_sha1),
58 - sha1_to_hex(new ? new->object.sha1 : null_sha1),
59 - changed ? "1" : "0", NULL);
56 + return run_hook_le(NULL, "post-checkout",
57 + sha1_to_hex(old ? old->object.sha1 : null_sha1),
58 + sha1_to_hex(new ? new->object.sha1 : null_sha1),
59 + changed ? "1" : "0", NULL);
60 /* "new" can be NULL when checking out from the index before
61 a commit exists. */
62
builtin/clone.c
+2 -2
@@ -660,8 +660,8 @@ static int checkout(void)
660 commit_locked_index(lock_file))
661 die(_("unable to write new index file"));
662
663 - err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
664 - sha1_to_hex(sha1), "1", NULL);
663 + err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
664 + sha1_to_hex(sha1), "1", NULL);
665
666 if (!err && option_recursive)
667 err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
builtin/commit.c
+28 -7
@@ -612,7 +612,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
612 /* This checks and barfs if author is badly specified */
613 determine_author_info(author_ident);
614
615 - if (!no_verify && run_hook(index_file, "pre-commit", NULL))
615 + if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
616 return 0;
617
618 if (squash_message) {
@@ -866,8 +866,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
866 return 0;
867 }
868
869 - if (run_hook(index_file, "prepare-commit-msg",
870 - git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
869 + if (run_commit_hook(use_editor, index_file, "prepare-commit-msg",
870 + git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
871 return 0;
872
873 if (use_editor) {
@@ -883,7 +883,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
883 }
884
885 if (!no_verify &&
886 - run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
886 + run_commit_hook(use_editor, index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
887 return 0;
888 }
889
@@ -1067,8 +1067,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
1067 use_editor = 0;
1068 if (0 <= edit_flag)
1069 use_editor = edit_flag;
1070 - if (!use_editor)
1071 - setenv("GIT_EDITOR", ":", 1);
1070
1071 /* Sanity check options */
1072 if (amend && !current_head)
@@ -1445,6 +1443,29 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
1443 return finish_command(&proc);
1444 }
1445
1446 +int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
1447 +{
1448 + const char *hook_env[3] = { NULL };
1449 + char index[PATH_MAX];
1450 + va_list args;
1451 + int ret;
1452 +
1453 + snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
1454 + hook_env[0] = index;
1455 +
1456 + /*
1457 + * Let the hook know that no editor will be launched.
1458 + */
1459 + if (!editor_is_used)
1460 + hook_env[1] = "GIT_EDITOR=:";
1461 +
1462 + va_start(args, name);
1463 + ret = run_hook_ve(hook_env, name, args);
1464 + va_end(args);
1465 +
1466 + return ret;
1467 +}
1468 +
1469 int cmd_commit(int argc, const char **argv, const char *prefix)
1470 {
1471 static struct wt_status s;
@@ -1669,7 +1690,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1690 "not exceeded, and then \"git reset HEAD\" to recover."));
1691
1692 rerere(0);
1672 - run_hook(get_index_file(), "post-commit", NULL);
1693 + run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
1694 if (amend && !no_post_rewrite) {
1695 struct notes_rewrite_cfg *cfg;
1696 cfg = init_copy_notes_for_rewrite("amend");
builtin/gc.c
+1 -1
@@ -179,7 +179,7 @@ static int need_to_gc(void)
179 else if (!too_many_loose_objects())
180 return 0;
181
182 - if (run_hook(NULL, "pre-auto-gc", NULL))
182 + if (run_hook_le(NULL, "pre-auto-gc", NULL))
183 return 0;
184 return 1;
185 }
builtin/merge.c
+3 -3
@@ -421,7 +421,7 @@ static void finish(struct commit *head_commit,
421 }
422
423 /* Run a post-merge hook */
424 - run_hook(NULL, "post-merge", squash ? "1" : "0", NULL);
424 + run_hook_le(NULL, "post-merge", squash ? "1" : "0", NULL);
425
426 strbuf_release(&reflog_message);
427 }
@@ -821,8 +821,8 @@ static void prepare_to_commit(struct commit_list *remoteheads)
821 if (0 < option_edit)
822 strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
823 write_merge_msg(&msg);
824 - if (run_hook(get_index_file(), "prepare-commit-msg",
825 - git_path("MERGE_MSG"), "merge", NULL, NULL))
824 + if (run_commit_hook(1, get_index_file(), "prepare-commit-msg",
825 + git_path("MERGE_MSG"), "merge", NULL))
826 abort_commit(remoteheads, NULL);
827 if (0 < option_edit) {
828 if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))
commit.h
+3
@@ -304,4 +304,7 @@ extern void check_commit_signature(const struct commit* commit, struct signature
304
305 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused);
306
307 +LAST_ARG_MUST_BE_NULL
308 +extern int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);
309 +
310 #endif /* COMMIT_H */
run-command.c
+32 -12
@@ -760,13 +760,11 @@ char *find_hook(const char *name)
760 return path;
761 }
762
763 -int run_hook(const char *index_file, const char *name, ...)
763 +int run_hook_ve(const char *const *env, const char *name, va_list args)
764 {
765 struct child_process hook;
766 struct argv_array argv = ARGV_ARRAY_INIT;
767 - const char *p, *env[2];
768 - char index[PATH_MAX];
769 - va_list args;
767 + const char *p;
768 int ret;
769
770 p = find_hook(name);
@@ -775,23 +773,45 @@ int run_hook(const char *index_file, const char *name, ...)
773
774 argv_array_push(&argv, p);
775
778 - va_start(args, name);
776 while ((p = va_arg(args, const char *)))
777 argv_array_push(&argv, p);
781 - va_end(args);
778
779 memset(&hook, 0, sizeof(hook));
780 hook.argv = argv.argv;
781 + hook.env = env;
782 hook.no_stdin = 1;
783 hook.stdout_to_stderr = 1;
787 - if (index_file) {
788 - snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
789 - env[0] = index;
790 - env[1] = NULL;
791 - hook.env = env;
792 - }
784
785 ret = run_command(&hook);
786 argv_array_clear(&argv);
787 return ret;
788 }
789 +
790 +int run_hook_le(const char *const *env, const char *name, ...)
791 +{
792 + va_list args;
793 + int ret;
794 +
795 + va_start(args, name);
796 + ret = run_hook_ve(env, name, args);
797 + va_end(args);
798 +
799 + return ret;
800 +}
801 +
802 +int run_hook_with_custom_index(const char *index_file, const char *name, ...)
803 +{
804 + const char *hook_env[3] = { NULL };
805 + char index[PATH_MAX];
806 + va_list args;
807 + int ret;
808 +
809 + snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
810 + hook_env[0] = index;
811 +
812 + va_start(args, name);
813 + ret = run_hook_ve(hook_env, name, args);
814 + va_end(args);
815 +
816 + return ret;
817 +}
run-command.h
+5 -1
@@ -47,7 +47,11 @@ int run_command(struct child_process *);
47
48 extern char *find_hook(const char *name);
49 LAST_ARG_MUST_BE_NULL
50 -extern int run_hook(const char *index_file, const char *name, ...);
50 +extern int run_hook_le(const char *const *env, const char *name, ...);
51 +extern int run_hook_ve(const char *const *env, const char *name, va_list args);
52 +
53 +LAST_ARG_MUST_BE_NULL
54 +extern int run_hook_with_custom_index(const char *index_file, const char *name, ...);
55
56 #define RUN_COMMAND_NO_STDIN 1
57 #define RUN_GIT_CMD 2 /*If this is to be git sub-command */
t/t7514-commit-patch.sh
+2 -2
@@ -15,7 +15,7 @@ test_expect_success 'setup (initial)' '
15 git commit -m commit1
16 '
17
18 -test_expect_failure 'edit hunk "commit -p -m message"' '
18 +test_expect_success 'edit hunk "commit -p -m message"' '
19 test_when_finished "rm -f editor_was_started" &&
20 rm -f editor_was_started &&
21 echo more >>file &&
@@ -23,7 +23,7 @@ test_expect_failure 'edit hunk "commit -p -m message"' '
23 test -r editor_was_started
24 '
25
26 -test_expect_failure 'edit hunk "commit --dry-run -p -m message"' '
26 +test_expect_success 'edit hunk "commit --dry-run -p -m message"' '
27 test_when_finished "rm -f editor_was_started" &&
28 rm -f editor_was_started &&
29 echo more >>file &&