git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: <git@vger.kernel.org>
Cc: Jeff King <peff@peff.net>, Duy Nguyen <pclouds@gmail.com>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>
Subject: [PATCH 2/5] builtin/receive-pack: add support for multiple hooks
Date: Wed, 24 Apr 2019 00:49:45 +0000	[thread overview]
Message-ID: <20190424004948.728326-3-sandals@crustytoothpaste.net> (raw)
In-Reply-To: <20190424004948.728326-1-sandals@crustytoothpaste.net>

Add support for multiple hooks for the pre-receive, post-receive,
update, post-update, and push-to-checkout hooks. Add tests for these
hooks using the multiple hook test framework.

Because the invocations of test_multiple_hooks contain multiple test
assertions, they (and the cd commands that surround them) must occur
outside of a subshell, or a failing test will not be noticed.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 builtin/receive-pack.c | 212 ++++++++++++++++++++++++-----------------
 t/t5516-fetch-push.sh  |  29 ++++++
 2 files changed, 152 insertions(+), 89 deletions(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 29f165d8bd..7454834d2a 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -680,72 +680,82 @@ typedef int (*feed_fn)(void *, const char **, size_t *);
 static int run_and_feed_hook(const char *hook_name, feed_fn feed,
 			     struct receive_hook_feed_state *feed_state)
 {
-	struct child_process proc = CHILD_PROCESS_INIT;
+	struct child_process proc;
+	struct string_list *hooks;
+	struct string_list_item *p;
 	struct async muxer;
 	const char *argv[2];
-	int code;
+	int code = 0;
 
-	argv[0] = find_hook(hook_name);
-	if (!argv[0])
+	hooks = find_hooks(hook_name);
+	if (!hooks)
 		return 0;
 
-	argv[1] = NULL;
+	for_each_string_list_item(p, hooks) {
+		argv[0] = p->string;
+		argv[1] = NULL;
 
-	proc.argv = argv;
-	proc.in = -1;
-	proc.stdout_to_stderr = 1;
-	proc.trace2_hook_name = hook_name;
+		child_process_init(&proc);
+		proc.argv = argv;
+		proc.in = -1;
+		proc.stdout_to_stderr = 1;
+		proc.trace2_hook_name = hook_name;
 
-	if (feed_state->push_options) {
-		int i;
-		for (i = 0; i < feed_state->push_options->nr; i++)
-			argv_array_pushf(&proc.env_array,
-				"GIT_PUSH_OPTION_%d=%s", i,
-				feed_state->push_options->items[i].string);
-		argv_array_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT=%d",
-				 feed_state->push_options->nr);
-	} else
-		argv_array_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT");
+		if (feed_state->push_options) {
+			int i;
+			for (i = 0; i < feed_state->push_options->nr; i++)
+				argv_array_pushf(&proc.env_array,
+					"GIT_PUSH_OPTION_%d=%s", i,
+					feed_state->push_options->items[i].string);
+			argv_array_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT=%d",
+					 feed_state->push_options->nr);
+		} else
+			argv_array_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT");
 
-	if (tmp_objdir)
-		argv_array_pushv(&proc.env_array, tmp_objdir_env(tmp_objdir));
+		if (tmp_objdir)
+			argv_array_pushv(&proc.env_array, tmp_objdir_env(tmp_objdir));
 
-	if (use_sideband) {
-		memset(&muxer, 0, sizeof(muxer));
-		muxer.proc = copy_to_sideband;
-		muxer.in = -1;
-		code = start_async(&muxer);
-		if (code)
-			return code;
-		proc.err = muxer.in;
-	}
+		if (use_sideband) {
+			memset(&muxer, 0, sizeof(muxer));
+			muxer.proc = copy_to_sideband;
+			muxer.in = -1;
+			code = start_async(&muxer);
+			if (code)
+				break;
+			proc.err = muxer.in;
+		}
 
-	prepare_push_cert_sha1(&proc);
+		prepare_push_cert_sha1(&proc);
 
-	code = start_command(&proc);
-	if (code) {
+		code = start_command(&proc);
+		if (code) {
+			if (use_sideband)
+				finish_async(&muxer);
+			break;
+		}
+
+		sigchain_push(SIGPIPE, SIG_IGN);
+
+		while (1) {
+			const char *buf;
+			size_t n;
+			if (feed(feed_state, &buf, &n))
+				break;
+			if (write_in_full(proc.in, buf, n) < 0)
+				break;
+		}
+		close(proc.in);
 		if (use_sideband)
 			finish_async(&muxer);
-		return code;
-	}
 
-	sigchain_push(SIGPIPE, SIG_IGN);
+		sigchain_pop(SIGPIPE);
 
-	while (1) {
-		const char *buf;
-		size_t n;
-		if (feed(feed_state, &buf, &n))
-			break;
-		if (write_in_full(proc.in, buf, n) < 0)
+		code = finish_command(&proc);
+		if (code)
 			break;
 	}
-	close(proc.in);
-	if (use_sideband)
-		finish_async(&muxer);
-
-	sigchain_pop(SIGPIPE);
-
-	return finish_command(&proc);
+        free_hooks(hooks);
+	return code;
 }
 
 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
