fsck: make fsck_config() re-usable

Move the fsck_config() function from builtin/fsck.c to fsck.[ch]. This allows for re-using it in other tools that expose fsck logic and want to support its configuration variables. A logical continuation of this change would be to use a common function for all of {fetch,receive}.fsck.* and fsck.*. See 5d477a334a6 (fsck (receive-pack): allow demoting errors to warnings, 2015-06-22) and my own 1362df0d413 (fetch: implement fetch.fsck.*, 2018-07-27) for the relevant code. However, those routines want to not parse the fsck.skipList into OIDs, but rather pass them along with the --strict option to another process. It would be possible to refactor that whole thing so we support e.g. a "fetch." prefix, then just keep track of the skiplist as a filename instead of parsing it, and learn to spew that all out from our internal structures into something we can append to the --strict option. But instead I'm planning to re-use this in "mktag", which'll just re-use these "fsck.*" variables as-is. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jan 5, 2021 at 20:42 UTC 1f3299fda9d0800d8e882540d36e4d78797e998e
3 files changed +32 -19
builtin/fsck.c
+1 -19
@@ -73,25 +73,7 @@ static const char *printable_type(const struct object_id *oid,
73
74 static int fsck_config(const char *var, const char *value, void *cb)
75 {
76 - if (strcmp(var, "fsck.skiplist") == 0) {
77 - const char *path;
78 - struct strbuf sb = STRBUF_INIT;
79 -
80 - if (git_config_pathname(&path, var, value))
81 - return 1;
82 - strbuf_addf(&sb, "skiplist=%s", path);
83 - free((char *)path);
84 - fsck_set_msg_types(&fsck_obj_options, sb.buf);
85 - strbuf_release(&sb);
86 - return 0;
87 - }
88 -
89 - if (skip_prefix(var, "fsck.", &var)) {
90 - fsck_set_msg_type(&fsck_obj_options, var, value);
91 - return 0;
92 - }
93 -
94 - return git_default_config(var, value, cb);
76 + return fsck_config_internal(var, value, cb, &fsck_obj_options);
77 }
78
79 static int objerror(struct object *obj, const char *err)
fsck.c
+24
@@ -1310,3 +1310,27 @@ int fsck_finish(struct fsck_options *options)
1310 oidset_clear(&gitmodules_done);
1311 return ret;
1312 }
1313 +
1314 +int fsck_config_internal(const char *var, const char *value, void *cb,
1315 + struct fsck_options *options)
1316 +{
1317 + if (strcmp(var, "fsck.skiplist") == 0) {
1318 + const char *path;
1319 + struct strbuf sb = STRBUF_INIT;
1320 +
1321 + if (git_config_pathname(&path, var, value))
1322 + return 1;
1323 + strbuf_addf(&sb, "skiplist=%s", path);
1324 + free((char *)path);
1325 + fsck_set_msg_types(options, sb.buf);
1326 + strbuf_release(&sb);
1327 + return 0;
1328 + }
1329 +
1330 + if (skip_prefix(var, "fsck.", &var)) {
1331 + fsck_set_msg_type(options, var, value);
1332 + return 0;
1333 + }
1334 +
1335 + return git_default_config(var, value, cb);
1336 +}
fsck.h
+7
@@ -103,4 +103,11 @@ void fsck_put_object_name(struct fsck_options *options,
103 const char *fsck_describe_object(struct fsck_options *options,
104 const struct object_id *oid);
105
106 +/*
107 + * git_config() callback for use by fsck-y tools that want to support
108 + * fsck.<msg> fsck.skipList etc.
109 + */
110 +int fsck_config_internal(const char *var, const char *value, void *cb,
111 + struct fsck_options *options);
112 +
113 #endif