Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/hook.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "config.h"
3
#include "gettext.h"
4
#include "hook.h"
5
#include "parse-options.h"
6
#include "strvec.h"
7
8
#define BUILTIN_HOOK_RUN_USAGE \
9
  N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
10
11
static const char * const builtin_hook_usage[] = {
12
  BUILTIN_HOOK_RUN_USAGE,
13
  NULL
14
};
15
16
static const char * const builtin_hook_run_usage[] = {
17
  BUILTIN_HOOK_RUN_USAGE,
18
  NULL
19
};
20
21
static int run(int argc, const char **argv, const char *prefix)
22
0
{
23
0
  int i;
24
0
  struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
25
0
  int ignore_missing = 0;
26
0
  const char *hook_name;
27
0
  struct option run_options[] = {
28
0
    OPT_BOOL(0, "ignore-missing", &ignore_missing,
29
0
       N_("silently ignore missing requested <hook-name>")),
30
0
    OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"),
31
0
         N_("file to read into hooks' stdin")),
32
0
    OPT_END(),
33
0
  };
34
0
  int ret;
35
36
0
  argc = parse_options(argc, argv, prefix, run_options,
37
0
           builtin_hook_run_usage,
38
0
           PARSE_OPT_KEEP_DASHDASH);
39
40
0
  if (!argc)
41
0
    goto usage;
42
43
  /*
44
   * Having a -- for "run" when providing <hook-args> is
45
   * mandatory.
46
   */
47
0
  if (argc > 1 && strcmp(argv[1], "--") &&
48
0
      strcmp(argv[1], "--end-of-options"))
49
0
    goto usage;
50
51
  /* Add our arguments, start after -- */
52
0
  for (i = 2 ; i < argc; i++)
53
0
    strvec_push(&opt.args, argv[i]);
54
55
  /* Need to take into account core.hooksPath */
56
0
  git_config(git_default_config, NULL);
57
58
0
  hook_name = argv[0];
59
0
  if (!ignore_missing)
60
0
    opt.error_if_missing = 1;
61
0
  ret = run_hooks_opt(the_repository, hook_name, &opt);
62
0
  if (ret < 0) /* error() return */
63
0
    ret = 1;
64
0
  return ret;
65
0
usage:
66
0
  usage_with_options(builtin_hook_run_usage, run_options);
67
0
}
68
69
int cmd_hook(int argc, const char **argv, const char *prefix)
70
0
{
71
0
  parse_opt_subcommand_fn *fn = NULL;
72
0
  struct option builtin_hook_options[] = {
73
0
    OPT_SUBCOMMAND("run", &fn, run),
74
0
    OPT_END(),
75
0
  };
76
77
0
  argc = parse_options(argc, argv, NULL, builtin_hook_options,
78
0
           builtin_hook_usage, 0);
79
80
0
  return fn(argc, argv, prefix);
81
0
}