progress.c 8.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>

#include "progress.h"
#include "error.h"

15 16 17
/*
 * Show updates to the percentage and number of objects received
 * separately from the throughput to give an accurate progress while
18
 * avoiding too much noise on the screen. (In milliseconds.)
19
 */
20 21
#define PROGRESS_UPDATE_TIME    60
#define THROUGHPUT_UPDATE_TIME 500
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

#define is_nl(c) ((c) == '\r' || (c) == '\n')

#define return_os_error(msg) do { \
	git_error_set(GIT_ERROR_OS, "%s", msg); return -1; } while(0)

GIT_INLINE(size_t) no_nl_len(const char *str, size_t len)
{
	size_t i = 0;

	while (i < len && !is_nl(str[i]))
		i++;

	return i;
}

GIT_INLINE(size_t) nl_len(bool *has_nl, const char *str, size_t len)
{
	size_t i = no_nl_len(str, len);

	*has_nl = false;

	while (i < len && is_nl(str[i])) {
		*has_nl = true;
		i++;
	}

	return i;
}

52
static int progress_write(cli_progress *progress, bool force, git_str *line)
53 54 55 56
{
	bool has_nl;
	size_t no_nl = no_nl_len(line->ptr, line->size);
	size_t nl = nl_len(&has_nl, line->ptr + no_nl, line->size - no_nl);
57
	uint64_t now = git_time_monotonic();
58 59 60 61 62
	size_t i;

	/* Avoid spamming the console with progress updates */
	if (!force && line->ptr[line->size - 1] != '\n' && progress->last_update) {
		if (now - progress->last_update < PROGRESS_UPDATE_TIME) {
63 64 65
			git_str_clear(&progress->deferred);
			git_str_put(&progress->deferred, line->ptr, line->size);
			return git_str_oom(&progress->deferred) ? -1 : 0;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
		}
	}

	/*
	 * If there's something on this line already (eg, a progress line
	 * with only a trailing `\r` that we'll print over) then we need
	 * to really print over it in case we're writing a shorter line.
	 */
	if (printf("%.*s", (int)no_nl, line->ptr) < 0)
		return_os_error("could not print status");

	if (progress->onscreen.size) {
		for (i = no_nl; i < progress->onscreen.size; i++) {
			if (printf(" ") < 0)
				return_os_error("could not print status");
		}
	}

	if (printf("%.*s", (int)nl, line->ptr + no_nl) < 0 ||
	    fflush(stdout) != 0)
		return_os_error("could not print status");

88
	git_str_clear(&progress->onscreen);
89 90 91 92

	if (line->ptr[line->size - 1] == '\n') {
		progress->last_update = 0;
	} else {
93
		git_str_put(&progress->onscreen, line->ptr, line->size);
94 95 96
		progress->last_update = now;
	}

97 98
	git_str_clear(&progress->deferred);
	return git_str_oom(&progress->onscreen) ? -1 : 0;
99 100 101 102 103 104 105
}

static int progress_printf(cli_progress *progress, bool force, const char *fmt, ...)
	GIT_FORMAT_PRINTF(3, 4);

int progress_printf(cli_progress *progress, bool force, const char *fmt, ...)
{
106
	git_str buf = GIT_BUF_INIT;
107 108 109 110
	va_list ap;
	int error;

	va_start(ap, fmt);
111
	error = git_str_vprintf(&buf, fmt, ap);
112 113 114 115 116 117 118
	va_end(ap);

	if (error < 0)
		return error;

	error = progress_write(progress, force, &buf);

119
	git_str_dispose(&buf);
120 121 122 123 124 125 126 127 128 129 130 131
	return error;
}

static int progress_complete(cli_progress *progress)
{
	if (progress->deferred.size)
		progress_write(progress, true, &progress->deferred);

	if (progress->onscreen.size)
		if (printf("\n") < 0)
			return_os_error("could not print status");

132 133
	git_str_clear(&progress->deferred);
	git_str_clear(&progress->onscreen);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	progress->last_update = 0;
	progress->action_start = 0;
	progress->action_finish = 0;

	return 0;
}

GIT_INLINE(int) percent(size_t completed, size_t total)
{
	if (total == 0)
		return (completed == 0) ? 100 : 0;

	return (int)(((double)completed / (double)total) * 100);
}

int cli_progress_fetch_sideband(const char *str, int len, void *payload)
{
	cli_progress *progress = (cli_progress *)payload;
	size_t remain;

	if (len <= 0)
		return 0;

	/* Accumulate the sideband data, then print it line-at-a-time. */
158
	if (git_str_put(&progress->sideband, str, len) < 0)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
		return -1;

	str = progress->sideband.ptr;
	remain = progress->sideband.size;

	while (remain) {
		bool has_nl;
		size_t line_len = nl_len(&has_nl, str, remain);

		if (!has_nl)
			break;

		if (line_len < INT_MAX) {
			int error = progress_printf(progress, true,
				"remote: %.*s", (int)line_len, str);

			if (error < 0)
				return error;
		}

		str += line_len;
		remain -= line_len;
	}

183
	git_str_consume_bytes(&progress->sideband, (progress->sideband.size - remain));
184 185 186 187 188 189 190 191 192 193

	return 0;
}

