smart_protocol.c 24.9 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
#include "revwalk.h"
21 22

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

26 27
bool git_smart__ofs_delta_enabled = true;

28 29 30 31 32
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;
33 34
	const char *line_end = NULL;
	git_pkt *pkt = NULL;
Edward Thomson committed
35
	size_t i;
36

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

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

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

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

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

			continue;
		}

		gitno_consume(buf, line_end);
		if (pkt->type == GIT_PKT_ERR) {
69
			git_error_set(GIT_ERROR_NET, "remote error: %s", ((git_pkt_err *)pkt)->error);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
			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;
}

86 87 88 89 90
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
91
	git_refspec *mapping = NULL;
92 93 94 95 96 97 98 99 100 101 102 103 104 105

	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
106
	mapping = git__calloc(1, sizeof(git_refspec));
107
	GIT_ERROR_CHECK_ALLOC(mapping);
108 109

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

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

117
		git__free(mapping);
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:
128
	git_error_set(GIT_ERROR_NET, "remote sent invalid symref");
129
	git_refspec__dispose(mapping);
130
	git__free(mapping);
131 132 133 134
	return -1;
}

int git_smart__detect_caps(git_pkt_ref *pkt, transport_smart_caps *caps, git_vector *symrefs)
135 136 137 138 139
{
	const char *ptr;

	/* No refs or capabilites, odd but not a problem */
	if (pkt == NULL || pkt->capabilities == NULL)
140
		return GIT_ENOTFOUND;
141 142 143 144 145 146

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

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

153 154 155 156 157 158 159
		/* 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;
		}

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

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

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

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

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

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

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

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

			continue;
		}

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

	return 0;
}

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

	do {
		if (buf->offset > 0)
221
			error = git_pkt_parse_line(&pkt, &line_end, ptr, buf->offset);
222 223 224 225 226 227 228
		else
			error = GIT_EBUFS;

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

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

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

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

247
	return error;
248 249 250 251 252 253
}

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

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

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

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

	return 0;
}

274 275 276
static int wait_while_ack(gitno_buffer *buf)
{
	int error;
277 278
	git_pkt *pkt = NULL;
	git_pkt_ack *ack = NULL;
279 280

	while (1) {
281
		git_pkt_free(pkt);
282

283
		if ((error = recv_pkt(&pkt, NULL, buf)) < 0)
284 285 286 287
			return error;

		if (pkt->type == GIT_PKT_NAK)
			break;
288 289
		if (pkt->type != GIT_PKT_ACK)
			continue;
290

291 292 293 294 295 296
		ack = (git_pkt_ack*)pkt;

		if (ack->status != GIT_ACK_CONTINUE &&
		    ack->status != GIT_ACK_COMMON &&
		    ack->status != GIT_ACK_READY) {
			break;
297 298 299
		}
	}

300
	git_pkt_free(pkt);
301 302 303
	return 0;
}

304
int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, const git_remote_head * const *wants, size_t count)
305 306
{
	transport_smart *t = (transport_smart *)transport;
307
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
308 309 310
	gitno_buffer *buf = &t->buffer;
	git_buf data = GIT_BUF_INIT;
	git_revwalk *walk = NULL;
311 312
	int error = -1;
	git_pkt_type pkt_type;
313 314 315
	unsigned int i;
	git_oid oid;

316
	if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0)
317
		return error;
318

319 320 321
	if ((error = git_revwalk_new(&walk, repo)) < 0)
		goto on_error;

322 323
	opts.insert_by_date = 1;
	if ((error = git_revwalk__push_glob(walk, "refs/*", &opts)) < 0)
324
		goto on_error;
325

326
	/*
327 328 329 330
	 * 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.
331 332
	 */
	i = 0;
333
	while (i < 256) {
334 335 336 337 338 339 340 341 342
		error = git_revwalk_next(&oid, walk);

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

			goto on_error;
		}

343 344 345 346
		git_pkt_buffer_have(&oid, &data);
		i++;
		if (i % 20 == 0) {
			if (t->cancelled.val) {
347
				git_error_set(GIT_ERROR_NET, "The fetch was cancelled by the user");
348 349 350 351 352
				error = GIT_EUSER;
				goto on_error;
			}

			git_pkt_buffer_flush(&data);
353 354
			if (git_buf_oom(&data)) {
				error = -1;
355
				goto on_error;
356
			}
357

358
			if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0)
359 360 361
				goto on_error;

			git_buf_clear(&data);
362
			if (t->caps.multi_ack || t->caps.multi_ack_detailed) {
363
				if ((error = store_common(t)) < 0)
364 365
					goto on_error;
			} else {
366
				if ((error = recv_pkt(NULL, &pkt_type, buf)) < 0)
367
					goto on_error;
368 369

				if (pkt_type == GIT_PKT_ACK) {
370 371 372 373
					break;
				} else if (pkt_type == GIT_PKT_NAK) {
					continue;
				} else {
374
					git_error_set(GIT_ERROR_NET, "unexpected pkt type");
375
					error = -1;
376 377 378 379 380 381 382 383 384 385
					goto on_error;
				}
			}
		}

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

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

