Commit 8cec2b8ae9b1fdf4638cb405cbd8c17454c1c076

Carlos Martín Nieto 2015-05-06T12:28:01

local: send the packbuilder progress via the sideband Set a callback for the packbuilder so we can send the sideband messages to the caller, formatting them as git would.

diff --git a/src/transports/local.c b/src/transports/local.c
index 305c71b..9dd4486 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -502,6 +502,33 @@ static int foreach_cb(void *buf, size_t len, void *payload)
 }
 
 static const char *counting_objects_fmt = "Counting objects %d\r";
+static const char *compressing_objects_fmt = "Compressing objects: %.0f%% (%d/%d)";
+
+static int local_counting(int stage, unsigned int current, unsigned int total, void *payload)
+{
+	git_buf progress_info = GIT_BUF_INIT;
+	transport_local *t = payload;
+
+	if (!t->progress_cb)
+		return 0;
+
+	if (stage == GIT_PACKBUILDER_ADDING_OBJECTS) {
+		git_buf_printf(&progress_info, counting_objects_fmt, current);
+	} else if (stage == GIT_PACKBUILDER_DELTAFICATION) {
+		float perc = (((float) current) / total) * 100;
+		git_buf_printf(&progress_info, compressing_objects_fmt, perc, current, total);
+		if (current == total)
+			git_buf_printf(&progress_info, ", done\n");
+		else
+			git_buf_putc(&progress_info, '\r');
+
+	}
+
+	if (git_buf_oom(&progress_info))
+		return -1;
+
+	return t->progress_cb(git_buf_cstr(&progress_info), git_buf_len(&progress_info), t->message_cb_payload);
+}
 
 static int local_download_pack(
 		git_transport *transport,
@@ -527,6 +554,8 @@ static int local_download_pack(
 	if ((error = git_packbuilder_new(&pack, t->repo)) < 0)
 		goto cleanup;
 
+	git_packbuilder_set_callbacks(pack, local_counting, t);
+
 	stats->total_objects = 0;
 	stats->indexed_objects = 0;
 	stats->received_objects = 0;