29
29
const char sign_off_header [] = "Signed-off-by: " ;
30
30
static const char cherry_picked_prefix [] = "(cherry picked from commit " ;
31
31
32
+ GIT_PATH_FUNC (git_path_commit_editmsg , "COMMIT_EDITMSG" )
33
+
32
34
GIT_PATH_FUNC (git_path_seq_dir , "sequencer" )
33
35
34
36
static GIT_PATH_FUNC (git_path_todo_file , "sequencer/todo" )
@@ -888,6 +890,31 @@ void commit_post_rewrite(const struct commit *old_head,
888
890
run_rewrite_hook (& old_head -> object .oid , new_head );
889
891
}
890
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
+
891
918
static const char implicit_ident_advice_noconfig [] =
892
919
N_ ("Your name and email address were configured automatically based\n"
893
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,
1048
1075
struct commit_list * parents = NULL ;
1049
1076
struct commit_extra_header * extra = NULL ;
1050
1077
struct strbuf err = STRBUF_INIT ;
1051
- struct strbuf amend_msg = STRBUF_INIT ;
1078
+ struct strbuf commit_msg = STRBUF_INIT ;
1052
1079
char * amend_author = NULL ;
1080
+ const char * hook_commit = NULL ;
1053
1081
enum commit_msg_cleanup_mode cleanup ;
1054
1082
int res = 0 ;
1055
1083
@@ -1066,8 +1094,9 @@ static int try_to_commit(struct strbuf *msg, const char *author,
1066
1094
const char * orig_message = NULL ;
1067
1095
1068
1096
find_commit_subject (message , & orig_message );
1069
- msg = & amend_msg ;
1097
+ msg = & commit_msg ;
1070
1098
strbuf_addstr (msg , orig_message );
1099
+ hook_commit = "HEAD" ;
1071
1100
}
1072
1101
author = amend_author = get_author (message );
1073
1102
unuse_commit_buffer (current_head , message );
@@ -1081,16 +1110,6 @@ static int try_to_commit(struct strbuf *msg, const char *author,
1081
1110
commit_list_insert (current_head , & parents );
1082
1111
}
1083
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
-
1094
1113
if (write_cache_as_tree (tree .hash , 0 , NULL )) {
1095
1114
res = error (_ ("git write-tree failed to write a tree" ));
1096
1115
goto out ;
@@ -1103,6 +1122,30 @@ static int try_to_commit(struct strbuf *msg, const char *author,
1103
1122
goto out ;
1104
1123
}
1105
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
+
1106
1149
if (commit_tree_extended (msg -> buf , msg -> len , tree .hash , parents ,
1107
1150
oid -> hash , author , opts -> gpg_sign , extra )) {
1108
1151
res = error (_ ("failed to write commit object" ));
@@ -1121,7 +1164,7 @@ static int try_to_commit(struct strbuf *msg, const char *author,
1121
1164
out :
1122
1165
free_commit_extra_headers (extra );
1123
1166
strbuf_release (& err );
1124
- strbuf_release (& amend_msg );
1167
+ strbuf_release (& commit_msg );
1125
1168
free (amend_author );
1126
1169
1127
1170
return res ;
0 commit comments