Re: [PATCH v2 3/6] hook: introduce "git hook list"
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Aug 12, 2021, 18:59 UTC
- Message-ID
- <xmqq5ywa4hg8.fsf@gitster.g>
- In-Reply-To
- <20210812004258.74318-4-emilyshaffer@google.com>
Emily Shaffer <emilyshaffer@google.com> writes:
Show 22 quoted lines
> +static int list(int argc, const char **argv, const char *prefix)
> +{
> + struct list_head *head, *pos;
> + const char *hookname = NULL;
> + struct strbuf hookdir_annotation = STRBUF_INIT;
> +
> + struct option list_options[] = {
> + OPT_END(),
> + };
> +
> + argc = parse_options(argc, argv, prefix, list_options,
> + builtin_hook_list_usage, 0);
> +
> + if (argc < 1)
> + usage_msg_opt(_("You must specify a hook event name to list."),
> + builtin_hook_list_usage, list_options);
> +
> + hookname = argv[0];
> +
> + head = hook_list(hookname);
> +
> + if (list_empty(head)) {The same "can't hook_list() signal an error by returning NULL?" comment applies here.
head = hook_list(hookname);
if (!head)
die(_("no such hook '%s'"), hookname);or something?
> + printf(_("no commands configured for hook '%s'\n"),
> + hookname);
> + return 0;If it is a normally expected state that there is no hook for the given name, signalling success by returning 0 here may be sensible, but then the message should at least go to the standard error stream to leave the standard output empty, so that a caller can reasonably do something like
for path in $(git hooks list "$1") do ls -l "$path" done
If we really want to show such a message, perhaps
if (list_empty(head)) {
if (!quiet)
warning(_("no commands configured"));
return 0;
}The normal display just shows the path without saying "command %s will run for hook %s"; the warning probably should do the same.
Having said that, if it truly is a normal and expected state that no hook is defined for the given name, I actually think there should be no message.
Show 8 quoted lines
> + }
> +
> + list_for_each(pos, head) {
> + struct hook *item = list_entry(pos, struct hook, list);
> + item = list_entry(pos, struct hook, list);
> + if (item)
> + printf("%s\n", item->hook_path);
> + }Show 33 quoted lines
> diff --git a/hook.c b/hook.c
> index 37f682c6d8..2714b63473 100644
> --- a/hook.c
> +++ b/hook.c
> @@ -96,22 +96,20 @@ int hook_exists(const char *name)
> struct list_head* hook_list(const char* hookname)
> {
> struct list_head *hook_head = xmalloc(sizeof(struct list_head));
> + const char *hook_path = find_hook(hookname);
> +
>
> INIT_LIST_HEAD(hook_head);
>
> if (!hookname)
> return NULL;
>
> - if (have_git_dir()) {
> - const char *hook_path = find_hook(hookname);
> -
> - /* Add the hook from the hookdir */
> - if (hook_path) {
> - struct hook *to_add = xmalloc(sizeof(*to_add));
> - to_add->hook_path = hook_path;
> - to_add->feed_pipe_cb_data = NULL;
> - list_add_tail(&to_add->list, hook_head);
> - }
> + /* Add the hook from the hookdir */
> + if (hook_path) {
> + struct hook *to_add = xmalloc(sizeof(*to_add));
> + to_add->hook_path = hook_path;
> + to_add->feed_pipe_cb_data = NULL;
> + list_add_tail(&to_add->list, hook_head);
> }I do not think this belongs to the step to add "list" command. The log message does not explain or justify why have-git-dir goes away, either.