local.c 17.4 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
#include "common.h"
9

10 11 12
#include "git2/types.h"
#include "git2/net.h"
#include "git2/repository.h"
13 14
#include "git2/object.h"
#include "git2/tag.h"
15
#include "git2/transport.h"
16 17 18 19 20
#include "git2/revwalk.h"
#include "git2/odb_backend.h"
#include "git2/pack.h"
#include "git2/commit.h"
#include "git2/revparse.h"
21

22 23
#include "pack-objects.h"
#include "refs.h"
24
#include "posix.h"
25 26
#include "path.h"
#include "buffer.h"
27 28
#include "repository.h"
#include "odb.h"
29 30
#include "push.h"
#include "remote.h"
31
#include "proxy.h"
32

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

49 50 51 52 53 54 55
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
56 57 58 59 60 61 62 63 64 65 66
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);
}

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

77 78 79 80
	if ((error = git_reference_lookup(&ref, t->repo, name)) < 0)
		return error;

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

92 93 94
	git_oid_cpy(&obj_id, git_reference_target(resolved));
	git_reference_free(resolved);

95
	head = git__calloc(1, sizeof(git_remote_head));
96
	GIT_ERROR_CHECK_ALLOC(head);
97 98

	head->name = git__strdup(name);
99
	GIT_ERROR_CHECK_ALLOC(head->name);
100

101 102
	git_oid_cpy(&head->oid, &obj_id);

103
	if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) {
104
		head->symref_target = git__strdup(git_reference_symbolic_target(ref));
105
		GIT_ERROR_CHECK_ALLOC(head->symref_target);
106 107
	}
	git_reference_free(ref);
108 109

	if ((error = git_vector_insert(&t->refs, head)) < 0) {
110
		free_head(head);
111
		return error;
112
	}
113 114 115

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

118
	if ((error = git_object_lookup(&obj, t->repo, &head->oid, GIT_OBJECT_ANY)) < 0)
119
		return error;
120

121 122
	head = NULL;

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

	/* And if it's a tag, peel it, and add it to the list */
132
	head = git__calloc(1, sizeof(git_remote_head));
133
	GIT_ERROR_CHECK_ALLOC(head);
134

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

141 142
	if (!(error = git_tag_peel(&target, (git_tag *)obj))) {
		git_oid_cpy(&head->oid, git_object_id(target));
143

144
		if ((error = git_vector_insert(&t->refs, head)) < 0) {
145
			free_head(head);
146 147
		}
	}
148

149
	git_object_free(obj);
150
	git_object_free(target);
151 152

	return error;
153 154
}

155
static int store_refs(transport_local *t)
156
{
157 158
	size_t i;
	git_remote_head *head;
159
	git_strarray ref_names = {0};
160

161
	assert(t);
162

163
	if (git_reference_list(&ref_names, t->repo) < 0)
164
		goto on_error;
165

166 167 168 169 170 171 172 173 174
	/* 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);

175
	/* Sort the references first */
176
	git__tsort((void **)ref_names.strings, ref_names.count, &git__strcmp_cb);
177

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

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

187
	t->have_refs = 1;
188
	git_strarray_free(&ref_names);
189 190 191
	return 0;

on_error:
192
	git_vector_free(&t->refs);
193
	git_strarray_free(&ref_names);
194
	return -1;
195
}
196

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

215
	GIT_UNUSED(cred_acquire_cb);
216
	GIT_UNUSED(cred_acquire_payload);
217
	GIT_UNUSED(proxy);
218

219 220 221
	if (t->connected)
		return 0;

Carlos Martín Nieto committed
222 223
	free_heads(&t->refs);

224
	t->url = git__strdup(url);
225
	GIT_ERROR_CHECK_ALLOC(t->url);
226 227
	t->direction = direction;
	t->flags = flags;
228

229
	/* 'url' may be a url or path; convert to a path */
230
	if ((error = git_path_from_url_or_path(&buf, url)) < 0) {
231
		git_buf_dispose(&buf);
232
		return error;
233
	}
234
	path = git_buf_cstr(&buf);
