smart_protocol.c 25.2 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8 9

#include "common.h"

10
#include "git2.h"
11
#include "git2/odb_backend.h"
12

13 14
#include "smart.h"
#include "refs.h"
15
#include "repository.h"
16 17 18
#include "push.h"
#include "pack-objects.h"
#include "remote.h"
19
#include "util.h"
20 21

#define NETWORK_XFER_THRESHOLD (100*1024)
22 23
/* The minimal interval between progress updates (in seconds). */
#define MIN_PROGRESS_UPDATE_INTERVAL 0.5
24

25 26
bool git_smart__ofs_delta_enabled = true;

27 28 29 30 31
int git_smart__store_refs(transport_smart *t, int flushes)
{
	gitno_buffer *buf = &t->buffer;
	git_vector *refs = &t->refs;
	int error, flush = 0, recvd;
32 33
	const char *line_end = NULL;
	git_pkt *pkt = NULL;
Edward Thomson committed
34
	size_t i;
35

36 37 38
	/* Clear existing refs in case git_remote_connect() is called again
	 * after git_remote_disconnect().
	 */
39 40
	git_vector_foreach(refs, i, pkt) {
		git_pkt_free(pkt);
Edward Thomson committed
41
	}
42
	git_vector_clear(refs);
43
	pkt = NULL;
44

45 46 47 48 49 50 51
	do {
		if (buf->offset > 0)
			error = git_pkt_parse_line(&pkt, buf->data, &line_end, buf->offset);
		else
			error = GIT_EBUFS;

		if (error < 0 && error != GIT_EBUFS)
52
			return error;
53 54 55

		if (error == GIT_EBUFS) {
			if ((recvd = gitno_recv(buf)) < 0)
56
				return recvd;
57

58
			if (recvd == 0) {
59
				giterr_set(GITERR_NET, "early EOF");
60
				return GIT_EEOF;
61 62 63 64 65 66 67
			}

			continue;
		}

		gitno_consume(buf, line_end);
		if (pkt->type == GIT_PKT_ERR) {
68
			giterr_set(GITERR_NET, "remote error: %s", ((git_pkt_err *)pkt)->error);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
			git__free(pkt);
			return -1;
		}

		if (pkt->type != GIT_PKT_FLUSH && git_vector_insert(refs, pkt) < 0)
			return -1;

		if (pkt->type == GIT_PKT_FLUSH) {
			flush++;
			git_pkt_free(pkt);
		}
	} while (flush < flushes);

	return flush;
}

85 86 87 88 89
static int append_symref(const char **out, git_vector *symrefs, const char *ptr)
{
	int error;
	const char *end;
	git_buf buf = GIT_BUF_INIT;
Jacques Germishuys committed
90
	git_refspec *mapping = NULL;
91 92 93 94 95 96 97 98 99 100 101 102 103 104

	ptr += strlen(GIT_CAP_SYMREF);
	if (*ptr != '=')
		goto on_invalid;

	ptr++;
	if (!(end = strchr(ptr, ' ')) &&
	    !(end = strchr(ptr, '\0')))
		goto on_invalid;

	if ((error = git_buf_put(&buf, ptr, end - ptr)) < 0)
		return error;

	/* symref mapping has refspec format */
Jacques Germishuys committed
105
	mapping = git__calloc(1, sizeof(git_refspec));
106 107 108
	GITERR_CHECK_ALLOC(mapping);

	error = git_refspec__parse(mapping, git_buf_cstr(&buf), true);
109
	git_buf_dispose(&buf);
110 111 112 113 114 115

	/* if the error isn't OOM, then it's a parse error; let's use a nicer message */
	if (error < 0) {
		if (giterr_last()->klass != GITERR_NOMEMORY)
			goto on_invalid;

116
		git__free(mapping);
117 118 119 120 121 122 123 124 125 126 127
		return error;
	}

	if ((error = git_vector_insert(symrefs, mapping)) < 0)
		return error;

	*out = end;
	return 0;

on_invalid:
	giterr_set(GITERR_NET, "remote sent invalid symref");
Jacques Germishuys committed
128
	git_refspec__free(mapping);
129
	git__free(mapping);
130 131 132 133
	return -1;
}

