sequencer: run 'prepare-commit-msg' hook
Commit 356ee4659b ("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 <dmitry.torokhov@gmail.com> Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Phillip Wood committed
Jan 24, 2018 at 12:34 UTC
66618a50f9c9f008d7aef751418f12ba9bfc6b85
4 files changed
+61
-19
builtin/commit.c
-2
@@ -66,8 +66,6 @@ N_("If you wish to skip this commit, use:\n"
66
"Then \"git cherry-pick --continue\" will resume cherry-picking\n"
67
"the remaining commits.\n");
68
69
-static GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG")
70
-
69
static const char *use_message_buffer;
70
static struct lock_file index_lock; /* real index */
71
static struct lock_file false_lock; /* used only for partial commits */
sequencer.c
+56
-13
@@ -29,6 +29,8 @@
29
const char sign_off_header[] = "Signed-off-by: ";
30
static const char cherry_picked_prefix[] = "(cherry picked from commit ";
31
32
+GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG")
33
+
34
GIT_PATH_FUNC(git_path_seq_dir, "sequencer")
35
36
static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
@@ -888,6 +890,31 @@ void commit_post_rewrite(const struct commit *old_head,
890
run_rewrite_hook(&old_head->object.oid, new_head);
891
}
892
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
+
918
static const char implicit_ident_advice_noconfig[] =
919
N_("Your name and email address were configured automatically based\n"
920
"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,
1075
struct commit_list *parents = NULL;
1076
struct commit_extra_header *extra = NULL;
1077
struct strbuf err = STRBUF_INIT;
1051
- struct strbuf amend_msg = STRBUF_INIT;
1078
+ struct strbuf commit_msg = STRBUF_INIT;
1079
char *amend_author = NULL;
1080
+ const char *hook_commit = NULL;
1081
enum commit_msg_cleanup_mode cleanup;
1082
int res = 0;
1083
@@ -1066,8 +1094,9 @@ static int try_to_commit(struct strbuf *msg, const char *author,
1094
const char *orig_message = NULL;
1095
1096
find_commit_subject(message, &orig_message);
1069
- msg = &amend_msg;
1097
+ msg = &commit_msg;
1098
strbuf_addstr(msg, orig_message);
1099
+ hook_commit = "HEAD";
1100
}
1101
author = amend_author = get_author(message);
1102
unuse_commit_buffer(current_head, message);
@@ -1081,16 +1110,6 @@ static int try_to_commit(struct strbuf *msg, const char *author,
1110
commit_list_insert(current_head, &parents);
1111
}
1112
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
-
1113
if (write_cache_as_tree(tree.hash, 0, NULL)) {
1114
res = error(_("git write-tree failed to write a tree"));
1115
goto out;
@@ -1103,6 +1122,30 @@ static int try_to_commit(struct strbuf *msg, const char *author,
1122
goto out;
1123
}
1124
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
+
1149
if (commit_tree_extended(msg->buf, msg->len, tree.hash, parents,
1150
oid->hash, author, opts->gpg_sign, extra)) {
1151
res = error(_("failed to write commit object"));
@@ -1121,7 +1164,7 @@ static int try_to_commit(struct strbuf *msg, const char *author,
1164
out:
1165
free_commit_extra_headers(extra);
1166
strbuf_release(&err);
1124
- strbuf_release(&amend_msg);
1167
+ strbuf_release(&commit_msg);
1168
free(amend_author);
1169
1170
return res;
sequencer.h
+1
@@ -1,6 +1,7 @@
1
#ifndef SEQUENCER_H
2
#define SEQUENCER_H
3
4
+const char *git_path_commit_editmsg(void);
5
const char *git_path_seq_dir(void);
6
7
#define APPEND_SIGNOFF_DEDUP (1u << 0)
t/t7505-prepare-commit-msg-hook.sh
+4
-4
@@ -252,10 +252,10 @@ test_rebase () {
252
'
253
}
254
255
-test_rebase failure -i
256
-test_rebase failure -p
255
+test_rebase success -i
256
+test_rebase success -p
257
258
-test_expect_failure 'with hook (cherry-pick)' '
258
+test_expect_success 'with hook (cherry-pick)' '
259
test_when_finished "git checkout -f master" &&
260
git checkout -B other b &&
261
git cherry-pick rebase-1 &&
@@ -310,7 +310,7 @@ test_expect_success 'with failing hook (merge)' '
310
311
'
312
313
-test_expect_failure C_LOCALE_OUTPUT 'with failing hook (cherry-pick)' '
313
+test_expect_success C_LOCALE_OUTPUT 'with failing hook (cherry-pick)' '
314
test_when_finished "git checkout -f master" &&
315
git checkout -B other b &&
316
test_must_fail git cherry-pick rebase-1 2>actual &&