local.c 16.5 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
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 10
#include "common.h"
#include "git2/types.h"
#include "git2/net.h"
#include "git2/repository.h"
11 12
#include "git2/object.h"
#include "git2/tag.h"
13
#include "git2/transport.h"
14 15 16 17 18
#include "git2/revwalk.h"
#include "git2/odb_backend.h"
#include "git2/pack.h"
#include "git2/commit.h"
#include "git2/revparse.h"
19
#include "git2/push.h"
20 21
#include "pack-objects.h"
#include "refs.h"
22
#include "posix.h"
23 24
#include "path.h"
#include "buffer.h"
25 26
#include "repository.h"
#include "odb.h"
27 28
#include "push.h"
#include "remote.h"
29

30
typedef struct {
31
	git_transport parent;
32
	git_remote *owner;
33 34 35 36
	char *url;
	int direction;
	int flags;
	git_atomic cancelled;
37
	git_repository *repo;
38 39 40
	git_transport_message_cb progress_cb;
	git_transport_message_cb error_cb;
	void *message_cb_payload;
41
	git_vector refs;
42 43
	unsigned connected : 1,
		have_refs : 1;
44
} transport_local;
45

46 47 48 49 50 51 52
static void free_head(git_remote_head *head)
{
	git__free(head->name);
	git__free(head->symref_target);
	git__free(head);
}

Carlos Martín Nieto committed
53 54 55 56 57 58 59 60 61 62 63
static void free_heads(git_vector *heads)
{
	git_remote_head *head;
	size_t i;

	git_vector_foreach(heads, i, head)
		free_head(head);

	git_vector_free(heads);
}

64
static int add_ref(transport_local *t, const char *name)
65 66
{
	const char peeled[] = "^{}";
67
	git_reference *ref, *resolved;
68
	git_remote_head *head;
69
	git_oid obj_id;
70 71
	git_object *obj = NULL, *target = NULL;
	git_buf buf = GIT_BUF_INIT;
72
	int error;
73

74 75 76 77
	if ((error = git_reference_lookup(&ref, t->repo, name)) < 0)
		return error;

	error = git_reference_resolve(&resolved, ref);
78
	if (error < 0) {
79
		git_reference_free(ref);
80
		if (!strcmp(name, GIT_HEAD_FILE) && error == GIT_ENOTFOUND) {
81 82
			/* This is actually okay.  Empty repos often have a HEAD that
			 * points to a nonexistent "refs/heads/master". */
83 84 85 86
			giterr_clear();
			return 0;
		}
		return error;
87 88
	}

89 90 91
	git_oid_cpy(&obj_id, git_reference_target(resolved));
	git_reference_free(resolved);

92 93 94 95 96 97
	head = git__calloc(1, sizeof(git_remote_head));
	GITERR_CHECK_ALLOC(head);

	head->name = git__strdup(name);
	GITERR_CHECK_ALLOC(head->name);

98 99 100 101 102 103 104
	git_oid_cpy(&head->oid, &obj_id);

	if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
		head->symref_target = git__strdup(git_reference_symbolic_target(ref));
		GITERR_CHECK_ALLOC(head->symref_target);
	}
	git_reference_free(ref);
105 106

	if ((error = git_vector_insert(&t->refs, head)) < 0) {
107
		free_head(head);
108
		return error;
109
	}
110 111 112

	/* If it's not a tag, we don't need to try to peel it */
	if (git__prefixcmp(name, GIT_REFS_TAGS_DIR))
113
		return 0;
114

115 116
	if ((error = git_object_lookup(&obj, t->repo, &head->oid, GIT_OBJ_ANY)) < 0)
		return error;
117

118 119
	head = NULL;

120 121 122 123
	/* If it's not an annotated tag, or if we're mocking
	 * git-receive-pack, just get out */
	if (git_object_type(obj) != GIT_OBJ_TAG ||
		t->direction != GIT_DIRECTION_FETCH) {
124 125 126
		git_object_free(obj);
		return 0;
	}
127 128

	/* And if it's a tag, peel it, and add it to the list */
129
	head = git__calloc(1, sizeof(git_remote_head));
130
	GITERR_CHECK_ALLOC(head);
131

Jacques Germishuys committed
132 133
	if (git_buf_join(&buf, 0, name, peeled) < 0) {
		free_head(head);
134
		return -1;
Jacques Germishuys committed
135
	}
136
	head->name = git_buf_detach(&buf);
137

