Skip to content

Commit 420ecf4

Browse files
committed
sequencer: use run_commit_hook()
This simplifies the implementation of run_prepare_commit_msg_hook() and will be used in the next commit. Signed-off-by: Phillip Wood <[email protected]>
1 parent 7305f8d commit 420ecf4

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

builtin/commit.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,28 +1443,6 @@ static int git_commit_config(const char *k, const char *v, void *cb)
14431443
return git_status_config(k, v, s);
14441444
}
14451445

1446-
int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
1447-
{
1448-
struct argv_array hook_env = ARGV_ARRAY_INIT;
1449-
va_list args;
1450-
int ret;
1451-
1452-
argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
1453-
1454-
/*
1455-
* Let the hook know that no editor will be launched.
1456-
*/
1457-
if (!editor_is_used)
1458-
argv_array_push(&hook_env, "GIT_EDITOR=:");
1459-
1460-
va_start(args, name);
1461-
ret = run_hook_ve(hook_env.argv,name, args);
1462-
va_end(args);
1463-
argv_array_clear(&hook_env);
1464-
1465-
return ret;
1466-
}
1467-
14681446
int cmd_commit(int argc, const char **argv, const char *prefix)
14691447
{
14701448
const char *argv_gc_auto[] = {"gc", "--auto", NULL};

commit.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,4 @@ void verify_merge_signature(struct commit *commit, int verbose);
389389
int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused);
390390
int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused);
391391

392-
LAST_ARG_MUST_BE_NULL
393-
int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);
394-
395392
#endif /* COMMIT_H */

sequencer.c

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,28 +1123,51 @@ void commit_post_rewrite(struct repository *r,
11231123
run_rewrite_hook(&old_head->object.oid, new_head);
11241124
}
11251125

1126+
int run_commit_hook(int editor_is_used, const char *index_file,
1127+
const char *name, ...)
1128+
{
1129+
struct argv_array hook_env = ARGV_ARRAY_INIT;
1130+
va_list args;
1131+
int ret;
1132+
1133+
argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
1134+
1135+
/*
1136+
* Let the hook know that no editor will be launched.
1137+
*/
1138+
if (!editor_is_used)
1139+
argv_array_push(&hook_env, "GIT_EDITOR=:");
1140+
1141+
va_start(args, name);
1142+
ret = run_hook_ve(hook_env.argv,name, args);
1143+
va_end(args);
1144+
argv_array_clear(&hook_env);
1145+
1146+
return ret;
1147+
}
1148+
11261149
static int run_prepare_commit_msg_hook(struct repository *r,
11271150
struct strbuf *msg,
11281151
const char *commit)
11291152
{
11301153
struct argv_array hook_env = ARGV_ARRAY_INIT;
1131-
int ret;
1132-
const char *name;
1154+
int ret = 0;
1155+
const char *name, *arg1 = NULL, *arg2 = NULL;
11331156

11341157
name = git_path_commit_editmsg();
11351158
if (write_message(msg->buf, msg->len, name, 0))
11361159
return -1;
11371160

1138-
argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", r->index_file);
1139-
argv_array_push(&hook_env, "GIT_EDITOR=:");
1140-
if (commit)
1141-
ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name,
1142-
"commit", commit, NULL);
1143-
else
1144-
ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name,
1145-
"message", NULL);
1146-
if (ret)
1161+
if (commit) {
1162+
arg1 = "commit";
1163+
arg2 = commit;
1164+
} else {
1165+
arg1 = "message";
1166+
}
1167+
if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name,
1168+
arg1, arg2, NULL))
11471169
ret = error(_("'prepare-commit-msg' hook failed"));
1170+
11481171
argv_array_clear(&hook_env);
11491172

11501173
return ret;

sequencer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,6 @@ int write_basic_state(struct replay_opts *opts, const char *head_name,
201201
void sequencer_post_commit_cleanup(struct repository *r);
202202
int sequencer_get_last_command(struct repository* r,
203203
enum replay_action *action);
204+
LAST_ARG_MUST_BE_NULL
205+
int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);
204206
#endif /* SEQUENCER_H */

0 commit comments

Comments
 (0)