int git_smart__detect_caps(git_pkt_ref *pkt, transport_smart_caps *caps, git_vector *symrefs)
134 135 136 137 138 139 140 141 142 143 144 145
{
	const char *ptr;

	/* No refs or capabilites, odd but not a problem */
	if (pkt == NULL || pkt->capabilities == NULL)
		return 0;

	ptr = pkt->capabilities;
	while (ptr != NULL && *ptr != '\0') {
		if (*ptr == ' ')
			ptr++;

146
		if (git_smart__ofs_delta_enabled && !git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) {
147 148 149 150 151
			caps->common = caps->ofs_delta = 1;
			ptr += strlen(GIT_CAP_OFS_DELTA);
			continue;
		}

152 153 154 155 156 157 158
		/* Keep multi_ack_detailed before multi_ack */
		if (!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK_DETAILED)) {
			caps->common = caps->multi_ack_detailed = 1;
			ptr += strlen(GIT_CAP_MULTI_ACK_DETAILED);
			continue;
		}

159
		if (!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK)) {
160 161 162 163 164
			caps->common = caps->multi_ack = 1;
			ptr += strlen(GIT_CAP_MULTI_ACK);
			continue;
		}

165
		if (!git__prefixcmp(ptr, GIT_CAP_INCLUDE_TAG)) {
166 167 168 169 170 171
			caps->common = caps->include_tag = 1;
			ptr += strlen(GIT_CAP_INCLUDE_TAG);
			continue;
		}

		/* Keep side-band check after side-band-64k */
172
		if (!git__prefixcmp(ptr, GIT_CAP_SIDE_BAND_64K)) {
173 174 175 176 177
			caps->common = caps->side_band_64k = 1;
			ptr += strlen(GIT_CAP_SIDE_BAND_64K);
			continue;
		}

178
		if (!git__prefixcmp(ptr, GIT_CAP_SIDE_BAND)) {
179 180 181 182 183
			caps->common = caps->side_band = 1;
			ptr += strlen(GIT_CAP_SIDE_BAND);
			continue;
		}

184 185 186 187 188 189
		if (!git__prefixcmp(ptr, GIT_CAP_DELETE_REFS)) {
			caps->common = caps->delete_refs = 1;
			ptr += strlen(GIT_CAP_DELETE_REFS);
			continue;
		}

190 191 192 193 194 195
		if (!git__prefixcmp(ptr, GIT_CAP_THIN_PACK)) {
			caps->common = caps->thin_pack = 1;
			ptr += strlen(GIT_CAP_THIN_PACK);
			continue;
		}

196 197 198 199 200 201 202 203 204
		if (!git__prefixcmp(ptr, GIT_CAP_SYMREF)) {
			int error;

			if ((error = append_symref(&ptr, symrefs, ptr)) < 0)
				return error;

			continue;
		}

205 206 207 208 209 210 211
		/* We don't know this capability, so skip it */
		ptr = strchr(ptr, ' ');
	}

	return 0;
}

212
static int recv_pkt(git_pkt **out_pkt, git_pkt_type *out_type, gitno_buffer *buf)
213 214
{
	const char *ptr = buf->data, *line_end = ptr;
215
	git_pkt *pkt = NULL;
216
	int error = 0, ret;
217 218 219 220 221 222 223 224 225 226 227

	do {
		if (buf->offset > 0)
			error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset);
		else
			error = GIT_EBUFS;

		if (error == 0)
			break; /* return the pkt */

		if (error < 0 && error != GIT_EBUFS)
228
			return error;
229

230
		if ((ret = gitno_recv(buf)) < 0) {
231
			return ret;
232 233 234 235
		} else if (ret == 0) {
			giterr_set(GITERR_NET, "early EOF");
			return GIT_EEOF;
		}
236 237 238
	} while (error);

	gitno_consume(buf, line_end);
239 240 241 242
	if (out_type != NULL)
		*out_type = pkt->type;
	if (out_pkt != NULL)
		*out_pkt = pkt;
243 244 245
	else
		git__free(pkt);

246
	return error;
247 248 249 250 251 252
}

static int store_common(transport_smart *t)
{
	git_pkt *pkt = NULL;
	gitno_buffer *buf = &t->buffer;
253
	int error;
254 255

	do {
256
		if ((error = recv_pkt(&pkt, NULL, buf)) < 0)
257
			return error;
258

259
		if (pkt->type != GIT_PKT_ACK) {
260 261 262 263
			git__free(pkt);
			return 0;
		}

264 265 266 267
		if (git_vector_insert(&t->common, pkt) < 0) {
			git__free(pkt);
			return -1;
		}
268 269 270 271 272 273 274
	} while (1);

	return 0;
}

