push.c 15.9 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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 "git2.h"

#include "common.h"
#include "pack.h"
#include "pack-objects.h"
#include "remote.h"
#include "vector.h"
#include "push.h"
16
#include "tree.h"
17

18 19 20 21
static int push_spec_rref_cmp(const void *a, const void *b)
{
	const push_spec *push_spec_a = a, *push_spec_b = b;

22
	return strcmp(push_spec_a->refspec.dst, push_spec_b->refspec.dst);
23 24 25 26 27 28 29 30 31
}

static int push_status_ref_cmp(const void *a, const void *b)
{
	const push_status *push_status_a = a, *push_status_b = b;

	return strcmp(push_status_a->ref, push_status_b->ref);
}

32 33 34 35 36 37 38 39 40 41 42 43
int git_push_new(git_push **out, git_remote *remote)
{
	git_push *p;

	*out = NULL;

	p = git__calloc(1, sizeof(*p));
	GITERR_CHECK_ALLOC(p);

	p->repo = remote->repo;
	p->remote = remote;
	p->report_status = 1;
44
	p->pb_parallelism = 1;
45

46
	if (git_vector_init(&p->specs, 0, push_spec_rref_cmp) < 0) {
47 48 49 50
		git__free(p);
		return -1;
	}

51
	if (git_vector_init(&p->status, 0, push_status_ref_cmp) < 0) {
52 53 54 55 56
		git_vector_free(&p->specs);
		git__free(p);
		return -1;
	}

57 58 59 60 61 62 63
	if (git_vector_init(&p->updates, 0, NULL) < 0) {
		git_vector_free(&p->status);
		git_vector_free(&p->specs);
		git__free(p);
		return -1;
	}

64 65 66 67
	*out = p;
	return 0;
}

68 69 70 71 72 73 74 75 76 77 78 79
int git_push_set_options(git_push *push, const git_push_options *opts)
{
	if (!push || !opts)
		return -1;

	GITERR_CHECK_VERSION(opts, GIT_PUSH_OPTIONS_VERSION, "git_push_options");

	push->pb_parallelism = opts->pb_parallelism;

	return 0;
}

80 81 82 83 84
static void free_refspec(push_spec *spec)
{
	if (spec == NULL)
		return;

85
	git_refspec__free(&spec->refspec);
86 87 88
	git__free(spec);
}

89
static int check_rref(char *ref)
90
{
91 92
	if (git__prefixcmp(ref, "refs/")) {
		giterr_set(GITERR_INVALID, "Not a valid reference '%s'", ref);
93 94
		return -1;
	}
95

96 97 98
	return 0;
}

99 100 101 102 103
static int check_lref(git_push *push, char *ref)
{
	/* lref must be resolvable to an existing object */
	git_object *obj;
	int error = git_revparse_single(&obj, push->repo, ref);
104
	git_object_free(obj);
105

106 107
	if (!error)
		return 0;
108

109 110 111 112 113 114
	if (error == GIT_ENOTFOUND)
		giterr_set(GITERR_REFERENCE,
			"src refspec '%s' does not match any existing object", ref);
	else
		giterr_set(GITERR_INVALID, "Not a valid reference '%s'", ref);
	return -1;
115 116 117
}

static int parse_refspec(git_push *push, push_spec **spec, const char *str)
118 119 120 121 122 123 124 125
{
	push_spec *s;

	*spec = NULL;

	s = git__calloc(1, sizeof(*s));
	GITERR_CHECK_ALLOC(s);

126 127 128
	if (git_refspec__parse(&s->refspec, str, false) < 0) {
		giterr_set(GITERR_INVALID, "invalid refspec %s", str);
		goto on_error;
129 130
	}

131 132 133
	if (s->refspec.src && s->refspec.src[0] != '\0' &&
	    check_lref(push, s->refspec.src) < 0) {
		goto on_error;
134 135
	}

136
	if (check_rref(s->refspec.dst) < 0)
137 138 139 140 141 142 143 144 145 146 147 148 149 150
		goto on_error;

	*spec = s;
	return 0;

on_error:
	free_refspec(s);
	return -1;
}

int git_push_add_refspec(git_push *push, const char *refspec)
{
	push_spec *spec;

151
	if (parse_refspec(push, &spec, refspec) < 0 ||
152 153 154 155 156 157
	    git_vector_insert(&push->specs, spec) < 0)
		return -1;

	return 0;
}