static int fetch_receiving(
	cli_progress *progress,
	const git_indexer_progress *stats)
{
	char *recv_units[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL };
	char *rate_units[] = { "B/s", "KiB/s", "MiB/s", "GiB/s", "TiB/s", NULL };
194
	uint64_t now, elapsed;
195

196
	double recv_len, rate;
197 198 199 200
	size_t recv_unit_idx = 0, rate_unit_idx = 0;
	bool done = (stats->received_objects == stats->total_objects);

	if (!progress->action_start)
201
		progress->action_start = git_time_monotonic();
202 203 204 205

	if (done && progress->action_finish)
		now = progress->action_finish;
	else if (done)
206
		progress->action_finish = now = git_time_monotonic();
207
	else
208
		now = git_time_monotonic();
209

210 211 212 213 214 215 216 217 218 219 220 221
	if (progress->throughput_update &&
	    now - progress->throughput_update < THROUGHPUT_UPDATE_TIME) {
		elapsed = progress->throughput_update -
		          progress->action_start;
		recv_len = progress->throughput_bytes;
	} else {
		elapsed = now - progress->action_start;
		recv_len = (double)stats->received_bytes;

		progress->throughput_update = now;
		progress->throughput_bytes = recv_len;
	}
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

	rate = elapsed ? recv_len / elapsed : 0;

	while (recv_len > 1024 && recv_units[recv_unit_idx+1]) {
		recv_len /= 1024;
		recv_unit_idx++;
	}

	while (rate > 1024 && rate_units[rate_unit_idx+1]) {
		rate /= 1024;
		rate_unit_idx++;
	}

	return progress_printf(progress, false,
		"Receiving objects: %3d%% (%d/%d), %.2f %s | %.2f %s%s\r",
		percent(stats->received_objects, stats->total_objects),
		stats->received_objects,
		stats->total_objects,
		recv_len, recv_units[recv_unit_idx],
		rate, rate_units[rate_unit_idx],
		done ? ", done." : "");
}

static int fetch_resolving(
	cli_progress *progress,
	const git_indexer_progress *stats)
{
	bool done = (stats->indexed_deltas == stats->total_deltas);

	return progress_printf(progress, false,
		"Resolving deltas: %3d%% (%d/%d)%s\r",
		percent(stats->indexed_deltas, stats->total_deltas),
		stats->indexed_deltas, stats->total_deltas,
		done ? ", done." : "");
}

int cli_progress_fetch_transfer(const git_indexer_progress *stats, void *payload)
{
	cli_progress *progress = (cli_progress *)payload;
	int error = 0;

	switch (progress->action) {
	case CLI_PROGRESS_NONE:
		progress->action = CLI_PROGRESS_RECEIVING;
		/* fall through */

	case CLI_PROGRESS_RECEIVING:
		if ((error = fetch_receiving(progress, stats)) < 0)
			break;

		/*
		 * Upgrade from receiving to resolving; do this after the
		 * final call to cli_progress_fetch_receiving (above) to
		 * ensure that we've printed a final "done" string after
		 * any sideband data.
		 */
		if (!stats->indexed_deltas)
			break;

		progress_complete(progress);
		progress->action = CLI_PROGRESS_RESOLVING;
		/* fall through */

	case CLI_PROGRESS_RESOLVING:
		error = fetch_resolving(progress, stats);
		break;

	default:
		/* should not be reached */
291
		GIT_ASSERT(!"unexpected progress state");
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	}

	return error;
}

void cli_progress_checkout(
	const char *path,
	size_t completed_steps,
	size_t total_steps,
	void *payload)
{
	cli_progress *progress = (cli_progress *)payload;
	bool done = (completed_steps == total_steps);

	GIT_UNUSED(path);

	if (progress->action != CLI_PROGRESS_CHECKING_OUT) {
		progress_complete(progress);
		progress->action = CLI_PROGRESS_CHECKING_OUT;
	}

	progress_printf(progress, false,
314
		"Checking out files: %3d%% (%" PRIuZ "/%" PRIuZ ")%s\r",
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		percent(completed_steps, total_steps),
		completed_steps, total_steps,
		done ? ", done." : "");
}

int cli_progress_abort(cli_progress *progress)
{
	if (progress->onscreen.size > 0 && printf("\n") < 0)
	    return_os_error("could not print status");

	return 0;
}

int cli_progress_finish(cli_progress *progress)
{
	int error = progress->action ? progress_complete(progress) : 0;

	progress->action = 0;
	return error;
}

void cli_progress_dispose(cli_progress *progress)
{
	if (progress == NULL)
		return;

341 342 343
	git_str_dispose(&progress->sideband);
	git_str_dispose(&progress->onscreen);
	git_str_dispose(&progress->deferred);
344 345 346

	memset(progress, 0, sizeof(cli_progress));
}