midx-write.c: extract `should_include_pack()`

The add_pack_to_midx() callback used via for_each_file_in_pack_dir() is used to add packs with .idx files to the MIDX being written. Within this function, we have a pair of checks that discards packs which: - appear in an existing MIDX, if we successfully read an existing MIDX from disk - or, appear in the "to_include" list, if invoking the MIDX write machinery with the `--stdin-packs` command-line argument. A future commit will want to call a slight variant of these checks from the code that reuses all packs from an existing MIDX, as well as the current location via add_pack_to_midx(). The latter will be modified in subsequent commits to only reuse packs which appear in the to_include list, if one was given. Prepare for that step by extracting these checks as a subroutine that may be called from both places. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed May 29, 2024 at 18:55 UTC 364c0ffc5a3b23071eced255b50f1191a0deffd7
1 file changed +28 -20
midx-write.c
+28 -20
@@ -100,6 +100,32 @@ struct write_midx_context {
100 struct string_list *to_include;
101 };
102
103 +static int should_include_pack(const struct write_midx_context *ctx,
104 + const char *file_name)
105 +{
106 + /*
107 + * Note that at most one of ctx->m and ctx->to_include are set,
108 + * so we are testing midx_contains_pack() and
109 + * string_list_has_string() independently (guarded by the
110 + * appropriate NULL checks).
111 + *
112 + * We could support passing to_include while reusing an existing
113 + * MIDX, but don't currently since the reuse process drags
114 + * forward all packs from an existing MIDX (without checking
115 + * whether or not they appear in the to_include list).
116 + *
117 + * If we added support for that, these next two conditional
118 + * should be performed independently (likely checking
119 + * to_include before the existing MIDX).
120 + */
121 + if (ctx->m && midx_contains_pack(ctx->m, file_name))
122 + return 0;
123 + else if (ctx->to_include &&
124 + !string_list_has_string(ctx->to_include, file_name))
125 + return 0;
126 + return 1;
127 +}
128 +
129 static void add_pack_to_midx(const char *full_path, size_t full_path_len,
130 const char *file_name, void *data)
131 {
@@ -108,29 +134,11 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
134
135 if (ends_with(file_name, ".idx")) {
136 display_progress(ctx->progress, ++ctx->pack_paths_checked);
111 - /*
112 - * Note that at most one of ctx->m and ctx->to_include are set,
113 - * so we are testing midx_contains_pack() and
114 - * string_list_has_string() independently (guarded by the
115 - * appropriate NULL checks).
116 - *
117 - * We could support passing to_include while reusing an existing
118 - * MIDX, but don't currently since the reuse process drags
119 - * forward all packs from an existing MIDX (without checking
120 - * whether or not they appear in the to_include list).
121 - *
122 - * If we added support for that, these next two conditional
123 - * should be performed independently (likely checking
124 - * to_include before the existing MIDX).
125 - */
126 - if (ctx->m && midx_contains_pack(ctx->m, file_name))
127 - return;
128 - else if (ctx->to_include &&
129 - !string_list_has_string(ctx->to_include, file_name))
137 +
138 + if (!should_include_pack(ctx, file_name))
139 return;
140
141 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
133 -
142 p = add_packed_git(full_path, full_path_len, 0);
143 if (!p) {
144 warning(_("failed to add packfile '%s'"),