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

8 9
#include "push.h"

10 11 12 13 14 15
#include "git2.h"

#include "pack.h"
#include "pack-objects.h"
#include "remote.h"
#include "vector.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
int git_push_new(git_push **out, git_remote *remote)
{
	git_push *p;

	*out = NULL;

	p = git__calloc(1, sizeof(*p));
39
	GIT_ERROR_CHECK_ALLOC(p);
40 41 42 43

	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
int git_push_set_options(git_push *push, const git_push_options *opts)
{
	if (!push || !opts)
		return -1;

73
	GIT_ERROR_CHECK_VERSION(opts, GIT_PUSH_OPTIONS_VERSION, "git_push_options");
74 75

	push->pb_parallelism = opts->pb_parallelism;
76 77
	push->connection.custom_headers = &opts->custom_headers;
	push->connection.proxy = &opts->proxy_opts;
78 79 80 81

	return 0;
}

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

87
	git_refspec__dispose(&spec->refspec);
88 89 90
	git__free(spec);
}

91
static int check_rref(char *ref)
92
{
93
	if (git__prefixcmp(ref, "refs/")) {
94
		git_error_set(GIT_ERROR_INVALID, "not a valid reference '%s'", ref);
95 96
		return -1;
	}
97

98 99 100
	return 0;
}

101 102 103 104 105
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);
106
	git_object_free(obj);
107

108 109
	if (!error)
		return 0;
110

111
	if (error == GIT_ENOTFOUND)
112
		git_error_set(GIT_ERROR_REFERENCE,
113 114
			"src refspec '%s' does not match any existing object", ref);
	else
115
		git_error_set(GIT_ERROR_INVALID, "not a valid reference '%s'", ref);
116
	return -1;
117 118 119
}

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

	*spec = NULL;

	s = git__calloc(1, sizeof(*s));
126
	GIT_ERROR_CHECK_ALLOC(s);
127

128
	if (git_refspec__parse(&s->refspec, str, false) < 0) {
129
		git_error_set(GIT_ERROR_INVALID, "invalid refspec %s", str);
130
		goto on_error;
131 132
	}

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

138
	if (check_rref(s->refspec.dst) < 0)
139 140 141 142 143 144 145 146 147 148 149 150 151 152
		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;

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

	return 0;
}

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

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

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

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

182 183 184
		/* Clear the buffer which can be dirty from previous iteration */
		git_buf_clear(&remote_ref_name);

185
		if ((error = git_refspec_transform(&remote_ref_name, fetch_spec, status->ref)) < 0)
186 187 188 189
			goto on_error;

		/* Find matching  push ref spec */
		git_vector_foreach(&push->specs, j, push_spec) {
190
			if (!strcmp(push_spec->refspec.dst, status->ref))
191 192 193 194 195 196 197
				break;
		}

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

198 199 200 201 202 203 204
		/* 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);
205
			}
206 207 208 209
		} else {
			error = git_reference_create(NULL, push->remote->repo,
						git_buf_cstr(&remote_ref_name), &push_spec->loid, 1,
						"update by push");
210 211 212 213
		}

		if (error < 0) {
			if (error != GIT_ENOTFOUND)
214
				goto on_error;
215

216
			git_error_clear();
217 218 219
			fire_callback = 0;
		}

220 221 222
		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);
223 224 225 226

			if (error < 0)
				goto on_error;
		}
227 228 229 230 231
	}

	error = 0;

on_error:
232
	git_buf_dispose(&remote_ref_name);
233 234 235
	return error;
}

236 237 238 239 240 241 242 243 244
/**
 * 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;

245
	if ((error = git_object_lookup(&obj, push->repo, id, GIT_OBJECT_TAG)) < 0)
246 247
		return error;

248
	while (git_object_type(obj) == GIT_OBJECT_TAG) {
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
		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;
}

267
static int queue_objects(git_push *push)
268 269 270 271 272 273 274 275 276 277 278 279 280
{
	git_remote_head *head;
	push_spec *spec;
	git_revwalk *rw;
	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) {
281
		git_object_t type;
282 283
		size_t size;

284 285 286 287 288 289 290 291 292 293
		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 */

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

297
		if (type == GIT_OBJECT_TAG) {
298 299
			git_object *target;

300
			if ((error = enqueue_tag(&target, push, &spec->loid)) < 0)
301 302
				goto on_error;

303
			if (git_object_type(target) == GIT_OBJECT_COMMIT) {
304 305 306 307 308 309 310 311 312 313 314 315 316
				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)
317 318
			goto on_error;

319
		if (!spec->refspec.force) {
320 321 322 323 324 325
			git_oid base;

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

			if (!git_odb_exists(push->repo->_odb, &spec->roid)) {
326
				git_error_set(GIT_ERROR_REFERENCE,
327
					"cannot push because a reference that you are trying to update on the remote contains commits that are not present locally.");
328 329 330 331 332 333 334 335 336
				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))) {
337
				git_error_set(GIT_ERROR_REFERENCE,
338
					"cannot push non-fastforwardable reference");
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
				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);
	}

356
	error = git_packbuilder_insert_walk(push->pb, rw);
357 358 359 360 361 362

