Skip to content

Commit b3d1754

Browse files
committed
Merge branch 'sj/ref-fsck'
"git fsck" infrastructure has been taught to also check the sanity of the ref database, in addition to the object database. * sj/ref-fsck: fsck: add ref name check for files backend files-backend: add unified interface for refs scanning builtin/refs: add verify subcommand refs: set up ref consistency check infrastructure fsck: add refs report function fsck: add a unified interface for reporting fsck messages fsck: make "fsck_error" callback generic fsck: rename objects-related fsck error functions fsck: rename "skiplist" to "skip_oids"
2 parents 87a1768 + 1c31be4 commit b3d1754

File tree

16 files changed

+477
-59
lines changed

16 files changed

+477
-59
lines changed

Documentation/fsck-msgids.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
`badParentSha1`::
2020
(ERROR) A commit object has a bad parent sha1.
2121

22+
`badRefFiletype`::
23+
(ERROR) A ref has a bad file type.
24+
25+
`badRefName`::
26+
(ERROR) A ref has an invalid format.
27+
2228
`badTagName`::
2329
(INFO) A tag has an invalid format.
2430

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/fsck.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,16 @@ static int objerror(struct object *obj, const char *err)
8989
return -1;
9090
}
9191

92-
static int fsck_error_func(struct fsck_options *o UNUSED,
93-
const struct object_id *oid,
94-
enum object_type object_type,
95-
enum fsck_msg_type msg_type,
96-
enum fsck_msg_id msg_id UNUSED,
97-
const char *message)
92+
static int fsck_objects_error_func(struct fsck_options *o UNUSED,
93+
void *fsck_report,
94+
enum fsck_msg_type msg_type,
95+
enum fsck_msg_id msg_id UNUSED,
96+
const char *message)
9897
{
98+
struct fsck_object_report *report = fsck_report;
99+
const struct object_id *oid = report->oid;
100+
enum object_type object_type = report->object_type;
101+
99102
switch (msg_type) {
100103
case FSCK_WARN:
101104
/* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
@@ -938,7 +941,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
938941

939942
fsck_walk_options.walk = mark_object;
940943
fsck_obj_options.walk = mark_used;
941-
fsck_obj_options.error_func = fsck_error_func;
944+
fsck_obj_options.error_func = fsck_objects_error_func;
942945
if (check_strict)
943946
fsck_obj_options.strict = 1;
944947

builtin/mktag.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ static int option_strict = 1;
1818
static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
1919

2020
static int mktag_fsck_error_func(struct fsck_options *o UNUSED,
21-
const struct object_id *oid UNUSED,
22-
enum object_type object_type UNUSED,
21+
void *fsck_report UNUSED,
2322
enum fsck_msg_type msg_type,
2423
enum fsck_msg_id msg_id UNUSED,
2524
const char *message)

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: 99 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void fsck_set_msg_types(struct fsck_options *options, const char *values)
205205
if (!strcmp(buf, "skiplist")) {
206206
if (equal == len)
207207
die("skiplist requires a path");
208-
oidset_parse_file(&options->skiplist, buf + equal + 1,
208+
oidset_parse_file(&options->skip_oids, buf + equal + 1,
209209
the_repository->hash_algo);
210210
buf += len + 1;
211211
continue;
@@ -223,25 +223,25 @@ void fsck_set_msg_types(struct fsck_options *options, const char *values)
223223
static int object_on_skiplist(struct fsck_options *opts,
224224
const struct object_id *oid)
225225
{
226-
return opts && oid && oidset_contains(&opts->skiplist, oid);
226+
return opts && oid && oidset_contains(&opts->skip_oids, oid);
227227
}
228228

229-
__attribute__((format (printf, 5, 6)))
230-
static int report(struct fsck_options *options,
231-
const struct object_id *oid, enum object_type object_type,
232-
enum fsck_msg_id msg_id, const char *fmt, ...)
229+
/*
230+
* Provide the common functionality for either fscking refs or objects.
231+
* It will get the current msg error type and call the error_func callback
232+
* which is registered in the "fsck_options" struct.
233+
*/
234+
static int fsck_vreport(struct fsck_options *options,
235+
void *fsck_report,
236+
enum fsck_msg_id msg_id, const char *fmt, va_list ap)
233237
{
234-
va_list ap;
235238
struct strbuf sb = STRBUF_INIT;
236239
enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
237240
int result;
238241

239242
if (msg_type == FSCK_IGNORE)
240243
return 0;
241244

242-
if (object_on_skiplist(options, oid))
243-
return 0;
244-
245245
if (msg_type == FSCK_FATAL)
246246
msg_type = FSCK_ERROR;
247247
else if (msg_type == FSCK_INFO)
@@ -250,16 +250,49 @@ static int report(struct fsck_options *options,
250250
prepare_msg_ids();
251251
strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased);
252252

253-
va_start(ap, fmt);
254253
strbuf_vaddf(&sb, fmt, ap);
255-
result = options->error_func(options, oid, object_type,
254+
result = options->error_func(options, fsck_report,
256255
msg_type, msg_id, sb.buf);
257256
strbuf_release(&sb);
257+
258+
return result;
259+
}
260+
261+
__attribute__((format (printf, 5, 6)))
262+
static int report(struct fsck_options *options,
263+
const struct object_id *oid, enum object_type object_type,
264+
enum fsck_msg_id msg_id, const char *fmt, ...)
265+
{
266+
va_list ap;
267+
struct fsck_object_report report = {
268+
.oid = oid,
269+
.object_type = object_type
270+
};
271+
int result;
272+
273+
if (object_on_skiplist(options, oid))
274+
return 0;
275+
276+
va_start(ap, fmt);
277+
result = fsck_vreport(options, &report, msg_id, fmt, ap);
258278
va_end(ap);
259279

260280
return result;
261281
}
262282

283+
int fsck_report_ref(struct fsck_options *options,
284+
struct fsck_ref_report *report,
285+
enum fsck_msg_id msg_id,
286+
const char *fmt, ...)
287+
{
288+
va_list ap;
289+
int result;
290+
va_start(ap, fmt);
291+
result = fsck_vreport(options, report, msg_id, fmt, ap);
292+
va_end(ap);
293+
return result;
294+
}
295+
263296
void fsck_enable_object_names(struct fsck_options *options)
264297
{
265298
if (!options->object_names)
@@ -1200,13 +1233,15 @@ int fsck_buffer(const struct object_id *oid, enum object_type type,
12001233
type);
12011234
}
12021235

1203-
int fsck_error_function(struct fsck_options *o,
1204-
const struct object_id *oid,
1205-
enum object_type object_type UNUSED,
1206-
enum fsck_msg_type msg_type,
1207-
enum fsck_msg_id msg_id UNUSED,
1208-
const char *message)
1236+
int fsck_objects_error_function(struct fsck_options *o,
1237+
void *fsck_report,
1238+
enum fsck_msg_type msg_type,
1239+
enum fsck_msg_id msg_id UNUSED,
1240+
const char *message)
12091241
{
1242+
struct fsck_object_report *report = fsck_report;
1243+
const struct object_id *oid = report->oid;
1244+
12101245
if (msg_type == FSCK_WARN) {
12111246
warning("object %s: %s", fsck_describe_object(o, oid), message);
12121247
return 0;
@@ -1215,6 +1250,32 @@ int fsck_error_function(struct fsck_options *o,
12151250
return 1;
12161251
}
12171252

1253+
int fsck_refs_error_function(struct fsck_options *options UNUSED,
1254+
void *fsck_report,
1255+
enum fsck_msg_type msg_type,
1256+
enum fsck_msg_id msg_id UNUSED,
1257+
const char *message)
1258+
{
1259+
struct fsck_ref_report *report = fsck_report;
1260+
struct strbuf sb = STRBUF_INIT;
1261+
int ret = 0;
1262+
1263+
strbuf_addstr(&sb, report->path);
1264+
1265+
if (report->oid)
1266+
strbuf_addf(&sb, " -> (%s)", oid_to_hex(report->oid));
1267+
else if (report->referent)
1268+
strbuf_addf(&sb, " -> (%s)", report->referent);
1269+
1270+
if (msg_type == FSCK_WARN)
1271+
warning("%s: %s", sb.buf, message);
1272+
else
1273+
ret = error("%s: %s", sb.buf, message);
1274+
1275+
strbuf_release(&sb);
1276+
return ret;
1277+
}
1278+
12181279
static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done,
12191280
enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type,
12201281
struct fsck_options *options, const char *blob_type)
@@ -1270,6 +1331,17 @@ int fsck_finish(struct fsck_options *options)
12701331
return ret;
12711332
}
12721333

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+
12731345
int git_fsck_config(const char *var, const char *value,
12741346
const struct config_context *ctx, void *cb)
12751347
{
@@ -1303,16 +1375,17 @@ int git_fsck_config(const char *var, const char *value,
13031375
* Custom error callbacks that are used in more than one place.
13041376
*/
13051377

1306-
int fsck_error_cb_print_missing_gitmodules(struct fsck_options *o,
1307-
const struct object_id *oid,
1308-
enum object_type object_type,
1309-
enum fsck_msg_type msg_type,
1310-
enum fsck_msg_id msg_id,
1311-
const char *message)
1378+
int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o,
1379+
void *fsck_report,
1380+
enum fsck_msg_type msg_type,
1381+
enum fsck_msg_id msg_id,
1382+
const char *message)
13121383
{
13131384
if (msg_id == FSCK_MSG_GITMODULES_MISSING) {
1314-
puts(oid_to_hex(oid));
1385+
struct fsck_object_report *report = fsck_report;
1386+
puts(oid_to_hex(report->oid));
13151387
return 0;
13161388
}
1317-
return fsck_error_function(o, oid, object_type, msg_type, msg_id, message);
1389+
return fsck_objects_error_function(o, fsck_report,
1390+
msg_type, msg_id, message);
13181391
}

0 commit comments

Comments
 (0)