388
			if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0)
389 390
				goto on_error;

391
			git_vector_foreach(&t->common, j, pkt) {
392 393
				if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0)
					goto on_error;
394 395
			}

396 397
			if (git_buf_oom(&data)) {
				error = -1;
398
				goto on_error;
399
			}
400 401 402 403 404 405
		}
	}

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

408
		if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0)
409 410
			goto on_error;

411
		git_vector_foreach(&t->common, j, pkt) {
412 413
			if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0)
				goto on_error;
414 415
		}

416 417
		if (git_buf_oom(&data)) {
			error = -1;
418
			goto on_error;
419
		}
420 421
	}

422 423 424
	if ((error = git_pkt_buffer_done(&data)) < 0)
		goto on_error;

425
	if (t->cancelled.val) {
426
		git_error_set(GIT_ERROR_NET, "The fetch was cancelled by the user");
427 428 429
		error = GIT_EUSER;
		goto on_error;
	}
430
	if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0)
431 432
		goto on_error;

433
	git_buf_dispose(&data);
434 435 436
	git_revwalk_free(walk);

	/* Now let's eat up whatever the server gives us */
437
	if (!t->caps.multi_ack && !t->caps.multi_ack_detailed) {
438
		if ((error = recv_pkt(NULL, &pkt_type, buf)) < 0)
439
			return error;
440 441

		if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) {
442
			git_error_set(GIT_ERROR_NET, "unexpected pkt type");
443 444 445
			return -1;
		}
	} else {
446
		error = wait_while_ack(buf);
447 448
	}

449
	return error;
450 451 452

on_error:
	git_revwalk_free(walk);
453
	git_buf_dispose(&data);
454 455 456
	return error;
}

457
static int no_sideband(transport_smart *t, struct git_odb_writepack *writepack, gitno_buffer *buf, git_indexer_progress *stats)
458 459 460 461 462
{
	int recvd;

	do {
		if (t->cancelled.val) {
463
			git_error_set(GIT_ERROR_NET, "the fetch was cancelled by the user");
464 465 466
			return GIT_EUSER;
		}

467
		if (writepack->append(writepack, buf->data, buf->offset, stats) < 0)
468 469 470 471 472
			return -1;

		gitno_consume_n(buf, buf->offset);

		if ((recvd = gitno_recv(buf)) < 0)
473
			return recvd;
474 475
	} while(recvd > 0);

476
	if (writepack->commit(writepack, stats) < 0)
477 478 479 480 481
		return -1;

	return 0;
}

482
struct network_packetsize_payload
483
{
484
	git_indexer_progress_cb callback;
485
	void *payload;
486
	git_indexer_progress *stats;
487
	size_t last_fired_bytes;
488 489
};

490
static int network_packetsize(size_t received, void *payload)
491 492 493 494 495 496 497 498
{
	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) {
499
		npp->last_fired_bytes = npp->stats->received_bytes;
500

501
		if (npp->callback(npp->stats, npp->payload))
502
			return GIT_EUSER;
503
	}
504 505

	return 0;
506 507 508 509 510
}