static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
{
Jacques Germishuys committed
275
	git_revwalk *walk = NULL;
276 277
	git_strarray refs;
	unsigned int i;
278
	git_reference *ref = NULL;
279
	int error;
280

281 282
	if ((error = git_reference_list(&refs, repo)) < 0)
		return error;
283

284 285
	if ((error = git_revwalk_new(&walk, repo)) < 0)
		return error;
286 287 288 289

	git_revwalk_sorting(walk, GIT_SORT_TIME);

	for (i = 0; i < refs.count; ++i) {
290 291 292
		git_reference_free(ref);
		ref = NULL;

293 294 295 296
		/* No tags */
		if (!git__prefixcmp(refs.strings[i], GIT_REFS_TAGS_DIR))
			continue;

297
		if ((error = git_reference_lookup(&ref, repo, refs.strings[i])) < 0)
298 299 300 301
			goto on_error;

		if (git_reference_type(ref) == GIT_REF_SYMBOLIC)
			continue;
302

303
		if ((error = git_revwalk_push(walk, git_reference_target(ref))) < 0)
304 305 306 307 308 309
			goto on_error;
	}

	*out = walk;

on_error:
310 311
	if (error)
		git_revwalk_free(walk);
312 313
	git_reference_free(ref);
	git_strarray_free(&refs);
314
	return error;
315 316
}

317 318 319 320 321 322 323 324
static int wait_while_ack(gitno_buffer *buf)
{
	int error;
	git_pkt_ack *pkt = NULL;

	while (1) {
		git__free(pkt);

325
		if ((error = recv_pkt((git_pkt **)&pkt, NULL, buf)) < 0)
326 327 328 329 330 331
			return error;

		if (pkt->type == GIT_PKT_NAK)
			break;

		if (pkt->type == GIT_PKT_ACK &&
332
		    (pkt->status != GIT_ACK_CONTINUE &&
333 334
		     pkt->status != GIT_ACK_COMMON &&
		     pkt->status != GIT_ACK_READY)) {
335
			git__free(pkt);
Edward Thomson committed
336
			return 0;
337 338 339 340 341 342 343
		}
	}

	git__free(pkt);
	return 0;
}

344
int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, const git_remote_head * const *wants, size_t count)
345 346 347 348 349
{
	transport_smart *t = (transport_smart *)transport;
	gitno_buffer *buf = &t->buffer;
	git_buf data = GIT_BUF_INIT;
	git_revwalk *walk = NULL;
350 351
	int error = -1;
	git_pkt_type pkt_type;
352 353 354
	unsigned int i;
	git_oid oid;

355
	if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0)
356
		return error;
357

358
	if ((error = fetch_setup_walk(&walk, repo)) < 0)
359
		goto on_error;
360

361
	/*
362 363 364 365
	 * Our support for ACK extensions is simply to parse them. On
	 * the first ACK we will accept that as enough common
	 * objects. We give up if we haven't found an answer in the
	 * first 256 we send.
366 367
	 */
	i = 0;
368
	while (i < 256) {
369 370 371 372 373 374 375 376 377
		error = git_revwalk_next(&oid, walk);

		if (error < 0) {
			if (GIT_ITEROVER == error)
				break;

			goto on_error;
		}

378 379 380 381 382 383 384 385 386 387
		git_pkt_buffer_have(&oid, &data);
		i++;
		if (i % 20 == 0) {
			if (t->cancelled.val) {
				giterr_set(GITERR_NET, "The fetch was cancelled by the user");
				error = GIT_EUSER;
				goto on_error;
			}

			git_pkt_buffer_flush(&data);
388 389
			if (git_buf_oom(&data)) {
				error = -1;
390
				goto on_error;
391
			}
392

393
			if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0)
394 395 396
				goto on_error;

			git_buf_clear(&data);
397
			if (t->caps.multi_ack || t->caps.multi_ack_detailed) {
398
				if ((error = store_common(t)) < 0)
399 400
					goto on_error;
			} else {
401
				if ((error = recv_pkt(NULL, &pkt_type, buf)) < 0)
402
					goto on_error;
403 404

				if (pkt_type == GIT_PKT_ACK) {
405 406 407 408 409
					break;
				} else if (pkt_type == GIT_PKT_NAK) {
					continue;
				} else {
					giterr_set(GITERR_NET, "Unexpected pkt type");
410
					error = -1;
411 412 413 414 415 416 417 418 419 420
					goto on_error;
				}
			}
		}

		if (t->common.length > 0)
			break;

		if (i % 20 == 0 && t->rpc) {
			git_pkt_ack *pkt;
421
			unsigned int j;
422

423
			if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0)
424 425
				goto on_error;