235 236

	error = git_repository_open(&repo, path);
237

238
	git_buf_dispose(&buf);
239

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

243 244
	t->repo = repo;

245 246
	if (store_refs(t) < 0)
		return -1;
247

248 249 250 251 252
	t->connected = 1;

	return 0;
}

253
static int local_ls(const git_remote_head ***out, size_t *size, git_transport *transport)
254 255 256
{
	transport_local *t = (transport_local *)transport;

257
	if (!t->have_refs) {
258
		git_error_set(GIT_ERROR_NET, "the transport has not yet loaded the refs");
259 260 261
		return -1;
	}

262
	*out = (const git_remote_head **)t->refs.contents;
263
	*size = t->refs.length;
264

265
	return 0;
266 267
}

268 269 270
static int local_negotiate_fetch(
	git_transport *transport,
	git_repository *repo,
271 272
	const git_remote_head * const *refs,
	size_t count)
273
{
274 275 276 277
	transport_local *t = (transport_local*)transport;
	git_remote_head *rhead;
	unsigned int i;

278 279
	GIT_UNUSED(refs);
	GIT_UNUSED(count);
280

281 282 283 284 285 286 287 288 289
	/* 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;
290
		else
291
			git_error_clear();
nulltoken committed
292
		git_object_free(obj);
293 294 295 296 297
	}

	return 0;
}

298 299 300 301 302 303 304 305 306 307
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;

308 309
	/* check for lhs, if it's empty it means to delete */
	if (lref[0] != '\0') {
310
		/* Create or update a ref */
311
		error = git_reference_create(NULL, remote_repo, rref, loid,
312
					     !git_oid_is_zero(roid), NULL);
313 314 315 316 317 318 319 320
	} else {
		/* Delete a ref */
		if ((error = git_reference_lookup(&remote_ref, remote_repo, rref)) < 0) {
			if (error == GIT_ENOTFOUND)
				error = 0;
			return error;
		}

321
		error = git_reference_delete(remote_ref);
322 323 324
		git_reference_free(remote_ref);
	}

325
	return error;
326 327
}

328
static int transfer_to_push_transfer(const git_indexer_progress *stats, void *payload)
329 330 331 332 333 334 335 336 337 338
{
	const git_remote_callbacks *cbs = payload;

	if (!cbs || !cbs->push_transfer_progress)
		return 0;

	return cbs->push_transfer_progress(stats->received_objects, stats->total_objects, stats->received_bytes,
					   cbs->payload);
}

339 340
static int local_push(
	git_transport *transport,
341 342
	git_push *push,
	const git_remote_callbacks *cbs)
343 344 345 346 347
{
	transport_local *t = (transport_local *)transport;
	git_repository *remote_repo = NULL;
	push_spec *spec;
	char *url = NULL;
348
	const char *path;
349
	git_buf buf = GIT_BUF_INIT, odb_path = GIT_BUF_INIT;
350 351 352
	int error;
	size_t j;

353 354
	GIT_UNUSED(cbs);

355
	/* 'push->remote->url' may be a url or path; convert to a path */
356
	if ((error = git_path_from_url_or_path(&buf, push->remote->url)) < 0) {
357
		git_buf_dispose(&buf);
358
		return error;
359
	}
360
	path = git_buf_cstr(&buf);
361 362 363

	error = git_repository_open(&remote_repo, path);

364
	git_buf_dispose(&buf);
365 366

	if (error < 0)
367 368
		return error;

nulltoken committed
369
	/* We don't currently support pushing locally to non-bare repos. Proper
370
	   non-bare repo push support would require checking configs to see if
371 372 373 374
	   we should override the default 'don't let this happen' behavior.

	   Note that this is only an issue when pushing to the current branch,
	   but we forbid all pushes just in case */
375
	if (!remote_repo->is_bare) {
376
		error = GIT_EBAREREPO;
377
		git_error_set(GIT_ERROR_INVALID, "local push doesn't (yet) support pushing to non-bare repos.");
378 379 380
		goto on_error;
	}

381 382
	if ((error = git_repository_item_path(&odb_path, remote_repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0
		|| (error = git_buf_joinpath(&odb_path, odb_path.ptr, "pack")) < 0)
383 384
		goto on_error;

385
	error = git_packbuilder_write(push->pb, odb_path.ptr, 0, transfer_to_push_transfer, (void *) cbs);
386
	git_buf_dispose(&odb_path);
387 388 389

	if (error < 0)
		goto on_error;
390 391 392 393 394 395

	push->unpack_ok = 1;

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

398
		status = git__calloc(1, sizeof(push_status));
399 400 401 402 403 404 405 406 407
		if (!status)
			goto on_error;

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

408
		error = local_push_update_remote_ref(remote_repo, spec->refspec.src, spec->refspec.dst,
409 410 411 412 413 414 415 416 417 418 419 420
			&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:
421
				last = git_error_last();
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

				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,
449
			NULL, NULL, NULL, GIT_DIRECTION_PUSH, flags))
450 451 452 453 454 455 456 457 458 459 460 461
			goto on_error;
	}

	error = 0;

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

	return error;
}

