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. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Tan committed
Apr 8, 2021 at 18:09 UTC
8102570374a87c032bff2d7114c67390f55f9a1b
1 file changed
+22
-48
fetch-pack.c
+22
-48
@@ -1351,35 +1351,11 @@ static int process_section_header(struct packet_reader *reader,
1351
return ret;
1352
}
1353
1354
-enum common_found {
1355
- /*
1356
- * No commit was found to be possessed by both the client and the
1357
- * server, and "ready" was not received.
1358
- */
1359
- NO_COMMON_FOUND,
1360
-
1361
- /*
1362
- * At least one commit was found to be possessed by both the client and
1363
- * the server, and "ready" was not received.
1364
- */
1365
- COMMON_FOUND,
1366
-
1367
- /*
1368
- * "ready" was received, indicating that the server is ready to send
1369
- * the packfile without any further negotiation.
1370
- */
1371
- READY
1372
-};
1373
-
1374
-static enum common_found process_acks(struct fetch_negotiator *negotiator,
1375
- struct packet_reader *reader,
1376
- struct oidset *common)
1354
+static int process_ack(struct fetch_negotiator *negotiator,
1355
+ struct packet_reader *reader,
1356
+ struct object_id *common_oid,
1357
+ int *received_ready)
1358
{
1378
- /* received */
1379
- int received_ready = 0;
1380
- int received_ack = 0;
1381
-
1382
- process_section_header(reader, "acknowledgments", 0);
1359
while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1360
const char *arg;
1361
@@ -1387,20 +1363,17 @@ static enum common_found process_acks(struct fetch_negotiator *negotiator,
1363
continue;
1364
1365
if (skip_prefix(reader->line, "ACK ", &arg)) {
1390
- struct object_id oid;
1391
- received_ack = 1;
1392
- if (!get_oid_hex(arg, &oid)) {
1366
+ if (!get_oid_hex(arg, common_oid)) {
1367
struct commit *commit;
1394
- oidset_insert(common, &oid);
1395
- commit = lookup_commit(the_repository, &oid);
1368
+ commit = lookup_commit(the_repository, common_oid);
1369
if (negotiator)
1370
negotiator->ack(negotiator, commit);
1371
}
1399
- continue;
1372
+ return 1;
1373
}
1374
1375
if (!strcmp(reader->line, "ready")) {
1403
- received_ready = 1;
1376
+ *received_ready = 1;
1377
continue;
1378
}
1379
@@ -1418,13 +1391,12 @@ static enum common_found process_acks(struct fetch_negotiator *negotiator,
1391
* sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1392
* otherwise.
1393
*/
1421
- if (received_ready && reader->status != PACKET_READ_DELIM)
1394
+ if (*received_ready && reader->status != PACKET_READ_DELIM)
1395
die(_("expected packfile to be sent after 'ready'"));
1423
- if (!received_ready && reader->status != PACKET_READ_FLUSH)
1396
+ if (!*received_ready && reader->status != PACKET_READ_FLUSH)
1397
die(_("expected no other sections to be sent after no 'ready'"));
1398
1426
- return received_ready ? READY :
1427
- (received_ack ? COMMON_FOUND : NO_COMMON_FOUND);
1399
+ return 0;
1400
}
1401
1402
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,
1545
struct fetch_negotiator negotiator_alloc;
1546
struct fetch_negotiator *negotiator;
1547
int seen_ack = 0;
1548
+ struct object_id common_oid;
1549
+ int received_ready = 0;
1550
struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1551
int i;
1552
struct strvec index_pack_args = STRVEC_INIT;
@@ -1631,22 +1605,22 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1605
break;
1606
case FETCH_PROCESS_ACKS:
1607
/* Process ACKs/NAKs */
1634
- switch (process_acks(negotiator, &reader, &common)) {
1635
- case READY:
1608
+ process_section_header(&reader, "acknowledgments", 0);
1609
+ while (process_ack(negotiator, &reader, &common_oid,
1610
+ &received_ready)) {
1611
+ in_vain = 0;
1612
+ seen_ack = 1;
1613
+ oidset_insert(&common, &common_oid);
1614
+ }
1615
+ if (received_ready) {
1616
/*
1617
* Don't check for response delimiter; get_pack() will
1618
* read the rest of this response.
1619
*/
1620
state = FETCH_GET_PACK;
1641
- break;
1642
- case COMMON_FOUND:
1643
- in_vain = 0;
1644
- seen_ack = 1;
1645
- /* fallthrough */
1646
- case NO_COMMON_FOUND:
1621
+ } else {
1622
do_check_stateless_delimiter(args, &reader);
1623
state = FETCH_SEND_REQUEST;
1649
- break;
1624
}
1625
break;
1626
case FETCH_GET_PACK: