git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 5/6] move run_commit_hook() to libgit and use it there

From
PGPhillip Wood via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 15, 2019, 10:25 UTC
Message-ID
<f394a0e1634d865b64b0e44135e525e3c5a2cf3f.1571135132.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.388.v2.git.1571135132.gitgitgadget@gmail.com>
From: Phillip Wood <phillip.wood@dunelm.org.uk>

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>
---
 builtin/commit.c | 22 ----------------------
 commit.c         | 24 ++++++++++++++++++++++++
 sequencer.c      | 23 ++++++++++-------------
 3 files changed, 34 insertions(+), 35 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 1921401117..d898a57f5d 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -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};
diff --git a/commit.c b/commit.c
index 26ce0770f6..7ca8d12174 100644
--- 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;
+}
diff --git a/sequencer.c b/sequencer.c
index 2adcf5a639..cdc0d1dfba 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -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;
 }
-- 
gitgitgadget
Previous: Phillip Wood via GitGitGadgetNext: Johannes Schindelin
Message 18 of 20 in “sequencer: start running post-commit hook again”
  1. 0/3 sequencer: start running post-commit hook againPhillip Wood via GitGitGadget, Oct 10, 2019
  2. 2/3 sequencer: use run_commit_hook()Phillip Wood via GitGitGadget, Oct 10, 2019
  3. Johannes SchindelinOct 10, 2019
  4. Junio C HamanoOct 11, 2019
  5. Phillip WoodOct 14, 2019
  6. Johannes SchindelinOct 14, 2019
  7. 1/3 sequencer.h fix placement of #endifPhillip Wood via GitGitGadget, Oct 10, 2019
  8. 3/3 sequencer: run post-commit hookPhillip Wood via GitGitGadget, Oct 10, 2019
  9. Johannes SchindelinOct 10, 2019
  10. Junio C HamanoOct 11, 2019
  11. Phillip WoodOct 11, 2019
  12. 0/6 sequencer: start running post-commit hook againPhillip Wood via GitGitGadget, Oct 15, 2019
  13. 1/6 t3404: remove unnecessary subshellPhillip Wood via GitGitGadget, Oct 15, 2019
  14. 2/6 t3404: set $EDITOR in subshellPhillip Wood via GitGitGadget, Oct 15, 2019
  15. 3/6 t3404: remove uneeded calls to set_fake_editorPhillip Wood via GitGitGadget, Oct 15, 2019
  16. 6/6 sequencer: run post-commit hookPhillip Wood via GitGitGadget, Oct 15, 2019
  17. 4/6 sequencer.h fix placement of #endifPhillip Wood via GitGitGadget, Oct 15, 2019
  18. 5/6 move run_commit_hook() to libgit and use it therePhillip Wood via GitGitGadget, Oct 15, 2019
  19. Johannes SchindelinOct 15, 2019
  20. Junio C HamanoOct 16, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.