hooks: remove implicit dependency on `the_repository`

We implicitly depend on `the_repository` in our hook subsystem because we use `strbuf_git_path()` to compute hook paths. Remove this dependency by accepting a `struct repository` as parameter instead. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 13, 2024 at 11:13 UTC 169c979771158ddc3310f7a1e0e141fd7d0ed088
18 files changed +47 -42
builtin/am.c
+5 -4
@@ -490,7 +490,8 @@ static int run_applypatch_msg_hook(struct am_state *state)
490 assert(state->msg);
491
492 if (!state->no_verify)
493 - ret = run_hooks_l("applypatch-msg", am_path(state, "final-commit"), NULL);
493 + ret = run_hooks_l(the_repository, "applypatch-msg",
494 + am_path(state, "final-commit"), NULL);
495
496 if (!ret) {
497 FREE_AND_NULL(state->msg);
@@ -512,7 +513,7 @@ static int run_post_rewrite_hook(const struct am_state *state)
513 strvec_push(&opt.args, "rebase");
514 opt.path_to_stdin = am_path(state, "rewritten");
515
515 - return run_hooks_opt("post-rewrite", &opt);
516 + return run_hooks_opt(the_repository, "post-rewrite", &opt);
517 }
518
519 /**
@@ -1663,7 +1664,7 @@ static void do_commit(const struct am_state *state)
1664 const char *reflog_msg, *author, *committer = NULL;
1665 struct strbuf sb = STRBUF_INIT;
1666
1666 - if (!state->no_verify && run_hooks("pre-applypatch"))
1667 + if (!state->no_verify && run_hooks(the_repository, "pre-applypatch"))
1668 exit(1);
1669
1670 if (write_index_as_tree(&tree, the_repository->index, get_index_file(), 0, NULL))
@@ -1716,7 +1717,7 @@ static void do_commit(const struct am_state *state)
1717 fclose(fp);
1718 }
1719
1719 - run_hooks("post-applypatch");
1720 + run_hooks(the_repository, "post-applypatch");
1721
1722 free_commit_list(parents);
1723 strbuf_release(&sb);
builtin/bugreport.c
+1 -1
@@ -58,7 +58,7 @@ static void get_populated_hooks(struct strbuf *hook_info, int nongit)
58 for (p = hook_name_list; *p; p++) {
59 const char *hook = *p;
60
61 - if (hook_exists(hook))
61 + if (hook_exists(the_repository, hook))
62 strbuf_addf(hook_info, "%s\n", hook);
63 }
64 }
builtin/checkout.c
+1 -1
@@ -125,7 +125,7 @@ static void branch_info_release(struct branch_info *info)
125 static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit,
126 int changed)
127 {
128 - return run_hooks_l("post-checkout",
128 + return run_hooks_l(the_repository, "post-checkout",
129 oid_to_hex(old_commit ? &old_commit->object.oid : null_oid()),
130 oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()),
131 changed ? "1" : "0", NULL);
builtin/clone.c
+1 -1
@@ -788,7 +788,7 @@ static int checkout(int submodule_progress, int filter_submodules)
788 if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
789 die(_("unable to write new index file"));
790
791 - err |= run_hooks_l("post-checkout", oid_to_hex(null_oid()),
791 + err |= run_hooks_l(the_repository, "post-checkout", oid_to_hex(null_oid()),
792 oid_to_hex(&oid), "1", NULL);
793
794 if (!err && (option_recurse_submodules.nr > 0)) {
builtin/gc.c
+1 -1
@@ -463,7 +463,7 @@ static int need_to_gc(void)
463 else
464 return 0;
465
466 - if (run_hooks("pre-auto-gc"))
466 + if (run_hooks(the_repository, "pre-auto-gc"))
467 return 0;
468 return 1;
469 }
builtin/hook.c
+1 -1
@@ -58,7 +58,7 @@ static int run(int argc, const char **argv, const char *prefix)
58 hook_name = argv[0];
59 if (!ignore_missing)
60 opt.error_if_missing = 1;
61 - ret = run_hooks_opt(hook_name, &opt);
61 + ret = run_hooks_opt(the_repository, hook_name, &opt);
62 if (ret < 0) /* error() return */
63 ret = 1;
64 return ret;
builtin/merge.c
+1 -1
@@ -478,7 +478,7 @@ static void finish(struct commit *head_commit,
478 }
479
480 /* Run a post-merge hook */
481 - run_hooks_l("post-merge", squash ? "1" : "0", NULL);
481 + run_hooks_l(the_repository, "post-merge", squash ? "1" : "0", NULL);
482
483 if (new_head)
484 apply_autostash_ref(the_repository, "MERGE_AUTOSTASH");
builtin/rebase.c
+1 -1
@@ -1774,7 +1774,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1774
1775 /* If a hook exists, give it a chance to interrupt*/
1776 if (!ok_to_skip_pre_rebase &&
1777 - run_hooks_l("pre-rebase", options.upstream_arg,
1777 + run_hooks_l(the_repository, "pre-rebase", options.upstream_arg,
1778 argc ? argv[0] : NULL, NULL))
1779 die(_("The pre-rebase hook refused to rebase."));
1780
builtin/receive-pack.c
+5 -5
@@ -792,7 +792,7 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,
792 struct child_process proc = CHILD_PROCESS_INIT;
793 struct async muxer;
794 int code;
795 - const char *hook_path = find_hook(hook_name);
795 + const char *hook_path = find_hook(the_repository, hook_name);
796
797 if (!hook_path)
798 return 0;
@@ -922,7 +922,7 @@ static int run_update_hook(struct command *cmd)
922 {
923 struct child_process proc = CHILD_PROCESS_INIT;
924 int code;
925 - const char *hook_path = find_hook("update");
925 + const char *hook_path = find_hook(the_repository, "update");
926
927 if (!hook_path)
928 return 0;
@@ -1098,7 +1098,7 @@ static int run_proc_receive_hook(struct command *commands,
1098 int hook_use_push_options = 0;
1099 int version = 0;
1100 int code;
1101 - const char *hook_path = find_hook("proc-receive");
1101 + const char *hook_path = find_hook(the_repository, "proc-receive");
1102
1103 if (!hook_path) {
1104 rp_error("cannot find hook 'proc-receive'");
@@ -1409,7 +1409,7 @@ static const char *push_to_checkout(unsigned char *hash,
1409 strvec_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
1410 strvec_pushv(&opt.env, env->v);
1411 strvec_push(&opt.args, hash_to_hex(hash));
1412 - if (run_hooks_opt(push_to_checkout_hook, &opt))
1412 + if (run_hooks_opt(the_repository, push_to_checkout_hook, &opt))
1413 return "push-to-checkout hook declined";
1414 else
1415 return NULL;
@@ -1618,7 +1618,7 @@ static void run_update_post_hook(struct command *commands)
1618 struct child_process proc = CHILD_PROCESS_INIT;
1619 const char *hook;
1620
1621 - hook = find_hook("post-update");
1621 + hook = find_hook(the_repository, "post-update");
1622 if (!hook)
1623 return;
1624
builtin/worktree.c
+1 -1
@@ -573,7 +573,7 @@ done:
573 NULL);
574 opt.dir = path;
575
576 - ret = run_hooks_opt("post-checkout", &opt);
576 + ret = run_hooks_opt(the_repository, "post-checkout", &opt);
577 }
578
579 strvec_clear(&child_env);
commit.c
+1 -1
@@ -1960,5 +1960,5 @@ int run_commit_hook(int editor_is_used, const char *index_file,
1960 va_end(args);
1961
1962 opt.invoked_hook = invoked_hook;
1963 - return run_hooks_opt(name, &opt);
1963 + return run_hooks_opt(the_repository, name, &opt);
1964 }
hook.c
+11 -10
@@ -10,14 +10,14 @@
10 #include "environment.h"
11 #include "setup.h"
12
13 -const char *find_hook(const char *name)
13 +const char *find_hook(struct repository *r, const char *name)
14 {
15 static struct strbuf path = STRBUF_INIT;
16
17 int found_hook;
18
19 strbuf_reset(&path);
20 - strbuf_git_path(&path, "hooks/%s", name);
20 + strbuf_repo_git_path(&path, r, "hooks/%s", name);
21 found_hook = access(path.buf, X_OK) >= 0;
22 #ifdef STRIP_EXTENSION
23 if (!found_hook) {
@@ -48,9 +48,9 @@ const char *find_hook(const char *name)
48 return path.buf;
49 }
50
51 -int hook_exists(const char *name)
51 +int hook_exists(struct repository *r, const char *name)
52 {
53 - return !!find_hook(name);
53 + return !!find_hook(r, name);
54 }
55
56 static int pick_next_hook(struct child_process *cp,
@@ -121,7 +121,8 @@ static void run_hooks_opt_clear(struct run_hooks_opt *options)
121 strvec_clear(&options->args);
122 }
123
124 -int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
124 +int run_hooks_opt(struct repository *r, const char *hook_name,
125 + struct run_hooks_opt *options)
126 {
127 struct strbuf abs_path = STRBUF_INIT;
128 struct hook_cb_data cb_data = {
@@ -129,7 +130,7 @@ int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
130 .hook_name = hook_name,
131 .options = options,
132 };
132 - const char *const hook_path = find_hook(hook_name);
133 + const char *const hook_path = find_hook(r, hook_name);
134 int ret = 0;
135 const struct run_process_parallel_opts opts = {
136 .tr2_category = "hook",
@@ -173,14 +174,14 @@ cleanup:
174 return ret;
175 }
176
176 -int run_hooks(const char *hook_name)
177 +int run_hooks(struct repository *r, const char *hook_name)
178 {
179 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
180
180 - return run_hooks_opt(hook_name, &opt);
181 + return run_hooks_opt(r, hook_name, &opt);
182 }
183
183 -int run_hooks_l(const char *hook_name, ...)
184 +int run_hooks_l(struct repository *r, const char *hook_name, ...)
185 {
186 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
187 va_list ap;
@@ -191,5 +192,5 @@ int run_hooks_l(const char *hook_name, ...)
192 strvec_push(&opt.args, arg);
193 va_end(ap);
194
194 - return run_hooks_opt(hook_name, &opt);
195 + return run_hooks_opt(r, hook_name, &opt);
196 }
hook.h
+8 -5
@@ -2,6 +2,8 @@
2 #define HOOK_H
3 #include "strvec.h"
4
5 +struct repository;
6 +
7 struct run_hooks_opt
8 {
9 /* Environment vars to be set for each hook */
@@ -55,12 +57,12 @@ struct hook_cb_data {
57 * or disabled. Note that this points to static storage that will be
58 * overwritten by further calls to find_hook and run_hook_*.
59 */
58 -const char *find_hook(const char *name);
60 +const char *find_hook(struct repository *r, const char *name);
61
62 /**
63 * A boolean version of find_hook()
64 */
63 -int hook_exists(const char *hookname);
65 +int hook_exists(struct repository *r, const char *hookname);
66
67 /**
68 * Takes a `hook_name`, resolves it to a path with find_hook(), and
@@ -70,13 +72,14 @@ int hook_exists(const char *hookname);
72 * Returns the status code of the run hook, or a negative value on
73 * error().
74 */
73 -int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options);
75 +int run_hooks_opt(struct repository *r, const char *hook_name,
76 + struct run_hooks_opt *options);
77
78 /**
79 * A wrapper for run_hooks_opt() which provides a dummy "struct
80 * run_hooks_opt" initialized with "RUN_HOOKS_OPT_INIT".
81 */
79 -int run_hooks(const char *hook_name);
82 +int run_hooks(struct repository *r, const char *hook_name);
83
84 /**
85 * Like run_hooks(), a wrapper for run_hooks_opt().
@@ -87,5 +90,5 @@ int run_hooks(const char *hook_name);
90 * hook. This function behaves like the old run_hook_le() API.
91 */
92 LAST_ARG_MUST_BE_NULL
90 -int run_hooks_l(const char *hook_name, ...);
93 +int run_hooks_l(struct repository *r, const char *hook_name, ...);
94 #endif
read-cache.c
+3 -3
@@ -3156,9 +3156,9 @@ static int do_write_locked_index(struct index_state *istate,
3156 else
3157 ret = close_lock_file_gently(lock);
3158
3159 - run_hooks_l("post-index-change",
3160 - istate->updated_workdir ? "1" : "0",
3161 - istate->updated_skipworktree ? "1" : "0", NULL);
3159 + run_hooks_l(the_repository, "post-index-change",
3160 + istate->updated_workdir ? "1" : "0",
3161 + istate->updated_skipworktree ? "1" : "0", NULL);
3162 istate->updated_workdir = 0;
3163 istate->updated_skipworktree = 0;
3164
refs.c
+1 -1
@@ -2132,7 +2132,7 @@ static int run_transaction_hook(struct ref_transaction *transaction,
2132 const char *hook;
2133 int ret = 0, i;
2134
2135 - hook = find_hook("reference-transaction");
2135 + hook = find_hook(transaction->ref_store->repo, "reference-transaction");
2136 if (!hook)
2137 return ret;
2138
reset.c
+1 -1
@@ -79,7 +79,7 @@ static int update_refs(const struct reset_head_opts *opts,
79 reflog_head);
80 }
81 if (!ret && run_hook)
82 - run_hooks_l("post-checkout",
82 + run_hooks_l(the_repository, "post-checkout",
83 oid_to_hex(head ? head : null_oid()),
84 oid_to_hex(oid), "1", NULL);
85 strbuf_release(&msg);
sequencer.c
+3 -3
@@ -1316,7 +1316,7 @@ static int run_rewrite_hook(const struct object_id *oldoid,
1316 struct child_process proc = CHILD_PROCESS_INIT;
1317 int code;
1318 struct strbuf sb = STRBUF_INIT;
1319 - const char *hook_path = find_hook("post-rewrite");
1319 + const char *hook_path = find_hook(the_repository, "post-rewrite");
1320
1321 if (!hook_path)
1322 return 0;
@@ -1614,7 +1614,7 @@ static int try_to_commit(struct repository *r,
1614 }
1615 }
1616
1617 - if (hook_exists("prepare-commit-msg")) {
1617 + if (hook_exists(r, "prepare-commit-msg")) {
1618 res = run_prepare_commit_msg_hook(r, msg, hook_commit);
1619 if (res)
1620 goto out;
@@ -5149,7 +5149,7 @@ cleanup_head_ref:
5149
5150 hook_opt.path_to_stdin = rebase_path_rewritten_list();
5151 strvec_push(&hook_opt.args, "rebase");
5152 - run_hooks_opt("post-rewrite", &hook_opt);
5152 + run_hooks_opt(r, "post-rewrite", &hook_opt);
5153 }
5154 apply_autostash(rebase_path_autostash());
5155
transport.c
+1 -1
@@ -1271,7 +1271,7 @@ static int run_pre_push_hook(struct transport *transport,
1271 struct ref *r;
1272 struct child_process proc = CHILD_PROCESS_INIT;
1273 struct strbuf buf;
1274 - const char *hook_path = find_hook("pre-push");
1274 + const char *hook_path = find_hook(the_repository, "pre-push");
1275
1276 if (!hook_path)
1277 return 0;