Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/verify-pack.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "config.h"
3
#include "gettext.h"
4
#include "run-command.h"
5
#include "parse-options.h"
6
#include "strbuf.h"
7
8
0
#define VERIFY_PACK_VERBOSE 01
9
0
#define VERIFY_PACK_STAT_ONLY 02
10
11
static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo)
12
0
{
13
0
  struct child_process index_pack = CHILD_PROCESS_INIT;
14
0
  struct strvec *argv = &index_pack.args;
15
0
  struct strbuf arg = STRBUF_INIT;
16
0
  int verbose = flags & VERIFY_PACK_VERBOSE;
17
0
  int stat_only = flags & VERIFY_PACK_STAT_ONLY;
18
0
  int err;
19
20
0
  strvec_push(argv, "index-pack");
21
22
0
  if (stat_only)
23
0
    strvec_push(argv, "--verify-stat-only");
24
0
  else if (verbose)
25
0
    strvec_push(argv, "--verify-stat");
26
0
  else
27
0
    strvec_push(argv, "--verify");
28
29
0
  if (hash_algo)
30
0
    strvec_pushf(argv, "--object-format=%s", hash_algo);
31
32
  /*
33
   * In addition to "foo.pack" we accept "foo.idx" and "foo";
34
   * normalize these forms to "foo.pack" for "index-pack --verify".
35
   */
36
0
  strbuf_addstr(&arg, path);
37
0
  if (strbuf_strip_suffix(&arg, ".idx") ||
38
0
      !ends_with(arg.buf, ".pack"))
39
0
    strbuf_addstr(&arg, ".pack");
40
0
  strvec_push(argv, arg.buf);
41
42
0
  index_pack.git_cmd = 1;
43
44
0
  err = run_command(&index_pack);
45
46
0
  if (verbose || stat_only) {
47
0
    if (err)
48
0
      printf("%s: bad\n", arg.buf);
49
0
    else {
50
0
      if (!stat_only)
51
0
        printf("%s: ok\n", arg.buf);
52
0
    }
53
0
  }
54
0
  strbuf_release(&arg);
55
56
0
  return err;
57
0
}
58
59
static const char * const verify_pack_usage[] = {
60
  N_("git verify-pack [-v | --verbose] [-s | --stat-only] [--] <pack>.idx..."),
61
  NULL
62
};
63
64
int cmd_verify_pack(int argc, const char **argv, const char *prefix)
65
0
{
66
0
  int err = 0;
67
0
  unsigned int flags = 0;
68
0
  const char *object_format = NULL;
69
0
  int i;
70
0
  const struct option verify_pack_options[] = {
71
0
    OPT_BIT('v', "verbose", &flags, N_("verbose"),
72
0
      VERIFY_PACK_VERBOSE),
73
0
    OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
74
0
      VERIFY_PACK_STAT_ONLY),
75
0
    OPT_STRING(0, "object-format", &object_format, N_("hash"),
76
0
         N_("specify the hash algorithm to use")),
77
0
    OPT_END()
78
0
  };
79
80
0
  git_config(git_default_config, NULL);
81
0
  argc = parse_options(argc, argv, prefix, verify_pack_options,
82
0
           verify_pack_usage, 0);
83
0
  if (argc < 1)
84
0
    usage_with_options(verify_pack_usage, verify_pack_options);
85
0
  for (i = 0; i < argc; i++) {
86
0
    if (verify_one_pack(argv[i], flags, object_format))
87
0
      err = 1;
88
0
  }
89
90
0
  return err;
91
0
}