@@ -793,30 +803,41 @@ static int run_receive_hook(struct command *commands,
 static int run_update_hook(struct command *cmd)
 {
 	const char *argv[5];
-	struct child_process proc = CHILD_PROCESS_INIT;
+	struct child_process proc;
+	struct string_list *hooks;
+	struct string_list_item *p;
 	int code;
 
-	argv[0] = find_hook("update");
-	if (!argv[0])
+	hooks = find_hooks("update");
+	if (!hooks)
 		return 0;
 
-	argv[1] = cmd->ref_name;
-	argv[2] = oid_to_hex(&cmd->old_oid);
-	argv[3] = oid_to_hex(&cmd->new_oid);
-	argv[4] = NULL;
+	for_each_string_list_item(p, hooks) {
+		child_process_init(&proc);
 
-	proc.no_stdin = 1;
-	proc.stdout_to_stderr = 1;
-	proc.err = use_sideband ? -1 : 0;
-	proc.argv = argv;
-	proc.trace2_hook_name = "update";
+		argv[0] = p->string;
+		argv[1] = cmd->ref_name;
+		argv[2] = oid_to_hex(&cmd->old_oid);
+		argv[3] = oid_to_hex(&cmd->new_oid);
+		argv[4] = NULL;
 
-	code = start_command(&proc);
-	if (code)
-		return code;
-	if (use_sideband)
-		copy_to_sideband(proc.err, -1, NULL);
-	return finish_command(&proc);
+		proc.no_stdin = 1;
+		proc.stdout_to_stderr = 1;
+		proc.err = use_sideband ? -1 : 0;
+		proc.argv = argv;
+		proc.trace2_hook_name = "update";
+
+		code = start_command(&proc);
+		if (code)
+			return code;
+		if (use_sideband)
+			copy_to_sideband(proc.err, -1, NULL);
+		code = finish_command(&proc);
+		if (code)
+			break;
+	}
+        free_hooks(hooks);
+	return code;
 }
 
 static int is_ref_checked_out(const char *ref)
@@ -1005,16 +1026,20 @@ static const char *update_worktree(unsigned char *sha1)
 	const char *retval;
 	const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : "..";
 	struct argv_array env = ARGV_ARRAY_INIT;
+	struct string_list *hooks;
 
 	if (is_bare_repository())
 		return "denyCurrentBranch = updateInstead needs a worktree";
 
 	argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir()));
 
-	if (!find_hook(push_to_checkout_hook))
+	hooks = find_hooks(push_to_checkout_hook);
+	if (!hooks)
 		retval = push_to_deploy(sha1, &env, work_tree);
-	else
+	else {
+		free_hooks(hooks);
 		retval = push_to_checkout(sha1, &env, work_tree);
+	}
 
 	argv_array_clear(&env);
 	return retval;
