move run_commit_hook() to libgit and use it there

This function was declared in commit.h but was implemented in builtin/commit.c so was not part of libgit. Move it to libgit so we can use it in the sequencer. This simplifies the implementation of run_prepare_commit_msg_hook() and will be used in the next commit. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Oct 15, 2019 at 10:25 UTC 49697cb72122cf84b44111124821c9a4bcba3ab6
3 files changed +34 -35
builtin/commit.c
-22
@@ -1443,28 +1443,6 @@ static int git_commit_config(const char *k, const char *v, void *cb)
1443 return git_status_config(k, v, s);
1444 }
1445
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 -
1446 int cmd_commit(int argc, const char **argv, const char *prefix)
1447 {
1448 const char *argv_gc_auto[] = {"gc", "--auto", NULL};
commit.c
+24
@@ -19,6 +19,7 @@
19 #include "advice.h"
20 #include "refs.h"
21 #include "commit-reach.h"
22 +#include "run-command.h"
23
24 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
25
@@ -1581,3 +1582,26 @@ size_t ignore_non_trailer(const char *buf, size_t len)
1582 }
1583 return boc ? len - boc : len - cutoff;
1584 }
1585 +
1586 +int run_commit_hook(int editor_is_used, const char *index_file,
1587 + const char *name, ...)
1588 +{
1589 + struct argv_array hook_env = ARGV_ARRAY_INIT;
1590 + va_list args;
1591 + int ret;
1592 +
1593 + argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
1594 +
1595 + /*
1596 + * Let the hook know that no editor will be launched.
1597 + */
1598 + if (!editor_is_used)
1599 + argv_array_push(&hook_env, "GIT_EDITOR=:");
1600 +
1601 + va_start(args, name);
1602 + ret = run_hook_ve(hook_env.argv,name, args);
1603 + va_end(args);
1604 + argv_array_clear(&hook_env);
1605 +
1606 + return ret;
1607 +}
sequencer.c
+10 -13
@@ -1127,25 +1127,22 @@ static int run_prepare_commit_msg_hook(struct repository *r,
1127 struct strbuf *msg,
1128 const char *commit)
1129 {
1130 - struct argv_array hook_env = ARGV_ARRAY_INIT;
1131 - int ret;
1132 - const char *name;
1130 + int ret = 0;
1131 + const char *name, *arg1 = NULL, *arg2 = NULL;
1132
1133 name = git_path_commit_editmsg();
1134 if (write_message(msg->buf, msg->len, name, 0))
1135 return -1;
1136
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)
1137 + if (commit) {
1138 + arg1 = "commit";
1139 + arg2 = commit;
1140 + } else {
1141 + arg1 = "message";
1142 + }
1143 + if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name,
1144 + arg1, arg2, NULL))
1145 ret = error(_("'prepare-commit-msg' hook failed"));
1148 - argv_array_clear(&hook_env);
1146
1147 return ret;
1148 }