138 139
	if (!(error = git_tag_peel(&target, (git_tag *)obj))) {
		git_oid_cpy(&head->oid, git_object_id(target));
140

141
		if ((error = git_vector_insert(&t->refs, head)) < 0) {
142
			free_head(head);
143 144
		}
	}
145

146
	git_object_free(obj);
147
	git_object_free(target);
148 149

	return error;
150 151
}

152
static int store_refs(transport_local *t)
153
{
154 155
	size_t i;
	git_remote_head *head;
156
	git_strarray ref_names = {0};
157

158
	assert(t);
159

160
	if (git_reference_list(&ref_names, t->repo) < 0)
161
		goto on_error;
162

163 164 165 166 167 168 169 170 171
	/* Clear all heads we might have fetched in a previous connect */
	git_vector_foreach(&t->refs, i, head) {
		git__free(head->name);
		git__free(head);
	}

	/* Clear the vector so we can reuse it */
	git_vector_clear(&t->refs);

172
	/* Sort the references first */
173
	git__tsort((void **)ref_names.strings, ref_names.count, &git__strcmp_cb);
174

175 176
	/* Add HEAD iff direction is fetch */
	if (t->direction == GIT_DIRECTION_FETCH && add_ref(t, GIT_HEAD_FILE) < 0)
177
		goto on_error;
178

179
	for (i = 0; i < ref_names.count; ++i) {
180 181
		if (add_ref(t, ref_names.strings[i]) < 0)
			goto on_error;
182
	}
183

184
	t->have_refs = 1;
185
	git_strarray_free(&ref_names);
186 187 188
	return 0;

on_error:
189
	git_vector_free(&t->refs);
190
	git_strarray_free(&ref_names);
191
	return -1;
192
}
193

194 195
/*
 * Try to open the url as a git directory. The direction doesn't
196
 * matter in this case because we're calculating the heads ourselves.
197
 */
198 199 200 201
static int local_connect(
	git_transport *transport,
	const char *url,
	git_cred_acquire_cb cred_acquire_cb,
202
	void *cred_acquire_payload,
203
	int direction, int flags)
204 205 206 207 208
{
	git_repository *repo;
	int error;
	transport_local *t = (transport_local *) transport;
	const char *path;
209 210
	git_buf buf = GIT_BUF_INIT;

211
	GIT_UNUSED(cred_acquire_cb);
212
	GIT_UNUSED(cred_acquire_payload);
213

214 215 216
	if (t->connected)
		return 0;

Carlos Martín Nieto committed
217 218
	free_heads(&t->refs);

219 220 221 222
	t->url = git__strdup(url);
	GITERR_CHECK_ALLOC(t->url);
	t->direction = direction;
	t->flags = flags;
223

224
	/* 'url' may be a url or path; convert to a path */
225
	if ((error = git_path_from_url_or_path(&buf, url)) < 0) {
226 227
		git_buf_free(&buf);
		return error;
228
	}
229
	path = git_buf_cstr(&buf);
230 231

	error = git_repository_open(&repo, path);
232 233 234

	git_buf_free(&buf);

235 236
	if (error < 0)
		return -1;
237

238 239
	t->repo = repo;

240 241
	if (store_refs(t) < 0)
		return -1;
242

243 244 245 246 247
	t->connected = 1;

	return 0;
}

248
static int local_ls(const git_remote_head ***out, size_t *size, git_transport *transport)
249 250 251
{
	transport_local *t = (transport_local *)transport;

252 253
	if (!t->have_refs) {
		giterr_set(GITERR_NET, "The transport has not yet loaded the refs");
254 255 256
		return -1;
	}

257
	*out = (const git_remote_head **)t->refs.contents;
258
	*size = t->refs.length;
259

260
	return 0;
261 262
}

263 264 265
static int local_negotiate_fetch(
	git_transport *transport,
	git_repository *repo,
266 267
	const git_remote_head * const *refs,
	size_t count)