158
int git_push_update_tips(git_push *push, const git_remote_callbacks *callbacks)
159 160 161
{
	git_buf remote_ref_name = GIT_BUF_INIT;
	size_t i, j;
162
	git_refspec *fetch_spec;
163
	push_spec *push_spec = NULL;
164 165 166 167 168
	git_reference *remote_ref;
	push_status *status;
	int error = 0;

	git_vector_foreach(&push->status, i, status) {
169
		int fire_callback = 1;
170

171 172 173 174
		/* Skip unsuccessful updates which have non-empty messages */
		if (status->msg)
			continue;

175
		/* Find the corresponding remote ref */
176 177
		fetch_spec = git_remote__matching_refspec(push->remote, status->ref);
		if (!fetch_spec)
178 179
			continue;

180
		if ((error = git_refspec_transform(&remote_ref_name, fetch_spec, status->ref)) < 0)
181 182 183 184
			goto on_error;

		/* Find matching  push ref spec */
		git_vector_foreach(&push->specs, j, push_spec) {
185
			if (!strcmp(push_spec->refspec.dst, status->ref))
186 187 188 189 190 191 192
				break;
		}

		/* Could not find the corresponding push ref spec for this push update */
		if (j == push->specs.length)
			continue;

193 194 195 196 197 198 199
		/* Update the remote ref */
		if (git_oid_iszero(&push_spec->loid)) {
			error = git_reference_lookup(&remote_ref, push->remote->repo, git_buf_cstr(&remote_ref_name));

			if (error >= 0) {
				error = git_reference_delete(remote_ref);
				git_reference_free(remote_ref);
200
			}
201 202 203 204
		} else {
			error = git_reference_create(NULL, push->remote->repo,
						git_buf_cstr(&remote_ref_name), &push_spec->loid, 1,
						"update by push");
205 206 207 208
		}

		if (error < 0) {
			if (error != GIT_ENOTFOUND)
209
				goto on_error;
210 211 212 213 214

			giterr_clear();
			fire_callback = 0;
		}

215 216 217
		if (fire_callback && callbacks && callbacks->update_tips) {
			error = callbacks->update_tips(git_buf_cstr(&remote_ref_name),
						&push_spec->roid, &push_spec->loid, callbacks->payload);
218 219 220 221

			if (error < 0)
				goto on_error;
		}
222 223 224 225 226 227 228 229 230
	}

	error = 0;

on_error:
	git_buf_free(&remote_ref_name);
	return error;
}

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
/**
 * Insert all tags until we find a non-tag object, which is returned
 * in `out`.
 */
