@matheusbsbs / matheus-bagliano / commits / 3473f3035d
Fork 0

git-commit: support variable number of hook arguments

This is a preparatory patch to allow using run_hook for the prepare-commit-msg hook. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paolo Bonzini committed Feb 5, 2008 at 11:01 UTC 3473f3035d04957ca24d3ccc232f4263b26d6bb8
1 file changed +35 -26
builtin-commit.c
+35 -26
@@ -343,6 +343,40 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
343 return s.commitable;
344 }
345
346 +static int run_hook(const char *index_file, const char *name, ...)
347 +{
348 + struct child_process hook;
349 + const char *argv[10], *env[2];
350 + char index[PATH_MAX];
351 + va_list args;
352 + int i;
353 +
354 + va_start(args, name);
355 + argv[0] = git_path("hooks/%s", name);
356 + i = 0;
357 + do {
358 + if (++i >= ARRAY_SIZE(argv))
359 + die ("run_hook(): too many arguments");
360 + argv[i] = va_arg(args, const char *);
361 + } while (argv[i]);
362 + va_end(args);
363 +
364 + snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
365 + env[0] = index;
366 + env[1] = NULL;
367 +
368 + if (access(argv[0], X_OK) < 0)
369 + return 0;
370 +
371 + memset(&hook, 0, sizeof(hook));
372 + hook.argv = argv;
373 + hook.no_stdin = 1;
374 + hook.stdout_to_stderr = 1;
375 + hook.env = env;
376 +
377 + return run_command(&hook);
378 +}
379 +
380 static const char sign_off_header[] = "Signed-off-by: ";
381
382 static int prepare_log_message(const char *index_file, const char *prefix)
@@ -677,31 +711,6 @@ int cmd_status(int argc, const char **argv, const char *prefix)
711 return commitable ? 0 : 1;
712 }
713
680 -static int run_hook(const char *index_file, const char *name, const char *arg)
681 -{
682 - struct child_process hook;
683 - const char *argv[3], *env[2];
684 - char index[PATH_MAX];
685 -
686 - argv[0] = git_path("hooks/%s", name);
687 - argv[1] = arg;
688 - argv[2] = NULL;
689 - snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
690 - env[0] = index;
691 - env[1] = NULL;
692 -
693 - if (access(argv[0], X_OK) < 0)
694 - return 0;
695 -
696 - memset(&hook, 0, sizeof(hook));
697 - hook.argv = argv;
698 - hook.no_stdin = 1;
699 - hook.stdout_to_stderr = 1;
700 - hook.env = env;
701 -
702 - return run_command(&hook);
703 -}
704 -
714 static void print_summary(const char *prefix, const unsigned char *sha1)
715 {
716 struct rev_info rev;
@@ -876,7 +885,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
885 launch_editor(git_path(commit_editmsg), NULL, env);
886 }
887 if (!no_verify &&
879 - run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
888 + run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
889 rollback_index_files();
890 exit(1);
891 }