git/builtin/hook.c
Adrian Ratiu d075ed4220 hook: make ungroup opt-out instead of opt-in
In 857f047e40 (hook: allow overriding the ungroup option, 2025-12-26),
I accidentally made the ungroup option opt-in instead of opt-out and
despite my best efforts to set it for all API users, I missed a case
which requires it to be set: the pre-push hook which regressed.

The only thing I needed in that commit was a way to change the default,
to convert the remaining receive-pack hooks which require ungroup == 0
for sideband output, so it doesn't matter if it's on or off by default.

Bring back the original behavior by setting it for all hooks in the
struct run_hooks_opt initializer, which nicely allows changing the
default value only where needed, in receive-pack.c.

While at it add a few hook tests which exercise receive-pack sideband
output since they are the only ungroup=0 exceptions and there are no
other tests exercising this functionality.

Fixes: 857f047e40f7 ("hook: allow overriding the ungroup option")
Reported-by: Kristoffer Haugsbakk <kristofferhaugsbakk@fastmail.com>
Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-01-15 13:10:30 -08:00

88 lines
2.1 KiB
C

#define USE_THE_REPOSITORY_VARIABLE
#include "builtin.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
#include "hook.h"
#include "parse-options.h"
#include "strvec.h"
#define BUILTIN_HOOK_RUN_USAGE \
N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
static const char * const builtin_hook_usage[] = {
BUILTIN_HOOK_RUN_USAGE,
NULL
};
static const char * const builtin_hook_run_usage[] = {
BUILTIN_HOOK_RUN_USAGE,
NULL
};
static int run(int argc, const char **argv, const char *prefix,
struct repository *repo UNUSED)
{
int i;
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
int ignore_missing = 0;
const char *hook_name;
struct option run_options[] = {
OPT_BOOL(0, "ignore-missing", &ignore_missing,
N_("silently ignore missing requested <hook-name>")),
OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"),
N_("file to read into hooks' stdin")),
OPT_END(),
};
int ret;
argc = parse_options(argc, argv, prefix, run_options,
builtin_hook_run_usage,
PARSE_OPT_KEEP_DASHDASH);
if (!argc)
goto usage;
/*
* Having a -- for "run" when providing <hook-args> is
* mandatory.
*/
if (argc > 1 && strcmp(argv[1], "--") &&
strcmp(argv[1], "--end-of-options"))
goto usage;
/* Add our arguments, start after -- */
for (i = 2 ; i < argc; i++)
strvec_push(&opt.args, argv[i]);
/* Need to take into account core.hooksPath */
repo_config(the_repository, git_default_config, NULL);
hook_name = argv[0];
if (!ignore_missing)
opt.error_if_missing = 1;
ret = run_hooks_opt(the_repository, hook_name, &opt);
if (ret < 0) /* error() return */
ret = 1;
return ret;
usage:
usage_with_options(builtin_hook_run_usage, run_options);
}
int cmd_hook(int argc,
const char **argv,
const char *prefix,
struct repository *repo)
{
parse_opt_subcommand_fn *fn = NULL;
struct option builtin_hook_options[] = {
OPT_SUBCOMMAND("run", &fn, run),
OPT_END(),
};
argc = parse_options(argc, argv, NULL, builtin_hook_options,
builtin_hook_usage, 0);
return fn(argc, argv, prefix, repo);
}