Skip to contentSkip to conversation

Push negotiation

PATCH Superseded Jonathan Tan 3 participants 33 messages Apr 9, 2021 – May 6, 2021 Documentation

lore
Share & tools
lore
#0 lore raw

cover Push negotiation

Here are patches for push negotiation. The basic idea is that we can reuse part of the fetch protocol (and code) to find out what is in common between the client and server, and then use that information to further narrow the objects sent in the packfile during a push.

Patch 1 is a bug fix that probably should be merged even if the rest aren't. Patches 2-4 are refactorings in preparation for the future patches. Patches 5-6 contain the actual logic and documentation.

I have written more about it in my prior work [1], although the commit messages and documentation in patches 5-6 should be enough to explain what's going on. (If they're not, feel free to make review comments.)

The main change from [1] is that the client-side code that used to be in builtin/fetch-pack.c is now in builtin/fetch.c, because I realized that builtin/fetch-pack.c does not support HTTP. Other than that, all the "what hasn't been done yet" items have been done except for statistics in the commit message.

[1] https://lore.kernel.org/git/[email protected]/

Jonathan Tan (6):
fetch-pack: buffer object-format with other args
fetch-pack: refactor process_acks()
fetch-pack: refactor add_haves()
fetch-pack: refactor command and capability write
fetch: teach independent negotiation (no packfile)
send-pack: support push negotiation

 Documentation/config/push.txt           |   7 +
 Documentation/technical/protocol-v2.txt |   8 +
 builtin/fetch.c                         |  27 ++-
 fetch-pack.c                            | 224 +++++++++++++++---------
 fetch-pack.h                            |  11 ++
 object.h                                |   2 +-
 send-pack.c                             |  61 ++++++-
 t/t5516-fetch-push.sh                   |  35 ++++
 t/t5701-git-serve.sh                    |   2 +-
 t/t5702-protocol-v2.sh                  |  89 ++++++++++
 transport-helper.c                      |  10 ++
 transport.c                             |  30 +++-
 transport.h                             |   6 +
 upload-pack.c                           |  18 +-
 14 files changed, 430 insertions(+), 100 deletions(-)
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#1 lore raw

PATCH 1/6 fetch-pack: buffer object-format with other args

In send_fetch_request(), "object-format" is written directly to the file descriptor, as opposed to the other arguments, which are buffered. Buffer "object-format" as well. "object-format" must be buffered; in particular, it must appear after "command=fetch" in the request.

This divergence was introduced in 4b831208bb ("fetch-pack: parse and advertise the object-format capability", 2020-05-27), perhaps as an oversight (the surrounding code at the point of this commit has already been using a request buffer.)

---
 fetch-pack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fetch-pack.c +1 -1
index 6e68276aa3..2318ebe680 100644
@@ -1251,7 +1251,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
 			die(_("mismatched algorithms: client %s; server %s"),
 			    the_hash_algo->name, hash_name);
-		packet_write_fmt(fd_out, "object-format=%s", the_hash_algo->name);
+		packet_buf_write(&req_buf, "object-format=%s", the_hash_algo->name);
 	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
 		die(_("the server does not support algorithm '%s'"),
 		    the_hash_algo->name);
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#4 lore raw

PATCH 2/6 fetch-pack: refactor process_acks()

In reply to Jonathan Tan

A subsequent commit will need part, but not all, of the functionality in process_acks(), so move some of its functionality to its sole caller do_fetch_pack_v2(). As a side effect, the resulting code is also shorter.

---
 fetch-pack.c | 70 +++++++++++++++++-----------------------------------
 1 file changed, 22 insertions(+), 48 deletions(-)
fetch-pack.c +22 -48
index 2318ebe680..9f3901cdba 100644
@@ -1351,35 +1351,11 @@ static int process_section_header(struct packet_reader *reader,
 	return ret;
 }
 
-enum common_found {
-	/*
-	 * No commit was found to be possessed by both the client and the
-	 * server, and "ready" was not received.
-	 */
-	NO_COMMON_FOUND,
-
-	/*
-	 * At least one commit was found to be possessed by both the client and
-	 * the server, and "ready" was not received.
-	 */
-	COMMON_FOUND,
-
-	/*
-	 * "ready" was received, indicating that the server is ready to send
-	 * the packfile without any further negotiation.
-	 */
-	READY
-};
-
-static enum common_found process_acks(struct fetch_negotiator *negotiator,
-				      struct packet_reader *reader,
-				      struct oidset *common)
+static int process_ack(struct fetch_negotiator *negotiator,
+		       struct packet_reader *reader,
+		       struct object_id *common_oid,
+		       int *received_ready)
 {
-	/* received */
-	int received_ready = 0;
-	int received_ack = 0;
-
-	process_section_header(reader, "acknowledgments", 0);
 	while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
 		const char *arg;
 
@@ -1387,20 +1363,17 @@ static enum common_found process_acks(struct fetch_negotiator *negotiator,
 			continue;
 
 		if (skip_prefix(reader->line, "ACK ", &arg)) {
-			struct object_id oid;
-			received_ack = 1;
-			if (!get_oid_hex(arg, &oid)) {
+			if (!get_oid_hex(arg, common_oid)) {
 				struct commit *commit;
-				oidset_insert(common, &oid);
-				commit = lookup_commit(the_repository, &oid);
+				commit = lookup_commit(the_repository, common_oid);
 				if (negotiator)
 					negotiator->ack(negotiator, commit);
 			}
-			continue;
+			return 1;
 		}
 
 		if (!strcmp(reader->line, "ready")) {
-			received_ready = 1;
+			*received_ready = 1;
 			continue;
 		}
 
@@ -1418,13 +1391,12 @@ static enum common_found process_acks(struct fetch_negotiator *negotiator,
 	 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
 	 * otherwise.
 	 */
-	if (received_ready && reader->status != PACKET_READ_DELIM)
+	if (*received_ready && reader->status != PACKET_READ_DELIM)
 		die(_("expected packfile to be sent after 'ready'"));
-	if (!received_ready && reader->status != PACKET_READ_FLUSH)
+	if (!*received_ready && reader->status != PACKET_READ_FLUSH)
 		die(_("expected no other sections to be sent after no 'ready'"));
 
-	return received_ready ? READY :
-		(received_ack ? COMMON_FOUND : NO_COMMON_FOUND);
+	return 0;
 }
 
 static void receive_shallow_info(struct fetch_pack_args *args,
@@ -1573,6 +1545,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 	struct fetch_negotiator negotiator_alloc;
 	struct fetch_negotiator *negotiator;
 	int seen_ack = 0;
+	struct object_id common_oid;
+	int received_ready = 0;
 	struct string_list packfile_uris = STRING_LIST_INIT_DUP;
 	int i;
 	struct strvec index_pack_args = STRVEC_INIT;
@@ -1631,22 +1605,22 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 			break;
 		case FETCH_PROCESS_ACKS:
 			/* Process ACKs/NAKs */
-			switch (process_acks(negotiator, &reader, &common)) {
-			case READY:
+			process_section_header(&reader, "acknowledgments", 0);
+			while (process_ack(negotiator, &reader, &common_oid,
+					   &received_ready)) {
+				in_vain = 0;
+				seen_ack = 1;
+				oidset_insert(&common, &common_oid);
+			}
+			if (received_ready) {
 				/*
 				 * Don't check for response delimiter; get_pack() will
 				 * read the rest of this response.
 				 */
 				state = FETCH_GET_PACK;
-				break;
-			case COMMON_FOUND:
-				in_vain = 0;
-				seen_ack = 1;
-				/* fallthrough */
-			case NO_COMMON_FOUND:
+			} else {
 				do_check_stateless_delimiter(args, &reader);
 				state = FETCH_SEND_REQUEST;
-				break;
 			}
 			break;
 		case FETCH_GET_PACK:
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#7 lore raw

PATCH 3/6 fetch-pack: refactor add_haves()

In reply to Jonathan Tan

A subsequent commit will need part, but not all, of the functionality in add_haves(), so move some of its functionality to its sole caller send_fetch_request().

---
 fetch-pack.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
fetch-pack.c +12 -16
index 9f3901cdba..128ad47d2a 100644
@@ -1195,11 +1195,9 @@ static void add_common(struct strbuf *req_buf, struct oidset *common)
 }
 
 static int add_haves(struct fetch_negotiator *negotiator,
-		     int seen_ack,
 		     struct strbuf *req_buf,
-		     int *haves_to_send, int *in_vain)
+		     int *haves_to_send)
 {
-	int ret = 0;
 	int haves_added = 0;
 	const struct object_id *oid;
 
@@ -1209,17 +1207,10 @@ static int add_haves(struct fetch_negotiator *negotiator,
 			break;
 	}
 
-	*in_vain += haves_added;
-	if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
-		/* Send Done */
-		packet_buf_write(req_buf, "done\n");
-		ret = 1;
-	}
-
 	/* Increase haves to send on next round */
 	*haves_to_send = next_flush(1, *haves_to_send);
 
-	return ret;
+	return haves_added;
 }
 
 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
@@ -1228,7 +1219,8 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 			      int *haves_to_send, int *in_vain,
 			      int sideband_all, int seen_ack)
 {
-	int ret = 0;
+	int haves_added;
+	int done_sent = 0;
 	const char *hash_name;
 	struct strbuf req_buf = STRBUF_INIT;
 
@@ -1312,9 +1304,13 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 	/* Add all of the common commits we've found in previous rounds */
 	add_common(&req_buf, common);
 
-	/* Add initial haves */
-	ret = add_haves(negotiator, seen_ack, &req_buf,
-			haves_to_send, in_vain);
+	haves_added = add_haves(negotiator, &req_buf, haves_to_send);
+	*in_vain += haves_added;
+	if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
+		/* Send Done */
+		packet_buf_write(&req_buf, "done\n");
+		done_sent = 1;
+	}
 
 	/* Send request */
 	packet_buf_flush(&req_buf);
@@ -1322,7 +1318,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 		die_errno(_("unable to write request to remote"));
 
 	strbuf_release(&req_buf);
-	return ret;
+	return done_sent;
 }
 
 /*
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#9 lore raw

PATCH 4/6 fetch-pack: refactor command and capability write

In reply to Jonathan Tan

A subsequent commit will need this functionality independent of the rest of send_fetch_request(), so put this into its own function.

---
 fetch-pack.c | 41 ++++++++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 17 deletions(-)
fetch-pack.c +24 -17
index 128ad47d2a..512fe5450d 100644
@@ -1213,29 +1213,23 @@ static int add_haves(struct fetch_negotiator *negotiator,
 	return haves_added;
 }
 
-static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
-			      struct fetch_pack_args *args,
-			      const struct ref *wants, struct oidset *common,
-			      int *haves_to_send, int *in_vain,
-			      int sideband_all, int seen_ack)
+static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+						 const struct string_list *server_options)
 {
-	int haves_added;
-	int done_sent = 0;
 	const char *hash_name;
-	struct strbuf req_buf = STRBUF_INIT;
 
 	if (server_supports_v2("fetch", 1))
-		packet_buf_write(&req_buf, "command=fetch");
+		packet_buf_write(req_buf, "command=fetch");
 	if (server_supports_v2("agent", 0))
-		packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
+		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
 	if (advertise_sid && server_supports_v2("session-id", 0))
-		packet_buf_write(&req_buf, "session-id=%s", trace2_session_id());
-	if (args->server_options && args->server_options->nr &&
+		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
+	if (server_options && server_options->nr &&
 	    server_supports_v2("server-option", 1)) {
 		int i;
-		for (i = 0; i < args->server_options->nr; i++)
-			packet_buf_write(&req_buf, "server-option=%s",
-					 args->server_options->items[i].string);
+		for (i = 0; i < server_options->nr; i++)
+			packet_buf_write(req_buf, "server-option=%s",
+					 server_options->items[i].string);
 	}
 
 	if (server_feature_v2("object-format", &hash_name)) {
@@ -1243,13 +1237,26 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
 			die(_("mismatched algorithms: client %s; server %s"),
 			    the_hash_algo->name, hash_name);
-		packet_buf_write(&req_buf, "object-format=%s", the_hash_algo->name);
+		packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
 	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
 		die(_("the server does not support algorithm '%s'"),
 		    the_hash_algo->name);
 	}
+	packet_buf_delim(req_buf);
+}
+
+static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
+			      struct fetch_pack_args *args,
+			      const struct ref *wants, struct oidset *common,
+			      int *haves_to_send, int *in_vain,
+			      int sideband_all, int seen_ack)
+{
+	int haves_added;
+	int done_sent = 0;
+	struct strbuf req_buf = STRBUF_INIT;
+
+	write_fetch_command_and_capabilities(&req_buf, args->server_options);
 
-	packet_buf_delim(&req_buf);
 	if (args->use_thin_pack)
 		packet_buf_write(&req_buf, "thin-pack");
 	if (args->no_progress)
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#11 lore raw

PATCH 5/6 fetch: teach independent negotiation (no packfile)

In reply to Jonathan Tan

Currently, the packfile negotiation step within a Git fetch cannot be done independent of sending the packfile, even though there is at least one application wherein this is useful. Therefore, make it possible for this negotiation step to be done independently. A subsequent commit will use this for one such application - push negotiation.

This feature is for protocol v2 only. (An implementation for protocol v0 would require a separate implementation in the fetch, transport, and transport helper code.)

In the protocol, the main hindrance towards independent negotiation is that the server can unilaterally decide to send the packfile. This is solved by a "wait-for-done" argument: the server will then wait for the client to say "done". In practice, the client will never say it; instead it will cease requests once it is satisfied.

In the client, the main change lies in the transport and transport helper code. fetch_refs_via_pack() performs everything needed - protocol version and capability checks, and the negotiation itself.

There are 2 code paths that do not go through fetch_refs_via_pack() that needed to be individually excluded: the bundle transport (excluded through requiring smart_options, which the bundle transport doesn't support) and transport helpers that do not support takeover. Fortunately, none of these support protocol v2.

---
 Documentation/technical/protocol-v2.txt |  8 +++
 builtin/fetch.c                         | 27 +++++++-
 fetch-pack.c                            | 89 +++++++++++++++++++++++--
 fetch-pack.h                            | 11 +++
 object.h                                |  2 +-
 t/t5701-git-serve.sh                    |  2 +-
 t/t5702-protocol-v2.sh                  | 89 +++++++++++++++++++++++++
 transport-helper.c                      | 10 +++
 transport.c                             | 30 +++++++--
 transport.h                             |  6 ++
 upload-pack.c                           | 18 +++--
 11 files changed, 275 insertions(+), 17 deletions(-)
Documentation/technical/protocol-v2.txt +8 -0
index a7c806a73e..0b371d8de4 100644
@@ -346,6 +346,14 @@ explained below.
 	client should download from all given URIs. Currently, the
 	protocols supported are "http" and "https".
 
+If the 'wait-for-done' feature is advertised, the following argument
+can be included in the client's request.
+
+    wait-for-done
+	Indicates to the server that it should never send "ready", but
+	should wait for the client to say "done" before sending the
+	packfile.
+
 The response of `fetch` is broken into a number of sections separated by
 delimiter packets (0001), with each section beginning with its section
 header. Most sections are sent only when the packfile is sent.
builtin/fetch.c +26 -1
index 0b90de87c7..597973848a 100644
@@ -82,6 +82,7 @@ static struct string_list server_options = STRING_LIST_INIT_DUP;
 static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
 static int fetch_write_commit_graph = -1;
 static int stdin_refspecs = 0;
+static int negotiate_only;
 
 static int git_fetch_config(const char *k, const char *v, void *cb)
 {
@@ -202,6 +203,8 @@ static struct option builtin_fetch_options[] = {
 			TRANSPORT_FAMILY_IPV6),
 	OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
 			N_("report that we have only objects reachable from this object")),
+	OPT_BOOL(0, "negotiate-only", &negotiate_only,
+		 N_("do not fetch a packfile; instead, print ancestors of negotiation tips")),
 	OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
 	OPT_BOOL(0, "auto-maintenance", &enable_auto_gc,
 		 N_("run 'maintenance --auto' after fetching")),
@@ -1986,7 +1989,29 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	if (remote) {
+	if (negotiate_only) {
+		struct oidset acked_commits = OIDSET_INIT;
+		struct oidset_iter iter;
+		const struct object_id *oid;
+
+		if (!remote)
+			die(_("Must supply remote when using --negotiate-only"));
+		gtransport = prepare_transport(remote, 1);
+		if (gtransport->smart_options) {
+			gtransport->smart_options->acked_commits = &acked_commits;
+		} else {
+			warning(_("Protocol does not support --negotiate-only, exiting."));
+			return 1;
+		}
+		if (server_options.nr)
+			gtransport->server_options = &server_options;
+		result = transport_fetch_refs(gtransport, NULL);
+
+		oidset_iter_init(&acked_commits, &iter);
+		while ((oid = oidset_iter_next(&iter)))
+			printf("%s\n", oid_to_hex(oid));
+		oidset_clear(&acked_commits);
+	} else if (remote) {
 		if (filter_options.choice || has_promisor_remote())
 			fetch_one_setup_partial(remote);
 		result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs);
fetch-pack.c +85 -4

127 lines not on this page · raw message

fetch-pack.h +11 -0
index f114d72775..7c8f49aca7 100644
@@ -5,6 +5,7 @@
 #include "run-command.h"
 #include "protocol.h"
 #include "list-objects-filter-options.h"
+#include "oidset.h"
 
 struct oid_array;
 
@@ -81,6 +82,16 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
 		       struct string_list *pack_lockfiles,
 		       enum protocol_version version);
 
+/*
+ * Execute the --negotiate-only mode of "git fetch", adding all known common
+ * commits to acked_commits.
+ */
+void negotiate_using_fetch(const struct oid_array *negotiation_tips,
+			   const struct string_list *server_options,
+			   int stateless_rpc,
+			   int fd[],
+			   struct oidset *acked_commits);
+
 /*
  * Print an appropriate error message for each sought ref that wasn't
  * matched.  Return 0 if all sought refs were matched, otherwise 1.
object.h +1 -1
index 59daadce21..4806fa8e66 100644
@@ -60,7 +60,7 @@ struct object_array {
 /*
  * object flag allocation:
  * revision.h:               0---------10         15             23------26
- * fetch-pack.c:             01
+ * fetch-pack.c:             01    67
  * negotiator/default.c:       2--5
  * walker.c:                 0-2
  * upload-pack.c:                4       11-----14  16-----19
t/t5701-git-serve.sh +1 -1
index 509f379d49..f03bb04803 100755
@@ -16,7 +16,7 @@ test_expect_success 'test capability advertisement' '
 	version 2
 	agent=git/$(git version | cut -d" " -f3)
 	ls-refs=unborn
-	fetch=shallow
+	fetch=shallow wait-for-done
 	server-option
 	object-format=$(test_oid algo)
 	0000
t/t5702-protocol-v2.sh +89 -0

101 lines not on this page · raw message

transport-helper.c +10 -0
index 4cd76366fa..4be035edb8 100644
@@ -684,6 +684,16 @@ static int fetch(struct transport *transport,
 		return transport->vtable->fetch(transport, nr_heads, to_fetch);
 	}
 
+	/*
+	 * If we reach here, then the server, the client, and/or the transport
+	 * helper does not support protocol v2. --negotiate-only requires
+	 * protocol v2.
+	 */
+	if (data->transport_options.acked_commits) {
+		warning(_("--negotiate-only requires protocol v2"));
+		return -1;
+	}
+
 	if (!data->get_refs_list_called)
 		get_refs_list_using_list(transport, 0);
 
