git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] fetch: only respect followRemoteHEAD with configured refspecs

From
Jeff King <peff@peff.net>
Date
Mar 18, 2025, 05:40 UTC
Message-ID
<20250318054017.GA2051617@coredump.intra.peff.net>
In-Reply-To
<20250318053905.GA2051217@coredump.intra.peff.net>

The new followRemoteHEAD feature is triggered for almost every fetch, causing us to ask the server about the remote "HEAD" and to consider updating our local tracking HEAD symref. This patch limits the feature only to the case when we are fetching a remote using its configured refspecs (typically into its refs/remotes/ hierarchy). There are two reasons for this.

One is efficiency. E.g., the fixes in 6c915c3f85 (fetch: do not ask for HEAD unnecessarily, 2024-12-06) and 20010b8c20 (fetch: avoid ls-refs only to ask for HEAD symref update, 2025-03-08) were aimed at reducing the work we do when we would not be able to update HEAD anyway. But they do not quite cover all cases. The remaining one is:

  git fetch origin refs/heads/foo:refs/remotes/origin/foo

which _sometimes_ can update HEAD, but usually not. And that leads us to the second point, which is being simple and explainable.

The code for updating the tracking HEAD symref requires both that we learned which ref the remote HEAD points at, and that the server advertised that ref to us. But because the v2 protocol narrows the server's advertisement, the command above would not typically update HEAD at all, unless it happened to point to the "foo" branch. Or even weirder, it probably _would_ update if the server is very old and supports only the v0 protocol, which always gives a full advertisement.

This creates confusing behavior for the user: sometimes we may try to update HEAD and sometimes not, depending on vague rules.

One option here would be to loosen the update code to accept the remote HEAD even if the server did not advertise that ref. I think that could work, but it may also lead to interesting corner cases (e.g., creating a dangling symref locally, even though the branch is not unborn on the server, if we happen not to have fetched it).

So let's instead simplify the rules: we'll only consider updating the tracking HEAD symref when we're doing a full fetch of the remote's configured refs. This is easy to implement; we can just set a flag at the moment we realize we're using the configured refspecs. And we can drop the special case code added by 6c915c3f85 and 20010b8c20, since this covers those cases. The existing tests from those commits still pass.

In t5505, an incidental call to "git fetch <remote> <refspec>" updated HEAD, which caused us to adjust the test in 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22). We can now adjust that back to how it was before the feature was added.

Even though t5505 is incidentally testing our new desired behavior, we'll add an explicit test in t5510 to make sure it is covered.

Signed-off-by: Jeff King <peff@peff.net>
---
The test is weird with the way it calls "cd", but it matches the nearby
ones. I think that could all be simplified, but I'll leave that for
another day (or never).
 Documentation/config/remote.adoc |  3 ++-
 builtin/fetch.c                  | 25 ++++++-------------------
 t/t5505-remote.sh                |  2 +-
 t/t5510-fetch.sh                 | 14 ++++++++++++++
 4 files changed, 23 insertions(+), 21 deletions(-)
diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc
index 25fe219d10..91e46f66f5 100644
--- a/Documentation/config/remote.adoc
+++ b/Documentation/config/remote.adoc
@@ -108,7 +108,8 @@ the values inherited from a lower priority configuration files (e.g.
 `$HOME/.gitconfig`).
 
 remote.<name>.followRemoteHEAD::
-	How linkgit:git-fetch[1] should handle updates to `remotes/<name>/HEAD`.
+	How linkgit:git-fetch[1] should handle updates to `remotes/<name>/HEAD`
+	when fetching using the configured refspecs of a remote.
 	The default value is "create", which will create `remotes/<name>/HEAD`
 	if it exists on the remote, but not locally; this will not touch an
 	already existing local reference. Setting it to "warn" will print
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 02af505469..66f5ae31b6 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -1691,21 +1691,6 @@ static int set_head(const struct ref *remote_refs, struct remote *remote)
 	return result;
 }
 
