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

Commit 86b4c16

Browse files
committed
Merge branch 'bp/commit-p-editor' into maint
* bp/commit-p-editor: run-command: mark run_hook_with_custom_index as deprecated merge hook tests: fix and update tests merge: fix GIT_EDITOR override for commit hook commit: fix patch hunk editing with "commit -p -m" test patch hunk editing with "commit -p -m" merge hook tests: use 'test_must_fail' instead of '!' merge hook tests: fix missing '&&' in test
2 parents 2f91649 + b549be0 commit 86b4c16

File tree

10 files changed

+137
-39
lines changed

10 files changed

+137
-39
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(0 < option_edit, 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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ 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+
__attribute__((deprecated))
55+
extern int run_hook_with_custom_index(const char *index_file, const char *name, ...);
5156

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

t/t7505-prepare-commit-msg-hook.sh

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,26 @@ test_expect_success 'with hook (-c)' '
134134

135135
test_expect_success 'with hook (merge)' '
136136
137-
head=`git rev-parse HEAD` &&
138-
git checkout -b other HEAD@{1} &&
139-
echo "more" >> file &&
137+
test_when_finished "git checkout -f master" &&
138+
git checkout -B other HEAD@{1} &&
139+
echo "more" >>file &&
140+
git add file &&
141+
git commit -m other &&
142+
git checkout - &&
143+
git merge --no-ff other &&
144+
test "`git log -1 --pretty=format:%s`" = "merge (no editor)"
145+
'
146+
147+
test_expect_success 'with hook and editor (merge)' '
148+
149+
test_when_finished "git checkout -f master" &&
150+
git checkout -B other HEAD@{1} &&
151+
echo "more" >>file &&
140152
git add file &&
141153
git commit -m other &&
142154
git checkout - &&
143-
git merge other &&
144-
test "`git log -1 --pretty=format:%s`" = merge
155+
env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
156+
test "`git log -1 --pretty=format:%s`" = "merge"
145157
'
146158

147159
cat > "$HOOK" <<'EOF'
@@ -151,34 +163,37 @@ EOF
151163

152164
test_expect_success 'with failing hook' '
153165
166+
test_when_finished "git checkout -f master" &&
154167
head=`git rev-parse HEAD` &&
155168
echo "more" >> file &&
156169
git add file &&
157-
! GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
170+
test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
158171
159172
'
160173

161174
test_expect_success 'with failing hook (--no-verify)' '
162175
176+
test_when_finished "git checkout -f master" &&
163177
head=`git rev-parse HEAD` &&
164178
echo "more" >> file &&
165179
git add file &&
166-
! GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
180+
test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
167181
168182
'
169183

170184
test_expect_success 'with failing hook (merge)' '
171185
186+
test_when_finished "git checkout -f master" &&
172187
git checkout -B other HEAD@{1} &&
173188
echo "more" >> file &&
174189
git add file &&
175190
rm -f "$HOOK" &&
176191
git commit -m other &&
177-
write_script "$HOOK" <<-EOF
192+
write_script "$HOOK" <<-EOF &&
178193
exit 1
179194
EOF
180195
git checkout - &&
181-
test_must_fail git merge other
196+
test_must_fail git merge --no-ff other
182197
183198
'
184199

t/t7514-commit-patch.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
3+
test_description='hunk edit with "commit -p -m"'
4+
. ./test-lib.sh
5+
6+
if ! test_have_prereq PERL
7+
then
8+
skip_all="skipping '$test_description' tests, perl not available"
9+
test_done
10+
fi
11+
12+
test_expect_success 'setup (initial)' '
13+
echo line1 >file &&
14+
git add file &&
15+
git commit -m commit1
16+
'
17+
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 &&
22+
echo e | env GIT_EDITOR=": >editor_was_started" git commit -p -m commit2 file &&
23+
test -r editor_was_started
24+
'
25+
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 &&
30+
echo e | env GIT_EDITOR=": >editor_was_started" git commit -p -m commit3 file &&
31+
test -r editor_was_started
32+
'
33+
34+
test_done

0 commit comments

Comments
 (0)