int git_smart__download_pack(
	git_transport *transport,
	git_repository *repo,
511 512
	git_indexer_progress *stats,
	git_indexer_progress_cb progress_cb,
513 514 515 516
	void *progress_payload)
{
	transport_smart *t = (transport_smart *)transport;
	gitno_buffer *buf = &t->buffer;
517 518
	git_odb *odb;
	struct git_odb_writepack *writepack = NULL;
519
	int error = 0;
520 521
	struct network_packetsize_payload npp = {0};

522
	memset(stats, 0, sizeof(git_indexer_progress));
523

524 525
	if (progress_cb) {
		npp.callback = progress_cb;
526 527 528 529
		npp.payload = progress_payload;
		npp.stats = stats;
		t->packetsize_cb = &network_packetsize;
		t->packetsize_payload = &npp;
530 531

		/* We might have something in the buffer already from negotiate_fetch */
532
		if (t->buffer.offset > 0 && !t->cancelled.val)
533
			if (t->packetsize_cb(t->buffer.offset, t->packetsize_payload))
534
				git_atomic_set(&t->cancelled, 1);
535 536
	}

537
	if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
538
		((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) != 0))
539
		goto done;
540 541 542

	/*
	 * If the remote doesn't support the side-band, we can feed
543
	 * the data directly to the pack writer. Otherwise, we need to
544 545 546
	 * check which one belongs there.
	 */
	if (!t->caps.side_band && !t->caps.side_band_64k) {
547 548
		error = no_sideband(t, writepack, buf, stats);
		goto done;
549 550 551
	}

	do {
552
		git_pkt *pkt = NULL;
553

554
		/* Check cancellation before network call */
555
		if (t->cancelled.val) {
556
			git_error_clear();
557
			error = GIT_EUSER;
558
			goto done;
559 560
		}

561
		if ((error = recv_pkt(&pkt, NULL, buf)) >= 0) {
562 563
			/* Check cancellation after network call */
			if (t->cancelled.val) {
564
				git_error_clear();
565 566 567 568
				error = GIT_EUSER;
			} else if (pkt->type == GIT_PKT_PROGRESS) {
				if (t->progress_cb) {
					git_pkt_progress *p = (git_pkt_progress *) pkt;
569 570 571 572 573 574 575 576

					if (p->len > INT_MAX) {
						git_error_set(GIT_ERROR_NET, "oversized progress message");
						error = GIT_ERROR;
						goto done;
					}

					error = t->progress_cb(p->data, (int)p->len, t->message_cb_payload);
577 578 579
				}
			} else if (pkt->type == GIT_PKT_DATA) {
				git_pkt_data *p = (git_pkt_data *) pkt;
580 581 582

				if (p->len)
					error = writepack->append(writepack, p->data, p->len, stats);
583 584 585 586 587
			} else if (pkt->type == GIT_PKT_FLUSH) {
				/* A flush indicates the end of the packfile */
				git__free(pkt);
				break;
			}
588
		}
589

590 591
		git_pkt_free(pkt);

592 593
		if (error < 0)
			goto done;
594 595 596

	} while (1);

597
	/*
598
	 * Trailing execution of progress_cb, if necessary...
599 600
	 * Only the callback through the npp datastructure currently
	 * updates the last_fired_bytes value. It is possible that
601
	 * progress has already been reported with the correct
602 603 604 605 606
	 * "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) {
607 608
		error = npp.callback(npp.stats, npp.payload);
		if (error != 0)
609 610 611
			goto done;
	}

612
	error = writepack->commit(writepack, stats);
613

614
done:
615 616
	if (writepack)
		writepack->free(writepack);
617
	if (progress_cb) {
618 619 620
		t->packetsize_cb = NULL;
		t->packetsize_payload = NULL;
	}
621

622 623
	return error;
}
624 625 626 627

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

631
	old_id[GIT_OID_HEXSZ] = '\0'; new_id[GIT_OID_HEXSZ] = '\0';
632 633

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

		if (i == 0) {
637
			++len; /* '\0' */
638
			if (push->report_status)
639 640
				len += strlen(GIT_CAP_REPORT_STATUS) + 1;
			len += strlen(GIT_CAP_SIDE_BAND_64K) + 1;
641 642
		}

643 644
		git_oid_fmt(old_id, &spec->roid);
		git_oid_fmt(new_id, &spec->loid);
645

646
		git_buf_printf(buf, "%04"PRIxZ"%s %s %s", len, old_id, new_id, spec->refspec.dst);
647 648 649

		if (i == 0) {
			git_buf_putc(buf, '\0');
650 651 652
			/* Core git always starts their capabilities string with a space */
			if (push->report_status) {
				git_buf_putc(buf, ' ');
653
				git_buf_printf(buf, GIT_CAP_REPORT_STATUS);
654 655 656
			}
			git_buf_putc(buf, ' ');
			git_buf_printf(buf, GIT_CAP_SIDE_BAND_64K);
657 658 659 660
		}

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