on_error:
	git_revwalk_free(rw);
	return error;
}

363 364 365
static int add_update(git_push *push, push_spec *spec)
{
	git_push_update *u = git__calloc(1, sizeof(git_push_update));
366
	GIT_ERROR_CHECK_ALLOC(u);
367 368

	u->src_refname = git__strdup(spec->refspec.src);
369
	GIT_ERROR_CHECK_ALLOC(u->src_refname);
370 371

	u->dst_refname = git__strdup(spec->refspec.dst);
372
	GIT_ERROR_CHECK_ALLOC(u->dst_refname);
373

374 375
	git_oid_cpy(&u->src, &spec->roid);
	git_oid_cpy(&u->dst, &spec->loid);
376 377 378 379

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

380 381 382 383 384 385
static int calculate_work(git_push *push)
{
	git_remote_head *head;
	push_spec *spec;
	unsigned int i, j;

386 387
	/* Update local and remote oids*/

388
	git_vector_foreach(&push->specs, i, spec) {
389
		if (spec->refspec.src && spec->refspec.src[0]!= '\0') {
390
			/* This is a create or update.  Local ref must exist. */
391
			if (git_reference_name_to_id(
392
					&spec->loid, push->repo, spec->refspec.src) < 0) {
393
				git_error_set(GIT_ERROR_REFERENCE, "no such reference '%s'", spec->refspec.src);
394 395
				return -1;
			}
396
		}
397

398 399 400 401 402
		/* 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;
403 404
			}
		}
405 406 407

		if (add_update(push, spec) < 0)
			return -1;
408 409 410 411 412
	}

	return 0;
}

413
static int do_push(git_push *push, const git_remote_callbacks *callbacks)
414
{
415
	int error = 0;
416 417
	git_transport *transport = push->remote->transport;

418
	if (!transport->push) {
419
		git_error_set(GIT_ERROR_NET, "remote transport doesn't support push");
420 421 422 423
		error = -1;
		goto on_error;
	}

424 425 426 427 428 429
	/*
	 * 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.
	 */

430 431 432 433 434
	if ((error = git_packbuilder_new(&push->pb, push->repo)) < 0)
		goto on_error;

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

435 436
	if (callbacks && callbacks->pack_progress)
		if ((error = git_packbuilder_set_callbacks(push->pb, callbacks->pack_progress, callbacks->payload)) < 0)
437 438
			goto on_error;

439 440 441
	if ((error = calculate_work(push)) < 0)
		goto on_error;

442 443 444
	if (callbacks && callbacks->push_negotiation &&
	    (error = callbacks->push_negotiation((const git_push_update **) push->updates.contents,
					  push->updates.length, callbacks->payload)) < 0)
445 446 447
	    goto on_error;

	if ((error = queue_objects(push)) < 0 ||
448
	    (error = transport->push(transport, push, callbacks)) < 0)
449 450 451 452 453 454 455 456 457
		goto on_error;

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

static int filter_refs(git_remote *remote)
{
Russell Belfer committed
458
	const git_remote_head **heads;
459 460
	size_t heads_len, i;

461
	git_vector_clear(&remote->refs);
462 463 464 465 466

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

	for (i = 0; i < heads_len; i++) {
Russell Belfer committed
467
		if (git_vector_insert(&remote->refs, (void *)heads[i]) < 0)
468 469 470 471
			return -1;
	}

	return 0;
472 473
}

474
int git_push_finish(git_push *push, const git_remote_callbacks *callbacks)
475 476 477 478
{
	int error;

	if (!git_remote_connected(push->remote) &&
479
	    (error = git_remote__connect(push->remote, GIT_DIRECTION_PUSH, callbacks, &push->connection)) < 0)
480 481 482
		return error;

	if ((error = filter_refs(push->remote)) < 0 ||
483
	    (error = do_push(push, callbacks)) < 0)
484 485
		return error;

486 487
	if (!push->unpack_ok) {
		error = -1;
488
		git_error_set(GIT_ERROR_NET, "unpacking the sent packfile failed on the remote");
489
	}
490

491
	return error;
492 493 494 495 496 497 498 499 500 501
}

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) {
502 503
		int error = cb(status->ref, status->msg, data);
		if (error)
504
			return git_error_set_after_callback(error);
505 506 507 508 509
	}

	return 0;
}

510 511 512 513 514
void git_push_status_free(push_status *status)
{
	if (status == NULL)
		return;

515
	git__free(status->msg);
516 517 518 519
	git__free(status->ref);
	git__free(status);
}

520 521 522 523
void git_push_free(git_push *push)
{
	push_spec *spec;
	push_status *status;
524
	git_push_update *update;
525 526 527 528 529 530 531 532 533 534 535
	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) {
536
		git_push_status_free(status);
537 538 539
	}
	git_vector_free(&push->status);

540 541 542 543 544 545 546
	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);

547 548
	git__free(push);
}
549

550
int git_push_options_init(git_push_options *opts, unsigned int version)
551
{
552 553 554
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_push_options, GIT_PUSH_OPTIONS_INIT);
	return 0;
555
}
556 557 558 559 560

int git_push_init_options(git_push_options *opts, unsigned int version)
{
	return git_push_options_init(opts, version);
}