builtin/refs: add verify subcommand

Introduce a new subcommand "verify" in git-refs(1) to allow the user to check the reference database consistency and also this subcommand will be used as the entry point of checking refs for "git-fsck(1)". Add "verbose" field into "fsck_options" to indicate whether we should print verbose messages when checking refs and objects consistency. Remove bit-field for "strict" field, this is because we cannot take address of a bit-field which makes it unhandy to set member variables when parsing the command line options. The "git-fsck(1)" declares "fsck_options" variable with "static" identifier which avoids complaint by the leak-checker. However, in "git-refs verify", we need to do memory clean manually. Thus add "fsck_options_clear" function in "fsck.c" to provide memory clean operation. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

shejialuo committed Aug 8, 2024 at 19:27 UTC bf061d26c7a595f1f46510aa584d50be1b1edb93
4 files changed +65 -1
Documentation/git-refs.txt
+13
@@ -10,6 +10,7 @@ SYNOPSIS
10 --------
11 [verse]
12 'git refs migrate' --ref-format=<format> [--dry-run]
13 +'git refs verify' [--strict] [--verbose]
14
15 DESCRIPTION
16 -----------
@@ -22,6 +23,9 @@ COMMANDS
23 migrate::
24 Migrate ref store between different formats.
25
26 +verify::
27 + Verify reference database consistency.
28 +
29 OPTIONS
30 -------
31
@@ -39,6 +43,15 @@ include::ref-storage-format.txt[]
43 can be used to double check that the migration works as expected before
44 performing the actual migration.
45
46 +The following options are specific to 'git refs verify':
47 +
48 +--strict::
49 + Enable stricter error checking. This will cause warnings to be
50 + reported as errors. See linkgit:git-fsck[1].
51 +
52 +--verbose::
53 + When verifying the reference database consistency, be chatty.
54 +
55 KNOWN LIMITATIONS
56 -----------------
57
builtin/refs.c
+34
@@ -1,4 +1,6 @@
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,6 +9,9 @@
9 #define REFS_MIGRATE_USAGE \
10 N_("git refs migrate --ref-format=<format> [--dry-run]")
11
12 +#define REFS_VERIFY_USAGE \
13 + N_("git refs verify [--strict] [--verbose]")
14 +
15 static int cmd_refs_migrate(int argc, const char **argv, const char *prefix)
16 {
17 const char * const migrate_usage[] = {
@@ -58,15 +63,44 @@ out:
63 return err;
64 }
65
66 +static int cmd_refs_verify(int argc, const char **argv, const char *prefix)
67 +{
68 + struct fsck_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT;
69 + const char * const verify_usage[] = {
70 + REFS_VERIFY_USAGE,
71 + NULL,
72 + };
73 + struct option options[] = {
74 + OPT_BOOL(0, "verbose", &fsck_refs_options.verbose, N_("be verbose")),
75 + OPT_BOOL(0, "strict", &fsck_refs_options.strict, N_("enable strict checking")),
76 + OPT_END(),
77 + };
78 + int ret;
79 +
80 + argc = parse_options(argc, argv, prefix, options, verify_usage, 0);
81 + if (argc)
82 + usage(_("'git refs verify' takes no arguments"));
83 +
84 + git_config(git_fsck_config, &fsck_refs_options);
85 + prepare_repo_settings(the_repository);
86 +
87 + ret = refs_fsck(get_main_ref_store(the_repository), &fsck_refs_options);
88 +
89 + fsck_options_clear(&fsck_refs_options);
90 + return ret;
91 +}
92 +
93 int cmd_refs(int argc, const char **argv, const char *prefix)
94 {
95 const char * const refs_usage[] = {
96 REFS_MIGRATE_USAGE,
97 + REFS_VERIFY_USAGE,
98 NULL,
99 };
100 parse_opt_subcommand_fn *fn = NULL;
101 struct option opts[] = {
102 OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate),
103 + OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify),
104 OPT_END(),
105 };
106
fsck.c
+11
@@ -1331,6 +1331,17 @@ int fsck_finish(struct fsck_options *options)
1331 return ret;
1332 }
1333
1334 +void fsck_options_clear(struct fsck_options *options)
1335 +{
1336 + free(options->msg_type);
1337 + oidset_clear(&options->skip_oids);
1338 + oidset_clear(&options->gitmodules_found);
1339 + oidset_clear(&options->gitmodules_done);
1340 + oidset_clear(&options->gitattributes_found);
1341 + oidset_clear(&options->gitattributes_done);
1342 + kh_clear_oid_map(options->object_names);
1343 +}
1344 +
1345 int git_fsck_config(const char *var, const char *value,
1346 const struct config_context *ctx, void *cb)
1347 {
fsck.h
+7 -1
@@ -153,7 +153,8 @@ struct fsck_ref_report {
153 struct fsck_options {
154 fsck_walk_func walk;
155 fsck_error error_func;
156 - unsigned strict:1;
156 + unsigned strict;
157 + unsigned verbose;
158 enum fsck_msg_type *msg_type;
159 struct oidset skip_oids;
160 struct oidset gitmodules_found;
@@ -231,6 +232,11 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
232 */
233 int fsck_finish(struct fsck_options *options);
234
235 +/*
236 + * Clear the fsck_options struct, freeing any allocated memory.
237 + */
238 +void fsck_options_clear(struct fsck_options *options);
239 +
240 /*
241 * Report an error or warning for refs.
242 */