Re: [PATCH v8 08/37] hook: add 'run' subcommand
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Mar 12, 2021, 10:22 UTC
- Message-ID
- <xmqqk0qc8yec.fsf@gitster.g>
- In-Reply-To
- <20210311021037.3001235-9-emilyshaffer@google.com>
Emily Shaffer <emilyshaffer@google.com> writes:
Show 33 quoted lines
> +static int run(int argc, const char **argv, const char *prefix)
> +{
> + struct strbuf hookname = STRBUF_INIT;
> + struct run_hooks_opt opt;
> + int rc = 0;
> +
> + struct option run_options[] = {
> + OPT_STRVEC('e', "env", &opt.env, N_("var"),
> + N_("environment variables for hook to use")),
> + OPT_STRVEC('a', "arg", &opt.args, N_("args"),
> + N_("argument to pass to hook")),
> + OPT_END(),
> + };
> +
> + run_hooks_opt_init(&opt);
> +
> + argc = parse_options(argc, argv, prefix, run_options,
> + builtin_hook_usage, 0);
> +
> + if (argc < 1)
> + usage_msg_opt(_("You must specify a hook event to run."),
> + builtin_hook_usage, run_options);
> +
> + strbuf_addstr(&hookname, argv[0]);
> + opt.run_hookdir = should_run_hookdir;
> +
> + rc = run_hooks(hookname.buf, &opt);
> +
> + strbuf_release(&hookname);
> + run_hooks_opt_clear(&opt);
> +
> + return rc;
> +}This looks like a small and clean example that is good for people to emulate when using the new run-hooks API. You opt_init(), futz with the its fields, call run_hooks(), and finally opt_clear() to release the resources.