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

[PATCH 2/4] hooks: support variable number of parameters

From
Aaron Schrab <aaron@schrab.com>
Date
Dec 28, 2012, 22:57 UTC
Message-ID
<1356735452-21667-3-git-send-email-aaron@schrab.com>
In-Reply-To
<1356735452-21667-1-git-send-email-aaron@schrab.com>

Define the run_hook_argv() function to allow hooks to be created where the number of parameters to be passed is variable. The existing run_hook() function uses stdarg to allow it to receive a variable number of arguments, but the number of arguments that a given caller is passing is fixed at compile time. This function will allow the caller of a hook to determine the number of arguments to pass when preparing to call the hook.

The first use of this function will be for a pre-push hook which will add an argument for every reference which is to be pushed.

Signed-off-by: Aaron Schrab <aaron@schrab.com>
---
 run-command.c |   20 +++++++++++++-------
 run-command.h |    2 ++
 2 files changed, 15 insertions(+), 7 deletions(-)
diff --git a/run-command.c b/run-command.c
index 49c8fa0..e07202b 100644
--- a/run-command.c
+++ b/run-command.c
@@ -2,7 +2,6 @@
 #include "run-command.h"
 #include "exec_cmd.h"
 #include "sigchain.h"
-#include "argv-array.h"
 
 #ifndef SHELL_PATH
 # define SHELL_PATH "/bin/sh"
@@ -746,10 +745,8 @@ char *find_hook(const char *name)
 
 int run_hook(const char *index_file, const char *name, ...)
 {
-	struct child_process hook;
 	struct argv_array argv = ARGV_ARRAY_INIT;
-	const char *p, *env[2];
-	char index[PATH_MAX];
+	const char *p;
 	va_list args;
 	int ret;
 
@@ -764,6 +761,17 @@ int run_hook(const char *index_file, const char *name, ...)
 		argv_array_push(&argv, p);
 	va_end(args);
 
+	ret = run_hook_argv(index_file, argv);
+	argv_array_clear(&argv);
+	return ret;
+}
+
+int run_hook_argv(const char *index_file, struct argv_array argv)
+{
+	struct child_process hook;
+	char index[PATH_MAX];
+	const char *env[2];
+
 	memset(&hook, 0, sizeof(hook));
 	hook.argv = argv.argv;
 	hook.no_stdin = 1;
@@ -775,7 +783,5 @@ int run_hook(const char *index_file, const char *name, ...)
 		hook.env = env;
 	}
 
-	ret = run_command(&hook);
-	argv_array_clear(&argv);
-	return ret;
+	return run_command(&hook);
 }
diff --git a/run-command.h b/run-command.h
index 221ce33..12faa5b 100644
--- a/run-command.h
+++ b/run-command.h
@@ -1,6 +1,7 @@
 #ifndef RUN_COMMAND_H
 #define RUN_COMMAND_H
 
+#include "argv-array.h"
 #ifndef NO_PTHREADS
 #include <pthread.h>
 #endif
@@ -47,6 +48,7 @@ int run_command(struct child_process *);
 
 extern char *find_hook(const char *name);
 extern int run_hook(const char *index_file, const char *name, ...);
+extern int run_hook_argv(const char *index_file, struct argv_array);
 
 #define RUN_COMMAND_NO_STDIN 1
 #define RUN_GIT_CMD	     2	/*If this is to be git sub-command */
-- 
1.7.10.4
Previous: Junio C HamanoNext: Aaron Schrab
Message 6 of 22 in “pre-push hook support”
  1. 0/4 pre-push hook supportAaron Schrab, Dec 28, 2012
  2. 1/4 hooks: Add function to check if a hook existsAaron Schrab, Dec 28, 2012
  3. Junio C HamanoDec 29, 2012
  4. Aaron SchrabDec 29, 2012
  5. Junio C HamanoDec 29, 2012
  6. 2/4 hooks: support variable number of parametersAaron Schrab, Dec 28, 2012
  7. 3/4 push: Add support for pre-push hooksAaron Schrab, Dec 28, 2012
  8. 4/4 Add sample pre-push hook scriptAaron Schrab, Dec 28, 2012
  9. Junio C HamanoDec 29, 2012
  10. Aaron SchrabDec 29, 2012
  11. Junio C HamanoDec 29, 2012
  12. 0/3 pre-push hook supportAaron Schrab, Jan 13, 2013
  13. 1/3 hooks: Add function to check if a hook existsAaron Schrab, Jan 13, 2013
  14. 2/3 push: Add support for pre-push hooksAaron Schrab, Jan 13, 2013
  15. Junio C HamanoJan 14, 2013
  16. Junio C HamanoJan 15, 2013
  17. Junio C HamanoJan 15, 2013
  18. 3/3 Add sample pre-push hook scriptAaron Schrab, Jan 13, 2013
  19. Junio C HamanoJan 14, 2013
  20. Junio C HamanoJan 14, 2013
  21. Junio C HamanoJan 14, 2013
  22. Junio C HamanoJan 15, 2013

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.