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

[PATCH 1/4] git-commit: support variable number of hook arguments

From
Paolo Bonzini <bonzini@gnu.org>
Date
Jan 21, 2008, 14:02 UTC
Message-ID
<2885c7896ba72adee6bc5c9e2210a8881904f601.1200933409.git.bonzini@gnu.org>
In-Reply-To
<2885c7896ba72adee6bc5c9e2210a8881904f601.head.git.bonzini@gnu.org>

This is a preparatory patch to allow using run_hook for the prepare-commit-msg hook.

Signed-off-by: Paolo Bonzini <bonzini@gnu.org>
---
 builtin-commit.c |   59 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 33 insertions(+), 26 deletions(-)
diff --git a/builtin-commit.c b/builtin-commit.c
index 0227936..fc59660 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -339,6 +339,38 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
 	return s.commitable;
 }
 
+static int run_hook(const char *index_file, const char *name, ...)
+{
+	struct child_process hook;
+	const char *argv[10], *env[2];
+	char index[PATH_MAX];
+	va_list args;
+	int i;
+
+	va_start(args, name);
+	argv[0] = git_path("hooks/%s", name);
+	i = 0;
+	do {
+		argv[++i] = va_arg(args, const char *);
+	} while (argv[i]);
+	va_end(args);
+
+	snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
+	env[0] = index;
+	env[1] = NULL;
+
+	if (access(argv[0], X_OK) < 0)
+		return 0;
+
+	memset(&hook, 0, sizeof(hook));
+	hook.argv = argv;
+	hook.no_stdin = 1;
+	hook.stdout_to_stderr = 1;
+	hook.env = env;
+
+	return run_command(&hook);
+}
+
 static const char sign_off_header[] = "Signed-off-by: ";
 
 static int prepare_log_message(const char *index_file, const char *prefix)
@@ -673,31 +705,6 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	return commitable ? 0 : 1;
 }
 
-static int run_hook(const char *index_file, const char *name, const char *arg)
-{
-	struct child_process hook;
-	const char *argv[3], *env[2];
-	char index[PATH_MAX];
-
-	argv[0] = git_path("hooks/%s", name);
-	argv[1] = arg;
-	argv[2] = NULL;
-	snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
-	env[0] = index;
-	env[1] = NULL;
-
-	if (access(argv[0], X_OK) < 0)
-		return 0;
-
-	memset(&hook, 0, sizeof(hook));
-	hook.argv = argv;
-	hook.no_stdin = 1;
-	hook.stdout_to_stderr = 1;
-	hook.env = env;
-
-	return run_command(&hook);
-}
-
 static void print_summary(const char *prefix, const unsigned char *sha1)
 {
 	struct rev_info rev;
@@ -872,7 +879,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		launch_editor(git_path(commit_editmsg), NULL, env);
 	}
 	if (!no_verify &&
-	    run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
+	    run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
 		rollback_index_files();
 		exit(1);
 	}
Previous: Paolo BonziniNext: Johannes Schindelin
Message 2 of 13 in “git-commit: add a prepare-commit-msg hook”
  1. git-commit: add a prepare-commit-msg hookPaolo Bonzini, Jan 21, 2008
  2. 1/4 git-commit: support variable number of hook argumentsPaolo Bonzini, Jan 21, 2008
  3. Johannes SchindelinFeb 4, 2008
  4. Junio C HamanoFeb 5, 2008
  5. 2/4 git-commit: set GIT_EDITOR=: if editor will not be launchedPaolo Bonzini, Jan 21, 2008
  6. 4/4 git-commit: add a prepare-commit-msg hookPaolo Bonzini, Jan 21, 2008
  7. Junio C HamanoFeb 5, 2008
  8. 3/4 git-commit: Refactor creation of log message.Paolo Bonzini, Jan 21, 2008
  9. Johannes SchindelinFeb 4, 2008
  10. Paolo BonziniFeb 4, 2008
  11. Junio C HamanoFeb 5, 2008
  12. Junio C HamanoFeb 5, 2008
  13. Paolo BonziniFeb 5, 2008

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.