]> git.ipfire.org Git - thirdparty/git.git/commitdiff
move run_commit_hook() to libgit and use it there
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Tue, 15 Oct 2019 10:25:31 +0000 (10:25 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 16 Oct 2019 01:30:51 +0000 (10:30 +0900)
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>
builtin/commit.c
commit.c
sequencer.c

index 192140111747cc0683c1ae19a785000110808ff3..d898a57f5d599b84a5457c9bfc95c3ea54caadad 100644 (file)
@@ -1443,28 +1443,6 @@ static int git_commit_config(const char *k, const char *v, void *cb)
        return git_status_config(k, v, s);
 }
 
-int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
-{
-       struct argv_array hook_env = ARGV_ARRAY_INIT;
-       va_list args;
-       int ret;
-
-       argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
-
-       /*
-        * Let the hook know that no editor will be launched.
-        */
-       if (!editor_is_used)
-               argv_array_push(&hook_env, "GIT_EDITOR=:");
-
-       va_start(args, name);
-       ret = run_hook_ve(hook_env.argv,name, args);
-       va_end(args);
-       argv_array_clear(&hook_env);
-
-       return ret;
-}
-
 int cmd_commit(int argc, const char **argv, const char *prefix)
 {
        const char *argv_gc_auto[] = {"gc", "--auto", NULL};
index 26ce0770f688eb94d5be5975d756752dbc0337a8..7ca8d12174105e197542b5df2e315e39b30d3b7e 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -19,6 +19,7 @@
 #include "advice.h"
 #include "refs.h"
 #include "commit-reach.h"
+#include "run-command.h"
 
 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
 
@@ -1581,3 +1582,26 @@ size_t ignore_non_trailer(const char *buf, size_t len)
        }
        return boc ? len - boc : len - cutoff;
 }
+
+int run_commit_hook(int editor_is_used, const char *index_file,
+                   const char *name, ...)
+{
+       struct argv_array hook_env = ARGV_ARRAY_INIT;
+       va_list args;
+       int ret;
+
+       argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
+
+       /*
+        * Let the hook know that no editor will be launched.
+        */
+       if (!editor_is_used)
+               argv_array_push(&hook_env, "GIT_EDITOR=:");
+
+       va_start(args, name);
+       ret = run_hook_ve(hook_env.argv,name, args);
+       va_end(args);
+       argv_array_clear(&hook_env);
+
+       return ret;
+}
index 2adcf5a639c3a7d0154f07cf07f420f5d4f54007..cdc0d1dfba77d29db321b1749ce666ead394844e 100644 (file)
@@ -1127,25 +1127,22 @@ static int run_prepare_commit_msg_hook(struct repository *r,
                                       struct strbuf *msg,
                                       const char *commit)
 {
-       struct argv_array hook_env = ARGV_ARRAY_INIT;
-       int ret;
-       const char *name;
+       int ret = 0;
+       const char *name, *arg1 = NULL, *arg2 = NULL;
 
        name = git_path_commit_editmsg();
        if (write_message(msg->buf, msg->len, name, 0))
                return -1;
 
-       argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", r->index_file);
-       argv_array_push(&hook_env, "GIT_EDITOR=:");
-       if (commit)
-               ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name,
-                                 "commit", commit, NULL);
-       else
-               ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name,
-                                 "message", NULL);
-       if (ret)
+       if (commit) {
+               arg1 = "commit";
+               arg2 = commit;
+       } else {
+               arg1 = "message";
+       }
+       if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name,
+                           arg1, arg2, NULL))
                ret = error(_("'prepare-commit-msg' hook failed"));
-       argv_array_clear(&hook_env);
 
        return ret;
 }