fetch-pack: refactor add_haves()

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(). 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:10 UTC 57c3451b2e36d003aa7fc398c644ce71ab668b5e
1 file changed +12 -16
fetch-pack.c
+12 -16
@@ -1195,11 +1195,9 @@ static void add_common(struct strbuf *req_buf, struct oidset *common)
1195 }
1196
1197 static int add_haves(struct fetch_negotiator *negotiator,
1198 - int seen_ack,
1198 struct strbuf *req_buf,
1200 - int *haves_to_send, int *in_vain)
1199 + int *haves_to_send)
1200 {
1202 - int ret = 0;
1201 int haves_added = 0;
1202 const struct object_id *oid;
1203
@@ -1209,17 +1207,10 @@ static int add_haves(struct fetch_negotiator *negotiator,
1207 break;
1208 }
1209
1212 - *in_vain += haves_added;
1213 - if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1214 - /* Send Done */
1215 - packet_buf_write(req_buf, "done\n");
1216 - ret = 1;
1217 - }
1218 -
1210 /* Increase haves to send on next round */
1211 *haves_to_send = next_flush(1, *haves_to_send);
1212
1222 - return ret;
1213 + return haves_added;
1214 }
1215
1216 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,
1219 int *haves_to_send, int *in_vain,
1220 int sideband_all, int seen_ack)
1221 {
1231 - int ret = 0;
1222 + int haves_added;
1223 + int done_sent = 0;
1224 const char *hash_name;
1225 struct strbuf req_buf = STRBUF_INIT;
1226
@@ -1312,9 +1304,13 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1304 /* Add all of the common commits we've found in previous rounds */
1305 add_common(&req_buf, common);
1306
1315 - /* Add initial haves */
1316 - ret = add_haves(negotiator, seen_ack, &req_buf,
1317 - haves_to_send, in_vain);
1307 + haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1308 + *in_vain += haves_added;
1309 + if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1310 + /* Send Done */
1311 + packet_buf_write(&req_buf, "done\n");
1312 + done_sent = 1;
1313 + }
1314
1315 /* Send request */
1316 packet_buf_flush(&req_buf);
@@ -1322,7 +1318,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1318 die_errno(_("unable to write request to remote"));
1319
1320 strbuf_release(&req_buf);
1325 - return ret;
1321 + return done_sent;
1322 }
1323
1324 /*