426
			git_vector_foreach(&t->common, j, pkt) {
427 428
				if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0)
					goto on_error;
429 430
			}

431 432
			if (git_buf_oom(&data)) {
				error = -1;
433
				goto on_error;
434
			}
435 436 437 438 439 440
		}
	}

	/* Tell the other end that we're done negotiating */
	if (t->rpc && t->common.length > 0) {
		git_pkt_ack *pkt;
441
		unsigned int j;
442

443
		if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0)
444 445
			goto on_error;

446
		git_vector_foreach(&t->common, j, pkt) {
447 448
			if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0)
				goto on_error;
449 450
		}

451 452
		if (git_buf_oom(&data)) {
			error = -1;
453
			goto on_error;
454
		}
455 456
	}

457 458 459
	if ((error = git_pkt_buffer_done(&data)) < 0)
		goto on_error;

460 461 462 463 464
	if (t->cancelled.val) {
		giterr_set(GITERR_NET, "The fetch was cancelled by the user");
		error = GIT_EUSER;
		goto on_error;
	}
465
	if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0)
466 467
		goto on_error;

468
	git_buf_dispose(&data);
469 470 471
	git_revwalk_free(walk);

	/* Now let's eat up whatever the server gives us */
472
	if (!t->caps.multi_ack && !t->caps.multi_ack_detailed) {
473
		if ((error = recv_pkt(NULL, &pkt_type, buf)) < 0)
474
			return error;
475 476

		if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) {
477 478 479 480
			giterr_set(GITERR_NET, "Unexpected pkt type");
			return -1;
		}
	} else {
481
		error = wait_while_ack(buf);
482 483
	}

484
	return error;
485 486 487

on_error:
	git_revwalk_free(walk);
488
	git_buf_dispose(&data);
489 490 491
	return error;
}

492
static int no_sideband(transport_smart *t, struct git_odb_writepack *writepack, gitno_buffer *buf, git_transfer_progress *stats)
493 494 495 496 497 498 499 500 501
{
	int recvd;

	do {
		if (t->cancelled.val) {
			giterr_set(GITERR_NET, "The fetch was cancelled by the user");
			return GIT_EUSER;
		}

502
		if (writepack->append(writepack, buf->data, buf->offset, stats) < 0)
503 504 505 506 507
			return -1;

		gitno_consume_n(buf, buf->offset);

		if ((recvd = gitno_recv(buf)) < 0)
508
			return recvd;
509 510
	} while(recvd > 0);

511
	if (writepack->commit(writepack, stats) < 0)
512 513 514 515 516
		return -1;

	return 0;
}

517
struct network_packetsize_payload
518
{
519
	git_transfer_progress_cb callback;
520 521
	void *payload;
	git_transfer_progress *stats;
522
	size_t last_fired_bytes;
523 524
};

525
static int network_packetsize(size_t received, void *payload)
526 527 528 529 530 531 532 533
{
	struct network_packetsize_payload *npp = (struct network_packetsize_payload*)payload;

	/* Accumulate bytes */
	npp->stats->received_bytes += received;

	/* Fire notification if the threshold is reached */
	if ((npp->stats->received_bytes - npp->last_fired_bytes) > NETWORK_XFER_THRESHOLD) {
534
		npp->last_fired_bytes = npp->stats->received_bytes;
535

536
		if (npp->callback(npp->stats, npp->payload))
537
			return GIT_EUSER;
538
	}
539 540

	return 0;
541 542 543 544 545 546
}

int git_smart__download_pack(
	git_transport *transport,
	git_repository *repo,
	git_transfer_progress *stats,
547
	git_transfer_progress_cb transfer_progress_cb,
548 549 550 551
	void *progress_payload)
{
	transport_smart *t = (transport_smart *)transport;
	gitno_buffer *buf = &t->buffer;
552 553
	git_odb *odb;
	struct git_odb_writepack *writepack = NULL;
554
	int error = 0;
555 556
	struct network_packetsize_payload npp = {0};

557 558
	memset(stats, 0, sizeof(git_transfer_progress));

559 560
	if (transfer_progress_cb) {
		npp.callback = transfer_progress_cb;
561 562 563 564
		npp.payload = progress_payload;
		npp.stats = stats;
		t->packetsize_cb = &network_packetsize;
		t->packetsize_payload = &npp;
565 566

		/* We might have something in the buffer already from negotiate_fetch */
567
		if (t->buffer.offset > 0 && !t->cancelled.val)
568
			if (t->packetsize_cb(t->buffer.offset, t->packetsize_payload))
569
				git_atomic_set(&t->cancelled, 1);
570 571
	}

572
	if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
573
		((error = git_odb_write_pack(&writepack, odb, transfer_progress_cb, progress_payload)) != 0))
