fetch: after refetch, encourage auto gc repacking

After invoking `fetch --refetch`, the object db will likely contain many duplicate objects. If auto-maintenance is enabled, invoke it with appropriate settings to encourage repacking/consolidation. * gc.autoPackLimit: unless this is set to 0 (disabled), override the value to 1 to force pack consolidation. * maintenance.incremental-repack.auto: unless this is set to 0, override the value to -1 to force incremental repacking. Signed-off-by: Robert Coup <robert@coup.net.nz> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Robert Coup committed Mar 28, 2022 at 14:02 UTC 7390f05a3c674e354ba2f52632046fa0a5c3e501
3 files changed +49 -2
Documentation/fetch-options.txt
+2 -1
@@ -169,7 +169,8 @@ ifndef::git-pull[]
169 associated objects that are already present locally, this option fetches
170 all objects as a fresh clone would. Use this to reapply a partial clone
171 filter from configuration or using `--filter=` when the filter
172 - definition has changed.
172 + definition has changed. Automatic post-fetch maintenance will perform
173 + object database pack consolidation to remove any duplicate objects.
174 endif::git-pull[]
175
176 --refmap=<refspec>::
builtin/fetch.c
+18 -1
@@ -2226,8 +2226,25 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
2226 NULL);
2227 }
2228
2229 - if (enable_auto_gc)
2229 + if (enable_auto_gc) {
2230 + if (refetch) {
2231 + /*
2232 + * Hint auto-maintenance strongly to encourage repacking,
2233 + * but respect config settings disabling it.
2234 + */
2235 + int opt_val;
2236 + if (git_config_get_int("gc.autopacklimit", &opt_val))
2237 + opt_val = -1;
2238 + if (opt_val != 0)
2239 + git_config_push_parameter("gc.autoPackLimit=1");
2240 +
2241 + if (git_config_get_int("maintenance.incremental-repack.auto", &opt_val))
2242 + opt_val = -1;
2243 + if (opt_val != 0)
2244 + git_config_push_parameter("maintenance.incremental-repack.auto=-1");
2245 + }
2246 run_auto_maintenance(verbosity < 0);
2247 + }
2248
2249 cleanup:
2250 string_list_clear(&list, 0);
t/t5616-partial-clone.sh
+29
@@ -216,6 +216,35 @@ test_expect_success 'fetch --refetch works with a shallow clone' '
216 test_line_count = 6 observed
217 '
218
219 +test_expect_success 'fetch --refetch triggers repacking' '
220 + GIT_TRACE2_CONFIG_PARAMS=gc.autoPackLimit,maintenance.incremental-repack.auto &&
221 + export GIT_TRACE2_CONFIG_PARAMS &&
222 +
223 + GIT_TRACE2_EVENT="$PWD/trace1.event" \
224 + git -C pc1 fetch --refetch origin &&
225 + test_subcommand git maintenance run --auto --no-quiet <trace1.event &&
226 + grep \"param\":\"gc.autopacklimit\",\"value\":\"1\" trace1.event &&
227 + grep \"param\":\"maintenance.incremental-repack.auto\",\"value\":\"-1\" trace1.event &&
228 +
229 + GIT_TRACE2_EVENT="$PWD/trace2.event" \
230 + git -c protocol.version=0 \
231 + -c gc.autoPackLimit=0 \
232 + -c maintenance.incremental-repack.auto=1234 \
233 + -C pc1 fetch --refetch origin &&
234 + test_subcommand git maintenance run --auto --no-quiet <trace2.event &&
235 + grep \"param\":\"gc.autopacklimit\",\"value\":\"0\" trace2.event &&
236 + grep \"param\":\"maintenance.incremental-repack.auto\",\"value\":\"-1\" trace2.event &&
237 +
238 + GIT_TRACE2_EVENT="$PWD/trace3.event" \
239 + git -c protocol.version=0 \
240 + -c gc.autoPackLimit=1234 \
241 + -c maintenance.incremental-repack.auto=0 \
242 + -C pc1 fetch --refetch origin &&
243 + test_subcommand git maintenance run --auto --no-quiet <trace3.event &&
244 + grep \"param\":\"gc.autopacklimit\",\"value\":\"1\" trace3.event &&
245 + grep \"param\":\"maintenance.incremental-repack.auto\",\"value\":\"0\" trace3.event
246 +'
247 +
248 test_expect_success 'partial clone with transfer.fsckobjects=1 works with submodules' '
249 test_create_repo submodule &&
250 test_commit -C submodule mycommit &&