462
typedef struct foreach_data {
463 464
	git_indexer_progress *stats;
	git_indexer_progress_cb progress_cb;
465 466 467 468 469 470 471 472 473
	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;
474
	return data->writepack->append(data->writepack, buf, len, data->stats);
475 476
}

477
static const char *counting_objects_fmt = "Counting objects %d\r";
478 479 480 481 482 483
static const char *compressing_objects_fmt = "Compressing objects: %.0f%% (%d/%d)";

static int local_counting(int stage, unsigned int current, unsigned int total, void *payload)
{
	git_buf progress_info = GIT_BUF_INIT;
	transport_local *t = payload;
484
	int error;
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

	if (!t->progress_cb)
		return 0;

	if (stage == GIT_PACKBUILDER_ADDING_OBJECTS) {
		git_buf_printf(&progress_info, counting_objects_fmt, current);
	} else if (stage == GIT_PACKBUILDER_DELTAFICATION) {
		float perc = (((float) current) / total) * 100;
		git_buf_printf(&progress_info, compressing_objects_fmt, perc, current, total);
		if (current == total)
			git_buf_printf(&progress_info, ", done\n");
		else
			git_buf_putc(&progress_info, '\r');

	}

	if (git_buf_oom(&progress_info))
		return -1;

504
	error = t->progress_cb(git_buf_cstr(&progress_info), (int)git_buf_len(&progress_info), t->message_cb_payload);
505
	git_buf_dispose(&progress_info);
506 507

	return error;
508
}
509

510 511 512
static int foreach_reference_cb(git_reference *reference, void *payload)
{
	git_revwalk *walk = (git_revwalk *)payload;
513 514
	int error;

515
	if (git_reference_type(reference) != GIT_REFERENCE_DIRECT) {
516
		git_reference_free(reference);
517
		return 0;
518
	}
519

520
	error = git_revwalk_hide(walk, git_reference_target(reference));
521 522
	/* The reference is in the local repository, so the target may not
	 * exist on the remote.  It also may not be a commit. */
523 524
	if (error == GIT_ENOTFOUND || error == GIT_ERROR_INVALID) {
		git_error_clear();
525 526 527
		error = 0;
	}

528 529
	git_reference_free(reference);

530 531 532
	return error;
}

533 534 535
static int local_download_pack(
		git_transport *transport,
		git_repository *repo,
536 537
		git_indexer_progress *stats,
		git_indexer_progress_cb progress_cb,
538 539 540 541 542 543 544 545 546
		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;
547
	git_odb *odb = NULL;
548
	git_buf progress_info = GIT_BUF_INIT;
549 550 551 552 553 554 555 556

	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;

557 558
	git_packbuilder_set_callbacks(pack, local_counting, t);

559 560 561 562 563 564 565
	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;
566
		if ((error = git_object_lookup(&obj, t->repo, &rhead->oid, GIT_OBJECT_ANY)) < 0)
567 568
			goto cleanup;

569
		if (git_object_type(obj) == GIT_OBJECT_COMMIT) {
570 571 572
			/* Revwalker includes only wanted commits */
			error = git_revwalk_push(walk, &rhead->oid);
		} else {
573 574
			/* Tag or some other wanted object. Add it on its own */
			error = git_packbuilder_insert_recur(pack, &rhead->oid, rhead->name);
575
		}
Linquize committed
576
		git_object_free(obj);
577 578
		if (error < 0)
			goto cleanup;
579 580
	}