574
		goto done;
575 576 577

	/*
	 * If the remote doesn't support the side-band, we can feed
578
	 * the data directly to the pack writer. Otherwise, we need to
579 580 581
	 * check which one belongs there.
	 */
	if (!t->caps.side_band && !t->caps.side_band_64k) {
582 583
		error = no_sideband(t, writepack, buf, stats);
		goto done;
584 585 586
	}

	do {
587
		git_pkt *pkt = NULL;
588

589
		/* Check cancellation before network call */
590
		if (t->cancelled.val) {
591
			giterr_clear();
592
			error = GIT_EUSER;
593
			goto done;
594 595
		}

596
		if ((error = recv_pkt(&pkt, NULL, buf)) >= 0) {
597 598 599 600 601 602 603 604 605 606 607
			/* Check cancellation after network call */
			if (t->cancelled.val) {
				giterr_clear();
				error = GIT_EUSER;
			} else if (pkt->type == GIT_PKT_PROGRESS) {
				if (t->progress_cb) {
					git_pkt_progress *p = (git_pkt_progress *) pkt;
					error = t->progress_cb(p->data, p->len, t->message_cb_payload);
				}
			} else if (pkt->type == GIT_PKT_DATA) {
				git_pkt_data *p = (git_pkt_data *) pkt;
608 609 610

				if (p->len)
					error = writepack->append(writepack, p->data, p->len, stats);
611 612 613 614 615
			} else if (pkt->type == GIT_PKT_FLUSH) {
				/* A flush indicates the end of the packfile */
				git__free(pkt);
				break;
			}
616
		}
617

618 619 620
		git__free(pkt);
		if (error < 0)
			goto done;
621 622 623

	} while (1);

624
	/*
625
	 * Trailing execution of transfer_progress_cb, if necessary...
626 627
	 * Only the callback through the npp datastructure currently
	 * updates the last_fired_bytes value. It is possible that
628
	 * progress has already been reported with the correct
629 630 631 632 633
	 * "received_bytes" value, but until (if?) this is unified
	 * then we will report progress again to be sure that the
	 * correct last received_bytes value is reported.
	 */
	if (npp.callback && npp.stats->received_bytes > npp.last_fired_bytes) {
634 635
		error = npp.callback(npp.stats, npp.payload);
		if (error != 0)
636 637 638
			goto done;
	}

639
	error = writepack->commit(writepack, stats);
640

641
done:
642 643
	if (writepack)
		writepack->free(writepack);
644
	if (transfer_progress_cb) {
645 646 647
		t->packetsize_cb = NULL;
		t->packetsize_payload = NULL;
	}
648

649 650
	return error;
}
651 652 653 654

static int gen_pktline(git_buf *buf, git_push *push)
{
	push_spec *spec;
655
	size_t i, len;
656
	char old_id[GIT_OID_HEXSZ+1], new_id[GIT_OID_HEXSZ+1];
657

658
	old_id[GIT_OID_HEXSZ] = '\0'; new_id[GIT_OID_HEXSZ] = '\0';
659 660

	git_vector_foreach(&push->specs, i, spec) {
661
		len = 2*GIT_OID_HEXSZ + 7 + strlen(spec->refspec.dst);
662 663

		if (i == 0) {
664
			++len; /* '\0' */
665
			if (push->report_status)
666 667
				len += strlen(GIT_CAP_REPORT_STATUS) + 1;
			len += strlen(GIT_CAP_SIDE_BAND_64K) + 1;
668 669
		}

670 671
		git_oid_fmt(old_id, &spec->roid);
		git_oid_fmt(new_id, &spec->loid);
672

673
		git_buf_printf(buf, "%04"PRIxZ"%s %s %s", len, old_id, new_id, spec->refspec.dst);
674 675 676

		if (i == 0) {
			git_buf_putc(buf, '\0');
677 678 679
			/* Core git always starts their capabilities string with a space */
			if (push->report_status) {
				git_buf_putc(buf, ' ');
680
				git_buf_printf(buf, GIT_CAP_REPORT_STATUS);
681 682 683
			}
			git_buf_putc(buf, ' ');
			git_buf_printf(buf, GIT_CAP_SIDE_BAND_64K);
684 685 686 687
		}

		git_buf_putc(buf, '\n');
	}
688

689 690 691 692
	git_buf_puts(buf, "0000");
	return git_buf_oom(buf) ? -1 : 0;
}