static int enqueue_tag(git_object **out, git_push *push, git_oid *id)
{
	git_object *obj = NULL, *target = NULL;
	int error;

	if ((error = git_object_lookup(&obj, push->repo, id, GIT_OBJ_TAG)) < 0)
		return error;

	while (git_object_type(obj) == GIT_OBJ_TAG) {
		if ((error = git_packbuilder_insert(push->pb, git_object_id(obj), NULL)) < 0)
			break;

		if ((error = git_tag_target(&target, (git_tag *) obj)) < 0)
			break;

		git_object_free(obj);
		obj = target;
	}

	if (error < 0)
		git_object_free(obj);
	else
		*out = obj;

	return error;
}

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
static int revwalk(git_vector *commits, git_push *push)
{
	git_remote_head *head;
	push_spec *spec;
	git_revwalk *rw;
	git_oid oid;
	unsigned int i;
	int error = -1;

	if (git_revwalk_new(&rw, push->repo) < 0)
		return -1;

	git_revwalk_sorting(rw, GIT_SORT_TIME);

	git_vector_foreach(&push->specs, i, spec) {
277 278 279
		git_otype type;
		size_t size;

280 281 282 283 284 285 286 287 288 289
		if (git_oid_iszero(&spec->loid))
			/*
			 * Delete reference on remote side;
			 * nothing to do here.
			 */
			continue;

		if (git_oid_equal(&spec->loid, &spec->roid))
			continue; /* up-to-date */

290 291 292 293 294 295
		if (git_odb_read_header(&size, &type, push->repo->_odb, &spec->loid) < 0)
			goto on_error;

		if (type == GIT_OBJ_TAG) {
			git_object *target;

296
			if ((error = enqueue_tag(&target, push, &spec->loid)) < 0)
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
				goto on_error;

			if (git_object_type(target) == GIT_OBJ_COMMIT) {
				if (git_revwalk_push(rw, git_object_id(target)) < 0) {
					git_object_free(target);
					goto on_error;
				}
			} else {
				if (git_packbuilder_insert(
					push->pb, git_object_id(target), NULL) < 0) {
					git_object_free(target);
					goto on_error;
				}
			}
			git_object_free(target);
		} else if (git_revwalk_push(rw, &spec->loid) < 0)
313 314
			goto on_error;

315
		if (!spec->refspec.force) {
316 317 318 319 320 321
			git_oid base;

			if (git_oid_iszero(&spec->roid))
				continue;

			if (!git_odb_exists(push->repo->_odb, &spec->roid)) {
322 323
				giterr_set(GITERR_REFERENCE, 
					"Cannot push because a reference that you are trying to update on the remote contains commits that are not present locally.");
324 325 326 327 328 329 330 331 332
				error = GIT_ENONFASTFORWARD;
				goto on_error;
			}

			error = git_merge_base(&base, push->repo,
					       &spec->loid, &spec->roid);

			if (error == GIT_ENOTFOUND ||
				(!error && !git_oid_equal(&base, &spec->roid))) {
333 334
				giterr_set(GITERR_REFERENCE,
					"Cannot push non-fastforwardable reference");
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
				error = GIT_ENONFASTFORWARD;
				goto on_error;
			}

			if (error < 0)
				goto on_error;
		}
	}

	git_vector_foreach(&push->remote->refs, i, head) {
		if (git_oid_iszero(&head->oid))
			continue;

		/* TODO */
		git_revwalk_hide(rw, &head->oid);
	}

	while ((error = git_revwalk_next(&oid, rw)) == 0) {
		git_oid *o = git__malloc(GIT_OID_RAWSZ);
354
		if (!o) {
355 356 357
			error = -1;
			goto on_error;
		}
358 359 360
		git_oid_cpy(o, &oid);
		if ((error = git_vector_insert(commits, o)) < 0)
			goto on_error;
361 362 363 364 365 366 367
	}

on_error:
	git_revwalk_free(rw);
	return error == GIT_ITEROVER ? 0 : error;
}

368 369 370 371 372 373 374 375 376 377 378 379 380 381
static int enqueue_object(
	const git_tree_entry *entry,
	git_packbuilder *pb)
{
	switch (git_tree_entry_type(entry)) {
		case GIT_OBJ_COMMIT:
			return 0;
		case GIT_OBJ_TREE:
			return git_packbuilder_insert_tree(pb, &entry->oid);
		default:
			return git_packbuilder_insert(pb, &entry->oid, entry->filename);
	}
}

382 383 384 385
static int queue_differences(
	git_tree *base,
	git_tree *delta,
	git_packbuilder *pb)
386
{
387 388 389 390
	git_tree *b_child = NULL, *d_child = NULL;
	size_t b_length = git_tree_entrycount(base);
	size_t d_length = git_tree_entrycount(delta);
	size_t i = 0, j = 0;
391 392
	int error;

393 394 395 396 397
	while (i < b_length && j < d_length) {
		const git_tree_entry *b_entry = git_tree_entry_byindex(base, i);
		const git_tree_entry *d_entry = git_tree_entry_byindex(delta, j);
		int cmp = 0;

398
		if (!git_oid__cmp(&b_entry->oid, &d_entry->oid))
399 400
			goto loop;

401
		cmp = strcmp(b_entry->filename, d_entry->filename);
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

		/* If the entries are both trees and they have the same name but are
		 * different, then we'll recurse after adding the right-hand entry */
		if (!cmp &&
			git_tree_entry__is_tree(b_entry) &&
			git_tree_entry__is_tree(d_entry)) {
			/* Add the right-hand entry */
			if ((error = git_packbuilder_insert(pb, &d_entry->oid,
				d_entry->filename)) < 0)
				goto on_error;

			/* Acquire the subtrees and recurse */
			if ((error = git_tree_lookup(&b_child,
					git_tree_owner(base), &b_entry->oid)) < 0 ||
				(error = git_tree_lookup(&d_child,
					git_tree_owner(delta), &d_entry->oid)) < 0 ||
				(error = queue_differences(b_child, d_child, pb)) < 0)
				goto on_error;

			git_tree_free(b_child); b_child = NULL;
			git_tree_free(d_child); d_child = NULL;
		}
		/* If the object is new or different in the right-hand tree,
		 * then enumerate it */
426 427 428
		else if (cmp >= 0 &&
			(error = enqueue_object(d_entry, pb)) < 0)
			goto on_error;
429 430 431 432 433 434 435 436

	loop:
		if (cmp <= 0) i++;
		if (cmp >= 0) j++;
	}

	/* Drain the right-hand tree of entries */
	for (; j < d_length; j++)
437 438
		if ((error = enqueue_object(git_tree_entry_byindex(delta, j), pb)) < 0)
			goto on_error;
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458

	error = 0;

on_error:
	if (b_child)
		git_tree_free(b_child);

	if (d_child)
		git_tree_free(d_child);

	return error;
}

