@@ -66,6 +66,7 @@ static char *edit_message, *use_message;
66
66
static char * author_name , * author_email , * author_date ;
67
67
static int all , edit_flag , also , interactive , only , amend , signoff ;
68
68
static int quiet , verbose , no_verify , allow_empty , dry_run , renew_authorship ;
69
+ static int no_post_rewrite ;
69
70
static char * untracked_files_arg , * force_date ;
70
71
/*
71
72
* The default commit message cleanup mode will remove the lines
@@ -137,6 +138,7 @@ static struct option builtin_commit_options[] = {
137
138
OPT_BOOLEAN ('z' , "null" , & null_termination ,
138
139
"terminate entries with NUL" ),
139
140
OPT_BOOLEAN (0 , "amend" , & amend , "amend previous commit" ),
141
+ OPT_BOOLEAN (0 , "no-post-rewrite" , & no_post_rewrite , "bypass post-rewrite hook" ),
140
142
{ OPTION_STRING , 'u' , "untracked-files" , & untracked_files_arg , "mode" , "show untracked files, optional modes: all, normal, no. (Default: all)" , PARSE_OPT_OPTARG , NULL , (intptr_t )"all" },
141
143
OPT_BOOLEAN (0 , "allow-empty" , & allow_empty , "ok to record an empty change" ),
142
144
/* end commit contents options */
@@ -1160,6 +1162,40 @@ static int git_commit_config(const char *k, const char *v, void *cb)
1160
1162
return git_status_config (k , v , s );
1161
1163
}
1162
1164
1165
+ static const char post_rewrite_hook [] = "hooks/post-rewrite" ;
1166
+
1167
+ static int run_rewrite_hook (const unsigned char * oldsha1 ,
1168
+ const unsigned char * newsha1 )
1169
+ {
1170
+ /* oldsha1 SP newsha1 LF NUL */
1171
+ static char buf [2 * 40 + 3 ];
1172
+ struct child_process proc ;
1173
+ const char * argv [3 ];
1174
+ int code ;
1175
+ size_t n ;
1176
+
1177
+ if (access (git_path (post_rewrite_hook ), X_OK ) < 0 )
1178
+ return 0 ;
1179
+
1180
+ argv [0 ] = git_path (post_rewrite_hook );
1181
+ argv [1 ] = "amend" ;
1182
+ argv [2 ] = NULL ;
1183
+
1184
+ memset (& proc , 0 , sizeof (proc ));
1185
+ proc .argv = argv ;
1186
+ proc .in = -1 ;
1187
+ proc .stdout_to_stderr = 1 ;
1188
+
1189
+ code = start_command (& proc );
1190
+ if (code )
1191
+ return code ;
1192
+ n = snprintf (buf , sizeof (buf ), "%s %s\n" ,
1193
+ sha1_to_hex (oldsha1 ), sha1_to_hex (newsha1 ));
1194
+ write_in_full (proc .in , buf , n );
1195
+ close (proc .in );
1196
+ return finish_command (& proc );
1197
+ }
1198
+
1163
1199
int cmd_commit (int argc , const char * * argv , const char * prefix )
1164
1200
{
1165
1201
struct strbuf sb = STRBUF_INIT ;
@@ -1303,6 +1339,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1303
1339
1304
1340
rerere (0 );
1305
1341
run_hook (get_index_file (), "post-commit" , NULL );
1342
+ if (amend && !no_post_rewrite ) {
1343
+ run_rewrite_hook (head_sha1 , commit_sha1 );
1344
+ }
1306
1345
if (!quiet )
1307
1346
print_summary (prefix , commit_sha1 );
1308
1347
0 commit comments