693 694 695 696 697 698
static int add_push_report_pkt(git_push *push, git_pkt *pkt)
{
	push_status *status;

	switch (pkt->type) {
		case GIT_PKT_OK:
699
			status = git__calloc(1, sizeof(push_status));
700 701 702 703 704 705 706 707 708 709
			GITERR_CHECK_ALLOC(status);
			status->msg = NULL;
			status->ref = git__strdup(((git_pkt_ok *)pkt)->ref);
			if (!status->ref ||
				git_vector_insert(&push->status, status) < 0) {
				git_push_status_free(status);
				return -1;
			}
			break;
		case GIT_PKT_NG:
710
			status = git__calloc(1, sizeof(push_status));
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
			GITERR_CHECK_ALLOC(status);
			status->ref = git__strdup(((git_pkt_ng *)pkt)->ref);
			status->msg = git__strdup(((git_pkt_ng *)pkt)->msg);
			if (!status->ref || !status->msg ||
				git_vector_insert(&push->status, status) < 0) {
				git_push_status_free(status);
				return -1;
			}
			break;
		case GIT_PKT_UNPACK:
			push->unpack_ok = ((git_pkt_unpack *)pkt)->unpack_ok;
			break;
		case GIT_PKT_FLUSH:
			return GIT_ITEROVER;
		default:
			giterr_set(GITERR_NET, "report-status: protocol error");
			return -1;
	}

	return 0;
}

733
static int add_push_report_sideband_pkt(git_push *push, git_pkt_data *data_pkt, git_buf *data_pkt_buf)
734 735
{
	git_pkt *pkt;
736
	const char *line, *line_end = NULL;
737
	size_t line_len;
738
	int error;
739 740 741 742 743 744 745 746 747 748 749 750 751
	int reading_from_buf = data_pkt_buf->size > 0;

	if (reading_from_buf) {
		/* We had an existing partial packet, so add the new
		 * packet to the buffer and parse the whole thing */
		git_buf_put(data_pkt_buf, data_pkt->data, data_pkt->len);
		line = data_pkt_buf->ptr;
		line_len = data_pkt_buf->size;
	}
	else {
		line = data_pkt->data;
		line_len = data_pkt->len;
	}
752 753 754 755

	while (line_len > 0) {
		error = git_pkt_parse_line(&pkt, line, &line_end, line_len);

756 757 758 759 760 761 762 763 764 765
		if (error == GIT_EBUFS) {
			/* Buffer the data when the inner packet is split
			 * across multiple sideband packets */
			if (!reading_from_buf)
				git_buf_put(data_pkt_buf, line, line_len);
			error = 0;
			goto done;
		}
		else if (error < 0)
			goto done;
766 767 768 769 770 771 772 773 774

		/* Advance in the buffer */
		line_len -= (line_end - line);
		line = line_end;

		error = add_push_report_pkt(push, pkt);

		git_pkt_free(pkt);

775
		if (error < 0 && error != GIT_ITEROVER)
776
			goto done;
777 778
	}

779 780 781 782 783 784
	error = 0;

done:
	if (reading_from_buf)
		git_buf_consume(data_pkt_buf, line_end);
	return error;
785 786
}