static int queue_objects(git_push *push)
{
	git_vector commits = GIT_VECTOR_INIT;
	git_oid *oid;
	size_t i;
	unsigned j;
	int error;
459 460 461 462

	if ((error = revwalk(&commits, push)) < 0)
		goto on_error;

463 464 465 466
	git_vector_foreach(&commits, i, oid) {
		git_commit *parent = NULL, *commit;
		git_tree *tree = NULL, *ptree = NULL;
		size_t parentcount;
467

468
		if ((error = git_commit_lookup(&commit,	push->repo, oid)) < 0)
469 470
			goto on_error;

471 472 473
		/* Insert the commit */
		if ((error = git_packbuilder_insert(push->pb, oid, NULL)) < 0)
			goto loop_error;
474

475
		parentcount = git_commit_parentcount(commit);
476

477
		if (!parentcount) {
478
			if ((error = git_packbuilder_insert_tree(push->pb,
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
				git_commit_tree_id(commit))) < 0)
				goto loop_error;
		} else {
			if ((error = git_tree_lookup(&tree, push->repo,
					git_commit_tree_id(commit))) < 0 ||
				(error = git_packbuilder_insert(push->pb,
					git_commit_tree_id(commit), NULL)) < 0)
				goto loop_error;

			/* For each parent, add the items which are different */
			for (j = 0; j < parentcount; j++) {
				if ((error = git_commit_parent(&parent, commit, j)) < 0 ||
					(error = git_commit_tree(&ptree, parent)) < 0 ||
					(error = queue_differences(ptree, tree, push->pb)) < 0)
					goto loop_error;

				git_tree_free(ptree); ptree = NULL;
				git_commit_free(parent); parent = NULL;
497 498
			}
		}
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

		error = 0;

	loop_error:
		if (tree)
			git_tree_free(tree);

		if (ptree)
			git_tree_free(ptree);

		if (parent)
			git_commit_free(parent);

		git_commit_free(commit);

		if (error < 0)
			goto on_error;
516
	}
517

518 519 520
	error = 0;

on_error:
521
	git_vector_free_deep(&commits);
522 523 524
	return error;
}

525 526 527 528 529 530 531
static int add_update(git_push *push, push_spec *spec)
{
	git_push_update *u = git__calloc(1, sizeof(git_push_update));
	GITERR_CHECK_ALLOC(u);

	u->src_refname = git__strdup(spec->refspec.src);
	GITERR_CHECK_ALLOC(u->src_refname);
532 533

	u->dst_refname = git__strdup(spec->refspec.dst);
534 535
	GITERR_CHECK_ALLOC(u->dst_refname);

536 537
	git_oid_cpy(&u->src, &spec->roid);
	git_oid_cpy(&u->dst, &spec->loid);
538 539 540 541

	return git_vector_insert(&push->updates, u);
}

542 543 544 545 546 547
static int calculate_work(git_push *push)
{
	git_remote_head *head;
	push_spec *spec;
	unsigned int i, j;

548 549
	/* Update local and remote oids*/

550
	git_vector_foreach(&push->specs, i, spec) {
551
		if (spec->refspec.src && spec->refspec.src[0]!= '\0') {
552
			/* This is a create or update.  Local ref must exist. */
553
			if (git_reference_name_to_id(
554 555
					&spec->loid, push->repo, spec->refspec.src) < 0) {
				giterr_set(GITERR_REFERENCE, "No such reference '%s'", spec->refspec.src);
556 557
				return -1;
			}
558
		}
559

560 561 562 563 564
		/* Remote ref may or may not (e.g. during create) already exist. */
		git_vector_foreach(&push->remote->refs, j, head) {
			if (!strcmp(spec->refspec.dst, head->name)) {
				git_oid_cpy(&spec->roid, &head->oid);
				break;
565 566
			}
		}
567 568 569

		if (add_update(push, spec) < 0)
			return -1;
570 571 572 573 574
	}

	return 0;
}

