Skip to content

Commit dcd7ab7

Browse files
10ne1gitster
authored andcommitted
hook: show config scope in git hook list
Users running "git hook list" can see which hooks are configured but have no way to tell at which config scope (local, global, system...) each hook was defined. Store the scope from ctx->kvi->scope in the single-pass config callback, then carry it through the cache to the hook structs, so we can expose it to users via the "git hook list --show-scope" flag, which mirrors the existing git config --show-scope convention. Without the flag the output is unchanged. Example usage: $ git hook list --show-scope pre-commit linter (global) no-leaks (local) hook from hookdir Traditional hooks from the hookdir are unaffected by --show-scope since the config scope concept does not apply to them. Suggested-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2a99233 commit dcd7ab7

5 files changed

Lines changed: 60 additions & 8 deletions

File tree

Documentation/git-hook.adoc

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

1414
DESCRIPTION
1515
-----------
@@ -113,7 +113,7 @@ Any positional arguments to the hook should be passed after a
113113
mandatory `--` (or `--end-of-options`, see linkgit:gitcli[7]). See
114114
linkgit:githooks[5] for arguments hooks might expect (if any).
115115
116-
list [-z]::
116+
list [-z] [--show-scope]::
117117
Print a list of hooks which will be run on `<hook-name>` event. If no
118118
hooks are configured for that event, print a warning and return 1.
119119
Use `-z` to terminate output lines with NUL instead of newlines.
@@ -134,6 +134,11 @@ OPTIONS
134134
-z::
135135
Terminate "list" output lines with NUL instead of newlines.
136136

137+
--show-scope::
138+
For "list"; print the config scope (e.g. `local`, `global`, `system`)
139+
in parentheses after the friendly name of each configured hook, to show
140+
where it was defined. Traditional hooks from the hookdir are unaffected.
141+
137142
WRAPPERS
138143
--------
139144

builtin/hook.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#define BUILTIN_HOOK_RUN_USAGE \
1010
N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
1111
#define BUILTIN_HOOK_LIST_USAGE \
12-
N_("git hook list [-z] <hook-name>")
12+
N_("git hook list [-z] [--show-scope] <hook-name>")
1313

