Skip to content

Commit 66618a5

Browse files
phillipwoodgitster
authored andcommitted
sequencer: run 'prepare-commit-msg' hook
Commit 356ee46 ("sequencer: try to commit without forking 'git commit'", 2017-11-24) forgot to run the 'prepare-commit-msg' hook when creating the commit. Fix this by writing the commit message to a different file and running the hook. Using a different file means that if the commit is cancelled the original message file is unchanged. Also move the checks for an empty commit so the order matches 'git commit'. Reported-by: Dmitry Torokhov <[email protected]> Helped-by: Ramsay Jones <[email protected]> Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15cd6d3 commit 66618a5

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

builtin/commit.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ N_("If you wish to skip this commit, use:\n"
6666
"Then \"git cherry-pick --continue\" will resume cherry-picking\n"
6767
"the remaining commits.\n");
6868

69-
static GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG")
70-
7169
static const char *use_message_buffer;
7270
static struct lock_file index_lock; /* real index */
7371
static struct lock_file false_lock; /* used only for partial commits */

sequencer.c

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
const char sign_off_header[] = "Signed-off-by: ";
3030
static const char cherry_picked_prefix[] = "(cherry picked from commit ";
3131

32+
GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG")
33+
3234
GIT_PATH_FUNC(git_path_seq_dir, "sequencer")
3335

3436
static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
@@ -888,6 +890,31 @@ void commit_post_rewrite(const struct commit *old_head,
888890
run_rewrite_hook(&old_head->object.oid, new_head);
889891
}
890892

893+
static int run_prepare_commit_msg_hook(struct strbuf *msg, const char *commit)
894+
{
895+
struct argv_array hook_env = ARGV_ARRAY_INIT;
896+
int ret;
897+
const char *name;
898+
899+
name = git_path_commit_editmsg();
900+
if (write_message(msg->buf, msg->len, name, 0))
901+
return -1;
902+
903+
argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", get_index_file());
904+
argv_array_push(&hook_env, "GIT_EDITOR=:");
905+
if (commit)
906+
ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name,
907+
"commit", commit, NULL);
908+
else
909+
ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name,
910+
"message", NULL);
911+
if (ret)
912+
ret = error(_("'prepare-commit-msg' hook failed"));
913+
argv_array_clear(&hook_env);
914+
915+
return ret;
916+
}
917+
891918
static const char implicit_ident_advice_noconfig[] =
892919
N_("Your name and email address were configured automatically based\n"
893920
"on your username and hostname. Please check that they are accurate.\n"
@@ -1048,8 +1075,9 @@ static int try_to_commit(struct strbuf *msg, const char *author,
10481075
struct commit_list *parents = NULL;
10491076
struct commit_extra_header *extra = NULL;
10501077
struct strbuf err = STRBUF_INIT;
1051-
struct strbuf amend_msg = STRBUF_INIT;
1078+
struct strbuf commit_msg = STRBUF_INIT;
10521079
char *amend_author = NULL;
1080+
const char *hook_commit = NULL;
10531081
enum commit_msg_cleanup_mode cleanup;
10541082
int res = 0;
10551083

@@ -1066,8 +1094,9 @@ static int try_to_commit(struct strbuf *msg, const char *author,
10661094
const char *orig_message = NULL;
10671095

10681096
find_commit_subject(message, &orig_message);
1069-
msg = &amend_msg;
1097+
msg = &commit_msg;
10701098
strbuf_addstr(msg, orig_message);
1099+
hook_commit = "HEAD";
10711100
}
10721101
author = amend_author = get_author(message);
10731102
unuse_commit_buffer(current_head, message);
@@ -1081,16 +1110,6 @@ static int try_to_commit(struct strbuf *msg, const char *author,
10811110
commit_list_insert(current_head, &parents);
10821111
}
10831112

1084-
cleanup = (flags & CLEANUP_MSG) ? COMMIT_MSG_CLEANUP_ALL :
1085-
opts->default_msg_cleanup;
1086-
1087-
if (cleanup != COMMIT_MSG_CLEANUP_NONE)
1088-
strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL);
1089-
if (!opts->allow_empty_message && message_is_empty(msg, cleanup)) {
1090-
res = 1; /* run 'git commit' to display error message */
1091-
goto out;
1092-
}
1093-
10941113
if (write_cache_as_tree(tree.hash, 0, NULL)) {
10951114
res = error(_("git write-tree failed to write a tree"));
10961115
goto out;
@@ -1103,6 +1122,30 @@ static int try_to_commit(struct strbuf *msg, const char *author,
11031122
goto out;
11041123
}
11051124

1125+
if (find_hook("prepare-commit-msg")) {
1126+
res = run_prepare_commit_msg_hook(msg, hook_commit);
1127+
if (res)
1128+
goto out;
1129+
if (strbuf_read_file(&commit_msg, git_path_commit_editmsg(),
1130+
2048) < 0) {
1131+
res = error_errno(_("unable to read commit message "
1132+
"from '%s'"),
1133+
git_path_commit_editmsg());
1134+
goto out;
1135+
}
1136+
msg = &commit_msg;
1137+
}
1138+
1139+
cleanup = (flags & CLEANUP_MSG) ? COMMIT_MSG_CLEANUP_ALL :
1140+
opts->default_msg_cleanup;
1141+
1142+
if (cleanup != COMMIT_MSG_CLEANUP_NONE)
1143+
strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL);
1144+
if (!opts->allow_empty_message && message_is_empty(msg, cleanup)) {
1145+
res = 1; /* run 'git commit' to display error message */
1146+
goto out;
1147+
}
1148+
11061149
if (commit_tree_extended(msg->buf, msg->len, tree.hash, parents,
11071150
oid->hash, author, opts->gpg_sign, extra)) {
11081151
res = error(_("failed to write commit object"));
@@ -1121,7 +1164,7 @@ static int try_to_commit(struct strbuf *msg, const char *author,
11211164
out:
11221165
free_commit_extra_headers(extra);
11231166
strbuf_release(&err);
1124-
strbuf_release(&amend_msg);
1167+
strbuf_release(&commit_msg);
11251168
free(amend_author);
11261169

11271170
return res;

sequencer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef SEQUENCER_H
22
#define SEQUENCER_H
33

4+
const char *git_path_commit_editmsg(void);
45
const char *git_path_seq_dir(void);
56

67
#define APPEND_SIGNOFF_DEDUP (1u << 0)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ test_rebase () {
252252
'
253253
}
254254

255-
test_rebase failure -i
256-
test_rebase failure -p
255+
test_rebase success -i
256+
test_rebase success -p
257257

258-
test_expect_failure 'with hook (cherry-pick)' '
258+
test_expect_success 'with hook (cherry-pick)' '
259259
test_when_finished "git checkout -f master" &&
260260
git checkout -B other b &&
261261
git cherry-pick rebase-1 &&
@@ -310,7 +310,7 @@ test_expect_success 'with failing hook (merge)' '
310310
311311
'
312312

313-
test_expect_failure C_LOCALE_OUTPUT 'with failing hook (cherry-pick)' '
313+
test_expect_success C_LOCALE_OUTPUT 'with failing hook (cherry-pick)' '
314314
test_when_finished "git checkout -f master" &&
315315
git checkout -B other b &&
316316
test_must_fail git cherry-pick rebase-1 2>actual &&

0 commit comments

Comments
 (0)