575
static int do_push(git_push *push, const git_remote_callbacks *callbacks)
576
{
577
	int error = 0;
578 579
	git_transport *transport = push->remote->transport;

580 581 582 583 584 585
	if (!transport->push) {
		giterr_set(GITERR_NET, "Remote transport doesn't support push");
		error = -1;
		goto on_error;
	}

586 587 588 589 590 591
	/*
	 * A pack-file MUST be sent if either create or update command
	 * is used, even if the server already has all the necessary
	 * objects.  In this case the client MUST send an empty pack-file.
	 */

592 593 594 595 596
	if ((error = git_packbuilder_new(&push->pb, push->repo)) < 0)
		goto on_error;

	git_packbuilder_set_threads(push->pb, push->pb_parallelism);

597 598
	if (callbacks && callbacks->pack_progress)
		if ((error = git_packbuilder_set_callbacks(push->pb, callbacks->pack_progress, callbacks->payload)) < 0)
599 600
			goto on_error;

601 602 603
	if ((error = calculate_work(push)) < 0)
		goto on_error;

604 605 606
	if (callbacks && callbacks->push_negotiation &&
	    (error = callbacks->push_negotiation((const git_push_update **) push->updates.contents,
					  push->updates.length, callbacks->payload)) < 0)
607 608 609
	    goto on_error;

	if ((error = queue_objects(push)) < 0 ||
610
	    (error = transport->push(transport, push, callbacks)) < 0)
611 612 613 614 615 616 617 618 619
		goto on_error;

on_error:
	git_packbuilder_free(push->pb);
	return error;
}

static int filter_refs(git_remote *remote)
{
Russell Belfer committed
620
	const git_remote_head **heads;
621 622
	size_t heads_len, i;

623
	git_vector_clear(&remote->refs);
624 625 626 627 628

	if (git_remote_ls(&heads, &heads_len, remote) < 0)
		return -1;

	for (i = 0; i < heads_len; i++) {
Russell Belfer committed
629
		if (git_vector_insert(&remote->refs, (void *)heads[i]) < 0)
630 631 632 633
			return -1;
	}

	return 0;
634 635
}

636
int git_push_finish(git_push *push, const git_remote_callbacks *callbacks)
637 638 639 640
{
	int error;

	if (!git_remote_connected(push->remote) &&
641
	    (error = git_remote_connect(push->remote, GIT_DIRECTION_PUSH, callbacks)) < 0)
642 643 644
		return error;

	if ((error = filter_refs(push->remote)) < 0 ||
645
	    (error = do_push(push, callbacks)) < 0)
646 647
		return error;

648 649 650 651
	if (!push->unpack_ok) {
		error = -1;
		giterr_set(GITERR_NET, "unpacking the sent packfile failed on the remote");
	}
652

653
	return error;
654 655 656 657 658 659 660 661 662 663
}

int git_push_status_foreach(git_push *push,
		int (*cb)(const char *ref, const char *msg, void *data),
		void *data)
{
	push_status *status;
	unsigned int i;

	git_vector_foreach(&push->status, i, status) {
664 665
		int error = cb(status->ref, status->msg, data);
		if (error)
666
			return giterr_set_after_callback(error);
667 668 669 670 671
	}

	return 0;
}

672 673 674 675 676
void git_push_status_free(push_status *status)
{
	if (status == NULL)
		return;

677
	git__free(status->msg);
678 679 680 681
	git__free(status->ref);
	git__free(status);
}

682 683 684 685
void git_push_free(git_push *push)
{
	push_spec *spec;
	push_status *status;
686
	git_push_update *update;
687 688 689 690 691 692 693 694 695 696 697
	unsigned int i;

	if (push == NULL)
		return;

	git_vector_foreach(&push->specs, i, spec) {
		free_refspec(spec);
	}
	git_vector_free(&push->specs);

	git_vector_foreach(&push->status, i, status) {
698
		git_push_status_free(status);
699 700 701
	}
	git_vector_free(&push->status);

702 703 704 705 706 707 708
	git_vector_foreach(&push->updates, i, update) {
		git__free(update->src_refname);
		git__free(update->dst_refname);
		git__free(update);
	}
	git_vector_free(&push->updates);

709 710
	git__free(push);
}
711

712
int git_push_init_options(git_push_options *opts, unsigned int version)
713
{
714 715 716
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_push_options, GIT_PUSH_OPTIONS_INIT);
	return 0;
717
}