1414
static const char * const builtin_hook_usage[] = {
1515
BUILTIN_HOOK_RUN_USAGE,
@@ -33,11 +33,14 @@ static int list(int argc, const char **argv, const char *prefix,
3333
struct string_list_item *item;
3434
const char *hookname = NULL;
3535
int line_terminator = '\n';
36+
int show_scope = 0;
3637
int ret = 0;
3738

3839
struct option list_options[] = {
3940
OPT_SET_INT('z', NULL, &line_terminator,
4041
N_("use NUL as line terminator"), '\0'),
42+
OPT_BOOL(0, "show-scope", &show_scope,
43+
N_("show the config scope that defined each hook")),
4144
OPT_END(),
4245
};
4346

@@ -70,7 +73,14 @@ static int list(int argc, const char **argv, const char *prefix,
7073
printf("%s%c", _("hook from hookdir"), line_terminator);
7174
break;
7275
case HOOK_CONFIGURED:
73-
printf("%s%c", h->u.configured.friendly_name, line_terminator);
76+
if (show_scope)
77+
printf("%s (%s)%c",
78+
h->u.configured.friendly_name,
79+
config_scope_name(h->u.configured.scope),
80+
line_terminator);
81+
else
82+
printf("%s%c", h->u.configured.friendly_name,
83+
line_terminator);
7484
break;
7585
default:
7686
BUG("unknown hook kind");

hook.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
114114

115115
/*
116116
* Cache entry stored as the .util pointer of string_list items inside the
117-
* hook config cache. For now carries only the command for the hook. Next
118-
* commits will add more data.
117+
* hook config cache.
119118
*/
120119
struct hook_config_cache_entry {
121120
char *command;
121+
enum config_scope scope;
122122
};
123123

124124
/*
@@ -135,7 +135,7 @@ struct hook_all_config_cb {
135135

136136
/* repo_config() callback that collects all hook.* configuration in one pass. */
137137
static int hook_config_lookup_all(const char *key, const char *value,
138-
const struct config_context *ctx UNUSED,
138+
const struct config_context *ctx,
139139
void *cb_data)
140140
{
141141
struct hook_all_config_cb *data = cb_data;
@@ -172,7 +172,19 @@ static int hook_config_lookup_all(const char *key, const char *value,
172172

173173
/* Re-insert if necessary to preserve last-seen order. */
174174
unsorted_string_list_remove(hooks, hook_name, 0);
175-
string_list_append(hooks, hook_name);
175+
176+
if (!ctx->kvi)
177+
BUG("hook config callback called without key-value info");
178+
179+
/*
180+
* Stash the config scope in the util pointer for
181+
* later retrieval in build_hook_config_map(). This
182+
* intermediate struct is transient and never leaves
183+
* that function, so we pack the enum value into the
184+
* pointer rather than heap-allocating a wrapper.
185+
*/
186+
string_list_append(hooks, hook_name)->util =
187+
(void *)(uintptr_t)ctx->kvi->scope;
176188
}
177189
} else if (!strcmp(subkey, "command")) {
178190
/* Store command overwriting the old value */
@@ -251,6 +263,8 @@ static void build_hook_config_map(struct repository *r,
251263

252264
for (size_t i = 0; i < hook_names->nr; i++) {
253265
const char *hname = hook_names->items[i].string;
266+
enum config_scope scope =
267+
(enum config_scope)(uintptr_t)hook_names->items[i].util;
254268
struct hook_config_cache_entry *entry;
255269
char *command;
256270

@@ -268,6 +282,7 @@ static void build_hook_config_map(struct repository *r,
268282
/* util stores a cache entry; owned by the cache. */
269283
CALLOC_ARRAY(entry, 1);
270284
entry->command = xstrdup(command);
285+
entry->scope = scope;
271286
string_list_append(hooks, hname)->util = entry;
272287
}
273288

@@ -348,6 +363,7 @@ static void list_hooks_add_configured(struct repository *r,
348363
hook->kind = HOOK_CONFIGURED;
349364
hook->u.configured.friendly_name = xstrdup(friendly_name);
350365
hook->u.configured.command = xstrdup(entry->command);
366+
hook->u.configured.scope = entry->scope;
351367

352368
string_list_append(list, friendly_name)->util = hook;
353369
}

hook.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef HOOK_H
22
#define HOOK_H
3+
#include "config.h"
34
#include "strvec.h"
45
#include "run-command.h"
56
#include "string-list.h"
@@ -29,6 +30,7 @@ struct hook {
2930
struct {
3031
const char *friendly_name;
3132
const char *command;
33+
enum config_scope scope;
3234
} configured;
3335
} u;
3436

t/t1800-hook.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,25 @@ test_expect_success 'configured hooks run before hookdir hook' '
408408
test_cmp expected actual
409409
'
410410

411+
test_expect_success 'git hook list --show-scope shows config scope' '
412+
test_config_global hook.global-hook.command "echo global" &&
413+
test_config_global hook.global-hook.event test-hook --add &&
414+
test_config hook.local-hook.command "echo local" &&
415+
test_config hook.local-hook.event test-hook --add &&
416+
417+
cat >expected <<-\EOF &&
418+
global-hook (global)
419+
local-hook (local)
420+
EOF
421+
git hook list --show-scope test-hook >actual &&
422+
test_cmp expected actual &&
423+
424+
# without --show-scope the scope must not appear
425+
git hook list test-hook >actual &&
426+
test_grep ! "(global)" actual &&
427+
test_grep ! "(local)" actual
428+
'
429+
411430
test_expect_success 'git hook run a hook with a bad shebang' '
412431
test_when_finished "rm -rf bad-hooks" &&
413432
mkdir bad-hooks &&

0 commit comments

Comments
 (0)