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

[PATCH 9/9] fetch: use ref prefix list to skip ls-refs

From
Jeff King <peff@peff.net>
Date
Mar 9, 2025, 03:21 UTC
Message-ID
<20250309032159.GI2334191@coredump.intra.peff.net>
In-Reply-To
<20250309030101.GA2334064@coredump.intra.peff.net>

In git-fetch we have an optimization to avoid issuing an ls-refs command to the server if we don't care about the value of any refs (e.g., because we are fetching exact object ids), saving a round-trip to the server. This comes from e70a3030e7 (fetch: do not list refs if fetching only hashes, 2018-09-27).

It uses an explicit flag "must_list_refs" to decide when we need to do so. That was needed back then, because the list of ref-prefixes was not always complete. If it was empty, it did not necessarily mean that we were not interested in any refs). But that is no longer the case; an empty list of prefixes means that we truly do not care about any refs.

And so rather than an explicit flag, we can just check whether we are interested in any ref prefixes. This simplifies the code slightly, as there is now a single source of truth for the decision.

It also fixes a bug in / optimizes a very unlikely case, which is:
  git fetch $remote ^foo $oid

I.e., a negative refspec combined with an exact oid fetch. This is somewhat nonsense, in that there are no positive refspecs mentioning refs to countermand with the negative one. But we should be able to do this without issuing an ls-refs command (excluding "foo" from the empty set will obviously still be the empty set).

However, the current code does not do so. The negative refspec is not counted as a noop in un-setting the must_list_refs flag (hardly the fault of e70a3030e7, as negative refspecs did not appear until much later). But by using the prefix list as a source of truth, this naturally just works; the negative refspec does not add a prefix to ask about, and hence does not trigger the ls-refs call.

This is esoteric enough that I didn't bother adding a test. The real value here is in the code simplification.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/fetch.c | 27 +++++++--------------------
 1 file changed, 7 insertions(+), 20 deletions(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index c26866e674..02af505469 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -1718,7 +1718,6 @@ static int do_fetch(struct transport *transport,
 	const struct ref *remote_refs;
 	struct transport_ls_refs_options transport_ls_refs_options =
 		TRANSPORT_LS_REFS_OPTIONS_INIT;
-	int must_list_refs = 1;
 	struct fetch_head fetch_head = { 0 };
 	struct strbuf err = STRBUF_INIT;
 
@@ -1737,21 +1736,7 @@ static int do_fetch(struct transport *transport,
 	}
 
 	if (rs->nr) {
-		int i;
-
 		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
-
-		/*
-		 * We can avoid listing refs if all of them are exact
-		 * OIDs
-		 */
-		must_list_refs = 0;
-		for (i = 0; i < rs->nr; i++) {
-			if (!rs->items[i].exact_sha1) {
-				must_list_refs = 1;
-				break;
-			}
-		}
 	} else {
 		struct branch *branch = branch_get(NULL);
 
@@ -1776,18 +1761,20 @@ static int do_fetch(struct transport *transport,
 				    "HEAD");
 	}
 
-	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
-		must_list_refs = 1;
+	if (tags == TAGS_SET || tags == TAGS_DEFAULT)
 		strvec_push(&transport_ls_refs_options.ref_prefixes,
 			    "refs/tags/");
-	}
 
-	if (must_list_refs &&
+	if (transport_ls_refs_options.ref_prefixes.nr &&
 	    uses_remote_tracking(transport, rs))
 		strvec_push(&transport_ls_refs_options.ref_prefixes,
 			    "HEAD");
 
-	if (must_list_refs) {
+	/*
+	 * Only initiate ref listing if we have at least one ref we want to
+	 * know about.
+	 */
+	if (transport_ls_refs_options.ref_prefixes.nr) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
 		remote_refs = transport_get_remote_refs(transport,
 							&transport_ls_refs_options);
-- 
2.49.0.rc1.381.gc60f5426ff
Previous: Taylor BlauNext: Taylor Blau
Message 58 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.