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

[RFC PATCH v2 2/4] transport: do not list refs if possible

From
JTJonathan Tan <jonathantanmy@google.com>
Date
Sep 27, 2018, 19:24 UTC
Message-ID
<61ad64245470e51cb97988e0f2f5ea76c9e2276c.1538075680.git.jonathantanmy@google.com>
In-Reply-To
<cover.1538075680.git.jonathantanmy@google.com>

When all refs to be fetched are exact OIDs, it is possible to perform a fetch without requiring the remote to list refs if protocol v2 is used. Teach Git to do this.

This currently has an effect only for lazy fetches done from partial clones. The change necessary to likewise optimize "git fetch <remote> <sha-1>" will be done in a subsequent patch.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
 fetch-pack.c           |  2 +-
 t/t5702-protocol-v2.sh |  5 +++++
 transport.c            | 13 +++++++++++--
 3 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/fetch-pack.c b/fetch-pack.c
index 75047a4b2a..15652b4776 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1598,7 +1598,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
 	if (nr_sought)
 		nr_sought = remove_duplicates_in_refs(sought, nr_sought);
 
-	if (!ref) {
+	if (version != protocol_v2 && !ref) {
 		packet_flush(fd[1]);
 		die(_("no matching remote head"));
 	}
diff --git a/t/t5702-protocol-v2.sh b/t/t5702-protocol-v2.sh
index 3beeed4546..a316bb9bf4 100755
--- a/t/t5702-protocol-v2.sh
+++ b/t/t5702-protocol-v2.sh
@@ -286,6 +286,11 @@ test_expect_success 'dynamically fetch missing object' '
 	grep "version 2" trace
 '
 
+test_expect_success 'when dynamically fetching missing object, do not list refs' '
+	cat trace &&
+	! grep "git> command=ls-refs" trace
+'
+
 test_expect_success 'partial fetch' '
 	rm -rf client "$(pwd)/trace" &&
 	git init client &&
diff --git a/transport.c b/transport.c
index 5fb9ff6b56..4329cca8e5 100644
--- a/transport.c
+++ b/transport.c
@@ -341,8 +341,17 @@ static int fetch_refs_via_pack(struct transport *transport,
 	args.server_options = transport->server_options;
 	args.negotiation_tips = data->options.negotiation_tips;
 
-	if (!data->got_remote_heads)
-		refs_tmp = get_refs_via_connect(transport, 0, NULL);
+	if (!data->got_remote_heads) {
+		int i;
+		int must_list_refs = 0;
+		for (i = 0; i < nr_heads; i++) {
+			if (!to_fetch[i]->exact_oid) {
+				must_list_refs = 1;
+				break;
+			}
+		}
+		refs_tmp = handshake(transport, 0, NULL, must_list_refs);
+	}
 
 	switch (data->version) {
 	case protocol_v2:
-- 
2.19.0.605.g01d371f741-goog
Previous: Jonathan TanNext: Stefan Beller
Message 5 of 12 in “transport: list refs before fetch if necessary”
  1. transport: list refs before fetch if necessaryJonathan Tan, Sep 25, 2018
  2. Junio C HamanoSep 25, 2018
  3. 0/4 Avoid ls-refs when possible in protocol v2Jonathan Tan, Sep 27, 2018
  4. 1/4 transport: allow skipping of ref listingJonathan Tan, Sep 27, 2018
  5. 2/4 transport: do not list refs if possibleJonathan Tan, Sep 27, 2018
  6. Stefan BellerSep 27, 2018
  7. Junio C HamanoOct 7, 2018
  8. 3/4 transport: list refs before fetch if necessaryJonathan Tan, Sep 27, 2018
  9. 4/4 fetch: do not list refs if fetching only hashesJonathan Tan, Sep 27, 2018
  10. Stefan BellerSep 27, 2018
  11. Jonathan TanSep 28, 2018
  12. Junio C HamanoSep 27, 2018

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.