Skip to content

Commit fe6127f

Browse files
nasamuffingitster
authored andcommitted
hook: add list command
Teach 'git hook list <hookname>', which checks the known configs in order to create an ordered list of hooks to run on a given hook event. Multiple commands can be specified for a given hook by providing multiple "hook.<hookname>.command = <path-to-hook>" lines. Hooks will be run in config order. If more properties need to be set on a given hook in the future, commands can also be specified by providing "hook.<hookname>.command = <hookcmd-name>", as well as a "[hookcmd <hookcmd-name>]" subsection; at minimum, this subsection must contain a "hookcmd.<hookcmd-name>.command = <path-to-hook>" line. For example: $ git config --list | grep ^hook hook.pre-commit.command=baz hook.pre-commit.command=~/bar.sh hookcmd.baz.command=~/baz/from/hookcmd.sh $ git hook list pre-commit ~/baz/from/hookcmd.sh ~/bar.sh Signed-off-by: Emily Shaffer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2c3382d commit fe6127f

File tree

6 files changed

+259
-7
lines changed

6 files changed

+259
-7
lines changed

Documentation/git-hook.txt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,47 @@ git-hook - Manage configured hooks
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git hook'
11+
'git hook' list <hook-name>
1212

1313
DESCRIPTION
1414
-----------
1515
You can list, add, and modify hooks with this command.
1616

17+
This command parses the default configuration files for sections "hook" and
18+
"hookcmd". "hook" is used to describe the commands which will be run during a
19+
particular hook event; commands are run in config order. "hookcmd" is used to
20+
describe attributes of a specific command. If additional attributes don't need
21+
to be specified, a command to run can be specified directly in the "hook"
22+
section; if a "hookcmd" by that name isn't found, Git will attempt to run the
23+
provided value directly. For example:
24+
25+
Global config
26+
----
27+
[hook "post-commit"]
28+
command = "linter"
29+
command = "~/typocheck.sh"
30+
31+
[hookcmd "linter"]
32+
command = "/bin/linter --c"
33+
----
34+
35+
Local config
36+
----
37+
[hook "prepare-commit-msg"]
38+
command = "linter"
39+
[hook "post-commit"]
40+
command = "python ~/run-test-suite.py"
41+
----
42+
43+
COMMANDS
44+
--------
45+
46+
list <hook-name>::
47+
48+
List the hooks which have been configured for <hook-name>. Hooks appear
49+
in the order they should be run, and note the config scope where the relevant
50+
`hook.<hook-name>.command` was specified, not the `hookcmd` (if applicable).
51+
1752
GIT
1853
---
1954
Part of the linkgit:git[1] suite

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ LIB_OBJS += grep.o
891891
LIB_OBJS += hashmap.o
892892
LIB_OBJS += help.o
893893
LIB_OBJS += hex.o
894+
LIB_OBJS += hook.o
894895
LIB_OBJS += ident.o
895896
LIB_OBJS += interdiff.o
896897
LIB_OBJS += json-writer.o

builtin/hook.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,68 @@
11
#include "cache.h"
22

33
#include "builtin.h"
4+
#include "config.h"
5+
#include "hook.h"
46
#include "parse-options.h"
7+
#include "strbuf.h"
58

69
static const char * const builtin_hook_usage[] = {
7-
N_("git hook"),
10+
N_("git hook list <hookname>"),
811
NULL
912
};
1013