662 663 664 665
	git_buf_puts(buf, "0000");
	return git_buf_oom(buf) ? -1 : 0;
}

666 667 668 669 670 671
static int add_push_report_pkt(git_push *push, git_pkt *pkt)
{
	push_status *status;

	switch (pkt->type) {
		case GIT_PKT_OK:
672
			status = git__calloc(1, sizeof(push_status));
673
			GIT_ERROR_CHECK_ALLOC(status);
674 675 676 677 678 679 680 681 682
			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:
683
			status = git__calloc(1, sizeof(push_status));
684
			GIT_ERROR_CHECK_ALLOC(status);
685 686 687 688 689 690 691 692 693 694 695 696 697 698
			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:
699
			git_error_set(GIT_ERROR_NET, "report-status: protocol error");
700 701 702 703 704 705
			return -1;
	}

	return 0;
}

706
static int add_push_report_sideband_pkt(git_push *push, git_pkt_data *data_pkt, git_buf *data_pkt_buf)
707 708
{
	git_pkt *pkt;
709
	const char *line, *line_end = NULL;
710
	size_t line_len;
711
	int error;
712 713 714 715 716 717 718 719 720 721 722 723 724
	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;
	}
725 726

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

729 730 731 732 733 734 735 736 737 738
		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;
739 740 741 742 743 744 745 746 747

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

		error = add_push_report_pkt(push, pkt);

		git_pkt_free(pkt);

748
		if (error < 0 && error != GIT_ITEROVER)
749
			goto done;
750 751
	}

752 753 754 755 756 757
	error = 0;

done:
	if (reading_from_buf)
		git_buf_consume(data_pkt_buf, line_end);
	return error;
758 759
}

760
static int parse_report(transport_smart *transport, git_push *push)
761
{
762 763
	git_pkt *pkt = NULL;
	const char *line_end = NULL;
764
	gitno_buffer *buf = &transport->buffer;
765
	int error, recvd;
766
	git_buf data_pkt_buf = GIT_BUF_INIT;
767 768 769

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

775 776 777 778
		if (error < 0 && error != GIT_EBUFS) {
			error = -1;
			goto done;
		}
779 780

		if (error == GIT_EBUFS) {
781 782 783 784
			if ((recvd = gitno_recv(buf)) < 0) {
				error = recvd;
				goto done;
			}
785 786

			if (recvd == 0) {
787
				git_error_set(GIT_ERROR_NET, "early EOF");
788 789
				error = GIT_EEOF;
				goto done;
790 791 792 793 794 795
			}
			continue;
		}

		gitno_consume(buf, line_end);

796
		error = 0;
797

798 799 800
		switch (pkt->type) {
			case GIT_PKT_DATA:
				/* This is a sideband packet which contains other packets */
801
				error = add_push_report_sideband_pkt(push, (git_pkt_data *)pkt, &data_pkt_buf);
802 803
				break;
			case GIT_PKT_ERR:
804
				git_error_set(GIT_ERROR_NET, "report-status: Error reported: %s",
805 806 807 808
					((git_pkt_err *)pkt)->error);
				error = -1;
				break;
			case GIT_PKT_PROGRESS:
809 810
				if (transport->progress_cb) {
					git_pkt_progress *p = (git_pkt_progress *) pkt;
811 812 813 814 815 816 817 818

					if (p->len > INT_MAX) {
						git_error_set(GIT_ERROR_NET, "oversized progress message");
						error = GIT_ERROR;
						goto done;
					}

					error = transport->progress_cb(p->data, (int)p->len, transport->message_cb_payload);
819
				}
820 821 822 823
				break;
			default:
				error = add_push_report_pkt(push, pkt);
				break;
824 825
		}

826
		git_pkt_free(pkt);
827

828
		/* add_push_report_pkt returns GIT_ITEROVER when it receives a flush */
829 830 831 832 833
		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 */
834
				git_error_set(GIT_ERROR_NET, "incomplete pack data pkt-line");
835 836 837 838
				error = GIT_ERROR;
			}
			goto done;
		}
839

840 841 842
		if (error < 0) {
			goto done;
		}
843
	}
844
done:
845
	git_buf_dispose(&data_pkt_buf);
846
	return error;
847 848
}

849 850 851
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));
852
	GIT_ERROR_CHECK_ALLOC(added);