787
static int parse_report(transport_smart *transport, git_push *push)
788
{
789 790
	git_pkt *pkt = NULL;
	const char *line_end = NULL;
791
	gitno_buffer *buf = &transport->buffer;
792
	int error, recvd;
793
	git_buf data_pkt_buf = GIT_BUF_INIT;
794 795 796 797 798 799 800 801

	for (;;) {
		if (buf->offset > 0)
			error = git_pkt_parse_line(&pkt, buf->data,
						   &line_end, buf->offset);
		else
			error = GIT_EBUFS;

802 803 804 805
		if (error < 0 && error != GIT_EBUFS) {
			error = -1;
			goto done;
		}
806 807

		if (error == GIT_EBUFS) {
808 809 810 811
			if ((recvd = gitno_recv(buf)) < 0) {
				error = recvd;
				goto done;
			}
812 813

			if (recvd == 0) {
814
				giterr_set(GITERR_NET, "early EOF");
815 816
				error = GIT_EEOF;
				goto done;
817 818 819 820 821 822
			}
			continue;
		}

		gitno_consume(buf, line_end);

823
		error = 0;
824

825 826 827
		switch (pkt->type) {
			case GIT_PKT_DATA:
				/* This is a sideband packet which contains other packets */
828
				error = add_push_report_sideband_pkt(push, (git_pkt_data *)pkt, &data_pkt_buf);
829 830 831 832 833 834 835
				break;
			case GIT_PKT_ERR:
				giterr_set(GITERR_NET, "report-status: Error reported: %s",
					((git_pkt_err *)pkt)->error);
				error = -1;
				break;
			case GIT_PKT_PROGRESS:
836 837 838 839
				if (transport->progress_cb) {
					git_pkt_progress *p = (git_pkt_progress *) pkt;
					error = transport->progress_cb(p->data, p->len, transport->message_cb_payload);
				}
840 841 842 843
				break;
			default:
				error = add_push_report_pkt(push, pkt);
				break;
844 845
		}

846
		git_pkt_free(pkt);
847

848
		/* add_push_report_pkt returns GIT_ITEROVER when it receives a flush */
849 850 851 852 853 854 855 856 857 858
		if (error == GIT_ITEROVER) {
			error = 0;
			if (data_pkt_buf.size > 0) {
				/* If there was data remaining in the pack data buffer,
				 * then the server sent a partial pkt-line */
				giterr_set(GITERR_NET, "Incomplete pack data pkt-line");
				error = GIT_ERROR;
			}
			goto done;
		}
859

860 861 862
		if (error < 0) {
			goto done;
		}
863
	}
864
done:
865
	git_buf_dispose(&data_pkt_buf);
866
	return error;
867 868
}

869 870 871 872 873 874 875
static int add_ref_from_push_spec(git_vector *refs, push_spec *push_spec)
{
	git_pkt_ref *added = git__calloc(1, sizeof(git_pkt_ref));
	GITERR_CHECK_ALLOC(added);

	added->type = GIT_PKT_REF;
	git_oid_cpy(&added->head.oid, &push_spec->loid);
876
	added->head.name = git__strdup(push_spec->refspec.dst);
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893

	if (!added->head.name ||
		git_vector_insert(refs, added) < 0) {
		git_pkt_free((git_pkt *)added);
		return -1;
	}

	return 0;
}

static int update_refs_from_report(
	git_vector *refs,
	git_vector *push_specs,
	git_vector *push_report)
{
	git_pkt_ref *ref;
	push_spec *push_spec;
894
	push_status *push_status;
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
	size_t i, j, refs_len;
	int cmp;

	/* For each push spec we sent to the server, we should have
	 * gotten back a status packet in the push report */
	if (push_specs->length != push_report->length) {
		giterr_set(GITERR_NET, "report-status: protocol error");
		return -1;
	}

	/* We require that push_specs be sorted with push_spec_rref_cmp,
	 * and that push_report be sorted with push_status_ref_cmp */
	git_vector_sort(push_specs);
	git_vector_sort(push_report);

	git_vector_foreach(push_specs, i, push_spec) {
		push_status = git_vector_get(push_report, i);

		/* For each push spec we sent to the server, we should have
		 * gotten back a status packet in the push report which matches */
915
		if (strcmp(push_spec->refspec.dst, push_status->ref)) {
916 917 918 919 920 921 922 923 924 925 926 927 928
			giterr_set(GITERR_NET, "report-status: protocol error");
			return -1;
		}
	}

	/* We require that refs be sorted with ref_name_cmp */
	git_vector_sort(refs);
	i = j = 0;
	refs_len = refs->length;

	/* Merge join push_specs with refs */
	while (i < push_specs->length && j < refs_len) {
		push_spec = git_vector_get(push_specs, i);
929
		push_status = git_vector_get(push_report, i);
930 931
		ref = git_vector_get(refs, j);

932
		cmp = strcmp(push_spec->refspec.dst, ref->head.name);
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951

		/* Iterate appropriately */
		if (cmp <= 0) i++;
		if (cmp >= 0) j++;

		/* Add case */
		if (cmp < 0 &&
			!push_status->msg &&
			add_ref_from_push_spec(refs, push_spec) < 0)
			return -1;

		/* Update case, delete case */
		if (cmp == 0 &&
			!push_status->msg)
			git_oid_cpy(&ref->head.oid, &push_spec->loid);
	}

	for (; i < push_specs->length; i++) {
		push_spec = git_vector_get(push_specs, i);
952
		push_status = git_vector_get(push_report, i);
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972

		/* Add case */
		if (!push_status->msg &&
			add_ref_from_push_spec(refs, push_spec) < 0)
			return -1;
	}

	/* Remove any refs which we updated to have a zero OID. */
	git_vector_rforeach(refs, i, ref) {
		if (git_oid_iszero(&ref->head.oid)) {
			git_vector_remove(refs, i);
			git_pkt_free((git_pkt *)ref);
		}
	}

	git_vector_sort(refs);

	return 0;
}