581 582 583
	if ((error = git_reference_foreach(repo, foreach_reference_cb, walk)))
		goto cleanup;

584 585 586
	if ((error = git_packbuilder_insert_walk(pack, walk)))
		goto cleanup;

587 588 589 590
	if ((error = git_buf_printf(&progress_info, counting_objects_fmt, git_packbuilder_object_count(pack))) < 0)
		goto cleanup;

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

594
	/* Walk the objects, building a packfile */
595 596
	if ((error = git_repository_odb__weakptr(&odb, repo)) < 0)
		goto cleanup;
597

598 599 600 601 602 603 604
	/* 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 &&
605
	    (error = t->progress_cb(git_buf_cstr(&progress_info), (int)git_buf_len(&progress_info), t->message_cb_payload)) < 0)
606 607
		goto cleanup;

608
	if ((error = git_odb_write_pack(&writepack, odb, progress_cb, progress_payload)) != 0)
609
		goto cleanup;
610 611 612 613 614 615 616 617 618

	/* 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;

619 620 621
		/* autodetect */
		git_packbuilder_set_threads(pack, 0);

622
		if ((error = git_packbuilder_foreach(pack, foreach_cb, &data)) != 0)
623 624
			goto cleanup;
	}
625

626 627 628 629
	error = writepack->commit(writepack, stats);

cleanup:
	if (writepack) writepack->free(writepack);
630
	git_buf_dispose(&progress_info);
631 632 633
	git_packbuilder_free(pack);
	git_revwalk_free(walk);
	return error;
634 635
}

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
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;
}

654
static int local_is_connected(git_transport *transport)
655 656 657
{
	transport_local *t = (transport_local *)transport;

658
	return t->connected;
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
}

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);
}

677
static int local_close(git_transport *transport)
678
{
679 680
	transport_local *t = (transport_local *)transport;

681
	t->connected = 0;
682 683 684 685 686 687 688 689 690 691

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

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

693
	return 0;
694 695
}

696 697
static void local_free(git_transport *transport)
{
698
	transport_local *t = (transport_local *)transport;
699

Carlos Martín Nieto committed
700
	free_heads(&t->refs);
701

702 703
	/* Close the transport, if it's still open. */
	local_close(transport);
704

705
	/* Free the transport */
706
	git__free(t);
707 708
}

709 710 711 712
/**************
 * Public API *
 **************/

713
int git_transport_local(git_transport **out, git_remote *owner, void *param)
714
{
Jacques Germishuys committed
715
	int error;
716 717
	transport_local *t;

718 719
	GIT_UNUSED(param);

720
	t = git__calloc(1, sizeof(transport_local));
721
	GIT_ERROR_CHECK_ALLOC(t);
722

723
	t->parent.version = GIT_TRANSPORT_VERSION;
724
	t->parent.set_callbacks = local_set_callbacks;
725
	t->parent.connect = local_connect;
726
	t->parent.negotiate_fetch = local_negotiate_fetch;
727
	t->parent.download_pack = local_download_pack;
728
	t->parent.push = local_push;
729 730
	t->parent.close = local_close;
	t->parent.free = local_free;
731 732 733 734
	t->parent.ls = local_ls;
	t->parent.is_connected = local_is_connected;
	t->parent.read_flags = local_read_flags;
	t->parent.cancel = local_cancel;
735

Jacques Germishuys committed
736 737 738 739 740
	if ((error = git_vector_init(&t->refs, 0, NULL)) < 0) {
		git__free(t);
		return error;
	}

741 742
	t->owner = owner;

743
	*out = (git_transport *) t;
744

745
	return 0;
746
}