git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
blob bfdc79e2f1a4351d9a350c6737bc142e08f2cdbc 3362 bytes (raw)
name: hook.c 	 # note: path name is non-authoritative(*)

  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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
 
#include "cache.h"
#include "hook.h"
#include "run-command.h"
#include "config.h"

const char *find_hook(const char *name)
{
	static struct strbuf path = STRBUF_INIT;

	strbuf_reset(&path);
	strbuf_git_path(&path, "hooks/%s", name);
	if (access(path.buf, X_OK) < 0) {
		int err = errno;

#ifdef STRIP_EXTENSION
		strbuf_addstr(&path, STRIP_EXTENSION);
		if (access(path.buf, X_OK) >= 0)
			return path.buf;
		if (errno == EACCES)
			err = errno;
#endif

		if (err == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
			static struct string_list advise_given = STRING_LIST_INIT_DUP;

			if (!string_list_lookup(&advise_given, name)) {
				string_list_insert(&advise_given, name);
				advise(_("The '%s' hook was ignored because "
					 "it's not set as executable.\n"
					 "You can disable this warning with "
					 "`git config advice.ignoredHook false`."),
				       path.buf);
			}
		}
		return NULL;
	}
	return path.buf;
}

int hook_exists(const char *name)
{
	return !!find_hook(name);
}

void run_hooks_opt_clear(struct run_hooks_opt *o)
{
	strvec_clear(&o->env);
	strvec_clear(&o->args);
}

static int pick_next_hook(struct child_process *cp,
			  struct strbuf *out,
			  void *pp_cb,
			  void **pp_task_cb)
{
	struct hook_cb_data *hook_cb = pp_cb;
	struct hook *run_me = hook_cb->run_me;

	if (!run_me)
		return 0;

	cp->no_stdin = 1;
	cp->env = hook_cb->options->env.v;
	cp->stdout_to_stderr = 1;
	cp->trace2_hook_name = hook_cb->hook_name;

	/* add command */
	strvec_push(&cp->args, run_me->hook_path);

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

	/* Provide context for errors if necessary */
	*pp_task_cb = run_me;

	/*
	 * This pick_next_hook() will be called again, we're only
	 * running one hook, so indicate that no more work will be
	 * done.
	 */
	hook_cb->run_me = NULL;

	return 1;
}

static int notify_start_failure(struct strbuf *out,
				void *pp_cb,
				void *pp_task_cp)
{
	struct hook_cb_data *hook_cb = pp_cb;
	struct hook *attempted = pp_task_cp;

	hook_cb->rc |= 1;

	strbuf_addf(out, _("Couldn't start hook '%s'\n"),
		    attempted->hook_path);

	return 1;
}

static int notify_hook_finished(int result,
				struct strbuf *out,
				void *pp_cb,
				void *pp_task_cb)
{
	struct hook_cb_data *hook_cb = pp_cb;

	hook_cb->rc |= result;

	return 0;
}

int run_hooks(const char *hook_name, const char *hook_path,
	      struct run_hooks_opt *options)
{
	struct hook my_hook = {
		.hook_path = hook_path,
	};
	struct hook_cb_data cb_data = {
		.rc = 0,
		.hook_name = hook_name,
		.options = options,
	};
	int jobs = 1;

	if (!options)
		BUG("a struct run_hooks_opt must be provided to run_hooks");

	cb_data.run_me = &my_hook;

	run_processes_parallel_tr2(jobs,
				   pick_next_hook,
				   notify_start_failure,
				   notify_hook_finished,
				   &cb_data,
				   "hook",
				   hook_name);

	return cb_data.rc;
}

int run_hooks_oneshot(const char *hook_name, struct run_hooks_opt *options)
{
	const char *hook_path;
	struct run_hooks_opt hook_opt_scratch = RUN_HOOKS_OPT_INIT;
	int ret = 0;

	if (!options)
		options = &hook_opt_scratch;

	hook_path = find_hook(hook_name);
	if (!hook_path)
		goto cleanup;

	ret = run_hooks(hook_name, hook_path, options);
cleanup:
	run_hooks_opt_clear(options);

	return ret;
}

debug log:

solving bfdc79e2f1a ...
found bfdc79e2f1a in https://public-inbox.org/git/patch-02.13-dbac4204f7b-20211012T131934Z-avarab@gmail.com/
found 240270db64e in https://public-inbox.org/git/patch-01.13-a39c0748d3f-20211012T131934Z-avarab@gmail.com/
found 55e1145a4b7 in https://80x24.org/mirrors/git.git
preparing index
index prepared:
100644 55e1145a4b7b8c59276f082ab0b240817702ace4	hook.c

applying [1/2] https://public-inbox.org/git/patch-01.13-a39c0748d3f-20211012T131934Z-avarab@gmail.com/
diff --git a/hook.c b/hook.c
index 55e1145a4b7..240270db64e 100644


applying [2/2] https://public-inbox.org/git/patch-02.13-dbac4204f7b-20211012T131934Z-avarab@gmail.com/
diff --git a/hook.c b/hook.c
index 240270db64e..bfdc79e2f1a 100644

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

index at:
100644 bfdc79e2f1a4351d9a350c6737bc142e08f2cdbc	hook.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

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).