Skip to content

Commit c70bc33

Browse files
committed
Merge branch 'ab/config-based-hooks-2'
More "config-based hooks". * ab/config-based-hooks-2: run-command: remove old run_hook_{le,ve}() hook API receive-pack: convert push-to-checkout hook to hook.h read-cache: convert post-index-change to use hook.h commit: convert {pre-commit,prepare-commit-msg} hook to hook.h git-p4: use 'git hook' to run hooks send-email: use 'git hook run' for 'sendemail-validate' git hook run: add an --ignore-missing flag hooks: convert worktree 'post-checkout' hook to hook library hooks: convert non-worktree 'post-checkout' hook to hook library merge: convert post-merge to use hook.h am: convert applypatch-msg to use hook.h rebase: convert pre-rebase to use hook.h hook API: add a run_hooks_l() wrapper am: convert {pre,post}-applypatch to use hook.h gc: use hook library for pre-auto-gc hook hook API: add a run_hooks() wrapper hook: add 'run' subcommand
2 parents bd75856 + 95ba86a commit c70bc33

27 files changed

+522
-158
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
/git-grep
7878
/git-hash-object
7979
/git-help
80+
/git-hook
8081
/git-http-backend
8182
/git-http-fetch
8283
/git-http-push

Documentation/git-hook.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
git-hook(1)
2+
===========
3+
4+
NAME
5+
----
6+
git-hook - Run git hooks
7+
8+
SYNOPSIS
9+
--------
10+
[verse]
11+
'git hook' run [--ignore-missing] <hook-name> [-- <hook-args>]
12+
13+
DESCRIPTION
14+
-----------
15+
16+
A command interface to running git hooks (see linkgit:githooks[5]),
17+
for use by other scripted git commands.
18+
19+
SUBCOMMANDS
20+
-----------
21+
22+
run::
23+
Run the `<hook-name>` hook. See linkgit:githooks[5] for
24+
supported hook names.
25+
+
26+
27+
Any positional arguments to the hook should be passed after a
28+
mandatory `--` (or `--end-of-options`, see linkgit:gitcli[7]). See
29+
linkgit:githooks[5] for arguments hooks might expect (if any).
30+
31+
OPTIONS
32+
-------
33+
34+
--ignore-missing::
35+
Ignore any missing hook by quietly returning zero. Used for
36+
tools that want to do a blind one-shot run of a hook that may
37+
or may not be present.
38+
39+
SEE ALSO
40+
--------
41+
linkgit:githooks[5]
42+
43+
GIT
44+
---
45+
Part of the linkgit:git[1] suite

Documentation/githooks.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,10 @@ and "0" meaning they were not.
698698
Only one parameter should be set to "1" when the hook runs. The hook
699699
running passing "1", "1" should not be possible.
700700

701+
SEE ALSO
702+
--------
703+
linkgit:git-hook[1]
704+
701705
GIT
702706
---
703707
Part of the linkgit:git[1] suite

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,7 @@ BUILTIN_OBJS += builtin/get-tar-commit-id.o
11091109
BUILTIN_OBJS += builtin/grep.o
11101110
BUILTIN_OBJS += builtin/hash-object.o
11111111
BUILTIN_OBJS += builtin/help.o
1112+
BUILTIN_OBJS += builtin/hook.o
11121113
BUILTIN_OBJS += builtin/index-pack.o
11131114
BUILTIN_OBJS += builtin/init-db.o
11141115
BUILTIN_OBJS += builtin/interpret-trailers.o

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix);
164164
int cmd_grep(int argc, const char **argv, const char *prefix);
165165
int cmd_hash_object(int argc, const char **argv, const char *prefix);
166166
int cmd_help(int argc, const char **argv, const char *prefix);
167+
int cmd_hook(int argc, const char **argv, const char *prefix);
167168
int cmd_index_pack(int argc, const char **argv, const char *prefix);
168169
int cmd_init_db(int argc, const char **argv, const char *prefix);
169170
int cmd_interpret_trailers(int argc, const char **argv, const char *prefix);

builtin/am.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ static int run_applypatch_msg_hook(struct am_state *state)
474474
int ret;
475475

476476
assert(state->msg);
477-
ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL);
477+
ret = run_hooks_l("applypatch-msg", am_path(state, "final-commit"), NULL);
478478

479479
if (!ret) {
480480
FREE_AND_NULL(state->msg);
@@ -1636,7 +1636,7 @@ static void do_commit(const struct am_state *state)
16361636
const char *reflog_msg, *author, *committer = NULL;
16371637
struct strbuf sb = STRBUF_INIT;
16381638

1639-
if (run_hook_le(NULL, "pre-applypatch", NULL))
1639+
if (run_hooks("pre-applypatch"))
16401640
exit(1);
16411641

16421642
if (write_cache_as_tree(&tree, 0, NULL))
@@ -1688,7 +1688,7 @@ static void do_commit(const struct am_state *state)
16881688
fclose(fp);
16891689
}
16901690