268
{
269 270 271 272
	transport_local *t = (transport_local*)transport;
	git_remote_head *rhead;
	unsigned int i;

273 274
	GIT_UNUSED(refs);
	GIT_UNUSED(count);
275

276 277 278 279 280 281 282 283 284
	/* Fill in the loids */
	git_vector_foreach(&t->refs, i, rhead) {
		git_object *obj;

		int error = git_revparse_single(&obj, repo, rhead->name);
		if (!error)
			git_oid_cpy(&rhead->loid, git_object_id(obj));
		else if (error != GIT_ENOTFOUND)
			return error;
285 286
		else
			giterr_clear();
nulltoken committed
287
		git_object_free(obj);
288 289 290 291 292
	}

	return 0;
}

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
static int local_push_copy_object(
	git_odb *local_odb,
	git_odb *remote_odb,
	git_pobject *obj)
{
	int error = 0;
	git_odb_object *odb_obj = NULL;
	git_odb_stream *odb_stream;
	size_t odb_obj_size;
	git_otype odb_obj_type;
	git_oid remote_odb_obj_oid;

	/* Object already exists in the remote ODB; do nothing and return 0*/
	if (git_odb_exists(remote_odb, &obj->id))
		return 0;

	if ((error = git_odb_read(&odb_obj, local_odb, &obj->id)) < 0)
		return error;

	odb_obj_size = git_odb_object_size(odb_obj);
	odb_obj_type = git_odb_object_type(odb_obj);

	if ((error = git_odb_open_wstream(&odb_stream, remote_odb,
		odb_obj_size, odb_obj_type)) < 0)
		goto on_error;

319
	if (git_odb_stream_write(odb_stream, (char *)git_odb_object_data(odb_obj),
320
		odb_obj_size) < 0 ||
321
		git_odb_stream_finalize_write(&remote_odb_obj_oid, odb_stream) < 0) {
322
		error = -1;
323
	} else if (git_oid__cmp(&obj->id, &remote_odb_obj_oid) != 0) {
324 325 326 327 328 329
		giterr_set(GITERR_ODB, "Error when writing object to remote odb "
			"during local push operation. Remote odb object oid does not "
			"match local oid.");
		error = -1;
	}

330
	git_odb_stream_free(odb_stream);
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

on_error:
	git_odb_object_free(odb_obj);
	return error;
}

static int local_push_update_remote_ref(
	git_repository *remote_repo,
	const char *lref,
	const char *rref,
	git_oid *loid,
	git_oid *roid)
{
	int error;
	git_reference *remote_ref = NULL;

347 348
	/* check for lhs, if it's empty it means to delete */
	if (lref[0] != '\0') {
349
		/* Create or update a ref */
350
		error = git_reference_create(NULL, remote_repo, rref, loid,
351
					     !git_oid_iszero(roid), NULL);
352 353 354 355 356 357 358 359
	} else {
		/* Delete a ref */
		if ((error = git_reference_lookup(&remote_ref, remote_repo, rref)) < 0) {
			if (error == GIT_ENOTFOUND)
				error = 0;
			return error;
		}

360
		error = git_reference_delete(remote_ref);
361 362 363
		git_reference_free(remote_ref);
	}

364
	return error;
365 366 367 368 369 370 371 372 373 374 375 376
}

static int local_push(
	git_transport *transport,
	git_push *push)
{
	transport_local *t = (transport_local *)transport;
	git_odb *remote_odb = NULL;
	git_odb *local_odb = NULL;
	git_repository *remote_repo = NULL;
	push_spec *spec;
	char *url = NULL;
377 378
	const char *path;
	git_buf buf = GIT_BUF_INIT;
379 380 381 382
	int error;
	unsigned int i;
	size_t j;

383
	/* 'push->remote->url' may be a url or path; convert to a path */
384
	if ((error = git_path_from_url_or_path(&buf, push->remote->url)) < 0) {
385 386
		git_buf_free(&buf);
		return error;
387
	}
388
	path = git_buf_cstr(&buf);
389 390 391 392 393 394

	error = git_repository_open(&remote_repo, path);

	git_buf_free(&buf);

	if (error < 0)
395 396
		return error;

nulltoken committed
397
	/* We don't currently support pushing locally to non-bare repos. Proper
398 399 400
	   non-bare repo push support would require checking configs to see if
	   we should override the default 'don't let this happen' behavior */
	if (!remote_repo->is_bare) {
401 402
		error = GIT_EBAREREPO;
		giterr_set(GITERR_INVALID, "Local push doesn't (yet) support pushing to non-bare repos.");
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
		goto on_error;
	}

	if ((error = git_repository_odb__weakptr(&remote_odb, remote_repo)) < 0 ||
		(error = git_repository_odb__weakptr(&local_odb, push->repo)) < 0)
		goto on_error;

	for (i = 0; i < push->pb->nr_objects; i++) {
		if ((error = local_push_copy_object(local_odb, remote_odb,
			&push->pb->object_list[i])) < 0)
			goto on_error;
	}

	push->unpack_ok = 1;

	git_vector_foreach(&push->specs, j, spec) {
		push_status *status;
		const git_error *last;
421
		char *ref = spec->refspec.dst;
422

423
		status = git__calloc(1, sizeof(push_status));
424 425 426 427 428 429 430 431 432
		if (!status)
			goto on_error;

		status->ref = git__strdup(ref);
		if (!status->ref) {
			git_push_status_free(status);
			goto on_error;
		}

433
		error = local_push_update_remote_ref(remote_repo, spec->refspec.src, spec->refspec.dst,
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
			&spec->loid, &spec->roid);

		switch (error) {
			case GIT_OK:
				break;
			case GIT_EINVALIDSPEC:
				status->msg = git__strdup("funny refname");
				break;
			case GIT_ENOTFOUND:
				status->msg = git__strdup("Remote branch not found to delete");
				break;
			default:
				last = giterr_last();

				if (last && last->message)
					status->msg = git__strdup(last->message);
				else
					status->msg = git__strdup("Unspecified error encountered");
				break;
		}

		/* failed to allocate memory for a status message */
		if (error < 0 && !status->msg) {
			git_push_status_free(status);
			goto on_error;
		}

		/* failed to insert the ref update status */
		if ((error = git_vector_insert(&push->status, status)) < 0) {
			git_push_status_free(status);
			goto on_error;
		}
	}

	if (push->specs.length) {
		int flags = t->flags;
		url = git__strdup(t->url);

		if (!url || t->parent.close(&t->parent) < 0 ||
			t->parent.connect(&t->parent, url,
474
			push->remote->callbacks.credentials, NULL, GIT_DIRECTION_PUSH, flags))
475 476 477 478 479 480 481 482 483 484 485 486
			goto on_error;
	}

	error = 0;

on_error:
	git_repository_free(remote_repo);
	git__free(url);

	return error;
}

