Skip to content

Commit 5a7da2d

Browse files
aschrabgitster
authored andcommitted
hooks: Add function to check if a hook exists
Create find_hook() function to determine if a given hook exists and is executable. If it is, the path to the script will be returned, otherwise NULL is returned. This encapsulates the tests that are used to check for the existence of a hook in one place, making it easier to modify those checks if that is found to be necessary. This also makes it simple for places that can use a hook to check if a hook exists before doing, possibly lengthy, setup work which would be pointless if no such hook is present. The returned value is left as a static value from get_pathname() rather than a duplicate because it is anticipated that the return value will either be used as a boolean, immediately added to an argv_array list which would result in it being duplicated at that point, or used to actually run the command without much intervening work. Callers which need to hold onto the returned value for a longer time are expected to duplicate the return value themselves. Signed-off-by: Aaron Schrab <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 94702dd commit 5a7da2d

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

builtin/commit.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,6 @@ static int git_commit_config(const char *k, const char *v, void *cb)
13271327
return git_status_config(k, v, s);
13281328
}
13291329

1330-
static const char post_rewrite_hook[] = "hooks/post-rewrite";
1331-
13321330
static int run_rewrite_hook(const unsigned char *oldsha1,
13331331
const unsigned char *newsha1)
13341332
{
@@ -1339,10 +1337,10 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
13391337
int code;
13401338
size_t n;
13411339

1342-
if (access(git_path(post_rewrite_hook), X_OK) < 0)
1340+
argv[0] = find_hook("post-rewrite");
1341+
if (!argv[0])
13431342
return 0;
13441343

1345-
argv[0] = git_path(post_rewrite_hook);
13461344
argv[1] = "amend";
13471345
argv[2] = NULL;
13481346

builtin/receive-pack.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ struct command {
182182
char ref_name[FLEX_ARRAY]; /* more */
183183
};
184184

185-
static const char pre_receive_hook[] = "hooks/pre-receive";
186-
static const char post_receive_hook[] = "hooks/post-receive";
187-
188185
static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
189186
static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
190187

@@ -242,10 +239,10 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_sta
242239
const char *argv[2];
243240
int code;
244241

245-
if (access(hook_name, X_OK) < 0)
242+
argv[0] = find_hook(hook_name);
243+
if (!argv[0])
246244
return 0;
247245

248-
argv[0] = hook_name;
249246
argv[1] = NULL;
250247

251248
memset(&proc, 0, sizeof(proc));
@@ -331,15 +328,14 @@ static int run_receive_hook(struct command *commands, const char *hook_name,
331328

332329
static int run_update_hook(struct command *cmd)
333330
{
334-
static const char update_hook[] = "hooks/update";
335331
const char *argv[5];
336332
struct child_process proc;
337333
int code;
338334

339-
if (access(update_hook, X_OK) < 0)
335+
argv[0] = find_hook("update");
336+
if (!argv[0])
340337
return 0;
341338

342-
argv[0] = update_hook;
343339
argv[1] = cmd->ref_name;
344340
argv[2] = sha1_to_hex(cmd->old_sha1);
345341
argv[3] = sha1_to_hex(cmd->new_sha1);
@@ -532,24 +528,25 @@ static const char *update(struct command *cmd)
532528
}
533529
}
534530

535-
static char update_post_hook[] = "hooks/post-update";
536-
537531
static void run_update_post_hook(struct command *commands)
538532
{
539533
struct command *cmd;
540534
int argc;
541535
const char **argv;
542536
struct child_process proc;
537+
char *hook;
543538

539+
hook = find_hook("post-update");
544540
for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
545541
if (cmd->error_string || cmd->did_not_exist)
546542
continue;
547543
argc++;
548544
}
549-
if (!argc || access(update_post_hook, X_OK) < 0)
545+
if (!argc || !hook)
550546
return;
547+
551548
argv = xmalloc(sizeof(*argv) * (2 + argc));
552-
argv[0] = update_post_hook;
549+
argv[0] = hook;
553550

554551
for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
555552
char *p;
@@ -704,7 +701,7 @@ static void execute_commands(struct command *commands, const char *unpacker_erro
704701
0, &cmd))
705702
set_connectivity_errors(commands);
706703

707-
if (run_receive_hook(commands, pre_receive_hook, 0)) {
704+
if (run_receive_hook(commands, "pre-receive", 0)) {
708705
for (cmd = commands; cmd; cmd = cmd->next) {
709706
if (!cmd->error_string)
710707
cmd->error_string = "pre-receive hook declined";
@@ -994,7 +991,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
994991
unlink_or_warn(pack_lockfile);
995992
if (report_status)
996993
report(commands, unpack_status);
997-
run_receive_hook(commands, post_receive_hook, 1);
994+
run_receive_hook(commands, "post-receive", 1);
998995
run_update_post_hook(commands);
999996
if (auto_gc) {
1000997
const char *argv_gc_auto[] = {

run-command.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,15 @@ int finish_async(struct async *async)
735735
#endif
736736
}
737737

738+
char *find_hook(const char *name)
739+
{
740+
char *path = git_path("hooks/%s", name);
741+
if (access(path, X_OK) < 0)
742+
path = NULL;
743+
744+
return path;
745+
}
746+
738747
int run_hook(const char *index_file, const char *name, ...)
739748
{
740749
struct child_process hook;
@@ -744,11 +753,13 @@ int run_hook(const char *index_file, const char *name, ...)
744753
va_list args;
745754
int ret;
746755

747-
if (access(git_path("hooks/%s", name), X_OK) < 0)
756+
p = find_hook(name);
757+
if (!p)
748758
return 0;
749759

760+
argv_array_push(&argv, p);
761+
750762
va_start(args, name);
751-
argv_array_push(&argv, git_path("hooks/%s", name));
752763
while ((p = va_arg(args, const char *)))
753764
argv_array_push(&argv, p);
754765
va_end(args);

run-command.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int start_command(struct child_process *);
4545
int finish_command(struct child_process *);
4646
int run_command(struct child_process *);
4747

48+
extern char *find_hook(const char *name);
4849
extern int run_hook(const char *index_file, const char *name, ...);
4950

5051
#define RUN_COMMAND_NO_STDIN 1

0 commit comments

Comments
 (0)