Revert "fsck: warn about symlink pointing inside a gitdir"
[alt-git.git] / hook.c
blob22b274b60b1949c906f8ff32048ff11e82c2c840
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 int found_hook;
12 strbuf_reset(&path);
13 strbuf_git_path(&path, "hooks/%s", name);
14 found_hook = access(path.buf, X_OK) >= 0;
15 #ifdef STRIP_EXTENSION
16 if (!found_hook) {
17 int err = errno;
19 strbuf_addstr(&path, STRIP_EXTENSION);
20 found_hook = access(path.buf, X_OK) >= 0;
21 if (!found_hook)
22 errno = err;
24 #endif
26 if (!found_hook) {
27 if (errno == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
28 static struct string_list advise_given = STRING_LIST_INIT_DUP;
30 if (!string_list_lookup(&advise_given, name)) {
31 string_list_insert(&advise_given, name);
32 advise(_("The '%s' hook was ignored because "
33 "it's not set as executable.\n"
34 "You can disable this warning with "
35 "`git config advice.ignoredHook false`."),
36 path.buf);
39 return NULL;
41 return path.buf;
44 int hook_exists(const char *name)
46 return !!find_hook(name);
49 static int pick_next_hook(struct child_process *cp,
50 struct strbuf *out,
51 void *pp_cb,
52 void **pp_task_cb)
54 struct hook_cb_data *hook_cb = pp_cb;
55 const char *hook_path = hook_cb->hook_path;
57 if (!hook_path)
58 return 0;
60 cp->no_stdin = 1;
61 strvec_pushv(&cp->env, hook_cb->options->env.v);
62 cp->stdout_to_stderr = 1;
63 cp->trace2_hook_name = hook_cb->hook_name;
64 cp->dir = hook_cb->options->dir;
66 strvec_push(&cp->args, hook_path);
67 strvec_pushv(&cp->args, hook_cb->options->args.v);
70 * This pick_next_hook() will be called again, we're only
71 * running one hook, so indicate that no more work will be
72 * done.
74 hook_cb->hook_path = NULL;
76 return 1;
79 static int notify_start_failure(struct strbuf *out,
80 void *pp_cb,
81 void *pp_task_cp)
83 struct hook_cb_data *hook_cb = pp_cb;
85 hook_cb->rc |= 1;
87 return 1;
90 static int notify_hook_finished(int result,
91 struct strbuf *out,
92 void *pp_cb,
93 void *pp_task_cb)
95 struct hook_cb_data *hook_cb = pp_cb;
96 struct run_hooks_opt *opt = hook_cb->options;
98 hook_cb->rc |= result;
100 if (opt->invoked_hook)
101 *opt->invoked_hook = 1;
103 return 0;
106 static void run_hooks_opt_clear(struct run_hooks_opt *options)
108 strvec_clear(&options->env);
109 strvec_clear(&options->args);
112 int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
114 struct strbuf abs_path = STRBUF_INIT;
115 struct hook_cb_data cb_data = {
116 .rc = 0,
117 .hook_name = hook_name,
118 .options = options,
120 const char *const hook_path = find_hook(hook_name);
121 int ret = 0;
122 const struct run_process_parallel_opts opts = {
123 .tr2_category = "hook",
124 .tr2_label = hook_name,
126 .processes = 1,
127 .ungroup = 1,
129 .get_next_task = pick_next_hook,
130 .start_failure = notify_start_failure,
131 .task_finished = notify_hook_finished,
133 .data = &cb_data,
136 if (!options)
137 BUG("a struct run_hooks_opt must be provided to run_hooks");
139 if (options->invoked_hook)
140 *options->invoked_hook = 0;
142 if (!hook_path && !options->error_if_missing)
143 goto cleanup;
145 if (!hook_path) {
146 ret = error("cannot find a hook named %s", hook_name);
147 goto cleanup;
150 cb_data.hook_path = hook_path;
151 if (options->dir) {
152 strbuf_add_absolute_path(&abs_path, hook_path);
153 cb_data.hook_path = abs_path.buf;
156 run_processes_parallel(&opts);
157 ret = cb_data.rc;
158 cleanup:
159 strbuf_release(&abs_path);
160 run_hooks_opt_clear(options);
161 return ret;
164 int run_hooks(const char *hook_name)
166 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
168 return run_hooks_opt(hook_name, &opt);
171 int run_hooks_l(const char *hook_name, ...)
173 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
174 va_list ap;
175 const char *arg;
177 va_start(ap, hook_name);
178 while ((arg = va_arg(ap, const char *)))
179 strvec_push(&opt.args, arg);
180 va_end(ap);
182 return run_hooks_opt(hook_name, &opt);