Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/refs.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "config.h"
3
#include "fsck.h"
4
#include "parse-options.h"
5
#include "refs.h"
6
#include "repository.h"
7
#include "strbuf.h"
8
9
#define REFS_MIGRATE_USAGE \
10
0
  N_("git refs migrate --ref-format=<format> [--dry-run]")
11
12
#define REFS_VERIFY_USAGE \
13
0
  N_("git refs verify [--strict] [--verbose]")
14
15
static int cmd_refs_migrate(int argc, const char **argv, const char *prefix)
16
0
{
17
0
  const char * const migrate_usage[] = {
18
0
    REFS_MIGRATE_USAGE,
19
0
    NULL,
20
0
  };
21
0
  const char *format_str = NULL;
22
0
  enum ref_storage_format format;
23
0
  unsigned int flags = 0;
24
0
  struct option options[] = {
25
0
    OPT_STRING_F(0, "ref-format", &format_str, N_("format"),
26
0
      N_("specify the reference format to convert to"),
27
0
      PARSE_OPT_NONEG),
28
0
    OPT_BIT(0, "dry-run", &flags,
29
0
      N_("perform a non-destructive dry-run"),
30
0
      REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN),
31
0
    OPT_END(),
32
0
  };
33
0
  struct strbuf errbuf = STRBUF_INIT;
34
0
  int err;
35
36
0
  argc = parse_options(argc, argv, prefix, options, migrate_usage, 0);
37
0
  if (argc)
38
0
    usage(_("too many arguments"));
39
0
  if (!format_str)
40
0
    usage(_("missing --ref-format=<format>"));
41
42
0
  format = ref_storage_format_by_name(format_str);
43
0
  if (format == REF_STORAGE_FORMAT_UNKNOWN) {
44
0
    err = error(_("unknown ref storage format '%s'"), format_str);
45
0
    goto out;
46
0
  }
47
48
0
  if (the_repository->ref_storage_format == format) {
49
0
    err = error(_("repository already uses '%s' format"),
50
0
          ref_storage_format_to_name(format));
51
0
    goto out;
52
0
  }
53
54
0
  if (repo_migrate_ref_storage_format(the_repository, format, flags, &errbuf) < 0) {
55
0
    err = error("%s", errbuf.buf);
56
0
    goto out;
57
0
  }
58
59
0
  err = 0;
60
61
0
out:
62
0
  strbuf_release(&errbuf);
63
0
  return err;
64
0
}
65
66
static int cmd_refs_verify(int argc, const char **argv, const char *prefix)
67
0
{
68
0
  struct fsck_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT;
69
0
  const char * const verify_usage[] = {
70
0
    REFS_VERIFY_USAGE,
71
0
    NULL,
72
0
  };
73
0
  struct option options[] = {
74
0
    OPT_BOOL(0, "verbose", &fsck_refs_options.verbose, N_("be verbose")),
75
0
    OPT_BOOL(0, "strict", &fsck_refs_options.strict, N_("enable strict checking")),
76
0
    OPT_END(),
77
0
  };
78
0
  int ret;
79
80
0
  argc = parse_options(argc, argv, prefix, options, verify_usage, 0);
81
0
  if (argc)
82
0
    usage(_("'git refs verify' takes no arguments"));
83
84
0
  git_config(git_fsck_config, &fsck_refs_options);
85
0
  prepare_repo_settings(the_repository);
86
87
0
  ret = refs_fsck(get_main_ref_store(the_repository), &fsck_refs_options);
88
89
0
  fsck_options_clear(&fsck_refs_options);
90
0
  return ret;
91
0
}
92
93
int cmd_refs(int argc, const char **argv, const char *prefix)
94
0
{
95
0
  const char * const refs_usage[] = {
96
0
    REFS_MIGRATE_USAGE,
97
0
    REFS_VERIFY_USAGE,
98
0
    NULL,
99
0
  };
100
0
  parse_opt_subcommand_fn *fn = NULL;
101
0
  struct option opts[] = {
102
0
    OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate),
103
0
    OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify),
104
0
    OPT_END(),
105
0
  };
106
107
0
  argc = parse_options(argc, argv, prefix, opts, refs_usage, 0);
108
0
  return fn(argc, argv, prefix);
109
0
}