Skip to content

Commit bf061d2

Browse files
shejialuogitster
authored andcommitted
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 <[email protected]> Mentored-by: Karthik Nayak <[email protected]> Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ab6f79d commit bf061d2

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

Documentation/git-refs.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git refs migrate' --ref-format=<format> [--dry-run]
13+
'git refs verify' [--strict] [--verbose]
1314

1415
DESCRIPTION
1516
-----------
@@ -22,6 +23,9 @@ COMMANDS
2223
migrate::
2324
Migrate ref store between different formats.
2425

26+
verify::
27+
Verify reference database consistency.
28+
2529
OPTIONS
2630
-------
2731

@@ -39,6 +43,15 @@ include::ref-storage-format.txt[]
3943
can be used to double check that the migration works as expected before
4044
performing the actual migration.
4145

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+
4255
KNOWN LIMITATIONS
4356
-----------------
4457

builtin/refs.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "builtin.h"
2+
#include "config.h"
3+
#include "fsck.h"
24
#include "parse-options.h"
35
#include "refs.h"
46
#include "repository.h"
@@ -7,6 +9,9 @@
79
#define REFS_MIGRATE_USAGE \
810
N_("git refs migrate --ref-format=<format> [--dry-run]")
911

12+
#define REFS_VERIFY_USAGE \
13+
N_("git refs verify [--strict] [--verbose]")
14+
1015
static int cmd_refs_migrate(int argc, const char **argv, const char *prefix)
1116
{
1217
const char * const migrate_usage[] = {
@@ -58,15 +63,44 @@ static int cmd_refs_migrate(int argc, const char **argv, const char *prefix)
5863
return err;
5964
}
6065

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+
6193
int cmd_refs(int argc, const char **argv, const char *prefix)
6294
{
6395
const char * const refs_usage[] = {
6496
REFS_MIGRATE_USAGE,
97+
REFS_VERIFY_USAGE,
6598
NULL,
6699
};
67100
parse_opt_subcommand_fn *fn = NULL;
68101
struct option opts[] = {
69102
OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate),
103+
OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify),
70104
OPT_END(),
71105
};
72106

fsck.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,17 @@ int fsck_finish(struct fsck_options *options)
13311331
return ret;
13321332
}
13331333

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+
13341345
int git_fsck_config(const char *var, const char *value,
13351346
const struct config_context *ctx, void *cb)
13361347
{

fsck.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ struct fsck_ref_report {
153153
struct fsck_options {
154154
fsck_walk_func walk;
155155
fsck_error error_func;
156-
unsigned strict:1;
156+
unsigned strict;
157+
unsigned verbose;
157158
enum fsck_msg_type *msg_type;
158159
struct oidset skip_oids;
159160
struct oidset gitmodules_found;
@@ -231,6 +232,11 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
231232
*/
232233
int fsck_finish(struct fsck_options *options);
233234

235+
/*
236+
* Clear the fsck_options struct, freeing any allocated memory.
237+
*/
238+
void fsck_options_clear(struct fsck_options *options);
239+
234240
/*
235241
* Report an error or warning for refs.
236242
*/

0 commit comments

Comments
 (0)