midx.c: extract MIDX lookup by object_dir

The first thing that write_midx_internal() does is load the MIDX corresponding to the given object directory, if one is present. Prepare for other functions in midx.c to do the same thing by extracting that operation out to a small helper function. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Oct 8, 2021 at 17:46 UTC 504131ac26895d1aaa814785d455c2c7f61c4e16
1 file changed +17 -10
midx.c
+17 -10
@@ -1107,6 +1107,22 @@ cleanup:
1107 return ret;
1108 }
1109
1110 +static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
1111 + const char *object_dir)
1112 +{
1113 + struct multi_pack_index *cur;
1114 +
1115 + /* Ensure the given object_dir is local, or a known alternate. */
1116 + find_odb(r, object_dir);
1117 +
1118 + for (cur = get_multi_pack_index(r); cur; cur = cur->next) {
1119 + if (!strcmp(object_dir, cur->object_dir))
1120 + return cur;
1121 + }
1122 +
1123 + return NULL;
1124 +}
1125 +
1126 static int write_midx_internal(const char *object_dir,
1127 struct string_list *packs_to_include,
1128 struct string_list *packs_to_drop,
@@ -1120,15 +1136,11 @@ static int write_midx_internal(const char *object_dir,
1136 struct hashfile *f = NULL;
1137 struct lock_file lk;
1138 struct write_midx_context ctx = { 0 };
1123 - struct multi_pack_index *cur;
1139 int pack_name_concat_len = 0;
1140 int dropped_packs = 0;
1141 int result = 0;
1142 struct chunkfile *cf;
1143
1129 - /* Ensure the given object_dir is local, or a known alternate. */
1130 - find_odb(the_repository, object_dir);
1131 -
1144 midx_name = get_midx_filename(object_dir);
1145 if (safe_create_leading_directories(midx_name))
1146 die_errno(_("unable to create leading directories of %s"),
@@ -1140,12 +1152,7 @@ static int write_midx_internal(const char *object_dir,
1152 * packs to include, since all packs and objects are copied
1153 * blindly from an existing MIDX if one is present.
1154 */
1143 - for (cur = get_multi_pack_index(the_repository); cur; cur = cur->next) {
1144 - if (!strcmp(object_dir, cur->object_dir)) {
1145 - ctx.m = cur;
1146 - break;
1147 - }
1148 - }
1155 + ctx.m = lookup_multi_pack_index(the_repository, object_dir);
1156 }
1157
1158 if (ctx.m && !midx_checksum_valid(ctx.m)) {