487 488
typedef struct foreach_data {
	git_transfer_progress *stats;
489
	git_transfer_progress_cb progress_cb;
490 491 492 493 494 495 496 497 498
	void *progress_payload;
	git_odb_writepack *writepack;
} foreach_data;

static int foreach_cb(void *buf, size_t len, void *payload)
{
	foreach_data *data = (foreach_data*)payload;

	data->stats->received_bytes += len;
499
	return data->writepack->append(data->writepack, buf, len, data->stats);
500 501
}

502 503
static const char *counting_objects_fmt = "Counting objects %d\r";

504 505 506 507
static int local_download_pack(
		git_transport *transport,
		git_repository *repo,
		git_transfer_progress *stats,
508
		git_transfer_progress_cb progress_cb,
509 510 511 512 513 514 515 516 517
		void *progress_payload)
{
	transport_local *t = (transport_local*)transport;
	git_revwalk *walk = NULL;
	git_remote_head *rhead;
	unsigned int i;
	int error = -1;
	git_packbuilder *pack = NULL;
	git_odb_writepack *writepack = NULL;
518
	git_odb *odb = NULL;
519
	git_buf progress_info = GIT_BUF_INIT;
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

	if ((error = git_revwalk_new(&walk, t->repo)) < 0)
		goto cleanup;
	git_revwalk_sorting(walk, GIT_SORT_TIME);

	if ((error = git_packbuilder_new(&pack, t->repo)) < 0)
		goto cleanup;

	stats->total_objects = 0;
	stats->indexed_objects = 0;
	stats->received_objects = 0;
	stats->received_bytes = 0;

	git_vector_foreach(&t->refs, i, rhead) {
		git_object *obj;
		if ((error = git_object_lookup(&obj, t->repo, &rhead->oid, GIT_OBJ_ANY)) < 0)
			goto cleanup;

		if (git_object_type(obj) == GIT_OBJ_COMMIT) {
			/* Revwalker includes only wanted commits */
			error = git_revwalk_push(walk, &rhead->oid);
541
			if (!error && !git_oid_iszero(&rhead->loid)) {
542
				error = git_revwalk_hide(walk, &rhead->loid);
543 544 545
				if (error == GIT_ENOTFOUND)
					error = 0;
			}
546
		} else {
547 548
			/* Tag or some other wanted object. Add it on its own */
			error = git_packbuilder_insert_recur(pack, &rhead->oid, rhead->name);
549
		}
Linquize committed
550
		git_object_free(obj);
551 552
		if (error < 0)
			goto cleanup;
553 554
	}

555 556 557
	if ((error = git_packbuilder_insert_walk(pack, walk)))
		goto cleanup;

558 559 560 561 562 563 564
	if ((error = git_buf_printf(&progress_info, counting_objects_fmt, git_packbuilder_object_count(pack))) < 0)
		goto cleanup;

	if (t->progress_cb &&
	    (error = t->progress_cb(git_buf_cstr(&progress_info), git_buf_len(&progress_info), t->message_cb_payload)) < 0)
		goto cleanup;

565
	/* Walk the objects, building a packfile */
566 567
	if ((error = git_repository_odb__weakptr(&odb, repo)) < 0)
		goto cleanup;
568

569 570 571 572 573 574 575 576 577 578
	/* One last one with the newline */
	git_buf_clear(&progress_info);
	git_buf_printf(&progress_info, counting_objects_fmt, git_packbuilder_object_count(pack));
	if ((error = git_buf_putc(&progress_info, '\n')) < 0)
		goto cleanup;

	if (t->progress_cb &&
	    (error = t->progress_cb(git_buf_cstr(&progress_info), git_buf_len(&progress_info), t->message_cb_payload)) < 0)
		goto cleanup;

579
	if ((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) != 0)
580
		goto cleanup;
581 582 583 584 585 586 587 588 589

	/* Write the data to the ODB */
	{
		foreach_data data = {0};
		data.stats = stats;
		data.progress_cb = progress_cb;
		data.progress_payload = progress_payload;
		data.writepack = writepack;

590 591 592
		/* autodetect */
		git_packbuilder_set_threads(pack, 0);

593
		if ((error = git_packbuilder_foreach(pack, foreach_cb, &data)) != 0)
594 595
			goto cleanup;
	}
596

597 598 599 600
	error = writepack->commit(writepack, stats);

cleanup:
	if (writepack) writepack->free(writepack);
601
	git_buf_free(&progress_info);
602 603 604
	git_packbuilder_free(pack);
	git_revwalk_free(walk);
	return error;
605 606
}

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
static int local_set_callbacks(
	git_transport *transport,
	git_transport_message_cb progress_cb,
	git_transport_message_cb error_cb,
	git_transport_certificate_check_cb certificate_check_cb,
	void *message_cb_payload)
{
	transport_local *t = (transport_local *)transport;

	GIT_UNUSED(certificate_check_cb);

	t->progress_cb = progress_cb;
	t->error_cb = error_cb;
	t->message_cb_payload = message_cb_payload;

	return 0;
}

