fetch: skip tasks related to fetching objects

cmd_fetch() does the following with the assumption that objects are fetched: * Run gc * Write commit graphs (if enabled by fetch.writeCommitGraph=true) However, neither of these tasks makes sense if objects are not fetched e.g. `git fetch --negotiate-only` never fetches objects. Speed up cmd_fetch() by bailing out early if we know for certain that objects will not be fetched. cmd_fetch() can bail out early whenever objects are not fetched, but for now this only considers --negotiate-only. The same optimization does not apply to `git fetch --dry-run` because that actually fetches objects; the dry run refers to not updating refs. Signed-off-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed Jan 18, 2022 at 16:00 UTC 135a12bc1472290ca6b9a4c2f06c838a1495a612
1 file changed +11
builtin/fetch.c
+11
@@ -2133,6 +2133,17 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
2133 strvec_clear(&options);
2134 }
2135
2136 + /*
2137 + * Skip irrelevant tasks because we know objects were not
2138 + * fetched.
2139 + *
2140 + * NEEDSWORK: as a future optimization, we can return early
2141 + * whenever objects were not fetched e.g. if we already have all
2142 + * of them.
2143 + */
2144 + if (negotiate_only)
2145 + goto cleanup;
2146 +
2147 prepare_repo_settings(the_repository);
2148 if (fetch_write_commit_graph > 0 ||
2149 (fetch_write_commit_graph < 0 &&