Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 15048f8

Browse files
benoit-pierregitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 91c9c86 commit 15048f8

File tree

9 files changed

+80
-32
lines changed

9 files changed

+80
-32
lines changed

builtin/checkout.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ struct checkout_opts {
5353
static int post_checkout_hook(struct commit *old, struct commit *new,
5454
int changed)
5555
{
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);
6060
/* "new" can be NULL when checking out from the index before
6161
a commit exists. */
6262

builtin/clone.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,8 @@ static int checkout(void)
660660
commit_locked_index(lock_file))
661661
die(_("unable to write new index file"));
662662

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);
665665

666666
if (!err && option_recursive)
667667
err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);

builtin/commit.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
612612
/* This checks and barfs if author is badly specified */
613613
determine_author_info(author_ident);
614614

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))
616616
return 0;
617617

618618
if (squash_message) {
@@ -866,8 +866,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
866866
return 0;
867867
}
868868

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))
871871
return 0;
872872

873873
if (use_editor) {
@@ -883,7 +883,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
883883
}
884884

885885
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)) {
887887
return 0;
888888
}
889889

@@ -1067,8 +1067,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
10671067
use_editor = 0;
10681068
if (0 <= edit_flag)
10691069
use_editor = edit_flag;
1070-
if (!use_editor)
1071-
setenv("GIT_EDITOR", ":", 1);
10721070

10731071
/* Sanity check options */
10741072
if (amend && !current_head)
@@ -1445,6 +1443,29 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
14451443
return finish_command(&proc);
14461444
}
14471445

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+
14481469
int cmd_commit(int argc, const char **argv, const char *prefix)
14491470
{
14501471
static struct wt_status s;
@@ -1669,7 +1690,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16691690
"not exceeded, and then \"git reset HEAD\" to recover."));
16701691

16711692
rerere(0);
1672-
run_hook(get_index_file(), "post-commit", NULL);
1693+
run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
16731694
if (amend && !no_post_rewrite) {
16741695
struct notes_rewrite_cfg *cfg;
16751696
cfg = init_copy_notes_for_rewrite("amend");

builtin/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static int need_to_gc(void)
179179
else if (!too_many_loose_objects())
180180
return 0;
181181

182-
if (run_hook(NULL, "pre-auto-gc", NULL))
182+
if (run_hook_le(NULL, "pre-auto-gc", NULL))
183183
return 0;
184184
return 1;
185185
}

builtin/merge.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ static void finish(struct commit *head_commit,
421421
}
422422

423423
/* 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);
425425

426426
strbuf_release(&reflog_message);
427427
}
@@ -821,8 +821,8 @@ static void prepare_to_commit(struct commit_list *remoteheads)
821821
if (0 < option_edit)
822822
strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
823823
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))
826826
abort_commit(remoteheads, NULL);
827827
if (0 < option_edit) {
828828
if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))

commit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,7 @@ extern void check_commit_signature(const struct commit* commit, struct signature
304304

305305
int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused);
306306

307+
LAST_ARG_MUST_BE_NULL
308+
extern int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);
309+
307310
#endif /* COMMIT_H */

run-command.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -760,13 +760,11 @@ char *find_hook(const char *name)
760760
return path;
761761
}
762762

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)
764764
{
765765
struct child_process hook;
766766
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;
770768
int ret;
771769

772770
p = find_hook(name);
@@ -775,23 +773,45 @@ int run_hook(const char *index_file, const char *name, ...)
775773

776774
argv_array_push(&argv, p);
777775

778-
va_start(args, name);
779776
while ((p = va_arg(args, const char *)))
780777
argv_array_push(&argv, p);
781-
va_end(args);
782778

783779
memset(&hook, 0, sizeof(hook));
784780
hook.argv = argv.argv;
781+
hook.env = env;
785782
hook.no_stdin = 1;
786783
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-
}
793784

794785
ret = run_command(&hook);
795786
argv_array_clear(&argv);
796787
return ret;
797788
}
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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ int run_command(struct child_process *);
4747

4848
extern char *find_hook(const char *name);
4949
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, ...);
5155

5256
#define RUN_COMMAND_NO_STDIN 1
5357
#define RUN_GIT_CMD 2 /*If this is to be git sub-command */

t/t7514-commit-patch.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ test_expect_success 'setup (initial)' '
1515
git commit -m commit1
1616
'
1717

18-
test_expect_failure 'edit hunk "commit -p -m message"' '
18+
test_expect_success 'edit hunk "commit -p -m message"' '
1919
test_when_finished "rm -f editor_was_started" &&
2020
rm -f editor_was_started &&
2121
echo more >>file &&
2222
echo e | env GIT_EDITOR=": >editor_was_started" git commit -p -m commit2 file &&
2323
test -r editor_was_started
2424
'
2525

26-
test_expect_failure 'edit hunk "commit --dry-run -p -m message"' '
26+
test_expect_success 'edit hunk "commit --dry-run -p -m message"' '
2727
test_when_finished "rm -f editor_was_started" &&
2828
rm -f editor_was_started &&
2929
echo more >>file &&

0 commit comments

Comments
 (0)