625
static int local_is_connected(git_transport *transport)
626 627 628
{
	transport_local *t = (transport_local *)transport;

629
	return t->connected;
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
}

static int local_read_flags(git_transport *transport, int *flags)
{
	transport_local *t = (transport_local *)transport;

	*flags = t->flags;

	return 0;
}

static void local_cancel(git_transport *transport)
{
	transport_local *t = (transport_local *)transport;

	git_atomic_set(&t->cancelled, 1);
}

648
static int local_close(git_transport *transport)
649
{
650 651
	transport_local *t = (transport_local *)transport;

652
	t->connected = 0;
653 654 655 656 657 658 659 660 661 662

	if (t->repo) {
		git_repository_free(t->repo);
		t->repo = NULL;
	}

	if (t->url) {
		git__free(t->url);
		t->url = NULL;
	}
663

664
	return 0;
665 666
}

667 668
static void local_free(git_transport *transport)
{
669
	transport_local *t = (transport_local *)transport;
670

Carlos Martín Nieto committed
671
	free_heads(&t->refs);
672

673 674
	/* Close the transport, if it's still open. */
	local_close(transport);
675

676
	/* Free the transport */
677
	git__free(t);
678 679
}

680 681 682 683
/**************
 * Public API *
 **************/

684
int git_transport_local(git_transport **out, git_remote *owner, void *param)
685
{
Jacques Germishuys committed
686
	int error;
687 688
	transport_local *t;

689 690
	GIT_UNUSED(param);

691
	t = git__calloc(1, sizeof(transport_local));
692
	GITERR_CHECK_ALLOC(t);
693

694
	t->parent.version = GIT_TRANSPORT_VERSION;
695
	t->parent.set_callbacks = local_set_callbacks;
696
	t->parent.connect = local_connect;
697
	t->parent.negotiate_fetch = local_negotiate_fetch;
698
	t->parent.download_pack = local_download_pack;
699
	t->parent.push = local_push;
700 701
	t->parent.close = local_close;
	t->parent.free = local_free;
702 703 704 705
	t->parent.ls = local_ls;
	t->parent.is_connected = local_is_connected;
	t->parent.read_flags = local_read_flags;
	t->parent.cancel = local_cancel;
706

Jacques Germishuys committed
707 708 709 710 711
	if ((error = git_vector_init(&t->refs, 0, NULL)) < 0) {
		git__free(t);
		return error;
	}

712 713
	t->owner = owner;

714
	*out = (git_transport *) t;
715

716
	return 0;
717
}