l10n: sv.po: Update Swedish translation (5484t0f0u)
[alt-git.git] / hook.c
bloba493939a4fc5901d85a9ad08ca10669641598389
1 #include "cache.h"
2 #include "hook.h"
3 #include "run-command.h"
4 #include "config.h"
6 const char *find_hook(const char *name)
8 static struct strbuf path = STRBUF_INIT;
10 strbuf_reset(&path);
11 strbuf_git_path(&path, "hooks/%s", name);
12 if (access(path.buf, X_OK) < 0) {
13 int err = errno;
15 #ifdef STRIP_EXTENSION
16 strbuf_addstr(&path, STRIP_EXTENSION);
17 if (access(path.buf, X_OK) >= 0)
18 return path.buf;
19 if (errno == EACCES)
20 err = errno;
21 #endif
23 if (err == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
24 static struct string_list advise_given = STRING_LIST_INIT_DUP;
26 if (!string_list_lookup(&advise_given, name)) {
27 string_list_insert(&advise_given, name);
28 advise(_("The '%s' hook was ignored because "
29 "it's not set as executable.\n"
30 "You can disable this warning with "
31 "`git config advice.ignoredHook false`."),
32 path.buf);
35 return NULL;
37 return path.buf;
40 int hook_exists(const char *name)
42 return !!find_hook(name);
45 static int pick_next_hook(struct child_process *cp,
46 struct strbuf *out,
47 void *pp_cb,
48 void **pp_task_cb)
50 struct hook_cb_data *hook_cb = pp_cb;
51 const char *hook_path = hook_cb->hook_path;
53 if (!hook_path)
54 return 0;
56 cp->no_stdin = 1;
57 strvec_pushv(&cp->env, hook_cb->options->env.v);
58 cp->stdout_to_stderr = 1;
59 cp->trace2_hook_name = hook_cb->hook_name;
60 cp->dir = hook_cb->options->dir;
62 strvec_push(&cp->args, hook_path);
63 strvec_pushv(&cp->args, hook_cb->options->args.v);
66 * This pick_next_hook() will be called again, we're only
67 * running one hook, so indicate that no more work will be
68 * done.
70 hook_cb->hook_path = NULL;
72 return 1;
75 static int notify_start_failure(struct strbuf *out,
76 void *pp_cb,
77 void *pp_task_cp)
79 struct hook_cb_data *hook_cb = pp_cb;
81 hook_cb->rc |= 1;
83 return 1;
86 static int notify_hook_finished(int result,
87 struct strbuf *out,
88 void *pp_cb,
89 void *pp_task_cb)
91 struct hook_cb_data *hook_cb = pp_cb;
92 struct run_hooks_opt *opt = hook_cb->options;
94 hook_cb->rc |= result;
96 if (opt->invoked_hook)
97 *opt->invoked_hook = 1;
99 return 0;
102 static void run_hooks_opt_clear(struct run_hooks_opt *options)
104 strvec_clear(&options->env);
105 strvec_clear(&options->args);
108 int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
110 struct strbuf abs_path = STRBUF_INIT;
111 struct hook_cb_data cb_data = {
112 .rc = 0,
113 .hook_name = hook_name,
114 .options = options,
116 const char *const hook_path = find_hook(hook_name);
117 int jobs = 1;
118 int ret = 0;
120 if (!options)
121 BUG("a struct run_hooks_opt must be provided to run_hooks");
123 if (options->invoked_hook)
124 *options->invoked_hook = 0;
126 if (!hook_path && !options->error_if_missing)
127 goto cleanup;
129 if (!hook_path) {
130 ret = error("cannot find a hook named %s", hook_name);
131 goto cleanup;
134 cb_data.hook_path = hook_path;
135 if (options->dir) {
136 strbuf_add_absolute_path(&abs_path, hook_path);
137 cb_data.hook_path = abs_path.buf;
140 run_processes_parallel_ungroup = 1;
141 run_processes_parallel_tr2(jobs,
142 pick_next_hook,
143 notify_start_failure,
144 notify_hook_finished,
145 &cb_data,
146 "hook",
147 hook_name);
148 ret = cb_data.rc;
149 cleanup:
150 strbuf_release(&abs_path);
151 run_hooks_opt_clear(options);
152 return ret;
155 int run_hooks(const char *hook_name)
157 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
159 return run_hooks_opt(hook_name, &opt);
162 int run_hooks_l(const char *hook_name, ...)
164 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
165 va_list ap;
166 const char *arg;
168 va_start(ap, hook_name);
169 while ((arg = va_arg(ap, const char *)))
170 strvec_push(&opt.args, arg);
171 va_end(ap);
173 return run_hooks_opt(hook_name, &opt);