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;
}
|