973 974 975 976 977 978 979 980 981 982
struct push_packbuilder_payload
{
	git_smart_subtransport_stream *stream;
	git_packbuilder *pb;
	git_push_transfer_progress cb;
	void *cb_payload;
	size_t last_bytes;
	double last_progress_report_time;
};

983 984
static int stream_thunk(void *buf, size_t size, void *data)
{
985 986 987 988 989 990 991 992 993
	int error = 0;
	struct push_packbuilder_payload *payload = data;

	if ((error = payload->stream->write(payload->stream, (const char *)buf, size)) < 0)
		return error;

	if (payload->cb) {
		double current_time = git__timer();
		payload->last_bytes += size;
994

995 996
		if ((current_time - payload->last_progress_report_time) >= MIN_PROGRESS_UPDATE_INTERVAL) {
			payload->last_progress_report_time = current_time;
997
			error = payload->cb(payload->pb->nr_written, payload->pb->nr_objects, payload->last_bytes, payload->cb_payload);
998 999 1000 1001
		}
	}

	return error;
1002 1003
}

1004
int git_smart__push(git_transport *transport, git_push *push, const git_remote_callbacks *cbs)
1005 1006
{
	transport_smart *t = (transport_smart *)transport;
1007
	struct push_packbuilder_payload packbuilder_payload = {0};
1008
	git_buf pktline = GIT_BUF_INIT;
1009
	int error = 0, need_pack = 0;
1010 1011
	push_spec *spec;
	unsigned int i;
1012

1013 1014
	packbuilder_payload.pb = push->pb;

1015
	if (cbs && cbs->push_transfer_progress) {
1016 1017
		packbuilder_payload.cb = cbs->push_transfer_progress;
		packbuilder_payload.cb_payload = cbs->payload;
1018 1019
	}

1020 1021 1022
#ifdef PUSH_DEBUG
{
	git_remote_head *head;
1023
	char hex[GIT_OID_HEXSZ+1]; hex[GIT_OID_HEXSZ] = '\0';
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

	git_vector_foreach(&push->remote->refs, i, head) {
		git_oid_fmt(hex, &head->oid);
		fprintf(stderr, "%s (%s)\n", hex, head->name);
	}

	git_vector_foreach(&push->specs, i, spec) {
		git_oid_fmt(hex, &spec->roid);
		fprintf(stderr, "%s (%s) -> ", hex, spec->lref);
		git_oid_fmt(hex, &spec->loid);
		fprintf(stderr, "%s (%s)\n", hex, spec->rref ?
			spec->rref : spec->lref);
	}
}
#endif

1040 1041 1042 1043 1044
	/*
	 * Figure out if we need to send a packfile; which is in all
	 * cases except when we only send delete commands
	 */
	git_vector_foreach(&push->specs, i, spec) {
1045
		if (spec->refspec.src && spec->refspec.src[0] != '\0') {
1046 1047 1048 1049 1050
			need_pack = 1;
			break;
		}
	}

1051 1052 1053 1054
	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_buf_cstr(&pktline), git_buf_len(&pktline))) < 0)
		goto done;
1055

1056 1057 1058
	if (need_pack &&
		(error = git_packbuilder_foreach(push->pb, &stream_thunk, &packbuilder_payload)) < 0)
		goto done;
1059 1060 1061 1062 1063

	/* If we sent nothing or the server doesn't support report-status, then
	 * we consider the pack to have been unpacked successfully */
	if (!push->specs.length || !push->report_status)
		push->unpack_ok = 1;
1064
	else if ((error = parse_report(t, push)) < 0)
1065
		goto done;
1066

1067
	/* If progress is being reported write the final report */
1068
	if (cbs && cbs->push_transfer_progress) {
1069
		error = cbs->push_transfer_progress(
1070 1071 1072
					push->pb->nr_written,
					push->pb->nr_objects,
					packbuilder_payload.last_bytes,
1073
					cbs->payload);
1074 1075 1076

		if (error < 0)
			goto done;
1077 1078
	}

1079
	if (push->status.length) {
1080
		error = update_refs_from_report(&t->refs, &push->specs, &push->status);
1081 1082 1083
		if (error < 0)
			goto done;

1084
		error = git_smart__update_heads(t, NULL);
1085
	}
1086

1087
done:
1088
	git_buf_dispose(&pktline);
1089 1090
	return error;
}