Skip to content

Commit 2c62320

Browse files
nasamuffingitster
authored andcommitted
hook: introduce "git hook list"
If more than one hook will be run, it may be useful to see a list of which hooks should be run. At very least, it will be useful for us to test the semantics of multihooks ourselves. For now, only list the hooks which will run in the order they will run in; later, it might be useful to include more information like where the hooks were configured and whether or not they will run. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 04a56ae commit 2c62320

5 files changed

Lines changed: 77 additions & 15 deletions

File tree

Documentation/git-hook.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git hook' run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]
12+
'git hook' list <hook-name>
1213

1314
DESCRIPTION
1415
-----------
@@ -28,6 +29,10 @@ Any positional arguments to the hook should be passed after a
2829
mandatory `--` (or `--end-of-options`, see linkgit:gitcli[7]). See
2930
linkgit:githooks[5] for arguments hooks might expect (if any).
3031

32+
list::
33+
Print a list of hooks which will be run on `<hook-name>` event. If no
34+
hooks are configured for that event, print nothing and return 1.
35+
3136
OPTIONS
3237
-------
3338

builtin/hook.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
#include "hook.h"
77
#include "parse-options.h"
88
#include "strvec.h"
9+
#include "abspath.h"
910

1011
#define BUILTIN_HOOK_RUN_USAGE \
1112
N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
13+
#define BUILTIN_HOOK_LIST_USAGE \
14+
N_("git hook list <hook-name>")
1215

1316
static const char * const builtin_hook_usage[] = {
1417
BUILTIN_HOOK_RUN_USAGE,
18+
BUILTIN_HOOK_LIST_USAGE,
1519
NULL
1620
};
1721

@@ -20,6 +24,54 @@ static const char * const builtin_hook_run_usage[] = {
2024
NULL
2125
};
2226

27+
static const char *const builtin_hook_list_usage[] = {
28+
BUILTIN_HOOK_LIST_USAGE,
29+
NULL
30+
};
31+
32+
static int list(int argc, const char **argv, const char *prefix,
33+
struct repository *repo UNUSED)
34+
{
35+
struct string_list *head;
36+
struct string_list_item *item;
37+
const char *hookname = NULL;
38+
int ret = 0;
39+
40+
struct option list_options[] = {
41+
OPT_END(),
42+
};
43+
44+
argc = parse_options(argc, argv, prefix, list_options,
45+
builtin_hook_list_usage, 0);
46+
47+
/*
48+
* The only unnamed argument provided should be the hook-name; if we add
49+
* arguments later they probably should be caught by parse_options.
50+
*/
51+
if (argc != 1)
52+
usage_msg_opt(_("You must specify a hook event name to list."),
53+
builtin_hook_list_usage, list_options);
54+
55+
hookname = argv[0];
56+
57+
head = list_hooks(the_repository, hookname);
58+
59+
if (!head->nr) {
60+
ret = 1; /* no hooks found */
61+
goto cleanup;
62+
}
63+
64+
for_each_string_list_item(item, head) {
65+
printf("%s\n", *item->string ? item->string
66+
: _("hook from hookdir"));
67+
}
68+
69+
cleanup:
70+
string_list_clear(head, 1);
71+
free(head);
72+
return ret;
73+
}
74+
2375
static int run(int argc, const char **argv, const char *prefix,
2476
struct repository *repo UNUSED)
2577
{
@@ -77,6 +129,7 @@ int cmd_hook(int argc,
77129
parse_opt_subcommand_fn *fn = NULL;
78130
struct option builtin_hook_options[] = {
79131
OPT_SUBCOMMAND("run", &fn, run),
132+
OPT_SUBCOMMAND("list", &fn, list),
80133
OPT_END(),
81134
};
82135

hook.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,7 @@ const char *find_hook(struct repository *r, const char *name)
4747
return path.buf;
4848
}
4949

50-
/*
51-
* Provides a list of hook commands to run for the 'hookname' event.
52-
*
53-
* This function consolidates hooks from two sources:
54-
* 1. The config-based hooks (not yet implemented).
55-
* 2. The "traditional" hook found in the repository hooks directory
56-
* (e.g., .git/hooks/pre-commit).
57-
*
58-
* The list is ordered by execution priority.
59-
*
60-
* The caller is responsible for freeing the memory of the returned list
61-
* using string_list_clear() and free().
62-
*/
63-
static struct string_list *list_hooks(struct repository *r, const char *hookname)
50+
struct string_list *list_hooks(struct repository *r, const char *hookname)
6451
{
6552
struct string_list *hook_head;
6653

hook.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,22 @@ struct hook_cb_data {
143143
struct repository *repository;
144144
};
145145

146-
/*
146+
/**
147+
* Provides a list of hook commands to run for the 'hookname' event.
148+
*
149+
* This function consolidates hooks from two sources:
150+
* 1. The config-based hooks (not yet implemented).
151+
* 2. The "traditional" hook found in the repository hooks directory
152+
* (e.g., .git/hooks/pre-commit).
153+
*
154+
* The list is ordered by execution priority.
155+
*
156+
* The caller is responsible for freeing the memory of the returned list
157+
* using string_list_clear() and free().
158+
*/
159+
struct string_list *list_hooks(struct repository *r, const char *hookname);
160+
161+
/**
147162
* Returns the path to the hook file, or NULL if the hook is missing
148163
* or disabled. Note that this points to static storage that will be
149164
* overwritten by further calls to find_hook and run_hook_*.

t/t1800-hook.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ test_expect_success 'git hook usage' '
1010
test_expect_code 129 git hook run &&
1111
test_expect_code 129 git hook run -h &&
1212
test_expect_code 129 git hook run --unknown 2>err &&
13+
test_expect_code 129 git hook list &&
14+
test_expect_code 129 git hook list -h &&
1315
grep "unknown option" err
1416
'
1517

0 commit comments

Comments
 (0)