Subject: Re: [PATCH v2 3/6] hook: introduce "git hook list"

From: Junio C Hamano <[email-masked]>
Date: Thu, 12 Aug 2021 11:59:51 -0700

Emily Shaffer <[email-masked]> writes:

> +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.

> +	}
> +
> +	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);
> +	}

> 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.