fetch-pack: use new fsck API to printing dangling submodules

Refactor the check added in 5476e1efde (fetch-pack: print and use dangling .gitmodules, 2021-02-22) to make use of us now passing the "msg_id" to the user defined "error_func". We can now compare against the FSCK_MSG_GITMODULES_MISSING instead of parsing the generated message. Let's also replace register_found_gitmodules() with directly manipulating the "gitmodules_found" member. A recent commit moved it into "fsck_options" so we could do this here. I'm sticking this callback in fsck.c. Perhaps in the future we'd like to accumulate such callbacks into another file (maybe fsck-cb.c, similar to parse-options-cb.c?), but while we've got just the one let's just put it into fsck.c. A better alternative in this case would be some library some more obvious library shared by fetch-pack.c ad builtin/index-pack.c, but there isn't such a thing. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Mar 28, 2021 at 15:15 UTC 3745e2693de3dd5420221782ed050cae6ebf6fec
4 files changed +39 -51
builtin/index-pack.c
+1 -20
@@ -120,7 +120,7 @@ static int nr_threads;
120 static int from_stdin;
121 static int strict;
122 static int do_fsck_object;
123 -static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
123 +static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
124 static int verbose;
125 static int show_resolving_progress;
126 static int show_stat;
@@ -1713,24 +1713,6 @@ static void show_pack_info(int stat_only)
1713 }
1714 }
1715
1716 -static int print_dangling_gitmodules(struct fsck_options *o,
1717 - const struct object_id *oid,
1718 - enum object_type object_type,
1719 - enum fsck_msg_type msg_type,
1720 - enum fsck_msg_id msg_id,
1721 - const char *message)
1722 -{
1723 - /*
1724 - * NEEDSWORK: Plumb the MSG_ID (from fsck.c) here and use it
1725 - * instead of relying on this string check.
1726 - */
1727 - if (starts_with(message, "gitmodulesMissing")) {
1728 - printf("%s\n", oid_to_hex(oid));
1729 - return 0;
1730 - }
1731 - return fsck_error_function(o, oid, object_type, msg_type, msg_id, message);
1732 -}
1733 -
1716 int cmd_index_pack(int argc, const char **argv, const char *prefix)
1717 {
1718 int i, fix_thin_pack = 0, verify = 0, stat_only = 0, rev_index;
@@ -1761,7 +1743,6 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1743
1744 read_replace_refs = 0;
1745 fsck_options.walk = mark_link;
1764 - fsck_options.error_func = print_dangling_gitmodules;
1746
1747 reset_pack_idx_option(&opts);
1748 git_config(git_index_pack_config, &opts);
fetch-pack.c
+8 -23
@@ -38,7 +38,7 @@ static int server_supports_filtering;
38 static int advertise_sid;
39 static struct shallow_lock shallow_lock;
40 static const char *alternate_shallow_file;
41 -static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
41 +static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
42 static struct strbuf fsck_msg_types = STRBUF_INIT;
43 static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
44
@@ -988,21 +988,6 @@ static int cmp_ref_by_name(const void *a_, const void *b_)
988 return strcmp(a->name, b->name);
989 }
990
991 -static void fsck_gitmodules_oids(struct oidset *gitmodules_oids)
992 -{
993 - struct oidset_iter iter;
994 - const struct object_id *oid;
995 -
996 - if (!oidset_size(gitmodules_oids))
997 - return;
998 -
999 - oidset_iter_init(gitmodules_oids, &iter);
1000 - while ((oid = oidset_iter_next(&iter)))
1001 - register_found_gitmodules(&fsck_options, oid);
1002 - if (fsck_finish(&fsck_options))
1003 - die("fsck failed");
1004 -}
1005 -
991 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
992 int fd[2],
993 const struct ref *orig_ref,
@@ -1017,7 +1002,6 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1002 int agent_len;
1003 struct fetch_negotiator negotiator_alloc;
1004 struct fetch_negotiator *negotiator;
1020 - struct oidset gitmodules_oids = OIDSET_INIT;
1005
1006 negotiator = &negotiator_alloc;
1007 fetch_negotiator_init(r, negotiator);
@@ -1134,9 +1118,10 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1118 else
1119 alternate_shallow_file = NULL;
1120 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
1137 - &gitmodules_oids))
1121 + &fsck_options.gitmodules_found))
1122 die(_("git fetch-pack: fetch failed."));
1139 - fsck_gitmodules_oids(&gitmodules_oids);
1123 + if (fsck_finish(&fsck_options))
1124 + die("fsck failed");
1125
1126 all_done:
1127 if (negotiator)
@@ -1587,7 +1572,6 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1572 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1573 int i;
1574 struct strvec index_pack_args = STRVEC_INIT;
1590 - struct oidset gitmodules_oids = OIDSET_INIT;
1575
1576 negotiator = &negotiator_alloc;
1577 fetch_negotiator_init(r, negotiator);
@@ -1678,7 +1662,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1662 process_section_header(&reader, "packfile", 0);
1663 if (get_pack(args, fd, pack_lockfiles,
1664 packfile_uris.nr ? &index_pack_args : NULL,
1681 - sought, nr_sought, &gitmodules_oids))
1665 + sought, nr_sought, &fsck_options.gitmodules_found))
1666 die(_("git fetch-pack: fetch failed."));
1667 do_check_stateless_delimiter(args, &reader);
1668
@@ -1721,7 +1705,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1705
1706 packname[the_hash_algo->hexsz] = '\0';
1707
1724 - parse_gitmodules_oids(cmd.out, &gitmodules_oids);
1708 + parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
1709
1710 close(cmd.out);
1711
@@ -1742,7 +1726,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1726 string_list_clear(&packfile_uris, 0);
1727 strvec_clear(&index_pack_args);
1728
1745 - fsck_gitmodules_oids(&gitmodules_oids);
1729 + if (fsck_finish(&fsck_options))
1730 + die("fsck failed");
1731
1732 if (negotiator)
1733 negotiator->release(negotiator);
fsck.c
+18 -5
@@ -1196,11 +1196,6 @@ int fsck_error_function(struct fsck_options *o,
1196 return 1;
1197 }
1198
1199 -void register_found_gitmodules(struct fsck_options *options, const struct object_id *oid)
1200 -{
1201 - oidset_insert(&options->gitmodules_found, oid);
1202 -}
1203 -
1199 int fsck_finish(struct fsck_options *options)
1200 {
1201 int ret = 0;
@@ -1266,3 +1261,21 @@ int git_fsck_config(const char *var, const char *value, void *cb)
1261
1262 return git_default_config(var, value, cb);
1263 }
1264 +
1265 +/*
1266 + * Custom error callbacks that are used in more than one place.
1267 + */
1268 +
1269 +int fsck_error_cb_print_missing_gitmodules(struct fsck_options *o,
1270 + const struct object_id *oid,
1271 + enum object_type object_type,
1272 + enum fsck_msg_type msg_type,
1273 + enum fsck_msg_id msg_id,
1274 + const char *message)
1275 +{
1276 + if (msg_id == FSCK_MSG_GITMODULES_MISSING) {
1277 + puts(oid_to_hex(oid));
1278 + return 0;
1279 + }
1280 + return fsck_error_function(o, oid, object_type, msg_type, msg_id, message);
1281 +}
fsck.h
+12 -3
@@ -111,6 +111,12 @@ int fsck_error_function(struct fsck_options *o,
111 const struct object_id *oid, enum object_type object_type,
112 enum fsck_msg_type msg_type, enum fsck_msg_id msg_id,
113 const char *message);
114 +int fsck_error_cb_print_missing_gitmodules(struct fsck_options *o,
115 + const struct object_id *oid,
116 + enum object_type object_type,
117 + enum fsck_msg_type msg_type,
118 + enum fsck_msg_id msg_id,
119 + const char *message);
120
121 struct fsck_options {
122 fsck_walk_func walk;
@@ -135,6 +141,12 @@ struct fsck_options {
141 .gitmodules_done = OIDSET_INIT, \
142 .error_func = fsck_error_function, \
143 }
144 +#define FSCK_OPTIONS_MISSING_GITMODULES { \
145 + .strict = 1, \
146 + .gitmodules_found = OIDSET_INIT, \
147 + .gitmodules_done = OIDSET_INIT, \
148 + .error_func = fsck_error_cb_print_missing_gitmodules, \
149 +}
150
151 /* descend in all linked child objects
152 * the return value is:
@@ -152,9 +164,6 @@ int fsck_walk(struct object *obj, void *data, struct fsck_options *options);
164 int fsck_object(struct object *obj, void *data, unsigned long size,
165 struct fsck_options *options);
166
155 -void register_found_gitmodules(struct fsck_options *options,
156 - const struct object_id *oid);
157 -
167 /*
168 * fsck a tag, and pass info about it back to the caller. This is
169 * exposed fsck_object() internals for git-mktag(1).