1691-
run_hook_le(NULL, "post-applypatch", NULL);
1691+
run_hooks("post-applypatch");
16921692

16931693
strbuf_release(&sb);
16941694
}

builtin/checkout.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "config.h"
1010
#include "diff.h"
1111
#include "dir.h"
12+
#include "hook.h"
1213
#include "ll-merge.h"
1314
#include "lockfile.h"
1415
#include "merge-recursive.h"
@@ -114,7 +115,7 @@ static void branch_info_release(struct branch_info *info)
114115
static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit,
115116
int changed)
116117
{
117-
return run_hook_le(NULL, "post-checkout",
118+
return run_hooks_l("post-checkout",
118119
oid_to_hex(old_commit ? &old_commit->object.oid : null_oid()),
119120
oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()),
120121
changed ? "1" : "0", NULL);

builtin/clone.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "connected.h"
3333
#include "packfile.h"
3434
#include "list-objects-filter-options.h"
35+
#include "hook.h"
3536

3637
/*
3738
* Overall FIXMEs:
@@ -705,7 +706,7 @@ static int checkout(int submodule_progress)
705706
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
706707
die(_("unable to write new index file"));
707708

708-
err |= run_hook_le(NULL, "post-checkout", oid_to_hex(null_oid()),
709+
err |= run_hooks_l("post-checkout", oid_to_hex(null_oid()),
709710
oid_to_hex(&oid), "1", NULL);
710711

711712
if (!err && (option_recurse_submodules.nr > 0)) {

builtin/gc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "remote.h"
3333
#include "object-store.h"
3434
#include "exec-cmd.h"
35+
#include "hook.h"
3536

3637
#define FAILED_RUN "failed to run %s"
3738

@@ -394,7 +395,7 @@ static int need_to_gc(void)
394395
else
395396
return 0;
396397

397-
if (run_hook_le(NULL, "pre-auto-gc", NULL))
398+
if (run_hooks("pre-auto-gc"))
398399
return 0;
399400
return 1;
400401
}

builtin/hook.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "cache.h"
2+
#include "builtin.h"
3+
#include "config.h"
4+
#include "hook.h"
5+
#include "parse-options.h"
6+
#include "strbuf.h"
7+
#include "strvec.h"
8+
9+
#define BUILTIN_HOOK_RUN_USAGE \
10+
N_("git hook run [--ignore-missing] <hook-name> [-- <hook-args>]")
11+
12+
static const char * const builtin_hook_usage[] = {
13+
BUILTIN_HOOK_RUN_USAGE,
14+
NULL
15+
};
16+
17+
static const char * const builtin_hook_run_usage[] = {
18+
BUILTIN_HOOK_RUN_USAGE,
19+
NULL
20+
};
21+
22+
static int run(int argc, const char **argv, const char *prefix)
23+
{
24+
int i;
25+
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
26+
int ignore_missing = 0;
27+
const char *hook_name;
28+
struct option run_options[] = {
29+
OPT_BOOL(0, "ignore-missing", &ignore_missing,
30+
N_("silently ignore missing requested <hook-name>")),
31+
OPT_END(),
32+
};
33+
int ret;
34+
35+
argc = parse_options(argc, argv, prefix, run_options,
36+
builtin_hook_run_usage,
37+
PARSE_OPT_KEEP_DASHDASH);
38+
39+
if (!argc)
40+
goto usage;
41+
42+
/*
43+
* Having a -- for "run" when providing <hook-args> is
44+
* mandatory.
45+
*/
46+
if (argc > 1 && strcmp(argv[1], "--") &&
47+
strcmp(argv[1], "--end-of-options"))
48+
goto usage;
49+
50+
/* Add our arguments, start after -- */
51+
for (i = 2 ; i < argc; i++)
52+
strvec_push(&opt.args, argv[i]);
53+
54+
/* Need to take into account core.hooksPath */
55+
git_config(git_default_config, NULL);
56+
57+
hook_name = argv[0];
58+
if (!ignore_missing)
59+
opt.error_if_missing = 1;
60+
ret = run_hooks_opt(hook_name, &opt);
61+
if (ret < 0) /* error() return */
62+
ret = 1;
63+
return ret;
64+
usage:
65+
usage_with_options(builtin_hook_run_usage, run_options);
66+
}
67+
68+
int cmd_hook(int argc, const char **argv, const char *prefix)
69+
{
70+
struct option builtin_hook_options[] = {
71+
OPT_END(),
72+
};
73+
74+
argc = parse_options(argc, argv, NULL, builtin_hook_options,
75+
builtin_hook_usage, PARSE_OPT_STOP_AT_NON_OPTION);
76+
if (!argc)
77+
goto usage;
78+
79+
if (!strcmp(argv[0], "run"))
80+
return run(argc, argv, prefix);
81+
82+
usage:
83+
usage_with_options(builtin_hook_usage, builtin_hook_options);
84+
}

0 commit comments

Comments
 (0)