-static int uses_remote_tracking(struct transport *transport, struct refspec *rs)
-{
-	if (!remote_is_configured(transport->remote, 0))
-		return 0;
-
-	if (!rs->nr)
-		rs = &transport->remote->fetch;
-
-	for (int i = 0; i < rs->nr; i++)
-		if (rs->items[i].dst)
-			return 1;
-
-	return 0;
-}
-
 static int do_fetch(struct transport *transport,
 		    struct refspec *rs,
 		    const struct fetch_config *config)
@@ -1720,6 +1705,7 @@ static int do_fetch(struct transport *transport,
 		TRANSPORT_LS_REFS_OPTIONS_INIT;
 	struct fetch_head fetch_head = { 0 };
 	struct strbuf err = STRBUF_INIT;
+	int do_set_head = 0;
 
 	if (tags == TAGS_DEFAULT) {
 		if (transport->remote->fetch_tags == 2)
@@ -1740,9 +1726,11 @@ static int do_fetch(struct transport *transport,
 	} else {
 		struct branch *branch = branch_get(NULL);
 
-		if (transport->remote->fetch.nr)
+		if (transport->remote->fetch.nr) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
+			do_set_head = 1;
+		}
 		if (branch_has_merge_config(branch) &&
 		    !strcmp(branch->remote_name, transport->remote->name)) {
 			int i;
@@ -1765,8 +1753,7 @@ static int do_fetch(struct transport *transport,
 		strvec_push(&transport_ls_refs_options.ref_prefixes,
 			    "refs/tags/");
 
-	if (transport_ls_refs_options.ref_prefixes.nr &&
-	    uses_remote_tracking(transport, rs))
+	if (do_set_head)
 		strvec_push(&transport_ls_refs_options.ref_prefixes,
 			    "HEAD");
 
@@ -1918,7 +1905,7 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
-	if (set_head(remote_refs, transport->remote))
+	if (do_set_head && set_head(remote_refs, transport->remote))
 		;
 		/*
 		 * Way too many cases where this can go wrong
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index bb7e0c6879..fed5823e85 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -499,7 +499,7 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 		cd test &&
 		git fetch two "refs/heads/*:refs/remotes/two/*" &&
 		git remote set-head --auto two >output 2>&1 &&
-		echo "${SQ}two/HEAD${SQ} is unchanged and points to ${SQ}main${SQ}" >expect &&
+		echo "${SQ}two/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
 		test_cmp expect output
 	)
 '
diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh
index 5f350facf5..ad23dd11ef 100755
--- a/t/t5510-fetch.sh
+++ b/t/t5510-fetch.sh
@@ -250,6 +250,20 @@ test_expect_success "fetch test followRemoteHEAD always" '
 	)
 '
 
+test_expect_success 'followRemoteHEAD does not kick in with refspecs' '
+	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	(
+		cd "$D" &&
+		cd two &&
+		git remote set-head origin other &&
+		git config set remote.origin.followRemoteHEAD always &&
+		git fetch origin refs/heads/main:refs/remotes/origin/main &&
+		echo refs/remotes/origin/other >expect &&
+		git symbolic-ref refs/remotes/origin/HEAD >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success 'fetch --prune on its own works as expected' '
 	cd "$D" &&
 	git clone . prune &&
-- 
2.49.0.390.gc9996a6a6c
Previous: Jeff KingNext: Taylor Blau
Message 53 of 61 in “Tags are no longer fetched when fetching specific commit”
  1. Igor TodorovskiJan 30, 2025
  2. Taylor BlauFeb 13, 2025
  3. Bence FerdinandyFeb 14, 2025
  4. Junio C HamanoFeb 14, 2025
  5. Jeff KingFeb 21, 2025
  6. fetch: fix following tags when fetching specific OIDTaylor Blau, Mar 7, 2025
  7. Taylor BlauMar 7, 2025
  8. Junio C HamanoMar 8, 2025
  9. Bence FerdinandyMar 8, 2025
  10. 0/9 fetch: further ref-prefix cleanups and optimizationsJeff King, Mar 9, 2025
  11. 1/9 t5702: fix typo in test nameJeff King, Mar 9, 2025
  12. Taylor BlauMar 12, 2025
  13. Jeff KingMar 13, 2025
  14. 2/9 t5516: prefer "oid" to "sha1" in some test titlesJeff King, Mar 9, 2025
  15. 3/9 t5516: drop NEEDSWORK about v2 reachability behaviorJeff King, Mar 9, 2025
  16. Taylor BlauMar 12, 2025
  17. 4/9 t5516: beef up exact-oid ref prefixes testJeff King, Mar 9, 2025
  18. 5/9 refspec_ref_prefixes(): clean up refspec_item logicJeff King, Mar 9, 2025
  19. Taylor BlauMar 12, 2025
  20. Jeff KingMar 13, 2025
  21. Junio C HamanoMar 13, 2025
  22. 0/4 refspec: treat 'fetch' as a Boolean valueTaylor Blau, Mar 17, 2025
  23. 1/4 refspec: treat 'fetch' as a Boolean valueTaylor Blau, Mar 17, 2025
  24. Jeff KingMar 18, 2025
  25. Jeff KingMar 18, 2025
  26. Taylor BlauMar 18, 2025
  27. 2/4 refspec: replace `refspec_init()` with fetch/push variantsTaylor Blau, Mar 17, 2025
  28. 3/4 refspec: remove refspec_item_init_or_die()Taylor Blau, Mar 17, 2025
  29. 4/4 refspec: replace `refspec_item_init()` with fetch/push variantsTaylor Blau, Mar 17, 2025
  30. Junio C HamanoMar 17, 2025
  31. Taylor BlauMar 18, 2025
  32. 0/4 refspec: treat 'fetch' as a Boolean valueTaylor Blau, Mar 18, 2025
  33. 1/4 refspec: treat 'fetch' as a Boolean valueTaylor Blau, Mar 18, 2025
  34. 2/4 refspec: replace `refspec_init()` with fetch/push variantsTaylor Blau, Mar 18, 2025
  35. 3/4 refspec: remove refspec_item_init_or_die()Taylor Blau, Mar 18, 2025
  36. 4/4 refspec: replace `refspec_item_init()` with fetch/push variantsTaylor Blau, Mar 18, 2025
  37. Elijah NewrenMar 19, 2025
  38. Taylor BlauMar 17, 2025
  39. Junio C HamanoMar 17, 2025
  40. Taylor BlauMar 18, 2025
  41. 6/9 fetch: ask server to advertise HEAD for config-less fetchJeff King, Mar 9, 2025
  42. Taylor BlauMar 12, 2025
  43. Jeff KingMar 13, 2025
  44. Junio C HamanoMar 13, 2025
  45. Taylor BlauMar 17, 2025
  46. 7/9 fetch: stop protecting additions to ref-prefix listJeff King, Mar 9, 2025
  47. Taylor BlauMar 12, 2025
  48. 8/9 fetch: avoid ls-refs only to ask for HEAD symref updateJeff King, Mar 9, 2025
  49. Junio C HamanoMar 13, 2025
  50. Jeff KingMar 17, 2025
  51. Junio C HamanoMar 17, 2025
  52. 0/2 limiting followRemoteHEAD being usedJeff King, Mar 18, 2025
  53. 1/2 fetch: only respect followRemoteHEAD with configured refspecsJeff King, Mar 18, 2025
  54. Taylor BlauMar 18, 2025
  55. 2/2 fetch: don't ask for remote HEAD if followRemoteHEAD is "never"Jeff King, Mar 18, 2025
  56. Junio C HamanoMar 18, 2025
  57. Taylor BlauMar 18, 2025
  58. 9/9 fetch: use ref prefix list to skip ls-refsJeff King, Mar 9, 2025
  59. Taylor BlauMar 12, 2025
  60. Taylor BlauMar 12, 2025
  61. Jeff KingMar 13, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.