@@ -1173,33 +1198,42 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 static void run_update_post_hook(struct command *commands)
 {
 	struct command *cmd;
-	struct child_process proc = CHILD_PROCESS_INIT;
-	const char *hook;
+	struct child_process proc;
+	struct string_list *hooks;
+	struct string_list_item *p;
 
-	hook = find_hook("post-update");
-	if (!hook)
+	hooks = find_hooks("post-update");
+	if (!hooks)
 		return;
 
-	for (cmd = commands; cmd; cmd = cmd->next) {
-		if (cmd->error_string || cmd->did_not_exist)
-			continue;
+	for_each_string_list_item(p, hooks) {
+		child_process_init(&proc);
+
+		for (cmd = commands; cmd; cmd = cmd->next) {
+			if (cmd->error_string || cmd->did_not_exist)
+				continue;
+			if (!proc.args.argc)
+				argv_array_push(&proc.args, p->string);
+			argv_array_push(&proc.args, cmd->ref_name);
+		}
 		if (!proc.args.argc)
-			argv_array_push(&proc.args, hook);
-		argv_array_push(&proc.args, cmd->ref_name);
-	}
-	if (!proc.args.argc)
-		return;
+			return;
 
-	proc.no_stdin = 1;
-	proc.stdout_to_stderr = 1;
-	proc.err = use_sideband ? -1 : 0;
-	proc.trace2_hook_name = "post-update";
+		proc.no_stdin = 1;
+		proc.stdout_to_stderr = 1;
+		proc.err = use_sideband ? -1 : 0;
+		proc.trace2_hook_name = "post-update";
 
-	if (!start_command(&proc)) {
-		if (use_sideband)
-			copy_to_sideband(proc.err, -1, NULL);
-		finish_command(&proc);
+		if (!start_command(&proc)) {
+			int ret;
+			if (use_sideband)
+				copy_to_sideband(proc.err, -1, NULL);
+			ret = finish_command(&proc);
+			if (ret)
+				break;
+		}
 	}
+        free_hooks(hooks);
 }
 
 static void check_aliased_update_internal(struct command *cmd,
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 49bf4280e8..3143422344 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -15,6 +15,7 @@ This test checks the following functionality:
 '
 
 . ./test-lib.sh
+. "$TEST_DIRECTORY/lib-hooks.sh"
 
 D=$(pwd)
 
@@ -1700,4 +1701,32 @@ test_expect_success 'updateInstead with push-to-checkout hook' '
 	)
 '
 
+test_expect_success 'setup' '
+	mk_test_with_hooks hooktest heads/master
+'
+
+cmd_receive () {
+	echo "$1" >>../file &&
+	git -C .. add file &&
+	git -C .. commit -m "$1" &&
+	git -C .. push hooktest refs/heads/master:refs/heads/master
+}
+
+cd hooktest
+test_multiple_hooks pre-receive cmd_receive
+test_multiple_hooks --ignore-exit-status post-receive cmd_receive
+test_multiple_hooks update cmd_receive
+test_multiple_hooks --ignore-exit-status post-update cmd_receive
+cd ..
+
+test_expect_success 'setup' '
+	rm -fr hooktest &&
+	git init hooktest &&
+	git -C hooktest config receive.denyCurrentBranch updateInstead
+'
+
+cd hooktest
+test_multiple_hooks push-to-checkout cmd_receive
+cd ..
+
 test_done

  parent reply	other threads:[~2019-04-24  0:54 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-24  0:49 [PATCH 0/5] Multiple hook support brian m. carlson
2019-04-24  0:49 ` [PATCH 1/5] run-command: add preliminary support for multiple hooks brian m. carlson
2019-04-24  2:27   ` Junio C Hamano
2019-04-24 18:48     ` Johannes Sixt
2019-04-25  0:55       ` Junio C Hamano
2019-04-25  9:39         ` Ævar Arnfjörð Bjarmason
2019-04-25 10:04           ` Junio C Hamano
2019-04-25 19:40         ` Johannes Sixt
2019-04-26 20:58           ` brian m. carlson
2019-04-26 21:53             ` Johannes Sixt
2019-04-24 22:32     ` brian m. carlson
2019-04-24  0:49 ` brian m. carlson [this message]
2019-04-24  0:49 ` [PATCH 3/5] sequencer: add " brian m. carlson
2019-04-24  9:51   ` Phillip Wood
2019-04-24 22:46     ` brian m. carlson
2019-04-25 14:59       ` Phillip Wood
2019-04-24  0:49 ` [PATCH 4/5] builtin/worktree: add support for multiple post-checkout hooks brian m. carlson
2019-04-24  0:49 ` [PATCH 5/5] transport: add support for multiple pre-push hooks brian m. carlson
2019-04-24  2:09 ` [PATCH 0/5] Multiple hook support Junio C Hamano
2019-04-24  2:22   ` brian m. carlson
2019-04-24  2:41     ` Junio C Hamano
2019-04-24  8:14     ` Ævar Arnfjörð Bjarmason
2019-04-24  2:34 ` Jonathan Nieder
2019-04-24  7:43   ` Ævar Arnfjörð Bjarmason
2019-04-24  8:22   ` Ævar Arnfjörð Bjarmason
2019-04-24 23:07   ` brian m. carlson
2019-04-24 23:26     ` Jonathan Nieder
2019-04-25 10:08     ` How to undo previously set configuration? (again) Ævar Arnfjörð Bjarmason
2019-04-25 10:43       ` Duy Nguyen
2019-04-25 11:58         ` Ævar Arnfjörð Bjarmason
2019-04-26 15:18           ` Ævar Arnfjörð Bjarmason
2019-04-25 14:36       ` Jonathan Nieder
2019-04-25 14:43         ` Barret Rhoden
2019-04-25 15:27           ` Ævar Arnfjörð Bjarmason
2019-04-25 15:25         ` Ævar Arnfjörð Bjarmason
2019-04-26  2:13         ` Junio C Hamano
2019-04-26  9:36         ` Duy Nguyen
2019-04-30 21:14           ` Jeff King
2019-05-01 11:41             ` Duy Nguyen
2019-05-01 12:18               ` Ævar Arnfjörð Bjarmason
2019-05-01 12:32                 ` Duy Nguyen
2019-05-01 21:09                   ` Jeff King
2019-05-01 21:15                 ` Jeff King
2019-04-24  8:10 ` [PATCH 0/5] Multiple hook support Ævar Arnfjörð Bjarmason
2019-04-24  9:55   ` Phillip Wood
2019-04-24 18:29   ` Bryan Turner
2019-04-24  9:49 ` Duy Nguyen
2019-04-24 22:49   ` brian m. carlson
2019-04-24 23:40   ` brian m. carlson
2019-04-25  0:08     ` Duy Nguyen
2019-04-30 21:39 ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190424004948.728326-3-sandals@crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).