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

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

From
JTJonathan Tan <jonathantanmy@google.com>
Date
May 4, 2021, 21:15 UTC
Message-ID
<8102570374a87c032bff2d7114c67390f55f9a1b.1620162764.git.jonathantanmy@google.com>
In-Reply-To
<cover.1620162764.git.jonathantanmy@google.com>

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.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 fetch-pack.c | 70 +++++++++++++++++-----------------------------------
 1 file changed, 22 insertions(+), 48 deletions(-)
diff --git a/fetch-pack.c b/fetch-pack.c
index 2318ebe680..9f3901cdba 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -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:
-- 
2.31.1.527.g47e6f16901-goog
Previous: Jonathan TanNext: Jonathan Tan
Message 25 of 33 in “Push negotiation”
  1. 0/6 Push negotiationJonathan Tan, Apr 9, 2021
  2. 1/6 fetch-pack: buffer object-format with other argsJonathan Tan, Apr 9, 2021
  3. Junio C HamanoApr 9, 2021
  4. Jonathan TanApr 9, 2021
  5. 2/6 fetch-pack: refactor process_acks()Jonathan Tan, Apr 9, 2021
  6. Junio C HamanoApr 9, 2021
  7. Derrick StoleeMay 3, 2021
  8. 3/6 fetch-pack: refactor add_haves()Jonathan Tan, Apr 9, 2021
  9. Junio C HamanoApr 9, 2021
  10. 4/6 fetch-pack: refactor command and capability writeJonathan Tan, Apr 9, 2021
  11. Junio C HamanoApr 9, 2021
  12. 5/6 fetch: teach independent negotiation (no packfile)Jonathan Tan, Apr 9, 2021
  13. Junio C HamanoApr 9, 2021
  14. Jonathan TanApr 9, 2021
  15. Derrick StoleeMay 3, 2021
  16. Derrick StoleeMay 3, 2021
  17. Jonathan TanMay 3, 2021
  18. 6/6 send-pack: support push negotiationJonathan Tan, Apr 9, 2021
  19. Derrick StoleeMay 3, 2021
  20. Jonathan TanMay 3, 2021
  21. Derrick StoleeMay 4, 2021
  22. Junio C HamanoApr 30, 2021
  23. Derrick StoleeApr 30, 2021
  24. 0/5 Push negotiationJonathan Tan, May 4, 2021
  25. 1/5 fetch-pack: refactor process_acks()Jonathan Tan, May 4, 2021
  26. 2/5 fetch-pack: refactor add_haves()Jonathan Tan, May 4, 2021
  27. 3/5 fetch-pack: refactor command and capability writeJonathan Tan, May 4, 2021
  28. 4/5 fetch: teach independent negotiation (no packfile)Jonathan Tan, May 4, 2021
  29. Junio C HamanoMay 5, 2021
  30. Derrick StoleeMay 5, 2021
  31. Junio C HamanoMay 6, 2021
  32. Derrick StoleeMay 5, 2021
  33. 5/5 send-pack: support push negotiationJonathan Tan, May 4, 2021

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

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