git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
0dab98168167514c098e90998513c5db94c53829 blob 3668 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
 
#include "cache.h"

#include "hook.h"
#include "config.h"
#include "run-command.h"

/*
 * NEEDSWORK: a stateful hook_head means we can't run two hook events in the
 * background at the same time - which might be ok, or might not.
 *
 * Maybe it's better to cache a list head per hookname, since we can probably
 * guess that the hook list won't change during a user-initiated operation. For
 * now, within list_hooks, call clear_hook_list() at the outset.
 */
static LIST_HEAD(hook_head);

void free_hook(struct hook *ptr)
{
	if (ptr) {
		strbuf_release(&ptr->command);
		free(ptr);
	}
}

static void emplace_hook(struct list_head *pos, const char *command, int quoted)
{
	struct hook *to_add = malloc(sizeof(struct hook));
	to_add->origin = current_config_scope();
	strbuf_init(&to_add->command, 0);
	if (quoted)
		strbuf_addf(&to_add->command, "'%s'", command);
	else
		strbuf_addstr(&to_add->command, command);

	list_add_tail(&to_add->list, pos);
}

static void remove_hook(struct list_head *to_remove)
{
	struct hook *hook_to_remove = list_entry(to_remove, struct hook, list);
	list_del(to_remove);
	free_hook(hook_to_remove);
}

void clear_hook_list(void)
{
	struct list_head *pos, *tmp;
	list_for_each_safe(pos, tmp, &hook_head)
		remove_hook(pos);
}

static int hook_config_lookup(const char *key, const char *value, void *hook_key_cb)
{
	const char *hook_key = hook_key_cb;

	if (!strcmp(key, hook_key)) {
		const char *command = value;
		struct strbuf hookcmd_name = STRBUF_INIT;
		struct list_head *pos = NULL, *tmp = NULL;

		/* Check if a hookcmd with that name exists. */
		strbuf_addf(&hookcmd_name, "hookcmd.%s.command", command);
		git_config_get_value(hookcmd_name.buf, &command);

		if (!command)
			BUG("git_config_get_value overwrote a string it shouldn't have");

		/*
		 * TODO: implement an option-getting callback, e.g.
		 *   get configs by pattern hookcmd.$value.*
		 *   for each key+value, do_callback(key, value, cb_data)
		 */

		list_for_each_safe(pos, tmp, &hook_head) {
			struct hook *hook = list_entry(pos, struct hook, list);
			/*
			 * The list of hooks to run can be reordered by being redeclared
			 * in the config. Options about hook ordering should be checked
			 * here.
			 */
			if (0 == strcmp(hook->command.buf, command))
				remove_hook(pos);
		}
		emplace_hook(pos, command, 0);
	}

	return 0;
}

struct list_head* hook_list(const struct strbuf* hookname)
{
	struct strbuf hook_key = STRBUF_INIT;
	const char *legacy_hook_path = NULL;

	if (!hookname)
		return NULL;

	/* hook_head is stateful */
	clear_hook_list();

	strbuf_addf(&hook_key, "hook.%s.command", hookname->buf);

	git_config(hook_config_lookup, (void*)hook_key.buf);

	legacy_hook_path = find_hook(hookname->buf);

	/* TODO: check hook.runHookDir */
	if (legacy_hook_path)
		emplace_hook(&hook_head, legacy_hook_path, 1);

	return &hook_head;
}

int run_hooks(const char *const *env, const struct strbuf *hookname,
	      const struct strvec *args)
{
	struct list_head *to_run, *pos = NULL, *tmp = NULL;
	int rc = 0;

	to_run = hook_list(hookname);

	list_for_each_safe(pos, tmp, to_run) {
		struct child_process hook_proc = CHILD_PROCESS_INIT;
		struct hook *hook = list_entry(pos, struct hook, list);

		/* add command */
		strvec_push(&hook_proc.args, hook->command.buf);

		/*
		 * add passed-in argv, without expanding - let the user get back
		 * exactly what they put in
		 */
		if (args)
			strvec_pushv(&hook_proc.args, args->v);

		hook_proc.env = env;
		hook_proc.no_stdin = 1;
		hook_proc.stdout_to_stderr = 1;
		hook_proc.trace2_hook_name = hook->command.buf;
		hook_proc.use_shell = 1;

		rc |= run_command(&hook_proc);
	}

	return rc;
}
debug log:

solving 0dab981681 ...
found 0dab981681 in https://public-inbox.org/git/20200909004939.1942347-7-emilyshaffer@google.com/
found b006950eb8 in https://public-inbox.org/git/20200909004939.1942347-4-emilyshaffer@google.com/

applying [1/2] https://public-inbox.org/git/20200909004939.1942347-4-emilyshaffer@google.com/
diff --git a/hook.c b/hook.c
new file mode 100644
index 0000000000..b006950eb8


applying [2/2] https://public-inbox.org/git/20200909004939.1942347-7-emilyshaffer@google.com/
diff --git a/hook.c b/hook.c
index b006950eb8..0dab981681 100644

Checking patch hook.c...
Applied patch hook.c cleanly.
Checking patch hook.c...
Applied patch hook.c cleanly.

index at:
100644 0dab98168167514c098e90998513c5db94c53829	hook.c

Code repositories for project(s) associated with this 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).