11-
int cmd_hook(int argc, const char **argv, const char *prefix)
14+
static int list(int argc, const char **argv, const char *prefix)
1215
{
13-
struct option builtin_hook_options[] = {
16+
struct list_head *head, *pos;
17+
struct hook *item;
18+
struct strbuf hookname = STRBUF_INIT;
19+
20+
struct option list_options[] = {
1421
OPT_END(),
1522
};
1623

17-
argc = parse_options(argc, argv, prefix, builtin_hook_options,
24+
argc = parse_options(argc, argv, prefix, list_options,
1825
builtin_hook_usage, 0);
1926

27+
if (argc < 1) {
28+
usage_msg_opt("a hookname must be provided to operate on.",
29+
builtin_hook_usage, list_options);
30+
}
31+
32+
strbuf_addstr(&hookname, argv[0]);
33+
34+
head = hook_list(&hookname);
35+
36+
if (list_empty(head)) {
37+
printf(_("no commands configured for hook '%s'\n"),
38+
hookname.buf);
39+
return 0;
40+
}
41+
42+
list_for_each(pos, head) {
43+
item = list_entry(pos, struct hook, list);
44+
if (item)
45+
printf("%s:\t%s\n",
46+
config_scope_name(item->origin),
47+
item->command.buf);
48+
}
49+
50+
clear_hook_list();
51+
strbuf_release(&hookname);
52+
2053
return 0;
2154
}
55+
56+
int cmd_hook(int argc, const char **argv, const char *prefix)
57+
{
58+
struct option builtin_hook_options[] = {
59+
OPT_END(),
60+
};
61+
if (argc < 2)
62+
usage_with_options(builtin_hook_usage, builtin_hook_options);
63+
64+
if (!strcmp(argv[1], "list"))
65+
return list(argc - 1, argv + 1, prefix);
66+
67+
usage_with_options(builtin_hook_usage, builtin_hook_options);
68+
}

hook.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "cache.h"
2+
3+
#include "hook.h"
4+
#include "config.h"
5+
6+
static LIST_HEAD(hook_head);
7+
8+
void free_hook(struct hook *ptr)
9+
{
10+
if (ptr) {
11+
strbuf_release(&ptr->command);
12+
free(ptr);
13+
}
14+
}
15+
16+
static void emplace_hook(struct list_head *pos, const char *command)
17+
{
18+
struct hook *to_add = malloc(sizeof(struct hook));
19+
to_add->origin = current_config_scope();
20+
strbuf_init(&to_add->command, 0);
21+
strbuf_addstr(&to_add->command, command);
22+
23+
list_add_tail(&to_add->list, pos);
24+
}
25+
26+
static void remove_hook(struct list_head *to_remove)
27+
{
28+
struct hook *hook_to_remove = list_entry(to_remove, struct hook, list);
29+
list_del(to_remove);
30+
free_hook(hook_to_remove);
31+
}
32+
33+
void clear_hook_list(void)
34+
{
35+
struct list_head *pos, *tmp;
36+
list_for_each_safe(pos, tmp, &hook_head)
37+
remove_hook(pos);
38+
}
39+
40+
static int hook_config_lookup(const char *key, const char *value, void *hook_key_cb)
41+
{
42+
const char *hook_key = hook_key_cb;
43+
44+
if (!strcmp(key, hook_key)) {
45+
const char *command = value;
46+
struct strbuf hookcmd_name = STRBUF_INIT;
47+
struct list_head *pos = NULL, *tmp = NULL;
48+
49+
/* Check if a hookcmd with that name exists. */
50+
strbuf_addf(&hookcmd_name, "hookcmd.%s.command", command);
51+
git_config_get_value(hookcmd_name.buf, &command);
52+
53+
if (!command)
54+
BUG("git_config_get_value overwrote a string it shouldn't have");
55+
56+
/*
57+
* TODO: implement an option-getting callback, e.g.
58+
* get configs by pattern hookcmd.$value.*
59+
* for each key+value, do_callback(key, value, cb_data)
60+
*/
61+
62+
list_for_each_safe(pos, tmp, &hook_head) {
63+
struct hook *hook = list_entry(pos, struct hook, list);
64+
/*
65+
* The list of hooks to run can be reordered by being redeclared
66+
* in the config. Options about hook ordering should be checked
67+
* here.
68+
*/
69+
if (0 == strcmp(hook->command.buf, command))
70+
remove_hook(pos);
71+
}
72+
emplace_hook(pos, command);
73+
}
74+
75+
return 0;
76+
}
77+
78+
struct list_head* hook_list(const struct strbuf* hookname)
79+
{
80+
struct strbuf hook_key = STRBUF_INIT;
81+
82+
if (!hookname)
83+
return NULL;
84+
85+
strbuf_addf(&hook_key, "hook.%s.command", hookname->buf);
86+
87+
git_config(hook_config_lookup, (void*)hook_key.buf);
88+
89+
return &hook_head;
90+
}

hook.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "config.h"
2+
#include "list.h"
3+
#include "strbuf.h"
4+
5+
struct hook
6+
{
7+
struct list_head list;
8+
enum config_scope origin;
9+
struct strbuf command;
10+
};
11+
12+
struct list_head* hook_list(const struct strbuf *hookname);
13+
14+
void free_hook(struct hook *ptr);
15+
void clear_hook_list(void);

t/t1360-config-based-hooks.sh

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,72 @@ test_description='config-managed multihooks, including git-hook command'
44

55
. ./test-lib.sh
66

7-
test_expect_success 'git hook command does not crash' '
8-
git hook
7+
ROOT=
8+
if test_have_prereq MINGW
9+
then
10+
# In Git for Windows, Unix-like paths work only in shell scripts;
11+
# `git.exe`, however, will prefix them with the pseudo root directory
12+
# (of the Unix shell). Let's accommodate for that.
13+
ROOT="$(cd / && pwd)"
14+
fi
15+
16+
setup_hooks () {
17+
test_config hook.pre-commit.command "/path/ghi" --add
18+
test_config_global hook.pre-commit.command "/path/def" --add
19+
}
20+
21+
setup_hookcmd () {
22+
test_config hook.pre-commit.command "abc" --add
23+
test_config_global hookcmd.abc.command "/path/abc" --add
24+
}
25+
26+
test_expect_success 'git hook rejects commands without a mode' '
27+
test_must_fail git hook pre-commit
28+
'
29+
30+
31+
test_expect_success 'git hook rejects commands without a hookname' '
32+
test_must_fail git hook list
33+
'
34+
35+
test_expect_success 'git hook list orders by config order' '
36+
setup_hooks &&
37+
38+
cat >expected <<-EOF &&
39+
global: $ROOT/path/def
40+
local: $ROOT/path/ghi
41+
EOF
42+
43+
git hook list pre-commit >actual &&
44+
test_cmp expected actual
45+
'
46+
47+
test_expect_success 'git hook list dereferences a hookcmd' '
48+
setup_hooks &&
49+
setup_hookcmd &&
50+
51+
cat >expected <<-EOF &&
52+
global: $ROOT/path/def
53+
local: $ROOT/path/ghi
54+
local: $ROOT/path/abc
55+
EOF
56+
57+
git hook list pre-commit >actual &&
58+
test_cmp expected actual
59+
'
60+
61+
test_expect_success 'git hook list reorders on duplicate commands' '
62+
setup_hooks &&
63+
64+
test_config hook.pre-commit.command "/path/def" --add &&
65+
66+
cat >expected <<-EOF &&
67+
local: $ROOT/path/ghi
68+
local: $ROOT/path/def
69+
EOF
70+
71+
git hook list pre-commit >actual &&
72+
test_cmp expected actual
973
'
1074

1175
test_done

0 commit comments

Comments
 (0)