]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fetch: fix following tags when fetching specific OID
authorTaylor Blau <me@ttaylorr.com>
Fri, 7 Mar 2025 23:27:03 +0000 (18:27 -0500)
committerJunio C Hamano <gitster@pobox.com>
Sat, 8 Mar 2025 00:15:18 +0000 (16:15 -0800)
In 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22),
unconditionally adds "HEAD" to the list of ref prefixes we send to the
server.

This breaks a core assumption that the list of prefixes we send to the
server is complete. We must either send all prefixes we care about, or
none at all (in the latter case the server then advertises everything).

The tag following code is careful to only add "refs/tags/" to the list
of prefixes if there are already entries in the prefix list. But because
the new code from 3f763ddf28 runs after the tag code, and because it
unconditionally adds to the prefix list, we may end up with a prefix
list that _should_ have "refs/tags/" in it, but doesn't.

When that is the case, the server does not advertise any tags, and our
auto-following breaks because we never learned about any tags in the
first place.

Fix this by only adding "HEAD" to the ref prefixes when we know that we
are already limiting the advertisement. In either case we'll learn about
HEAD (either through the limited advertisement, or implicitly through a
full advertisement).

Reported-by: Igor Todorovski <itodorov@ca.ibm.com>
Co-authored-by: Jeff King <peff@peff.net>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/fetch.c
t/t5503-tagfollow.sh

index fe2b26c74aecab386b3fb9be5b27761f09be800c..a06d13050165fcb6fc013ddbf44a6413ab47aa36 100644 (file)
@@ -1777,7 +1777,9 @@ static int do_fetch(struct transport *transport,
 
        if (uses_remote_tracking(transport, rs)) {
                must_list_refs = 1;
-               strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+               if (transport_ls_refs_options.ref_prefixes.nr)
+                       strvec_push(&transport_ls_refs_options.ref_prefixes,
+                                   "HEAD");
        }
 
        if (must_list_refs) {
index 195fc64dd44ae74c1546698e111f1a19c07dbb04..845ca43ea0a7d7db61d4c6be7ae162708bcd6b3c 100755 (executable)
@@ -160,4 +160,18 @@ test_expect_success 'new clone fetch main and tags' '
        test_cmp expect actual
 '
 
+test_expect_success 'fetch specific OID with tag following' '
+       git init --bare clone3.git &&
+       (
+               cd clone3.git &&
+               git remote add origin .. &&
+               git fetch origin $B:refs/heads/main &&
+
+               git -C .. for-each-ref >expect &&
+               git for-each-ref >actual &&
+
+               test_cmp expect actual
+       )
+'
+
 test_done