853 854 855

	added->type = GIT_PKT_REF;
	git_oid_cpy(&added->head.oid, &push_spec->loid);
856
	added->head.name = git__strdup(push_spec->refspec.dst);
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873

	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;
874
	push_status *push_status;
875 876 877 878 879 880
	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) {
881
		git_error_set(GIT_ERROR_NET, "report-status: protocol error");
882 883 884 885 886 887 888 889 890 891 892 893 894
		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 */
895
		if (strcmp(push_spec->refspec.dst, push_status->ref)) {
896
			git_error_set(GIT_ERROR_NET, "report-status: protocol error");
897 898 899 900 901 902 903 904 905 906 907 908
			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);
909
		push_status = git_vector_get(push_report, i);
910 911
		ref = git_vector_get(refs, j);

912
		cmp = strcmp(push_spec->refspec.dst, ref->head.name);
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

		/* 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);
932
		push_status = git_vector_get(push_report, i);
933 934 935 936 937 938 939 940 941

		/* 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) {
942
		if (git_oid_is_zero(&ref->head.oid)) {
943 944 945 946 947 948 949 950 951 952
			git_vector_remove(refs, i);
			git_pkt_free((git_pkt *)ref);
		}
	}

	git_vector_sort(refs);

	return 0;
}

953 954 955 956
struct push_packbuilder_payload
{
	git_smart_subtransport_stream *stream;
	git_packbuilder *pb;
957
	git_push_transfer_progress_cb cb;
958 959 960 961 962
	void *cb_payload;
	size_t last_bytes;
	double last_progress_report_time;
};

963 964
static int stream_thunk(void *buf, size_t size, void *data)
{
965 966 967 968 969 970 971 972 973
	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;
974

975 976
		if ((current_time - payload->last_progress_report_time) >= MIN_PROGRESS_UPDATE_INTERVAL) {
			payload->last_progress_report_time = current_time;
977
			error = payload->cb(payload->pb->nr_written, payload->pb->nr_objects, payload->last_bytes, payload->cb_payload);
978 979 980 981
		}
	}

	return error;
982 983
}

984
int git_smart__push(git_transport *transport, git_push *push, const git_remote_callbacks *cbs)
985 986
{
	transport_smart *t = (transport_smart *)transport;
987
	struct push_packbuilder_payload packbuilder_payload = {0};
988
	git_buf pktline = GIT_BUF_INIT;
989
	int error = 0, need_pack = 0;
990 991
	push_spec *spec;
	unsigned int i;
992

993 994
	packbuilder_payload.pb = push->pb;

995
	if (cbs && cbs->push_transfer_progress) {
996 997
		packbuilder_payload.cb = cbs->push_transfer_progress;
		packbuilder_payload.cb_payload = cbs->payload;
998 999
	}

1000 1001 1002
#ifdef PUSH_DEBUG
{
	git_remote_head *head;
1003
	char hex[GIT_OID_HEXSZ+1]; hex[GIT_OID_HEXSZ] = '\0';
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

	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

1020 1021 1022 1023 1024
	/*
	 * 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) {
1025
		if (spec->refspec.src && spec->refspec.src[0] != '\0') {
1026 1027 1028 1029 1030
			need_pack = 1;
			break;
		}
	}

1031 1032 1033 1034
	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;
1035

1036 1037 1038
	if (need_pack &&
		(error = git_packbuilder_foreach(push->pb, &stream_thunk, &packbuilder_payload)) < 0)
		goto done;
1039 1040 1041 1042 1043

	/* 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;
1044
	else if ((error = parse_report(t, push)) < 0)
1045
		goto done;
1046

1047
	/* If progress is being reported write the final report */
1048
	if (cbs && cbs->push_transfer_progress) {
1049
		error = cbs->push_transfer_progress(
1050 1051 1052
					push->pb->nr_written,
					push->pb->nr_objects,
					packbuilder_payload.last_bytes,
1053
					cbs->payload);
1054 1055 1056

		if (error < 0)
			goto done;
1057 1058
	}

1059
	if (push->status.length) {
1060
		error = update_refs_from_report(&t->refs, &push->specs, &push->status);
1061 1062 1063
		if (error < 0)
			goto done;

1064
		error = git_smart__update_heads(t, NULL);
1065
	}
1066

1067
done:
1068
	git_buf_dispose(&pktline);
1069 1070
	return error;
}