transport.c +25 -5
index ef66e73090..80caeaa72f 100644
@@ -392,16 +392,29 @@ static int fetch_refs_via_pack(struct transport *transport,
 	else if (data->version <= protocol_v1)
 		die_if_server_options(transport);
 
+	if (data->options.acked_commits) {
+		if (data->version < protocol_v2) {
+			warning(_("--negotiate-only requires protocol v2"));
+			ret = -1;
+		} else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
+			warning(_("server does not support wait-for-done"));
+			ret = -1;
+		} else {
+			negotiate_using_fetch(data->options.negotiation_tips,
+					      transport->server_options,
+					      transport->stateless_rpc,
+					      data->fd,
+					      data->options.acked_commits);
+			ret = 0;
+		}
+		goto cleanup;
+	}
+
 	refs = fetch_pack(&args, data->fd,
 			  refs_tmp ? refs_tmp : transport->remote_refs,
 			  to_fetch, nr_heads, &data->shallow,
 			  &transport->pack_lockfiles, data->version);
 
-	close(data->fd[0]);
-	close(data->fd[1]);
-	if (finish_connect(data->conn))
-		ret = -1;
-	data->conn = NULL;
 	data->got_remote_heads = 0;
 	data->options.self_contained_and_connected =
 		args.self_contained_and_connected;
