Commit 19ec5923db885f089a1b862615f752ddf6c80dec

Colin Stolley 2022-02-07T09:29:40

push: Prepare pack before sending pack header. For large pushes, preparing the pack can take a while. Currently we send the pack header first, followed by preparing the pack and then finally sending the pack. Unfortunately github.com will terminate a git-receive-pack command over http if it is idle for more than 10 seconds. This is easily exceeded for a large push, and so the push is rejected with a Broken Pipe error. This patch moves the pack preparation ahead of sending the pack header, so that the timeout is avoided. prepare_pack() can be called multiple times but will only do the work once, so the original PREPARE_PACK call inside git_packbuilder_foreach() remains.

diff --git a/src/pack-objects.c b/src/pack-objects.c
index e5fc625..abe978b 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -1308,7 +1308,7 @@ static int ll_find_deltas(git_packbuilder *pb, git_pobject **list,
 #define ll_find_deltas(pb, l, ls, w, d) find_deltas(pb, l, &ls, w, d)
 #endif
 
-static int prepare_pack(git_packbuilder *pb)
+int prepare_pack(git_packbuilder *pb)
 {
 	git_pobject **delta_list;
 	size_t i, n = 0;
diff --git a/src/pack-objects.h b/src/pack-objects.h
index db2038b..35bae3a 100644
--- a/src/pack-objects.h
+++ b/src/pack-objects.h
@@ -97,5 +97,7 @@ struct git_packbuilder {
 };
 
 int git_packbuilder__write_buf(git_str *buf, git_packbuilder *pb);
+int prepare_pack(git_packbuilder *pb);
+
 
 #endif
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index a9a623c..625f850 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -1034,6 +1034,10 @@ int git_smart__push(git_transport *transport, git_push *push, const git_remote_c
 		}
 	}
 
+	/* prepare pack before sending pack header to avoid timeouts */
+	if (need_pack && ((error = prepare_pack(push->pb))) < 0)
+		goto done;
+
 	if ((error = git_smart__get_push_stream(t, &packbuilder_payload.stream)) < 0 ||
 		(error = gen_pktline(&pktline, push)) < 0 ||
 		(error = packbuilder_payload.stream->write(packbuilder_payload.stream, git_str_cstr(&pktline), git_str_len(&pktline))) < 0)