@@ -412,6 +425,13 @@ static int fetch_refs_via_pack(struct transport *transport,
 	if (report_unmatched_refs(to_fetch, nr_heads))
 		ret = -1;
 
+cleanup:
+	close(data->fd[0]);
+	close(data->fd[1]);
+	if (finish_connect(data->conn))
+		ret = -1;
+	data->conn = NULL;
+
 	free_refs(refs_tmp);
 	free_refs(refs);
 	return ret;
transport.h +6 -0
index 4d5db0a7f2..b8b3d42e62 100644
@@ -47,6 +47,12 @@ struct git_transport_options {
 	 * transport_set_option().
 	 */
 	struct oid_array *negotiation_tips;
+
+	/*
+	 * If set, whenever transport_fetch_refs() is called, add known common
+	 * commits to this oidset instead of fetching any packfiles.
+	 */
+	struct oidset *acked_commits;
 };
 
 enum transport_family {
upload-pack.c +13 -5
index e19583ae0f..b432ef0119 100644
@@ -103,6 +103,7 @@ struct upload_pack_data {
 	unsigned use_ofs_delta : 1;
 	unsigned no_progress : 1;
 	unsigned use_include_tag : 1;
+	unsigned wait_for_done : 1;
 	unsigned allow_filter : 1;
 	unsigned allow_filter_fallback : 1;
 	unsigned long tree_filter_max_depth;
@@ -1496,6 +1497,10 @@ static void process_args(struct packet_reader *request,
 			data->done = 1;
 			continue;
 		}
+		if (!strcmp(arg, "wait-for-done")) {
+			data->wait_for_done = 1;
+			continue;
+		}
 
 		/* Shallow related arguments */
 		if (process_shallow(arg, &data->shallows))
@@ -1578,7 +1583,7 @@ static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
 				    oid_to_hex(&acks->oid[i]));
 	}
 
-	if (ok_to_give_up(data)) {
+	if (!data->wait_for_done && ok_to_give_up(data)) {
 		/* Send Ready */
 		packet_writer_write(&data->writer, "ready\n");
 		return 1;
@@ -1668,10 +1673,13 @@ int upload_pack_v2(struct repository *r, struct strvec *keys,
 		case FETCH_PROCESS_ARGS:
 			process_args(request, &data);
 
-			if (!data.want_obj.nr) {
+			if (!data.want_obj.nr && !data.wait_for_done) {
 				/*
-				 * Request didn't contain any 'want' lines,
-				 * guess they didn't want anything.
+				 * Request didn't contain any 'want' lines (and
+				 * the request does not contain
+				 * "wait-for-done", in which it is reasonable
+				 * to just send 'have's without 'want's); guess
+				 * they didn't want anything.
 				 */
 				state = FETCH_DONE;
 			} else if (data.haves.nr) {
@@ -1723,7 +1731,7 @@ int upload_pack_advertise(struct repository *r,
 		int allow_sideband_all_value;
 		char *str = NULL;
 
-		strbuf_addstr(value, "shallow");
+		strbuf_addstr(value, "shallow wait-for-done");
 
 		if (!repo_config_get_bool(the_repository,
 					 "uploadpack.allowfilter",
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#17 lore raw

PATCH 6/6 send-pack: support push negotiation

In reply to Jonathan Tan

Teach Git the push.negotiate config variable.

---
 Documentation/config/push.txt |  7 ++++
 send-pack.c                   | 61 ++++++++++++++++++++++++++++++++---
 t/t5516-fetch-push.sh         | 35 ++++++++++++++++++++
 3 files changed, 99 insertions(+), 4 deletions(-)
Documentation/config/push.txt +7 -0
index 21b256e0a4..f2667b2689 100644
@@ -120,3 +120,10 @@ push.useForceIfIncludes::
 	`--force-if-includes` as an option to linkgit:git-push[1]
 	in the command line. Adding `--no-force-if-includes` at the
 	time of push overrides this configuration setting.
+
+push.negotiate::
+	If set to "true", attempt to reduce the size of the packfile
+	sent by rounds of negotiation in which the client and the
+	server attempt to find commits in common. If "false", Git will
+	rely solely on the server's ref advertisement to find commits
+	in common.
send-pack.c +57 -4

102 lines not on this page · raw message

t/t5516-fetch-push.sh +35 -0
index f11742ed59..62fb9074a2 100755
@@ -191,6 +191,41 @@ test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
 	)
 '
 
+grep_wrote () {
+	object_count=$1
+	file_name=$2
+	grep 'write_pack_file/wrote.*"value":"'$1'"' $2
+}
+
+test_expect_success 'push with negotiation' '
+	# Without negotiation
+	mk_empty testrepo &&
+	git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
+	git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
+	echo now pushing without negotiation &&
+	GIT_TRACE2_EVENT="$(pwd)/event" git push testrepo refs/heads/main:refs/remotes/origin/main &&
+	grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
+
+	# Same commands, but with negotiation
+	rm event &&
+	mk_empty testrepo &&
+	git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
+	git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
+	GIT_TRACE2_EVENT="$(pwd)/event" git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
+	grep_wrote 2 event # 1 commit, 1 tree
+'
+
+test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
+	rm event &&
+	mk_empty testrepo &&
+	git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
+	git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
+	GIT_TRACE_PACKET="$(pwd)/trace" GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
+		git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
+	grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
+	test_i18ngrep "push negotiation failed" err
+'
+
 test_expect_success 'push without wildcard' '
 	mk_empty testrepo &&
 
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
#2 lore raw

Re: [PATCH 1/6] fetch-pack: buffer object-format with other args

In reply to Jonathan Tan
Quoted 9 lines · Jonathan Tan

In send_fetch_request(), "object-format" is written directly to the file descriptor, as opposed to the other arguments, which are buffered. Buffer "object-format" as well. "object-format" must be buffered; in particular, it must appear after "command=fetch" in the request.

This divergence was introduced in 4b831208bb ("fetch-pack: parse and advertise the object-format capability", 2020-05-27), perhaps as an oversight (the surrounding code at the point of this commit has already been using a request buffer.)

Good find. Did this actually resulted in a data corruption and that was how you discovered it?

Quoted 19 lines
---
 fetch-pack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fetch-pack.c +1 -1
index 6e68276aa3..2318ebe680 100644
@@ -1251,7 +1251,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
 			die(_("mismatched algorithms: client %s; server %s"),
 			    the_hash_algo->name, hash_name);
-		packet_write_fmt(fd_out, "object-format=%s", the_hash_algo->name);
+		packet_buf_write(&req_buf, "object-format=%s", the_hash_algo->name);
 	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
 		die(_("the server does not support algorithm '%s'"),
 		    the_hash_algo->name);
#5 lore raw

Re: [PATCH 2/6] fetch-pack: refactor process_acks()

In reply to Jonathan Tan
Quoted 9 lines · Jonathan Tan

A subsequent commit will need part, but not all, of the functionality in process_acks(), so move some of its functionality to its sole caller do_fetch_pack_v2(). As a side effect, the resulting code is also shorter.

---
 fetch-pack.c | 70 +++++++++++++++++-----------------------------------
 1 file changed, 22 insertions(+), 48 deletions(-)

Nicely done.

I first found the patch hard to follow, but that wasn't due to this patch, but because I wasn't all that familiar with the original anymore (even though I do recall futzing with the codepath that involves the in_vain stuff myself at some point in the past).

And indeed the resulting code is easy to see what is going on (I'd need a lot more concentration than I can muster this late in the night to be able to tell if the behaviour does not change with the patch, though).

Quoted 127 lines
fetch-pack.c +22 -48
index 2318ebe680..9f3901cdba 100644
@@ -1351,35 +1351,11 @@ static int process_section_header(struct packet_reader *reader,
 	return ret;
 }
 
-enum common_found {
-	/*
-	 * No commit was found to be possessed by both the client and the
-	 * server, and "ready" was not received.
-	 */
-	NO_COMMON_FOUND,
-
-	/*
-	 * At least one commit was found to be possessed by both the client and
-	 * the server, and "ready" was not received.
-	 */
-	COMMON_FOUND,
-
-	/*
-	 * "ready" was received, indicating that the server is ready to send
-	 * the packfile without any further negotiation.
-	 */
-	READY
-};
-
-static enum common_found process_acks(struct fetch_negotiator *negotiator,
-				      struct packet_reader *reader,
-				      struct oidset *common)
+static int process_ack(struct fetch_negotiator *negotiator,
+		       struct packet_reader *reader,
+		       struct object_id *common_oid,
+		       int *received_ready)
 {
-	/* received */
-	int received_ready = 0;
-	int received_ack = 0;
-
-	process_section_header(reader, "acknowledgments", 0);
 	while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
 		const char *arg;
 
@@ -1387,20 +1363,17 @@ static enum common_found process_acks(struct fetch_negotiator *negotiator,
 			continue;
 
 		if (skip_prefix(reader->line, "ACK ", &arg)) {
-			struct object_id oid;
-			received_ack = 1;
-			if (!get_oid_hex(arg, &oid)) {
+			if (!get_oid_hex(arg, common_oid)) {
 				struct commit *commit;
-				oidset_insert(common, &oid);
-				commit = lookup_commit(the_repository, &oid);
+				commit = lookup_commit(the_repository, common_oid);
 				if (negotiator)
 					negotiator->ack(negotiator, commit);
 			}
-			continue;
+			return 1;
 		}
 
 		if (!strcmp(reader->line, "ready")) {
-			received_ready = 1;
+			*received_ready = 1;
 			continue;
 		}
 
@@ -1418,13 +1391,12 @@ static enum common_found process_acks(struct fetch_negotiator *negotiator,
 	 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
 	 * otherwise.
 	 */
-	if (received_ready && reader->status != PACKET_READ_DELIM)
+	if (*received_ready && reader->status != PACKET_READ_DELIM)
 		die(_("expected packfile to be sent after 'ready'"));
-	if (!received_ready && reader->status != PACKET_READ_FLUSH)
+	if (!*received_ready && reader->status != PACKET_READ_FLUSH)
 		die(_("expected no other sections to be sent after no 'ready'"));
 
-	return received_ready ? READY :
-		(received_ack ? COMMON_FOUND : NO_COMMON_FOUND);
+	return 0;
 }
 
 static void receive_shallow_info(struct fetch_pack_args *args,
@@ -1573,6 +1545,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 	struct fetch_negotiator negotiator_alloc;
 	struct fetch_negotiator *negotiator;
 	int seen_ack = 0;
+	struct object_id common_oid;
+	int received_ready = 0;
 	struct string_list packfile_uris = STRING_LIST_INIT_DUP;
 	int i;
 	struct strvec index_pack_args = STRVEC_INIT;
@@ -1631,22 +1605,22 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 			break;
 		case FETCH_PROCESS_ACKS:
 			/* Process ACKs/NAKs */
-			switch (process_acks(negotiator, &reader, &common)) {
-			case READY:
+			process_section_header(&reader, "acknowledgments", 0);
+			while (process_ack(negotiator, &reader, &common_oid,
+					   &received_ready)) {
+				in_vain = 0;
+				seen_ack = 1;
+				oidset_insert(&common, &common_oid);
+			}
+			if (received_ready) {
 				/*
 				 * Don't check for response delimiter; get_pack() will
 				 * read the rest of this response.
 				 */
 				state = FETCH_GET_PACK;
-				break;
-			case COMMON_FOUND:
-				in_vain = 0;
-				seen_ack = 1;
-				/* fallthrough */
-			case NO_COMMON_FOUND:
+			} else {
 				do_check_stateless_delimiter(args, &reader);
 				state = FETCH_SEND_REQUEST;
-				break;
 			}
 			break;
 		case FETCH_GET_PACK:
#8 lore raw

Re: [PATCH 3/6] fetch-pack: refactor add_haves()

In reply to Jonathan Tan
Quoted 33 lines · Jonathan Tan

A subsequent commit will need part, but not all, of the functionality in add_haves(), so move some of its functionality to its sole caller send_fetch_request().

---
 fetch-pack.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
fetch-pack.c +1 -6
index 9f3901cdba..128ad47d2a 100644
@@ -1195,11 +1195,9 @@ static void add_common(struct strbuf *req_buf, struct oidset *common)
 }
 
 static int add_haves(struct fetch_negotiator *negotiator,
-		     int seen_ack,
 		     struct strbuf *req_buf,
-		     int *haves_to_send, int *in_vain)
+		     int *haves_to_send)
 {
-	int ret = 0;
 	int haves_added = 0;
 	const struct object_id *oid;
 
@@ -1209,17 +1207,10 @@ static int add_haves(struct fetch_negotiator *negotiator,
 			break;
 	}
 
-	*in_vain += haves_added;
-	if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
-		/* Send Done */

fetch-pack.c static int add_haves(struct fetch_negotiator *negotiator, · line 1214

Quoted 10 lines
fetch-pack.c +1 -5
-		packet_buf_write(req_buf, "done\n");
-		ret = 1;
-	}
-
 	/* Increase haves to send on next round */
 	*haves_to_send = next_flush(1, *haves_to_send);
 
-	return ret;
+	return haves_added;
 }

OK, so the helper used to

- send out have's
- increment *in_vain with the number of have's sent out
- decide when to send done and send it
- increase haves-to-send for the next round.

But the new version skips the second and third step, and let the caller be responsible for in_vain management. The only piece of information the caller needs to do so from this function is the number of have's we send out, so that is what we return from the function.

fetch-pack.c static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, · line 1313

Quoted 10 lines
fetch-pack.c +7 -3
-	/* Add initial haves */
-	ret = add_haves(negotiator, seen_ack, &req_buf,
-			haves_to_send, in_vain);
+	haves_added = add_haves(negotiator, &req_buf, haves_to_send);
+	*in_vain += haves_added;
+	if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
+		/* Send Done */
+		packet_buf_write(&req_buf, "done\n");
+		done_sent = 1;
+	}

And indeed, this caller does the *in_vain management add_haves() used to do and give "done" as needed.

fetch-pack.c static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, · line 1321

Quoted 6 lines
fetch-pack.c +1 -1
@@ -1322,7 +1318,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 		die_errno(_("unable to write request to remote"));
 
 	strbuf_release(&req_buf);
-	return ret;
+	return done_sent;

And of course, the value returned by this function is if we sent "done", which is far easier to see with the new variable name.

Again, very nicely done.

#10 lore raw

Re: [PATCH 4/6] fetch-pack: refactor command and capability write

In reply to Jonathan Tan
Quoted 2 lines · Jonathan Tan

A subsequent commit will need this functionality independent of the rest of send_fetch_request(), so put this into its own function.

OK. send_fetch_request() used to do the "command=fetch", capability response, and concluded them with a delim packet, which is now done with the new helper write_fetch_command_and_capabilities().

Mostly code movement, without any behaviour change.

Makes sense.

#12 lore raw

Re: [PATCH 5/6] fetch: teach independent negotiation (no packfile)

In reply to Jonathan Tan
Quoted 5 lines · Jonathan Tan

There are 2 code paths that do not go through fetch_refs_via_pack() that needed to be individually excluded: the bundle transport (excluded through requiring smart_options, which the bundle transport doesn't support) and transport helpers that do not support takeover. Fortunately, none of these support protocol v2.

I am a bit puzzled by this mention of "Fortunately". If one says "this shiny new feature only works with protocol v2" and "transport X does not support protocol v2", doesn't it imply that the shiny new feature cannot be used with the transport X, which is unfortunate?

I can understand "while interacting with the bundle transport, you cannot do independent negotiation, but there is nothing to negotiate with a static file that is a bundle anyway, so nothing is lost" as an explanation, though.

Quoted 4 lines
 Documentation/technical/protocol-v2.txt |  8 +++
 builtin/fetch.c                         | 27 +++++++-
 fetch-pack.c                            | 89 +++++++++++++++++++++++--
 fetch-pack.h                            | 11 +++
Quoted 8 lines
 object.h                                |  2 +-
 t/t5701-git-serve.sh                    |  2 +-
 t/t5702-protocol-v2.sh                  | 89 +++++++++++++++++++++++++
 transport-helper.c                      | 10 +++
 transport.c                             | 30 +++++++--
 transport.h                             |  6 ++
 upload-pack.c                           | 18 +++--
 11 files changed, 275 insertions(+), 17 deletions(-)

It is a bit surprising that there isn't much code removed, as I expected that we'd be factoring out and reusing existing code used in negotiation for fetching into a new helper function (hence the existing codepath would lose a lot of code to be replaced by a call to a new helper function), but that is apparently not what is going on.

I'll have to revisit this step and the next step tomorrow.

Thanks.

#3 lore raw

Re: [PATCH 1/6] fetch-pack: buffer object-format with other args

In reply to Junio C Hamano
Quoted 7 lines
Quoted 5 lines · Jonathan Tan

In send_fetch_request(), "object-format" is written directly to the file descriptor, as opposed to the other arguments, which are buffered. Buffer "object-format" as well. "object-format" must be buffered; in particular, it must appear after "command=fetch" in the request.

Quoted 7 lines
Quoted 4 lines

This divergence was introduced in 4b831208bb ("fetch-pack: parse and advertise the object-format capability", 2020-05-27), perhaps as an oversight (the surrounding code at the point of this commit has already been using a request buffer.)

Good find. Did this actually resulted in a data corruption and that was how you discovered it?

No, I did not see any data corruption. I discovered it because I was looking at this function, thinking that I needed to refactor it.

#13 lore raw

Re: [PATCH 5/6] fetch: teach independent negotiation (no packfile)

In reply to Junio C Hamano
Quoted 8 lines
Quoted 5 lines · Jonathan Tan

There are 2 code paths that do not go through fetch_refs_via_pack() that needed to be individually excluded: the bundle transport (excluded through requiring smart_options, which the bundle transport doesn't support) and transport helpers that do not support takeover. Fortunately, none of these support protocol v2.

Quoted 4 lines

I am a bit puzzled by this mention of "Fortunately". If one says "this shiny new feature only works with protocol v2" and "transport X does not support protocol v2", doesn't it imply that the shiny new feature cannot be used with the transport X, which is unfortunate?

I meant to say that we don't have to do much about these cases because they are out of scope for the support that we planned (protocol v2), but perhaps "fortunately" is the wrong way to say it. Perhaps a better way of explaining it is that most code paths go through fetch_refs_via_pack(), and for the ones that don't (bundle transport and non-takeover transport helpers), we need to address them separately. But in this case, we are not planning to support either, so we just check if we have requested independent negotiation and if yes, report failure.

Quoted 18 lines

I can understand "while interacting with the bundle transport, you cannot do independent negotiation, but there is nothing to negotiate with a static file that is a bundle anyway, so nothing is lost" as an explanation, though.

Quoted 4 lines
 Documentation/technical/protocol-v2.txt |  8 +++
 builtin/fetch.c                         | 27 +++++++-
 fetch-pack.c                            | 89 +++++++++++++++++++++++--
 fetch-pack.h                            | 11 +++
Quoted 8 lines
 object.h                                |  2 +-
 t/t5701-git-serve.sh                    |  2 +-
 t/t5702-protocol-v2.sh                  | 89 +++++++++++++++++++++++++
 transport-helper.c                      | 10 +++
 transport.c                             | 30 +++++++--
 transport.h                             |  6 ++
 upload-pack.c                           | 18 +++--
 11 files changed, 275 insertions(+), 17 deletions(-)
Quoted 6 lines

It is a bit surprising that there isn't much code removed, as I expected that we'd be factoring out and reusing existing code used in negotiation for fetching into a new helper function (hence the existing codepath would lose a lot of code to be replaced by a call to a new helper function), but that is apparently not what is going on.

That's what I was trying to do (hence the earlier patches), but the main thing I ran into is that a lot of the fetch code assumes that you're fetching at least one ref, so I couldn't use a lot of it without more code updating. Having said that, I may have missed more opportunities for reuse.

Quoted 3 lines

I'll have to revisit this step and the next step tomorrow.

Thanks.

Thanks for taking a look.

#21 lore raw
In reply to Jonathan Tan
Quoted 13 lines · Jonathan Tan

Here are patches for push negotiation. The basic idea is that we can reuse part of the fetch protocol (and code) to find out what is in common between the client and server, and then use that information to further narrow the objects sent in the packfile during a push.

Patch 1 is a bug fix that probably should be merged even if the rest aren't. Patches 2-4 are refactorings in preparation for the future patches. Patches 5-6 contain the actual logic and documentation.

I have written more about it in my prior work [1], although the commit messages and documentation in patches 5-6 should be enough to explain what's going on. (If they're not, feel free to make review comments.)

Quoted 7 lines

The main change from [1] is that the client-side code that used to be in builtin/fetch-pack.c is now in builtin/fetch.c, because I realized that builtin/fetch-pack.c does not support HTTP. Other than that, all the "what hasn't been done yet" items have been done except for statistics in the commit message.

[1] https://lore.kernel.org/git/[email protected]/

So... anybody else wants to review this and give it a-OK?

Quoted 23 lines

Jonathan Tan (6):
fetch-pack: buffer object-format with other args
fetch-pack: refactor process_acks()
fetch-pack: refactor add_haves()
fetch-pack: refactor command and capability write
fetch: teach independent negotiation (no packfile)
send-pack: support push negotiation

 Documentation/config/push.txt           |   7 +
 Documentation/technical/protocol-v2.txt |   8 +
 builtin/fetch.c                         |  27 ++-
 fetch-pack.c                            | 224 +++++++++++++++---------
 fetch-pack.h                            |  11 ++
 object.h                                |   2 +-
 send-pack.c                             |  61 ++++++-
 t/t5516-fetch-push.sh                   |  35 ++++
 t/t5701-git-serve.sh                    |   2 +-
 t/t5702-protocol-v2.sh                  |  89 ++++++++++
 transport-helper.c                      |  10 ++
 transport.c                             |  30 +++-
 transport.h                             |   6 +
 upload-pack.c                           |  18 +-
 14 files changed, 430 insertions(+), 100 deletions(-)
#22 lore raw
Quoted 21 lines · Junio C Hamano
Quoted 19 lines · Jonathan Tan

Here are patches for push negotiation. The basic idea is that we can reuse part of the fetch protocol (and code) to find out what is in common between the client and server, and then use that information to further narrow the objects sent in the packfile during a push.

Patch 1 is a bug fix that probably should be merged even if the rest aren't. Patches 2-4 are refactorings in preparation for the future patches. Patches 5-6 contain the actual logic and documentation.

I have written more about it in my prior work [1], although the commit messages and documentation in patches 5-6 should be enough to explain what's going on. (If they're not, feel free to make review comments.)

The main change from [1] is that the client-side code that used to be in builtin/fetch-pack.c is now in builtin/fetch.c, because I realized that builtin/fetch-pack.c does not support HTTP. Other than that, all the "what hasn't been done yet" items have been done except for statistics in the commit message.

Quoted 3 lines
Quoted 1 line

[1] https://lore.kernel.org/git/[email protected]/

So... anybody else wants to review this and give it a-OK?

I put this on my calendar for Monday morning. I hope that's not too long to wait.

Thanks,
-Stolee

#14 lore raw

Re: [PATCH 5/6] fetch: teach independent negotiation (no packfile)

In reply to Jonathan Tan
Quoted 5 lines · Jonathan Tan

Currently, the packfile negotiation step within a Git fetch cannot be done independent of sending the packfile, even though there is at least one application wherein this is useful. Therefore, make it possible for this negotiation step to be done independently. A subsequent commit will use this for one such application - push negotiation.

...

Quoted 4 lines
Documentation/technical/protocol-v2.txt +0 -0
@@ -346,6 +346,14 @@ explained below.
 	client should download from all given URIs. Currently, the

Documentation/technical/protocol-v2.txt explained below. · line 356

Quoted 10 lines
Documentation/technical/protocol-v2.txt +8 -0
 	protocols supported are "http" and "https".
 
+If the 'wait-for-done' feature is advertised, the following argument
+can be included in the client's request.
+
+    wait-for-done
+	Indicates to the server that it should never send "ready", but
+	should wait for the client to say "done" before sending the
+	packfile.
+

Ok, this is a good way to handle the compatibility case. I'll check in the next patch how this feature is checked before sending a pack-file in 'git push'.

Documentation/technical/protocol-v2.txt static struct option builtin_fetch_options[] = { · line 207

Quoted 6 lines
Documentation/technical/protocol-v2.txt +2 -0
@@ -202,6 +203,8 @@ static struct option builtin_fetch_options[] = {
 			TRANSPORT_FAMILY_IPV6),
 	OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
 			N_("report that we have only objects reachable from this object")),
+	OPT_BOOL(0, "negotiate-only", &negotiate_only,
+		 N_("do not fetch a packfile; instead, print ancestors of negotiation tips")),

It seems a little strange for this step, which is mostly used for tests, to be so visible. I think it would be too much work to create a test helper with an equivalent implementation, so maybe describe this option as only for tests in Documentation/git-fetch.txt.

Documentation/technical/protocol-v2.txt static struct option builtin_fetch_options[] = {

Quoted 8 lines
Documentation/technical/protocol-v2.txt +7 -1
-	if (remote) {
+	if (negotiate_only) {
+		struct oidset acked_commits = OIDSET_INIT;
+		struct oidset_iter iter;
+		const struct object_id *oid;
+
+		if (!remote)
+			die(_("Must supply remote when using --negotiate-only"));

Please use lowercase letters to start your error messages.

Quoted 16 lines
Documentation/technical/protocol-v2.txt +16 -0
+		gtransport = prepare_transport(remote, 1);
+		if (gtransport->smart_options) {
+			gtransport->smart_options->acked_commits = &acked_commits;
+		} else {
+			warning(_("Protocol does not support --negotiate-only, exiting."));
+			return 1;
+		}
+		if (server_options.nr)
+			gtransport->server_options = &server_options;
+		result = transport_fetch_refs(gtransport, NULL);
+
+		oidset_iter_init(&acked_commits, &iter);
+		while ((oid = oidset_iter_next(&iter)))
+			printf("%s\n", oid_to_hex(oid));
+		oidset_clear(&acked_commits);
+	} else if (remote) {

...

Documentation/technical/protocol-v2.txt

Quoted 7 lines
Documentation/technical/protocol-v2.txt +7 -0
+static int add_to_object_array(const struct object_id *oid, void *data)
+{
+	struct object_array *a = data;
+
+	add_object_array(parse_object(the_repository, oid), "", a);
+	return 0;
+}

Why parse_object() over lookup_object()?

Documentation/technical/protocol-v2.txt

Quoted 5 lines
Documentation/technical/protocol-v2.txt +5 -0
+void negotiate_using_fetch(const struct oid_array *negotiation_tips,
+			   const struct string_list *server_options,
+			   int stateless_rpc,
+			   int fd[],
+			   struct oidset *acked_commits)

It might be worth a careful doc comment to say that we _must_ have already verified the wait-for-done capability.

Documentation/technical/protocol-v2.txt

Quoted 9 lines
Documentation/technical/protocol-v2.txt +9 -0
+{
+	struct fetch_negotiator negotiator;
+	struct packet_reader reader;
+	struct object_array nt_object_array = OBJECT_ARRAY_INIT;
+	struct strbuf req_buf = STRBUF_INIT;
+	int haves_to_send = INITIAL_FLUSH;
+	int in_vain = 0;
+	int seen_ack = 0;
+	int last_iteration = 0;

As will become clear later, create:

	timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
Quoted 30 lines
Documentation/technical/protocol-v2.txt +30 -0
+
+	fetch_negotiator_init(the_repository, &negotiator);
+	mark_tips(&negotiator, negotiation_tips);
+
+	packet_reader_init(&reader, fd[0], NULL, 0,
+			   PACKET_READ_CHOMP_NEWLINE |
+			   PACKET_READ_DIE_ON_ERR_PACKET);
+
+	oid_array_for_each((struct oid_array *) negotiation_tips,
+			   add_to_object_array,
+			   &nt_object_array);
+
+	while (!last_iteration) {
+		int haves_added;
+		struct object_id common_oid;
+		int received_ready = 0;
+
+		strbuf_reset(&req_buf);
+		write_fetch_command_and_capabilities(&req_buf, server_options);
+
+		packet_buf_write(&req_buf, "wait-for-done");
+
+		haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
+		in_vain += haves_added;
+		if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
+			last_iteration = 1;
+
+		/* Send request */
+		packet_buf_flush(&req_buf);
+		if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)

Documentation/technical/protocol-v2.txt

Quoted 10 lines
Documentation/technical/protocol-v2.txt +10 -0
+			die_errno(_("unable to write request to remote"));
+
+		/* Process ACKs/NAKs */
+		process_section_header(&reader, "acknowledgments", 0);
+		while (process_ack(&negotiator, &reader, &common_oid,
+				   &received_ready)) {
+			struct commit *commit = lookup_commit(the_repository,
+							      &common_oid);
+			if (commit)
+				commit->object.flags |= COMMON;

Making note of this portion of the code: you can update a min_generation value here, decreasing it if we see a smaller generation.

Documentation/technical/protocol-v2.txt

Quoted 11 lines
Documentation/technical/protocol-v2.txt +11 -0
+			in_vain = 0;
+			seen_ack = 1;
+			oidset_insert(acked_commits, &common_oid);
+		}
+		if (received_ready)
+			die(_("unexpected 'ready' from remote"));
+		else
+			do_check_stateless_delimiter(stateless_rpc, &reader);
+		if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
+						 REACH_SCRATCH, 0,
+						 GENERATION_NUMBER_ZERO))

And finally here you can use min_generation. Otherwise, you will suffer performance problems when there is a deep history but this returns false.

As long as min_generation is the minimum generation of a commit with the COMMON flag, you will be correct to use that limit.

Documentation/technical/protocol-v2.txt

Quoted 3 lines
Documentation/technical/protocol-v2.txt +3 -0
+			last_iteration = 1;
+	}
+	strbuf_release(&req_buf);

Don't forget to clear the COMMON flag before returning.

Documentation/technical/protocol-v2.txt

Quoted 11 lines
Documentation/technical/protocol-v2.txt +10 -0
+}
+
 
+/*
+ * Execute the --negotiate-only mode of "git fetch", adding all known common
+ * commits to acked_commits.
+ */> +void negotiate_using_fetch(const struct oid_array *negotiation_tips,
+			   const struct string_list *server_options,
+			   int stateless_rpc,
+			   int fd[],
+			   struct oidset *acked_commits);

Here is a good place to specifically mention the wait-for-done capability, or else this method will die() when seeing the "ready" from the server.

Quoted 107 lines
Documentation/technical/protocol-v2.txt +89 -0
+setup_negotiate_only () {
+	SERVER="$1"
+	URI="$2"
+
+	rm -rf "$SERVER" client
+
+	git init "$SERVER"
+	test_commit -C "$SERVER" one
+	test_commit -C "$SERVER" two
+
+	git clone "$URI" client
+	test_commit -C client three
+}
+
+test_expect_success 'file:// --negotiate-only' '
+	SERVER="server" &&
+	URI="file://$(pwd)/server" &&
+
+	setup_negotiate_only "$SERVER" "$URI" &&
+
+	git -C client fetch \
+		--no-tags \
+		--negotiate-only \
+		--negotiation-tip=$(git -C client rev-parse HEAD) \
+		origin >out &&
+	COMMON=$(git -C "$SERVER" rev-parse two) &&
+	grep "$COMMON" out
+'
+
+test_expect_success 'file:// --negotiate-only with protocol v0' '
+	SERVER="server" &&
+	URI="file://$(pwd)/server" &&
+
+	setup_negotiate_only "$SERVER" "$URI" &&
+
+	test_must_fail git -c protocol.version=0 -C client fetch \
+		--no-tags \
+		--negotiate-only \
+		--negotiation-tip=$(git -C client rev-parse HEAD) \
+		origin 2>err &&
+	test_i18ngrep "negotiate-only requires protocol v2" err
+'
+
 # Test protocol v2 with 'http://' transport
 #
 . "$TEST_DIRECTORY"/lib-httpd.sh
@@ -1035,6 +1078,52 @@ test_expect_success 'packfile-uri with transfer.fsckobjects fails when .gitmodul
 	test_i18ngrep "disallowed submodule name" err
 '
 
+test_expect_success 'http:// --negotiate-only' '
+	SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
+	URI="$HTTPD_URL/smart/server" &&
+
+	setup_negotiate_only "$SERVER" "$URI" &&
+
+	git -C client fetch \
+		--no-tags \
+		--negotiate-only \
+		--negotiation-tip=$(git -C client rev-parse HEAD) \
+		origin >out &&
+	COMMON=$(git -C "$SERVER" rev-parse two) &&
+	grep "$COMMON" out
+'
+
+test_expect_success 'http:// --negotiate-only without wait-for-done support' '
+	SERVER="server" &&
+	URI="$HTTPD_URL/one_time_perl/server" &&
+
+	setup_negotiate_only "$SERVER" "$URI" &&
+
+	echo "s/ wait-for-done/ xxxx-xxx-xxxx/" \
+		>"$HTTPD_ROOT_PATH/one-time-perl" &&
+
+	test_must_fail git -C client fetch \
+		--no-tags \
+		--negotiate-only \
+		--negotiation-tip=$(git -C client rev-parse HEAD) \
+		origin 2>err &&
+	test_i18ngrep "server does not support wait-for-done" err
+'
+
+test_expect_success 'http:// --negotiate-only with protocol v0' '
+	SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
+	URI="$HTTPD_URL/smart/server" &&
+
+	setup_negotiate_only "$SERVER" "$URI" &&
+
+	test_must_fail git -c protocol.version=0 -C client fetch \
+		--no-tags \
+		--negotiate-only \
+		--negotiation-tip=$(git -C client rev-parse HEAD) \
+		origin 2>err &&
+	test_i18ngrep "negotiate-only requires protocol v2" err
+'
+
 # DO NOT add non-httpd-specific tests here, because the last part of this
 # test script is only executed when httpd is available and enabled.
 
transport-helper.c +0 -0
index 4cd76366fa..4be035edb8 100644
@@ -684,6 +684,16 @@ static int fetch(struct transport *transport,
 		return transport->vtable->fetch(transport, nr_heads, to_fetch);
 	}
 

transport-helper.c static int fetch(struct transport *transport, · line 696

Quoted 10 lines
transport-helper.c +10 -0
+	/*
+	 * If we reach here, then the server, the client, and/or the transport
+	 * helper does not support protocol v2. --negotiate-only requires
+	 * protocol v2.
+	 */
+	if (data->transport_options.acked_commits) {
+		warning(_("--negotiate-only requires protocol v2"));
+		return -1;
+	}
+

This method continues to do a lot that doesn't seem specific to --negotiate-only. The warning message seems incorrect to me, but is also seems like this would break several cases when using protocol v0. It is equally possible that I'm misunderstanding what is going on here.

Quoted 7 lines
transport-helper.c +7 -0
+	if (data->options.acked_commits) {
+		if (data->version < protocol_v2) {
+			warning(_("--negotiate-only requires protocol v2"));
+			ret = -1;
+		} else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
+			warning(_("server does not support wait-for-done"));
+			ret = -1;

transport-helper.c static int fetch(struct transport *transport,

Quoted 10 lines
transport-helper.c +10 -0
+		} else {
+			negotiate_using_fetch(data->options.negotiation_tips,
+					      transport->server_options,
+					      transport->stateless_rpc,
+					      data->fd,
+					      data->options.acked_commits);
+			ret = 0;
+		}
+		goto cleanup;
+	}

These prereqs look right. This makes it look rather strange that negotiate_using_fetch() returns void (and has several die() calls inside) and could instead return an 'int' that is consumed here (and use error() calls instead of die()).

Quoted 10 lines
transport-helper.c +0 -5
 	refs = fetch_pack(&args, data->fd,
 			  refs_tmp ? refs_tmp : transport->remote_refs,
 			  to_fetch, nr_heads, &data->shallow,
 			  &transport->pack_lockfiles, data->version);
 
-	close(data->fd[0]);
-	close(data->fd[1]);
-	if (finish_connect(data->conn))
-		ret = -1;
-	data->conn = NULL;

...

transport-helper.c

Quoted 7 lines
transport-helper.c +7 -0
+cleanup:
+	close(data->fd[0]);
+	close(data->fd[1]);
+	if (finish_connect(data->conn))
+		ret = -1;
+	data->conn = NULL;
+

Good to keep these after skipping a big chunk of the fetch logic.

transport-helper.c

Quoted 6 lines
transport-helper.c +6 -0
+
+	/*
+	 * If set, whenever transport_fetch_refs() is called, add known common
+	 * commits to this oidset instead of fetching any packfiles.
+	 */
+	struct oidset *acked_commits;

nit: s/If set/If allocated/

Quoted 18 lines
transport-helper.c +6 -3
@@ -1668,10 +1673,13 @@ int upload_pack_v2(struct repository *r, struct strvec *keys,
 		case FETCH_PROCESS_ARGS:
 			process_args(request, &data);
 
-			if (!data.want_obj.nr) {
+			if (!data.want_obj.nr && !data.wait_for_done) {
 				/*
-				 * Request didn't contain any 'want' lines,
-				 * guess they didn't want anything.
+				 * Request didn't contain any 'want' lines (and
+				 * the request does not contain
+				 * "wait-for-done", in which it is reasonable
+				 * to just send 'have's without 'want's); guess
+				 * they didn't want anything.
 				 */
 				state = FETCH_DONE;
 			} else if (data.haves.nr) {

transport-helper.c int upload_pack_advertise(struct repository *r, · line 1734

Quoted 5 lines
transport-helper.c +1 -1
@@ -1723,7 +1731,7 @@ int upload_pack_advertise(struct repository *r,
 		int allow_sideband_all_value;
 		char *str = NULL;
 
-		strbuf_addstr(value, "shallow");
+		strbuf_addstr(value, "shallow wait-for-done");

It might be nice to have this wait-for-done capability configurable. I'd likely want this on by default, but in case there is an issue it is easier to change config values than ship new binaries.

This is a pretty big patch. It might have been nice to split the server-side stuff out as its own patch with a follow-up for the client code and the tests.

Thanks,
-Stolee

#18 lore raw

Re: [PATCH 6/6] send-pack: support push negotiation

In reply to Jonathan Tan
Quoted 1 line · Jonathan Tan

Teach Git the push.negotiate config variable.

...

Documentation/config/push.txt push.useForceIfIncludes:: · line 129

Quoted 6 lines
Documentation/config/push.txt +6 -0
+push.negotiate::
+	If set to "true", attempt to reduce the size of the packfile
+	sent by rounds of negotiation in which the client and the
+	server attempt to find commits in common. If "false", Git will
+	rely solely on the server's ref advertisement to find commits
+	in common.

Works for me.

send-pack.c static void feed_object(const struct object_id *oid, FILE *fh, int negative) · line 61

Quoted 12 lines
send-pack.c +3 -1
index 5f215b13c7..9cb9f71650 100644
@@ -56,7 +56,9 @@ static void feed_object(const struct object_id *oid, FILE *fh, int negative)
 /*
  * Make a pack stream and spit it out into file descriptor fd
  */
-static int pack_objects(int fd, struct ref *refs, struct oid_array *extra, struct send_pack_args *args)
+static int pack_objects(int fd, struct ref *refs, struct oid_array *advertised,
+			struct oid_array *negotiated,
+			struct send_pack_args *args)

At the moment, I don't see why we need two oid_arrays here. Instead, this 'extra' could instead be renamed to something like 'server_objects' or 'base_objects' to make it clear that we don't want those objects, and can even use them and their reachable objects as delta bases, when appropriate.

Or, just don't touch it.

Quoted 5 lines
send-pack.c +5 -0
+static void get_commons_through_negotiation(const char *url,
+					    const struct ref *remote_refs,
+					    struct oid_array *commons)
+{
+	struct child_process child = CHILD_PROCESS_INIT;

send-pack.c static void reject_invalid_nonce(const char *nonce, int len) · line 430

Quoted 10 lines
send-pack.c +10 -0
+	const struct ref *ref;
+	int len = the_hash_algo->hexsz + 1; /* hash + NL */
+
+	child.git_cmd = 1;
+	child.no_stdin = 1;
+	child.out = -1;
+	strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
+	for (ref = remote_refs; ref; ref = ref->next)
+		strvec_pushf(&child.args, "--negotiation-tip=%s", oid_to_hex(&ref->new_oid));
+	strvec_push(&child.args, url);

Oh! We are using a 'git fetch --negotiate-only' subprocess here. You can ignore my previous message about updating the docs for this to be used only by tests.

Quoted 7 lines
send-pack.c +7 -0
+
+	if (start_command(&child))
+		die(_("send-pack: unable to fork off fetch subprocess"));
+
+	do {
+		char hex_hash[GIT_MAX_HEXSZ + 1];
+		int read_len = read_in_full(child.out, hex_hash, len);

send-pack.c static void reject_invalid_nonce(const char *nonce, int len) · line 447

Quoted 10 lines
send-pack.c +10 -0
+		struct object_id oid;
+		const char *end;
+
+		if (!read_len)
+			break;
+		if (read_len != len)
+			die("invalid length read %d", read_len);
+		if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
+			die("invalid hash");
+		oid_array_append(commons, &oid);

This appends, so there is no reason why it needs to be empty before the method. Is there a way we could feed the extra_have set when calling this method? Or is it happening at a strange time?

Quoted 40 lines
send-pack.c +17 -0
+	} while (1);
+
+	if (finish_command(&child)) {
+		/*
+		 * The information that push negotiation provides is useful but
+		 * not mandatory.
+		 */
+		warning(_("push negotiation failed; proceeding anyway with push"));
+	}
+}
+
 int send_pack(struct send_pack_args *args,
 	      int fd[], struct child_process *conn,
 	      struct ref *remote_refs,
 	      struct oid_array *extra_have)
 {
+	struct oid_array commons = OID_ARRAY_INIT;
 	int in = fd[0];
 	int out = fd[1];
 	struct strbuf req_buf = STRBUF_INIT;
@@ -426,6 +474,7 @@ int send_pack(struct send_pack_args *args,
 	int quiet_supported = 0;
 	int agent_supported = 0;
 	int advertise_sid = 0;
+	int push_negotiate = 0;
 	int use_atomic = 0;
 	int atomic_supported = 0;
 	int use_push_options = 0;
@@ -437,6 +486,10 @@ int send_pack(struct send_pack_args *args,
 	const char *push_cert_nonce = NULL;
 	struct packet_reader reader;
 
+	git_config_get_bool("push.negotiate", &push_negotiate);
+	if (push_negotiate)
+		get_commons_through_negotiation(args->url, remote_refs, &commons);
+
 	git_config_get_bool("transfer.advertisesid", &advertise_sid);
 
 	/* Does the other end support the reporting? */

send-pack.c int send_pack(struct send_pack_args *args, · line 681

Quoted 5 lines
send-pack.c +1 -1
@@ -625,7 +678,7 @@ int send_pack(struct send_pack_args *args,
 			   PACKET_READ_DIE_ON_ERR_PACKET);
 
 	if (need_pack_data && cmds_sent) {
-		if (pack_objects(out, remote_refs, extra_have, args) < 0) {
+		if (pack_objects(out, remote_refs, extra_have, &commons, args) < 0) {

Here, it would be nice if extra_have and commons were merged before calling pack_objects(). I mentioned a way to perhaps make that easier above, but the context might not make that be super-simple. Running a loop here to scan 'commons' and append them to 'extra_have' might be a sufficient approach.

Generally, this approach seems like it would work. I have not done any local testing, yet.

Thanks,
-Stolee

#15 lore raw

Re: [PATCH 5/6] fetch: teach independent negotiation (no packfile)

In reply to Derrick Stolee
Quoted 15 lines · Derrick Stolee
Quoted 5 lines · Jonathan Tan

Currently, the packfile negotiation step within a Git fetch cannot be done independent of sending the packfile, even though there is at least one application wherein this is useful. Therefore, make it possible for this negotiation step to be done independently. A subsequent commit will use this for one such application - push negotiation.

...

Quoted 8 lines
transport-helper.c +0 -0
index 4cd76366fa..4be035edb8 100644
@@ -684,6 +684,16 @@ static int fetch(struct transport *transport,
 		return transport->vtable->fetch(transport, nr_heads, to_fetch);
 	}
 
Quoted 16 lines

transport-helper.c static int fetch(struct transport *transport, · line 696

Quoted 10 lines
transport-helper.c +10 -0
+	/*
+	 * If we reach here, then the server, the client, and/or the transport
+	 * helper does not support protocol v2. --negotiate-only requires
+	 * protocol v2.
+	 */
+	if (data->transport_options.acked_commits) {
+		warning(_("--negotiate-only requires protocol v2"));
+		return -1;
+	}
+

This method continues to do a lot that doesn't seem specific to --negotiate-only. The warning message seems incorrect to me, but is also seems like this would break several cases when using protocol v0. It is equally possible that I'm misunderstanding what is going on here.

I didn't see this cause any failures when running the test suite with GIT_TEST_PROTOCOL_VERSION=0, but I _do_ see failures in your tests added in PATCH 6 with that environment variable set. You might want to set it manually in the important test cases.

Thanks,
-Stolee

#6 lore raw

Re: [PATCH 2/6] fetch-pack: refactor process_acks()

In reply to Jonathan Tan

fetch-pack.c static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, · line 1634

Quoted 7 lines · Jonathan Tan

A subsequent commit will need part, but not all, of the functionality in process_acks(), so move some of its functionality to its sole caller do_fetch_pack_v2(). As a side effect, the resulting code is also shorter.

fetch-pack.c +0 -1
 		case FETCH_PROCESS_ACKS:
 			/* Process ACKs/NAKs */
-			switch (process_acks(negotiator, &reader, &common)) {

Good riddance to this nested switch!

I am unfamiliar with this code, but I believe these changes to be correct.

Thanks,
-Stolee

#16 lore raw

Re: [PATCH 5/6] fetch: teach independent negotiation (no packfile)

In reply to Derrick Stolee
Quoted 13 lines · Jonathan Tan
Quoted 5 lines

Currently, the packfile negotiation step within a Git fetch cannot be done independent of sending the packfile, even though there is at least one application wherein this is useful. Therefore, make it possible for this negotiation step to be done independently. A subsequent commit will use this for one such application - push negotiation.

...

Quoted 6 lines
Documentation/technical/protocol-v2.txt +0 -0
@@ -346,6 +346,14 @@ explained below.
 	client should download from all given URIs. Currently, the
 	protocols supported are "http" and "https".
 
Quoted 12 lines

Documentation/technical/protocol-v2.txt explained below. · line 356

Quoted 8 lines
Documentation/technical/protocol-v2.txt +8 -0
+If the 'wait-for-done' feature is advertised, the following argument
+can be included in the client's request.
+
+    wait-for-done
+	Indicates to the server that it should never send "ready", but
+	should wait for the client to say "done" before sending the
+	packfile.
+

Ok, this is a good way to handle the compatibility case. I'll check in the next patch how this feature is checked before sending a pack-file in 'git push'.

Thanks.

Quoted 11 lines

Documentation/technical/protocol-v2.txt static struct option builtin_fetch_options[] = { · line 207

Quoted 6 lines
Documentation/technical/protocol-v2.txt +2 -0
@@ -202,6 +203,8 @@ static struct option builtin_fetch_options[] = {
 			TRANSPORT_FAMILY_IPV6),
 	OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
 			N_("report that we have only objects reachable from this object")),
+	OPT_BOOL(0, "negotiate-only", &negotiate_only,
+		 N_("do not fetch a packfile; instead, print ancestors of negotiation tips")),

It seems a little strange for this step, which is mostly used for tests, to be so visible. I think it would be too much work to create a test helper with an equivalent implementation, so maybe describe this option as only for tests in Documentation/git-fetch.txt.

As you mention in your review of the subsequent patch [1], this is because we're calling "fetch" as a subprocess from "push", so it needs to accept command-line arguments specifying that we're not interested in the packfile.

[1] https://lore.kernel.org/git/[email protected]/

Quoted 10 lines

Documentation/technical/protocol-v2.txt static struct option builtin_fetch_options[] = {

Quoted 8 lines
Documentation/technical/protocol-v2.txt +7 -1
-	if (remote) {
+	if (negotiate_only) {
+		struct oidset acked_commits = OIDSET_INIT;
+		struct oidset_iter iter;
+		const struct object_id *oid;
+
+		if (!remote)
+			die(_("Must supply remote when using --negotiate-only"));

Please use lowercase letters to start your error messages.

Thanks - done.

Quoted 9 lines

Documentation/technical/protocol-v2.txt

Quoted 7 lines
Documentation/technical/protocol-v2.txt +7 -0
+static int add_to_object_array(const struct object_id *oid, void *data)
+{
+	struct object_array *a = data;
+
+	add_object_array(parse_object(the_repository, oid), "", a);
+	return 0;
+}

Why parse_object() over lookup_object()?

Switched to lookup_object(). I don't think it matters here because we'll need to parse all commits when doing negotiation anyway (to find their parents), but I changed it to avoid confusion.

Quoted 8 lines

Documentation/technical/protocol-v2.txt

Quoted 5 lines
Documentation/technical/protocol-v2.txt +5 -0
+void negotiate_using_fetch(const struct oid_array *negotiation_tips,
+			   const struct string_list *server_options,
+			   int stateless_rpc,
+			   int fd[],
+			   struct oidset *acked_commits)

It might be worth a careful doc comment to say that we _must_ have already verified the wait-for-done capability.

OK - written in the .h file.

[snip min_generation explanation]

Quoted 5 lines

And finally here you can use min_generation. Otherwise, you will suffer performance problems when there is a deep history but this returns false.

As long as min_generation is the minimum generation of a commit with the COMMON flag, you will be correct to use that limit.

Thanks for the precise guidance of how to use generation numbers to speed things up. I've implemented your suggestions.

Quoted 6 lines

Documentation/technical/protocol-v2.txt

Quoted 4 lines
Documentation/technical/protocol-v2.txt +3 -0
+			last_iteration = 1;
+	}
+	strbuf_release(&req_buf);

Don't forget to clear the COMMON flag before returning.

OK - done. (I don't think it matters here, but I guess it's clearer if we do as you suggest. The loop will take some time to run, but it's small compared to the network time needed anyway.)

Quoted 11 lines

Documentation/technical/protocol-v2.txt

Quoted 8 lines
Documentation/technical/protocol-v2.txt +8 -0
+/*
+ * Execute the --negotiate-only mode of "git fetch", adding all known common
+ * commits to acked_commits.
+ */> +void negotiate_using_fetch(const struct oid_array *negotiation_tips,
+			   const struct string_list *server_options,
+			   int stateless_rpc,
+			   int fd[],
+			   struct oidset *acked_commits);

Here is a good place to specifically mention the wait-for-done capability, or else this method will die() when seeing the "ready" from the server.

Acknowledged.

Quoted 8 lines
Quoted 8 lines
transport-helper.c +0 -0
index 4cd76366fa..4be035edb8 100644
@@ -684,6 +684,16 @@ static int fetch(struct transport *transport,
 		return transport->vtable->fetch(transport, nr_heads, to_fetch);
 	}
 
Quoted 16 lines

transport-helper.c static int fetch(struct transport *transport, · line 696

Quoted 10 lines
transport-helper.c +10 -0
+	/*
+	 * If we reach here, then the server, the client, and/or the transport
+	 * helper does not support protocol v2. --negotiate-only requires
+	 * protocol v2.
+	 */
+	if (data->transport_options.acked_commits) {
+		warning(_("--negotiate-only requires protocol v2"));
+		return -1;
+	}
+

This method continues to do a lot that doesn't seem specific to --negotiate-only. The warning message seems incorrect to me, but is also seems like this would break several cases when using protocol v0. It is equally possible that I'm misunderstanding what is going on here.

It's specific to --negotiate-only in that acked_commits is set if and only if we use --negotiate-only. What cases are you think of that break when using protocol v0?

Quoted 14 lines
Quoted 14 lines
transport-helper.c +14 -0
+	if (data->options.acked_commits) {
+		if (data->version < protocol_v2) {
+			warning(_("--negotiate-only requires protocol v2"));
+			ret = -1;
+		} else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
+			warning(_("server does not support wait-for-done"));
+			ret = -1;
+		} else {
+			negotiate_using_fetch(data->options.negotiation_tips,
+					      transport->server_options,
+					      transport->stateless_rpc,
+					      data->fd,
+					      data->options.acked_commits);
+			ret = 0;
Quoted 8 lines

transport-helper.c

Quoted 3 lines
transport-helper.c +3 -0
+		}
+		goto cleanup;
+	}

These prereqs look right. This makes it look rather strange that negotiate_using_fetch() returns void (and has several die() calls inside) and could instead return an 'int' that is consumed here (and use error() calls instead of die()).

When negotiate_using_fetch() is used, we are already past the capability advertisement step, so we need to believe here that the wait-for-done prerequisite has been met. (Admittedly, said prerequisite wasn't documented, but I've documented it following your suggestion above.) So I think it would be too late to return an "int" after several negotiation steps have occurred and the packfile downloaded.

Quoted 8 lines

transport-helper.c

Quoted 6 lines
transport-helper.c +6 -0
+
+	/*
+	 * If set, whenever transport_fetch_refs() is called, add known common
+	 * commits to this oidset instead of fetching any packfiles.
+	 */
+	struct oidset *acked_commits;

nit: s/If set/If allocated/

Changed.

Quoted 21 lines
Quoted 21 lines
transport-helper.c +6 -3
@@ -1668,10 +1673,13 @@ int upload_pack_v2(struct repository *r, struct strvec *keys,
 		case FETCH_PROCESS_ARGS:
 			process_args(request, &data);
 
-			if (!data.want_obj.nr) {
+			if (!data.want_obj.nr && !data.wait_for_done) {
 				/*
-				 * Request didn't contain any 'want' lines,
-				 * guess they didn't want anything.
+				 * Request didn't contain any 'want' lines (and
+				 * the request does not contain
+				 * "wait-for-done", in which it is reasonable
+				 * to just send 'have's without 'want's); guess
+				 * they didn't want anything.
 				 */
 				state = FETCH_DONE;
 			} else if (data.haves.nr) {
@@ -1723,7 +1731,7 @@ int upload_pack_advertise(struct repository *r,
 		int allow_sideband_all_value;
 		char *str = NULL;
 
Quoted 6 lines

transport-helper.c int upload_pack_advertise(struct repository *r, · line 1734

Quoted 2 lines
transport-helper.c +1 -1
-		strbuf_addstr(value, "shallow");
+		strbuf_addstr(value, "shallow wait-for-done");

It might be nice to have this wait-for-done capability configurable. I'd likely want this on by default, but in case there is an issue it is easier to change config values than ship new binaries.

I felt that we didn't need this knob on the server side as this doesn't expose any more information than a regular fetch, but I'm open to adding it if neeed be.

Quoted 6 lines

This is a pretty big patch. It might have been nice to split the server-side stuff out as its own patch with a follow-up for the client code and the tests.

Thanks,
-Stolee

Personally I prefer that such changes (server and client) be together, so that I can see on both sides how things work. There is (in theory) already a clear divide between both sides, even within a patch, since the server and client parts are usually in different parts. But if the majority prefers that such changes be separate, I'm OK with doing that too.

Also, thanks for your review.

#19 lore raw

Re: [PATCH 6/6] send-pack: support push negotiation

In reply to Derrick Stolee

[snip asking about whether extra_have (a.k.a. advertised) and commons (a.k.a. negotiated) can be merged]

Quoted 11 lines

Here, it would be nice if extra_have and commons were merged before calling pack_objects(). I mentioned a way to perhaps make that easier above, but the context might not make that be super-simple. Running a loop here to scan 'commons' and append them to 'extra_have' might be a sufficient approach.

Generally, this approach seems like it would work. I have not done any local testing, yet.

Thanks,
-Stolee

I was reluctant to merge them because that would involve either (1) adding commons to "extra_have" (as you suggest) or (2) iterating through "extra_have" in order to add it to the "commons" set. For (1), this would modify "extra_have", which is passed in from the outside. Looking at its callers, the main one in git_transport_push() in transport.c calls send_pack() with a set that has traversed the transport API, so I think it would be confusing if such a set suddenly changed. For (2), the extra loop seems more troublesome than having two parameters with clearer names indicating where they come from. I don't mind changing to (2), though, if people want it.

#20 lore raw

Re: [PATCH 6/6] send-pack: support push negotiation

Quoted 15 lines · Jonathan Tan

[snip asking about whether extra_have (a.k.a. advertised) and commons (a.k.a. negotiated) can be merged]

Quoted 11 lines

Here, it would be nice if extra_have and commons were merged before calling pack_objects(). I mentioned a way to perhaps make that easier above, but the context might not make that be super-simple. Running a loop here to scan 'commons' and append them to 'extra_have' might be a sufficient approach.

Generally, this approach seems like it would work. I have not done any local testing, yet.

Thanks,
-Stolee

Quoted 10 lines

I was reluctant to merge them because that would involve either (1) adding commons to "extra_have" (as you suggest) or (2) iterating through "extra_have" in order to add it to the "commons" set. For (1), this would modify "extra_have", which is passed in from the outside. Looking at its callers, the main one in git_transport_push() in transport.c calls send_pack() with a set that has traversed the transport API, so I think it would be confusing if such a set suddenly changed. For (2), the extra loop seems more troublesome than having two parameters with clearer names indicating where they come from. I don't mind changing to (2), though, if people want it.

I suppose this concern about "ownership" is valid and worth having the two parameters in the helper function for extra safety.

Thanks,
-Stolee

#23 lore raw

v2 cover Push negotiation

In reply to Jonathan Tan

Thanks for the review, Junio and Stolee.

The first patch in version 1 has been merged to master, I believe, so here are the remaining 5 patches.

As Stolee pointed out, some tests in version 1 fail when GIT_TEST_PROTOCOL_VERSION is set to 0. I have corrected that, but there is one test that still fails in t5601. That seems to also fail in "master". I'll see if I can fix it and send a separate patch for that.

Jonathan Tan (5):
fetch-pack: refactor process_acks()
fetch-pack: refactor add_haves()
fetch-pack: refactor command and capability write
fetch: teach independent negotiation (no packfile)
send-pack: support push negotiation

 Documentation/config/push.txt           |   7 +
 Documentation/technical/protocol-v2.txt |   8 +
 builtin/fetch.c                         |  27 ++-
 fetch-pack.c                            | 246 ++++++++++++++++--------
 fetch-pack.h                            |  14 ++
 object.h                                |   2 +-
 send-pack.c                             |  61 +++++-
 t/t5516-fetch-push.sh                   |  35 ++++
 t/t5701-git-serve.sh                    |   2 +-
 t/t5702-protocol-v2.sh                  |  89 +++++++++
 transport-helper.c                      |  10 +
 transport.c                             |  30 ++-
 transport.h                             |   6 +
 upload-pack.c                           |  18 +-
 14 files changed, 455 insertions(+), 100 deletions(-)

Range-diff against v1:

-:  ---------- > 1:  8102570374 fetch-pack: refactor process_acks()
-:  ---------- > 2:  57c3451b2e fetch-pack: refactor add_haves()
-:  ---------- > 3:  6871d0cec6 fetch-pack: refactor command and capability write
1:  3ebe4ada28 ! 4:  1de34a6dce fetch: teach independent negotiation (no packfile)
    @@ Commit message
         There are 2 code paths that do not go through fetch_refs_via_pack() that
         needed to be individually excluded: the bundle transport (excluded
         through requiring smart_options, which the bundle transport doesn't
    -    support) and transport helpers that do not support takeover.
    -    Fortunately, none of these support protocol v2.
    +    support) and transport helpers that do not support takeover. If or when
    +    we support independent negotiation for protocol v0, we will need to
    +    modify these 2 code paths to support it. But for now, report failure if
    +    independent negotiation is requested in these cases.
     
         Signed-off-by: Jonathan Tan <[email protected]>
         Signed-off-by: Junio C Hamano <[email protected]>
    @@ builtin/fetch.c: int cmd_fetch(int argc, const char **argv, const char *prefix)
     +		const struct object_id *oid;
     +
     +		if (!remote)
    -+			die(_("Must supply remote when using --negotiate-only"));
    ++			die(_("must supply remote when using --negotiate-only"));
     +		gtransport = prepare_transport(remote, 1);
     +		if (gtransport->smart_options) {
     +			gtransport->smart_options->acked_commits = &acked_commits;
    @@ fetch-pack.c
      #include "fsck.h"
      #include "shallow.h"
     +#include "commit-reach.h"
    ++#include "commit-graph.h"
      
      static int transfer_unpack_limit = -1;
      static int fetch_unpack_limit = -1;
    @@ fetch-pack.c: struct ref *fetch_pack(struct fetch_pack_args *args,
     +{
     +	struct object_array *a = data;
     +
    -+	add_object_array(parse_object(the_repository, oid), "", a);
    ++	add_object_array(lookup_object(the_repository, oid), "", a);
     +	return 0;
     +}
     +
    ++static void clear_common_flag(struct oidset *s)
    ++{
    ++	struct oidset_iter iter;
    ++	const struct object_id *oid;
    ++	oidset_iter_init(s, &iter);
    ++
    ++	while ((oid = oidset_iter_next(&iter))) {
    ++		struct object *obj = lookup_object(the_repository, oid);
    ++		obj->flags &= ~COMMON;
    ++	}
    ++}
    ++
     +void negotiate_using_fetch(const struct oid_array *negotiation_tips,
     +			   const struct string_list *server_options,
     +			   int stateless_rpc,
    @@ fetch-pack.c: struct ref *fetch_pack(struct fetch_pack_args *args,
     +	int in_vain = 0;
     +	int seen_ack = 0;
     +	int last_iteration = 0;
    ++	timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
     +
     +	fetch_negotiator_init(the_repository, &negotiator);
     +	mark_tips(&negotiator, negotiation_tips);
    @@ fetch-pack.c: struct ref *fetch_pack(struct fetch_pack_args *args,
     +				   &received_ready)) {
     +			struct commit *commit = lookup_commit(the_repository,
     +							      &common_oid);
    -+			if (commit)
    ++			if (commit) {
    ++				timestamp_t generation;
    ++
    ++				parse_commit_or_die(commit);
     +				commit->object.flags |= COMMON;
    ++				generation = commit_graph_generation(commit);
    ++				if (generation < min_generation)
    ++					min_generation = generation;
    ++			}
     +			in_vain = 0;
     +			seen_ack = 1;
     +			oidset_insert(acked_commits, &common_oid);
    @@ fetch-pack.c: struct ref *fetch_pack(struct fetch_pack_args *args,
     +			do_check_stateless_delimiter(stateless_rpc, &reader);
     +		if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
     +						 REACH_SCRATCH, 0,
    -+						 GENERATION_NUMBER_ZERO))
    ++						 min_generation))
     +			last_iteration = 1;
     +	}
    ++	clear_common_flag(acked_commits);
     +	strbuf_release(&req_buf);
     +}
     +
    @@ fetch-pack.h: struct ref *fetch_pack(struct fetch_pack_args *args,
     +/*
     + * Execute the --negotiate-only mode of "git fetch", adding all known common
     + * commits to acked_commits.
    ++ *
    ++ * In the capability advertisement that has happened prior to invoking this
    ++ * function, the "wait-for-done" capability must be present.
     + */
     +void negotiate_using_fetch(const struct oid_array *negotiation_tips,
     +			   const struct string_list *server_options,
    @@ t/t5702-protocol-v2.sh: test_expect_success 'deepen-relative' '
     +
     +	setup_negotiate_only "$SERVER" "$URI" &&
     +
    -+	git -C client fetch \
    ++	git -c protocol.version=2 -C client fetch \
     +		--no-tags \
     +		--negotiate-only \
     +		--negotiation-tip=$(git -C client rev-parse HEAD) \
    @@ t/t5702-protocol-v2.sh: test_expect_success 'packfile-uri with transfer.fsckobje
     +
     +	setup_negotiate_only "$SERVER" "$URI" &&
     +
    -+	git -C client fetch \
    ++	git -c protocol.version=2 -C client fetch \
     +		--no-tags \
     +		--negotiate-only \
     +		--negotiation-tip=$(git -C client rev-parse HEAD) \
    @@ t/t5702-protocol-v2.sh: test_expect_success 'packfile-uri with transfer.fsckobje
     +	echo "s/ wait-for-done/ xxxx-xxx-xxxx/" \
     +		>"$HTTPD_ROOT_PATH/one-time-perl" &&
     +
    -+	test_must_fail git -C client fetch \
    ++	test_must_fail git -c protocol.version=2 -C client fetch \
     +		--no-tags \
     +		--negotiate-only \
     +		--negotiation-tip=$(git -C client rev-parse HEAD) \
    @@ transport.h: struct git_transport_options {
      	struct oid_array *negotiation_tips;
     +
     +	/*
    -+	 * If set, whenever transport_fetch_refs() is called, add known common
    -+	 * commits to this oidset instead of fetching any packfiles.
    ++	 * If allocated, whenever transport_fetch_refs() is called, add known
    ++	 * common commits to this oidset instead of fetching any packfiles.
     +	 */
     +	struct oidset *acked_commits;
      };
2:  bd55d6ba36 ! 5:  d38a8b7d66 send-pack: support push negotiation
    @@ t/t5516-fetch-push.sh: test_expect_success 'fetch with pushInsteadOf (should not
     +	git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
     +	git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
     +	echo now pushing without negotiation &&
    -+	GIT_TRACE2_EVENT="$(pwd)/event" git push testrepo refs/heads/main:refs/remotes/origin/main &&
    ++	GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
     +	grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
     +
     +	# Same commands, but with negotiation
    @@ t/t5516-fetch-push.sh: test_expect_success 'fetch with pushInsteadOf (should not
     +	mk_empty testrepo &&
     +	git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
     +	git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
    -+	GIT_TRACE2_EVENT="$(pwd)/event" git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
    ++	GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
     +	grep_wrote 2 event # 1 commit, 1 tree
     +'
     +
    @@ t/t5516-fetch-push.sh: test_expect_success 'fetch with pushInsteadOf (should not
     +	mk_empty testrepo &&
     +	git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
     +	git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
    -+	GIT_TRACE_PACKET="$(pwd)/trace" GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
    ++	GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
     +		git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
     +	grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
     +	test_i18ngrep "push negotiation failed" err
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#24 lore raw

PATCH v2 1/5 fetch-pack: refactor process_acks()

A subsequent commit will need part, but not all, of the functionality in process_acks(), so move some of its functionality to its sole caller do_fetch_pack_v2(). As a side effect, the resulting code is also shorter.

---
 fetch-pack.c | 70 +++++++++++++++++-----------------------------------
 1 file changed, 22 insertions(+), 48 deletions(-)
fetch-pack.c +22 -48
index 2318ebe680..9f3901cdba 100644
@@ -1351,35 +1351,11 @@ static int process_section_header(struct packet_reader *reader,
 	return ret;
 }
 
-enum common_found {
-	/*
-	 * No commit was found to be possessed by both the client and the
-	 * server, and "ready" was not received.
-	 */
-	NO_COMMON_FOUND,
-
-	/*
-	 * At least one commit was found to be possessed by both the client and
-	 * the server, and "ready" was not received.
-	 */
-	COMMON_FOUND,
-
-	/*
-	 * "ready" was received, indicating that the server is ready to send
-	 * the packfile without any further negotiation.
-	 */
-	READY
-};
-
-static enum common_found process_acks(struct fetch_negotiator *negotiator,
-				      struct packet_reader *reader,
-				      struct oidset *common)
+static int process_ack(struct fetch_negotiator *negotiator,
+		       struct packet_reader *reader,
+		       struct object_id *common_oid,
+		       int *received_ready)
 {
-	/* received */
-	int received_ready = 0;
-	int received_ack = 0;
-
-	process_section_header(reader, "acknowledgments", 0);
 	while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
 		const char *arg;
 
@@ -1387,20 +1363,17 @@ static enum common_found process_acks(struct fetch_negotiator *negotiator,
 			continue;
 
 		if (skip_prefix(reader->line, "ACK ", &arg)) {
-			struct object_id oid;
-			received_ack = 1;
-			if (!get_oid_hex(arg, &oid)) {
+			if (!get_oid_hex(arg, common_oid)) {
 				struct commit *commit;
-				oidset_insert(common, &oid);
-				commit = lookup_commit(the_repository, &oid);
+				commit = lookup_commit(the_repository, common_oid);
 				if (negotiator)
 					negotiator->ack(negotiator, commit);
 			}
-			continue;
+			return 1;
 		}
 
 		if (!strcmp(reader->line, "ready")) {
-			received_ready = 1;
+			*received_ready = 1;
 			continue;
 		}
 
@@ -1418,13 +1391,12 @@ static enum common_found process_acks(struct fetch_negotiator *negotiator,
 	 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
 	 * otherwise.
 	 */
-	if (received_ready && reader->status != PACKET_READ_DELIM)
+	if (*received_ready && reader->status != PACKET_READ_DELIM)
 		die(_("expected packfile to be sent after 'ready'"));
-	if (!received_ready && reader->status != PACKET_READ_FLUSH)
+	if (!*received_ready && reader->status != PACKET_READ_FLUSH)
 		die(_("expected no other sections to be sent after no 'ready'"));
 
-	return received_ready ? READY :
-		(received_ack ? COMMON_FOUND : NO_COMMON_FOUND);
+	return 0;
 }
 
 static void receive_shallow_info(struct fetch_pack_args *args,
@@ -1573,6 +1545,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 	struct fetch_negotiator negotiator_alloc;
 	struct fetch_negotiator *negotiator;
 	int seen_ack = 0;
+	struct object_id common_oid;
+	int received_ready = 0;
 	struct string_list packfile_uris = STRING_LIST_INIT_DUP;
 	int i;
 	struct strvec index_pack_args = STRVEC_INIT;
@@ -1631,22 +1605,22 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 			break;
 		case FETCH_PROCESS_ACKS:
 			/* Process ACKs/NAKs */
-			switch (process_acks(negotiator, &reader, &common)) {
-			case READY:
+			process_section_header(&reader, "acknowledgments", 0);
+			while (process_ack(negotiator, &reader, &common_oid,
+					   &received_ready)) {
+				in_vain = 0;
+				seen_ack = 1;
+				oidset_insert(&common, &common_oid);
+			}
+			if (received_ready) {
 				/*
 				 * Don't check for response delimiter; get_pack() will
 				 * read the rest of this response.
 				 */
 				state = FETCH_GET_PACK;
-				break;
-			case COMMON_FOUND:
-				in_vain = 0;
-				seen_ack = 1;
-				/* fallthrough */
-			case NO_COMMON_FOUND:
+			} else {
 				do_check_stateless_delimiter(args, &reader);
 				state = FETCH_SEND_REQUEST;
-				break;
 			}
 			break;
 		case FETCH_GET_PACK:
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#25 lore raw

PATCH v2 2/5 fetch-pack: refactor add_haves()

In reply to Jonathan Tan

A subsequent commit will need part, but not all, of the functionality in add_haves(), so move some of its functionality to its sole caller send_fetch_request().

---
 fetch-pack.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
fetch-pack.c +12 -16
index 9f3901cdba..128ad47d2a 100644
@@ -1195,11 +1195,9 @@ static void add_common(struct strbuf *req_buf, struct oidset *common)
 }
 
 static int add_haves(struct fetch_negotiator *negotiator,
-		     int seen_ack,
 		     struct strbuf *req_buf,
-		     int *haves_to_send, int *in_vain)
+		     int *haves_to_send)
 {
-	int ret = 0;
 	int haves_added = 0;
 	const struct object_id *oid;
 
@@ -1209,17 +1207,10 @@ static int add_haves(struct fetch_negotiator *negotiator,
 			break;
 	}
 
-	*in_vain += haves_added;
-	if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
-		/* Send Done */
-		packet_buf_write(req_buf, "done\n");
-		ret = 1;
-	}
-
 	/* Increase haves to send on next round */
 	*haves_to_send = next_flush(1, *haves_to_send);
 
-	return ret;
+	return haves_added;
 }
 
 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
@@ -1228,7 +1219,8 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 			      int *haves_to_send, int *in_vain,
 			      int sideband_all, int seen_ack)
 {
-	int ret = 0;
+	int haves_added;
+	int done_sent = 0;
 	const char *hash_name;
 	struct strbuf req_buf = STRBUF_INIT;
 
@@ -1312,9 +1304,13 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 	/* Add all of the common commits we've found in previous rounds */
 	add_common(&req_buf, common);
 
-	/* Add initial haves */
-	ret = add_haves(negotiator, seen_ack, &req_buf,
-			haves_to_send, in_vain);
+	haves_added = add_haves(negotiator, &req_buf, haves_to_send);
+	*in_vain += haves_added;
+	if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
+		/* Send Done */
+		packet_buf_write(&req_buf, "done\n");
+		done_sent = 1;
+	}
 
 	/* Send request */
 	packet_buf_flush(&req_buf);
@@ -1322,7 +1318,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 		die_errno(_("unable to write request to remote"));
 
 	strbuf_release(&req_buf);
-	return ret;
+	return done_sent;
 }
 
 /*
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#26 lore raw

PATCH v2 3/5 fetch-pack: refactor command and capability write

In reply to Jonathan Tan

A subsequent commit will need this functionality independent of the rest of send_fetch_request(), so put this into its own function.

---
 fetch-pack.c | 41 ++++++++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 17 deletions(-)
fetch-pack.c +24 -17
index 128ad47d2a..512fe5450d 100644
@@ -1213,29 +1213,23 @@ static int add_haves(struct fetch_negotiator *negotiator,
 	return haves_added;
 }
 
-static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
-			      struct fetch_pack_args *args,
-			      const struct ref *wants, struct oidset *common,
-			      int *haves_to_send, int *in_vain,
-			      int sideband_all, int seen_ack)
+static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+						 const struct string_list *server_options)
 {
-	int haves_added;
-	int done_sent = 0;
 	const char *hash_name;
-	struct strbuf req_buf = STRBUF_INIT;
 
 	if (server_supports_v2("fetch", 1))
-		packet_buf_write(&req_buf, "command=fetch");
+		packet_buf_write(req_buf, "command=fetch");
 	if (server_supports_v2("agent", 0))
-		packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
+		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
 	if (advertise_sid && server_supports_v2("session-id", 0))
-		packet_buf_write(&req_buf, "session-id=%s", trace2_session_id());
-	if (args->server_options && args->server_options->nr &&
+		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
+	if (server_options && server_options->nr &&
 	    server_supports_v2("server-option", 1)) {
 		int i;
-		for (i = 0; i < args->server_options->nr; i++)
-			packet_buf_write(&req_buf, "server-option=%s",
-					 args->server_options->items[i].string);
+		for (i = 0; i < server_options->nr; i++)
+			packet_buf_write(req_buf, "server-option=%s",
+					 server_options->items[i].string);
 	}
 
 	if (server_feature_v2("object-format", &hash_name)) {
@@ -1243,13 +1237,26 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
 			die(_("mismatched algorithms: client %s; server %s"),
 			    the_hash_algo->name, hash_name);
-		packet_buf_write(&req_buf, "object-format=%s", the_hash_algo->name);
+		packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
 	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
 		die(_("the server does not support algorithm '%s'"),
 		    the_hash_algo->name);
 	}
+	packet_buf_delim(req_buf);
+}
+
+static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
+			      struct fetch_pack_args *args,
+			      const struct ref *wants, struct oidset *common,
+			      int *haves_to_send, int *in_vain,
+			      int sideband_all, int seen_ack)
+{
+	int haves_added;
+	int done_sent = 0;
+	struct strbuf req_buf = STRBUF_INIT;
+
+	write_fetch_command_and_capabilities(&req_buf, args->server_options);
 
-	packet_buf_delim(&req_buf);
 	if (args->use_thin_pack)
 		packet_buf_write(&req_buf, "thin-pack");
 	if (args->no_progress)
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#27 lore raw

PATCH v2 4/5 fetch: teach independent negotiation (no packfile)

In reply to Jonathan Tan

Currently, the packfile negotiation step within a Git fetch cannot be done independent of sending the packfile, even though there is at least one application wherein this is useful. Therefore, make it possible for this negotiation step to be done independently. A subsequent commit will use this for one such application - push negotiation.

This feature is for protocol v2 only. (An implementation for protocol v0 would require a separate implementation in the fetch, transport, and transport helper code.)

In the protocol, the main hindrance towards independent negotiation is that the server can unilaterally decide to send the packfile. This is solved by a "wait-for-done" argument: the server will then wait for the client to say "done". In practice, the client will never say it; instead it will cease requests once it is satisfied.

In the client, the main change lies in the transport and transport helper code. fetch_refs_via_pack() performs everything needed - protocol version and capability checks, and the negotiation itself.

There are 2 code paths that do not go through fetch_refs_via_pack() that needed to be individually excluded: the bundle transport (excluded through requiring smart_options, which the bundle transport doesn't support) and transport helpers that do not support takeover. If or when we support independent negotiation for protocol v0, we will need to modify these 2 code paths to support it. But for now, report failure if independent negotiation is requested in these cases.

---
 Documentation/technical/protocol-v2.txt |   8 ++
 builtin/fetch.c                         |  27 +++++-
 fetch-pack.c                            | 111 +++++++++++++++++++++++-
 fetch-pack.h                            |  14 +++
 object.h                                |   2 +-
 t/t5701-git-serve.sh                    |   2 +-
 t/t5702-protocol-v2.sh                  |  89 +++++++++++++++++++
 transport-helper.c                      |  10 +++
 transport.c                             |  30 +++++--
 transport.h                             |   6 ++
 upload-pack.c                           |  18 ++--
 11 files changed, 300 insertions(+), 17 deletions(-)
Documentation/technical/protocol-v2.txt +8 -0
index a7c806a73e..0b371d8de4 100644
@@ -346,6 +346,14 @@ explained below.
 	client should download from all given URIs. Currently, the
 	protocols supported are "http" and "https".
 
+If the 'wait-for-done' feature is advertised, the following argument
+can be included in the client's request.
+
+    wait-for-done
+	Indicates to the server that it should never send "ready", but
+	should wait for the client to say "done" before sending the
+	packfile.
+
 The response of `fetch` is broken into a number of sections separated by
 delimiter packets (0001), with each section beginning with its section
 header. Most sections are sent only when the packfile is sent.
builtin/fetch.c +26 -1
index 0b90de87c7..a732295a6b 100644
@@ -82,6 +82,7 @@ static struct string_list server_options = STRING_LIST_INIT_DUP;
 static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
 static int fetch_write_commit_graph = -1;
 static int stdin_refspecs = 0;
+static int negotiate_only;
 
 static int git_fetch_config(const char *k, const char *v, void *cb)
 {
@@ -202,6 +203,8 @@ static struct option builtin_fetch_options[] = {
 			TRANSPORT_FAMILY_IPV6),
 	OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
 			N_("report that we have only objects reachable from this object")),
+	OPT_BOOL(0, "negotiate-only", &negotiate_only,
+		 N_("do not fetch a packfile; instead, print ancestors of negotiation tips")),
 	OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
 	OPT_BOOL(0, "auto-maintenance", &enable_auto_gc,
 		 N_("run 'maintenance --auto' after fetching")),
@@ -1986,7 +1989,29 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	if (remote) {
+	if (negotiate_only) {
+		struct oidset acked_commits = OIDSET_INIT;
+		struct oidset_iter iter;
+		const struct object_id *oid;
+
+		if (!remote)
+			die(_("must supply remote when using --negotiate-only"));
+		gtransport = prepare_transport(remote, 1);
+		if (gtransport->smart_options) {
+			gtransport->smart_options->acked_commits = &acked_commits;
+		} else {
+			warning(_("Protocol does not support --negotiate-only, exiting."));
+			return 1;
+		}
+		if (server_options.nr)
+			gtransport->server_options = &server_options;
+		result = transport_fetch_refs(gtransport, NULL);
+
+		oidset_iter_init(&acked_commits, &iter);
+		while ((oid = oidset_iter_next(&iter)))
+			printf("%s\n", oid_to_hex(oid));
+		oidset_clear(&acked_commits);
+	} else if (remote) {
 		if (filter_options.choice || has_promisor_remote())
 			fetch_one_setup_partial(remote);
 		result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs);
fetch-pack.c +107 -4

149 lines not on this page · raw message

fetch-pack.h +14 -0
index f114d72775..7f94a2a583 100644
@@ -5,6 +5,7 @@
 #include "run-command.h"
 #include "protocol.h"
 #include "list-objects-filter-options.h"
+#include "oidset.h"
 
 struct oid_array;
 
@@ -81,6 +82,19 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
 		       struct string_list *pack_lockfiles,
 		       enum protocol_version version);
 
+/*
+ * Execute the --negotiate-only mode of "git fetch", adding all known common
+ * commits to acked_commits.
+ *
+ * In the capability advertisement that has happened prior to invoking this
+ * function, the "wait-for-done" capability must be present.
+ */
+void negotiate_using_fetch(const struct oid_array *negotiation_tips,
+			   const struct string_list *server_options,
+			   int stateless_rpc,
+			   int fd[],
+			   struct oidset *acked_commits);
+
 /*
  * Print an appropriate error message for each sought ref that wasn't
  * matched.  Return 0 if all sought refs were matched, otherwise 1.
object.h +1 -1
index 59daadce21..4806fa8e66 100644
@@ -60,7 +60,7 @@ struct object_array {
 /*
  * object flag allocation:
  * revision.h:               0---------10         15             23------26
- * fetch-pack.c:             01
+ * fetch-pack.c:             01    67
  * negotiator/default.c:       2--5
  * walker.c:                 0-2
  * upload-pack.c:                4       11-----14  16-----19
t/t5701-git-serve.sh +1 -1
index 509f379d49..f03bb04803 100755
@@ -16,7 +16,7 @@ test_expect_success 'test capability advertisement' '
 	version 2
 	agent=git/$(git version | cut -d" " -f3)
 	ls-refs=unborn
-	fetch=shallow
+	fetch=shallow wait-for-done
 	server-option
 	object-format=$(test_oid algo)
 	0000
t/t5702-protocol-v2.sh +89 -0

101 lines not on this page · raw message

transport-helper.c +10 -0
index 4cd76366fa..4be035edb8 100644
@@ -684,6 +684,16 @@ static int fetch(struct transport *transport,
 		return transport->vtable->fetch(transport, nr_heads, to_fetch);
 	}
 
+	/*
+	 * If we reach here, then the server, the client, and/or the transport
+	 * helper does not support protocol v2. --negotiate-only requires
+	 * protocol v2.
+	 */
+	if (data->transport_options.acked_commits) {
+		warning(_("--negotiate-only requires protocol v2"));
+		return -1;
+	}
+
 	if (!data->get_refs_list_called)
 		get_refs_list_using_list(transport, 0);
 
transport.c +25 -5
index ef66e73090..80caeaa72f 100644
@@ -392,16 +392,29 @@ static int fetch_refs_via_pack(struct transport *transport,
 	else if (data->version <= protocol_v1)
 		die_if_server_options(transport);
 
+	if (data->options.acked_commits) {
+		if (data->version < protocol_v2) {
+			warning(_("--negotiate-only requires protocol v2"));
+			ret = -1;
+		} else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
+			warning(_("server does not support wait-for-done"));
+			ret = -1;
+		} else {
+			negotiate_using_fetch(data->options.negotiation_tips,
+					      transport->server_options,
+					      transport->stateless_rpc,
+					      data->fd,
+					      data->options.acked_commits);
+			ret = 0;
+		}
+		goto cleanup;
+	}
+
 	refs = fetch_pack(&args, data->fd,
 			  refs_tmp ? refs_tmp : transport->remote_refs,
 			  to_fetch, nr_heads, &data->shallow,
 			  &transport->pack_lockfiles, data->version);
 
-	close(data->fd[0]);
-	close(data->fd[1]);
-	if (finish_connect(data->conn))
-		ret = -1;
-	data->conn = NULL;
 	data->got_remote_heads = 0;
 	data->options.self_contained_and_connected =
 		args.self_contained_and_connected;
@@ -412,6 +425,13 @@ static int fetch_refs_via_pack(struct transport *transport,
 	if (report_unmatched_refs(to_fetch, nr_heads))
 		ret = -1;
 
+cleanup:
+	close(data->fd[0]);
+	close(data->fd[1]);
+	if (finish_connect(data->conn))
+		ret = -1;
+	data->conn = NULL;
+
 	free_refs(refs_tmp);
 	free_refs(refs);
 	return ret;
transport.h +6 -0
index 4d5db0a7f2..1cbab11373 100644
@@ -47,6 +47,12 @@ struct git_transport_options {
 	 * transport_set_option().
 	 */
 	struct oid_array *negotiation_tips;
+
+	/*
+	 * If allocated, whenever transport_fetch_refs() is called, add known
+	 * common commits to this oidset instead of fetching any packfiles.
+	 */
+	struct oidset *acked_commits;
 };
 
 enum transport_family {
upload-pack.c +13 -5
index e19583ae0f..b432ef0119 100644
@@ -103,6 +103,7 @@ struct upload_pack_data {
 	unsigned use_ofs_delta : 1;
 	unsigned no_progress : 1;
 	unsigned use_include_tag : 1;
+	unsigned wait_for_done : 1;
 	unsigned allow_filter : 1;
 	unsigned allow_filter_fallback : 1;
 	unsigned long tree_filter_max_depth;
@@ -1496,6 +1497,10 @@ static void process_args(struct packet_reader *request,
 			data->done = 1;
 			continue;
 		}
+		if (!strcmp(arg, "wait-for-done")) {
+			data->wait_for_done = 1;
+			continue;
+		}
 
 		/* Shallow related arguments */
 		if (process_shallow(arg, &data->shallows))
@@ -1578,7 +1583,7 @@ static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
 				    oid_to_hex(&acks->oid[i]));
 	}
 
-	if (ok_to_give_up(data)) {
+	if (!data->wait_for_done && ok_to_give_up(data)) {
 		/* Send Ready */
 		packet_writer_write(&data->writer, "ready\n");
 		return 1;
@@ -1668,10 +1673,13 @@ int upload_pack_v2(struct repository *r, struct strvec *keys,
 		case FETCH_PROCESS_ARGS:
 			process_args(request, &data);
 
-			if (!data.want_obj.nr) {
+			if (!data.want_obj.nr && !data.wait_for_done) {
 				/*
-				 * Request didn't contain any 'want' lines,
-				 * guess they didn't want anything.
+				 * Request didn't contain any 'want' lines (and
+				 * the request does not contain
+				 * "wait-for-done", in which it is reasonable
+				 * to just send 'have's without 'want's); guess
+				 * they didn't want anything.
 				 */
 				state = FETCH_DONE;
 			} else if (data.haves.nr) {
@@ -1723,7 +1731,7 @@ int upload_pack_advertise(struct repository *r,
 		int allow_sideband_all_value;
 		char *str = NULL;
 
-		strbuf_addstr(value, "shallow");
+		strbuf_addstr(value, "shallow wait-for-done");
 
 		if (!repo_config_get_bool(the_repository,
 					 "uploadpack.allowfilter",
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#32 lore raw

PATCH v2 5/5 send-pack: support push negotiation

In reply to Jonathan Tan

Teach Git the push.negotiate config variable.

---
 Documentation/config/push.txt |  7 ++++
 send-pack.c                   | 61 ++++++++++++++++++++++++++++++++---
 t/t5516-fetch-push.sh         | 35 ++++++++++++++++++++
 3 files changed, 99 insertions(+), 4 deletions(-)
Documentation/config/push.txt +7 -0
index 21b256e0a4..f2667b2689 100644
@@ -120,3 +120,10 @@ push.useForceIfIncludes::
 	`--force-if-includes` as an option to linkgit:git-push[1]
 	in the command line. Adding `--no-force-if-includes` at the
 	time of push overrides this configuration setting.
+
+push.negotiate::
+	If set to "true", attempt to reduce the size of the packfile
+	sent by rounds of negotiation in which the client and the
+	server attempt to find commits in common. If "false", Git will
+	rely solely on the server's ref advertisement to find commits
+	in common.
send-pack.c +57 -4

102 lines not on this page · raw message

t/t5516-fetch-push.sh +35 -0
index f11742ed59..0916f76302 100755
@@ -191,6 +191,41 @@ test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
 	)
 '
 
+grep_wrote () {
+	object_count=$1
+	file_name=$2
+	grep 'write_pack_file/wrote.*"value":"'$1'"' $2
+}
+
+test_expect_success 'push with negotiation' '
+	# Without negotiation
+	mk_empty testrepo &&
+	git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
+	git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
+	echo now pushing without negotiation &&
+	GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
+	grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
+
+	# Same commands, but with negotiation
+	rm event &&
+	mk_empty testrepo &&
+	git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
+	git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
+	GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
+	grep_wrote 2 event # 1 commit, 1 tree
+'
+
+test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
+	rm event &&
+	mk_empty testrepo &&
+	git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
+	git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
+	GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
+		git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
+	grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
+	test_i18ngrep "push negotiation failed" err
+'
+
 test_expect_success 'push without wildcard' '
 	mk_empty testrepo &&
 
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#28 lore raw

Re: [PATCH v2 4/5] fetch: teach independent negotiation (no packfile)

In reply to Jonathan Tan

fetch-pack.c struct ref *fetch_pack(struct fetch_pack_args *args, · line 1973

Quoted 5 lines · Jonathan Tan
fetch-pack.c +5 -0
+static int add_to_object_array(const struct object_id *oid, void *data)
+{
+	struct object_array *a = data;
+
+	add_object_array(lookup_object(the_repository, oid), "", a);

Moving to lookup_object() made me look around, but at this point the object in question (which comes from the negotiation_tips) has been instantiated, so it is OK.

Side note. The big difference between lookup and parse is what happens when this process hasn't seen the object yet---the former will just return NULL instead of instantiating the in-core copy; for that reason, it is easier on the readers to use parse_object() if we know we want an in-core object *and* when we use it we want to see it parsed already).

fetch-pack.c struct ref *fetch_pack(struct fetch_pack_args *args, · line 1984

Quoted 8 lines
fetch-pack.c +8 -0
+static void clear_common_flag(struct oidset *s)
+{
+	struct oidset_iter iter;
+	const struct object_id *oid;
+	oidset_iter_init(s, &iter);
+
+	while ((oid = oidset_iter_next(&iter))) {
+		struct object *obj = lookup_object(the_repository, oid);

This one obviously is OK ;-) The fact we are clearing by definition means we already do have in-core objects.

fetch-pack.c struct ref *fetch_pack(struct fetch_pack_args *args, · line 1987

Quoted 3 lines
fetch-pack.c +3 -0
+		obj->flags &= ~COMMON;
+	}
+}

Thanks.

#29 lore raw

Re: [PATCH v2 4/5] fetch: teach independent negotiation (no packfile)

Quoted 12 lines · Junio C Hamano

fetch-pack.c struct ref *fetch_pack(struct fetch_pack_args *args, · line 1973

Quoted 5 lines · Jonathan Tan
fetch-pack.c +5 -0
+static int add_to_object_array(const struct object_id *oid, void *data)
+{
+	struct object_array *a = data;
+
+	add_object_array(lookup_object(the_repository, oid), "", a);

Moving to lookup_object() made me look around, but at this point the object in question (which comes from the negotiation_tips) has been instantiated, so it is OK.

Quoted 6 lines

Side note. The big difference between lookup and parse is what happens when this process hasn't seen the object yet---the former will just return NULL instead of instantiating the in-core copy; for that reason, it is easier on the readers to use parse_object() if we know we want an in-core object *and* when we use it we want to see it parsed already).

Please forgive my incorrect recommendation here. I was expecting lookup_object() to behave like lookup_commit(), which creates the object if it is not already in the cached set.

Quoted 11 lines

fetch-pack.c struct ref *fetch_pack(struct fetch_pack_args *args, · line 1984

Quoted 8 lines
fetch-pack.c +8 -0
+static void clear_common_flag(struct oidset *s)
+{
+	struct oidset_iter iter;
+	const struct object_id *oid;
+	oidset_iter_init(s, &iter);
+
+	while ((oid = oidset_iter_next(&iter))) {
+		struct object *obj = lookup_object(the_repository, oid);

This one obviously is OK ;-) The fact we are clearing by definition means we already do have in-core objects.

Thanks for your careful eye here.

-Stolee

#31 lore raw

Re: [PATCH v2 4/5] fetch: teach independent negotiation (no packfile)

In reply to Jonathan Tan
Quoted 15 lines · Jonathan Tan
fetch-pack.c +15 -0
+void negotiate_using_fetch(const struct oid_array *negotiation_tips,
+			   const struct string_list *server_options,
+			   int stateless_rpc,
+			   int fd[],
+			   struct oidset *acked_commits)
+{
+	struct fetch_negotiator negotiator;
+	struct packet_reader reader;
+	struct object_array nt_object_array = OBJECT_ARRAY_INIT;
+	struct strbuf req_buf = STRBUF_INIT;
+	int haves_to_send = INITIAL_FLUSH;
+	int in_vain = 0;
+	int seen_ack = 0;
+	int last_iteration = 0;
+	timestamp_t min_generation = GENERATION_NUMBER_INFINITY;

...

Quoted 17 lines
fetch-pack.c +17 -0
+		/* Process ACKs/NAKs */
+		process_section_header(&reader, "acknowledgments", 0);
+		while (process_ack(&negotiator, &reader, &common_oid,
+				   &received_ready)) {
+			struct commit *commit = lookup_commit(the_repository,
+							      &common_oid);
+			if (commit) {
+				timestamp_t generation;
+
+				parse_commit_or_die(commit);
+				commit->object.flags |= COMMON;
+				generation = commit_graph_generation(commit);
+				if (generation < min_generation)
+					min_generation = generation;
+			}
+			in_vain = 0;
+			seen_ack = 1;

fetch-pack.c struct ref *fetch_pack(struct fetch_pack_args *args, · line 2062

Quoted 10 lines
fetch-pack.c +10 -0
+			oidset_insert(acked_commits, &common_oid);
+		}
+		if (received_ready)
+			die(_("unexpected 'ready' from remote"));
+		else
+			do_check_stateless_delimiter(stateless_rpc, &reader);
+		if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
+						 REACH_SCRATCH, 0,
+						 min_generation))
+			last_iteration = 1;

I'm just chiming in to confirm that this use of min_generation is correct. Thanks!

-Stolee

#30 lore raw

Re: [PATCH v2 4/5] fetch: teach independent negotiation (no packfile)

In reply to Derrick Stolee
Quoted 11 lines · Derrick Stolee
Quoted 10 lines

Moving to lookup_object() made me look around, but at this point the object in question (which comes from the negotiation_tips) has been instantiated, so it is OK.

Side note. The big difference between lookup and parse is what happens when this process hasn't seen the object yet---the former will just return NULL instead of instantiating the in-core copy; for that reason, it is easier on the readers to use parse_object() if we know we want an in-core object *and* when we use it we want to see it parsed already).

Quoted 3 lines

Please forgive my incorrect recommendation here. I was expecting lookup_object() to behave like lookup_commit(), which creates the object if it is not already in the cached set.

It is not wrong per-se to recommend or use lookup_object() in this case, as long as who recommends or uses the function is certain that the object should already exist in-core.

But it is just that the lookup_object() is often not what the caller wants to use; it is a very rare use case for a caller to be able to say "I am OK for you to return NULL to me for this object, even if it is in our object store, when we do not yet have it in-core". The only exception is the implementation of lookup_<type>() helpers, where they handle "not loaded yet" answer from lookup_object() with create_object() and always return usable in-core object to the caller.

So the sole practical use case for lookup_object() outside the lookup_<type>() implementation becomes "I am 100% sure that an object with this object name has already been loaded in-core, so please let me access it", which may be optimized for a wrong case (because most of the API pass in-core objects, not object names, around). I am not sure if this particular API strangeness can be remedied, though.

Thanks.