clone.c 15.8 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 "clone.h"

10 11
#include "git2/clone.h"
#include "git2/remote.h"
12
#include "git2/revparse.h"
13 14
#include "git2/branch.h"
#include "git2/config.h"
15
#include "git2/checkout.h"
16 17
#include "git2/commit.h"
#include "git2/tree.h"
18 19

#include "remote.h"
20
#include "futils.h"
21
#include "refs.h"
22
#include "fs_path.h"
23
#include "repository.h"
24
#include "odb.h"
25

26
static int clone_local_into(git_repository *repo, git_remote *remote, const git_fetch_options *fetch_opts, const git_checkout_options *co_opts, const char *branch, int link);
27

28
static int create_branch(
29 30 31
	git_reference **branch,
	git_repository *repo,
	const git_oid *target,
32 33
	const char *name,
	const char *log_message)
34
{
Vicent Marti committed
35
	git_commit *head_obj = NULL;
36
	git_reference *branch_ref = NULL;
37
	git_str refname = GIT_STR_INIT;
38
	int error;
Ben Straub committed
39 40

	/* Find the target commit */
Vicent Marti committed
41
	if ((error = git_commit_lookup(&head_obj, repo, target)) < 0)
42
		return error;
Ben Straub committed
43 44

	/* Create the new branch */
45
	if ((error = git_str_printf(&refname, GIT_REFS_HEADS_DIR "%s", name)) < 0)
46
		return error;
Ben Straub committed
47

48 49
	error = git_reference_create(&branch_ref, repo, git_str_cstr(&refname), target, 0, log_message);
	git_str_dispose(&refname);
Vicent Marti committed
50
	git_commit_free(head_obj);
51

52
	if (!error)
53 54 55 56
		*branch = branch_ref;
	else
		git_reference_free(branch_ref);

57 58 59 60 61 62 63 64 65 66
	return error;
}

static int setup_tracking_config(
	git_repository *repo,
	const char *branch_name,
	const char *remote_name,
	const char *merge_target)
{
	git_config *cfg;
67
	git_str remote_key = GIT_STR_INIT, merge_key = GIT_STR_INIT;
68 69 70 71 72
	int error = -1;

	if (git_repository_config__weakptr(&cfg, repo) < 0)
		return -1;

73
	if (git_str_printf(&remote_key, "branch.%s.remote", branch_name) < 0)
74 75
		goto cleanup;

76
	if (git_str_printf(&merge_key, "branch.%s.merge", branch_name) < 0)
77 78
		goto cleanup;

79
	if (git_config_set_string(cfg, git_str_cstr(&remote_key), remote_name) < 0)
80 81
		goto cleanup;

82
	if (git_config_set_string(cfg, git_str_cstr(&merge_key), merge_target) < 0)
83 84 85 86 87
		goto cleanup;

	error = 0;

cleanup:
88 89
	git_str_dispose(&remote_key);
	git_str_dispose(&merge_key);
90 91 92 93 94 95 96
	return error;
}

static int create_tracking_branch(
	git_reference **branch,
	git_repository *repo,
	const git_oid *target,
97 98
	const char *branch_name,
	const char *log_message)
99 100 101
{
	int error;

102
	if ((error = create_branch(branch, repo, target, branch_name, log_message)) < 0)
103 104 105 106 107 108 109
		return error;

	return setup_tracking_config(
		repo,
		branch_name,
		GIT_REMOTE_ORIGIN,
		git_reference_name(*branch));
110 111
}

112 113 114
static int update_head_to_new_branch(
	git_repository *repo,
	const git_oid *target,
115 116
	const char *name,
	const char *reflog_message)
117
{
Michael Schubert committed
118
	git_reference *tracking_branch = NULL;
119
	int error;
120 121 122 123

	if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR))
		name += strlen(GIT_REFS_HEADS_DIR);

124
	error = create_tracking_branch(&tracking_branch, repo, target, name,
125
			reflog_message);
Ben Straub committed
126

127 128
	if (!error)
		error = git_repository_set_head(
129
			repo, git_reference_name(tracking_branch));
130 131 132

	git_reference_free(tracking_branch);

133 134 135 136
	/* if it already existed, then the user's refspec created it for us, ignore it' */
	if (error == GIT_EEXISTS)
		error = 0;

137
	return error;
138 139
}

140 141
static int update_head_to_default(git_repository *repo)
{
142
	git_str initialbranch = GIT_STR_INIT;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	const char *branch_name;
	int error = 0;

	if ((error = git_repository_initialbranch(&initialbranch, repo)) < 0)
		goto done;

	if (git__prefixcmp(initialbranch.ptr, GIT_REFS_HEADS_DIR) != 0) {
		git_error_set(GIT_ERROR_INVALID, "invalid initial branch '%s'", initialbranch.ptr);
		error = -1;
		goto done;
	}

	branch_name = initialbranch.ptr + strlen(GIT_REFS_HEADS_DIR);

	error = setup_tracking_config(repo, branch_name, GIT_REMOTE_ORIGIN,
		initialbranch.ptr);

done:
161
	git_str_dispose(&initialbranch);
162 163 164
	return error;
}

165 166 167
static int update_remote_head(
	git_repository *repo,
	git_remote *remote,
168
	git_str *target,
169 170 171
	const char *reflog_message)
{
	git_refspec *refspec;
172
	git_reference *remote_head = NULL;
173 174
	git_str remote_head_name = GIT_STR_INIT;
	git_str remote_branch_name = GIT_STR_INIT;
175 176 177
	int error;

	/* Determine the remote tracking ref name from the local branch */
178
	refspec = git_remote__matching_refspec(remote, git_str_cstr(target));
179 180 181 182 183 184 185

	if (refspec == NULL) {
		git_error_set(GIT_ERROR_NET, "the remote's default branch does not fit the refspec configuration");
		error = GIT_EINVALIDSPEC;
		goto cleanup;
	}

186
	if ((error = git_refspec__transform(
187
		&remote_branch_name,
188
		refspec,
189
		git_str_cstr(target))) < 0)
190 191
		goto cleanup;

192
	if ((error = git_str_printf(&remote_head_name,
193 194
		"%s%s/%s",
		GIT_REFS_REMOTES_DIR,
195
		git_remote_name(remote),
196 197 198 199 200 201
		GIT_HEAD_FILE)) < 0)
		goto cleanup;

	error = git_reference_symbolic_create(
		&remote_head,
		repo,
202 203
		git_str_cstr(&remote_head_name),
		git_str_cstr(&remote_branch_name),
204
		true,
205 206 207
		reflog_message);

cleanup:
208
	git_reference_free(remote_head);
209 210
	git_str_dispose(&remote_branch_name);
	git_str_dispose(&remote_head_name);
211 212 213
	return error;
}

214 215 216 217
static int update_head_to_remote(
		git_repository *repo,
		git_remote *remote,
		const char *reflog_message)
218
{
219
	int error = 0;
220 221
	size_t refs_len;
	const git_remote_head *remote_head, **refs;
222
	const git_oid *remote_head_id;
223
	git_str branch = GIT_STR_INIT;
Ben Straub committed
224

225 226
	if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0)
		return error;
227

228 229
	/* We cloned an empty repository or one with an unborn HEAD */
	if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE))
230
		return update_head_to_default(repo);
231

232
	/* We know we have HEAD, let's see where it points */
233
	remote_head = refs[0];
Edward Thomson committed
234
	GIT_ASSERT(remote_head);
235

236
	remote_head_id = &remote_head->oid;
237

238
	error = git_remote__default_branch(&branch, remote);
239 240
	if (error == GIT_ENOTFOUND) {
		error = git_repository_set_head_detached(
241
			repo, remote_head_id);
242 243 244
		goto cleanup;
	}

245
	if ((error = update_remote_head(repo, remote, &branch, reflog_message)) < 0)
246
		goto cleanup;
247

248 249 250
	error = update_head_to_new_branch(
		repo,
		remote_head_id,
251
		git_str_cstr(&branch),
252
		reflog_message);
Ben Straub committed
253

254
cleanup:
255
	git_str_dispose(&branch);
256

257
	return error;
258 259
}

260 261
static int update_head_to_branch(
		git_repository *repo,
262
		git_remote *remote,
263 264
		const char *branch,
		const char *reflog_message)
265 266
{
	int retcode;
267
	git_str remote_branch_name = GIT_STR_INIT;
268
	git_reference *remote_ref = NULL;
269
	git_str default_branch = GIT_STR_INIT;
nulltoken committed
270

271
	GIT_ASSERT_ARG(remote);
Edward Thomson committed
272
	GIT_ASSERT_ARG(branch);
273

274
	if ((retcode = git_str_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s",
275
		git_remote_name(remote), branch)) < 0 )
276 277
		goto cleanup;

278
	if ((retcode = git_reference_lookup(&remote_ref, repo, git_str_cstr(&remote_branch_name))) < 0)
279 280
		goto cleanup;

281 282 283 284
	if ((retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch,
			reflog_message)) < 0)
		goto cleanup;

285
	if ((retcode = git_remote__default_branch(&default_branch, remote)) < 0)
286 287
		goto cleanup;

288
	if (!git_remote__matching_refspec(remote, git_str_cstr(&default_branch)))
289 290
		goto cleanup;

291
	retcode = update_remote_head(repo, remote, &default_branch, reflog_message);
292 293 294

cleanup:
	git_reference_free(remote_ref);
295 296
	git_str_dispose(&remote_branch_name);
	git_str_dispose(&default_branch);
297 298 299
	return retcode;
}

300 301 302 303 304 305 306
static int default_repository_create(git_repository **out, const char *path, int bare, void *payload)
{
	GIT_UNUSED(payload);

	return git_repository_init(out, path, bare);
}

307 308 309 310 311 312 313
static int default_remote_create(
		git_remote **out,
		git_repository *repo,
		const char *name,
		const char *url,
		void *payload)
{
314
	GIT_UNUSED(payload);
315

316
	return git_remote_create(out, repo, name, url);
317 318
}

319 320 321 322
/*
 * submodules?
 */

323 324 325 326 327 328 329
static int create_and_configure_origin(
		git_remote **out,
		git_repository *repo,
		const char *url,
		const git_clone_options *options)
{
	int error;
Ben Straub committed
330
	git_remote *origin = NULL;
331
	char buf[GIT_PATH_MAX];
332 333
	git_remote_create_cb remote_create = options->remote_cb;
	void *payload = options->remote_cb_payload;
334 335

	/* If the path exists and is a dir, the url should be the absolute path */
336
	if (git_fs_path_root(url) < 0 && git_fs_path_exists(url) && git_fs_path_isdir(url)) {
337 338 339 340 341
		if (p_realpath(url, buf) == NULL)
			return -1;

		url = buf;
	}
342

343 344
	if (!remote_create) {
		remote_create = default_remote_create;
345
		payload = NULL;
346
	}
347

348
	if ((error = remote_create(&origin, repo, "origin", url, payload)) < 0)
349 350 351 352 353 354
		goto on_error;

	*out = origin;
	return 0;

on_error:
Ben Straub committed
355
	git_remote_free(origin);
356 357
	return error;
}
358

359 360 361
static bool should_checkout(
	git_repository *repo,
	bool is_bare,
362
	const git_checkout_options *opts)
363 364 365 366 367 368 369
{
	if (is_bare)
		return false;

	if (!opts)
		return false;

370
	if (opts->checkout_strategy == GIT_CHECKOUT_NONE)
371 372
		return false;

373
	return !git_repository_head_unborn(repo);
374
}
Ben Straub committed
375

376
static int checkout_branch(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, const char *reflog_message)
377 378 379 380
{
	int error;

	if (branch)
381
		error = update_head_to_branch(repo, remote, branch, reflog_message);
382 383
	/* Point HEAD to the same ref as the remote's head */
	else
384
		error = update_head_to_remote(repo, remote, reflog_message);
385 386 387 388 389 390 391

	if (!error && should_checkout(repo, git_repository_is_bare(repo), co_opts))
		error = git_checkout_head(repo, co_opts);

	return error;
}

392
static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch_options *opts, const git_checkout_options *co_opts, const char *branch)
393
{
394
	int error;
395
	git_str reflog_message = GIT_STR_INIT;
396
	git_fetch_options fetch_opts;
397
	git_remote *remote;
398

Edward Thomson committed
399 400
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(_remote);
401 402

	if (!git_repository_is_empty(repo)) {
403
		git_error_set(GIT_ERROR_INVALID, "the repository is not empty");
404 405 406
		return -1;
	}

407
	if ((error = git_remote_dup(&remote, _remote)) < 0)
408 409
		return error;

410 411
	memcpy(&fetch_opts, opts, sizeof(git_fetch_options));
	fetch_opts.update_fetchhead = 0;
412
	fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
413
	git_str_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
414

415
	if ((error = git_remote_fetch(remote, NULL, &fetch_opts, git_str_cstr(&reflog_message))) != 0)
416 417
		goto cleanup;

418
	error = checkout_branch(repo, remote, co_opts, branch, git_str_cstr(&reflog_message));
419 420

cleanup:
421
	git_remote_free(remote);
422
	git_str_dispose(&reflog_message);
423 424 425 426

	return error;
}

427
int git_clone__should_clone_local(const char *url_or_path, git_clone_local_t local)
428
{
429
	git_str fromurl = GIT_STR_INIT;
430 431
	const char *path = url_or_path;
	bool is_url, is_local;
432 433

	if (local == GIT_CLONE_NO_LOCAL)
434
		return 0;
435

436 437
	if ((is_url = git_fs_path_is_local_file_url(url_or_path)) != 0) {
		if (git_fs_path_fromurl(&fromurl, url_or_path) < 0) {
438 439 440
			is_local = -1;
			goto done;
		}
441

442 443
		path = fromurl.ptr;
	}
444

445
	is_local = (!is_url || local != GIT_CLONE_LOCAL_AUTO) &&
446
		git_fs_path_isdir(path);
447

448
done:
449
	git_str_dispose(&fromurl);
450
	return is_local;
451 452
}

453
static int git__clone(
454
	git_repository **out,
455 456
	const char *url,
	const char *local_path,
457 458
	const git_clone_options *_options,
	int use_existing)
459
{
460
	int error = 0;
Ben Straub committed
461
	git_repository *repo = NULL;
462
	git_remote *origin;
463
	git_clone_options options = GIT_CLONE_OPTIONS_INIT;
464
	uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
465
	git_repository_create_cb repository_cb;
466

Edward Thomson committed
467 468 469
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(url);
	GIT_ASSERT_ARG(local_path);
Ben Straub committed
470

471 472 473
	if (_options)
		memcpy(&options, _options, sizeof(git_clone_options));

474
	GIT_ERROR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
475

476
	/* Only clone to a new directory or an empty directory */
477
	if (git_fs_path_exists(local_path) && !use_existing && !git_fs_path_is_empty_dir(local_path)) {
478
		git_error_set(GIT_ERROR_INVALID,
Russell Belfer committed
479
			"'%s' exists and is not an empty directory", local_path);
480
		return GIT_EEXISTS;
Ben Straub committed
481 482
	}

483
	/* Only remove the root directory on failure if we create it */
484
	if (git_fs_path_exists(local_path))
485
		rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
486

487 488 489 490 491 492
	if (options.repository_cb)
		repository_cb = options.repository_cb;
	else
		repository_cb = default_repository_create;

	if ((error = repository_cb(&repo, local_path, options.bare, options.repository_cb_payload)) < 0)
493
		return error;
494

495
	if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
496
		int clone_local = git_clone__should_clone_local(url, options.local);
497 498
		int link = options.local != GIT_CLONE_LOCAL_NO_LINKS;

499
		if (clone_local == 1)
500
			error = clone_local_into(
501
				repo, origin, &options.fetch_opts, &options.checkout_opts,
502
				options.checkout_branch, link);
503
		else if (clone_local == 0)
504
			error = clone_into(
505
				repo, origin, &options.fetch_opts, &options.checkout_opts,
506
				options.checkout_branch);
507 508
		else
			error = -1;
509

510 511
		git_remote_free(origin);
	}
512

513
	if (error != 0) {
514
		git_error_state last_error = {0};
515
		git_error_state_capture(&last_error, error);
516

517 518
		git_repository_free(repo);
		repo = NULL;
519

520
		(void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
521

522
		git_error_state_restore(&last_error);
523
	}
Ben Straub committed
524

525
	*out = repo;
526
	return error;
527
}
528

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
int git_clone(
	git_repository **out,
	const char *url,
	const char *local_path,
	const git_clone_options *_options)
{
	return git__clone(out, url, local_path, _options, 0);
}

int git_clone__submodule(
	git_repository **out,
	const char *url,
	const char *local_path,
	const git_clone_options *_options)
{
	return git__clone(out, url, local_path, _options, 1);
}

547
int git_clone_options_init(git_clone_options *opts, unsigned int version)
548
{
549 550 551
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT);
	return 0;
552
}
553

554
#ifndef GIT_DEPRECATE_HARD
555 556 557 558
int git_clone_init_options(git_clone_options *opts, unsigned int version)
{
	return git_clone_options_init(opts, version);
}
559
#endif
560

561 562 563
static bool can_link(const char *src, const char *dst, int link)
{
#ifdef GIT_WIN32
564 565 566
	GIT_UNUSED(src);
	GIT_UNUSED(dst);
	GIT_UNUSED(link);
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
	return false;
#else

	struct stat st_src, st_dst;

	if (!link)
		return false;

	if (p_stat(src, &st_src) < 0)
		return false;

	if (p_stat(dst, &st_dst) < 0)
		return false;

	return st_src.st_dev == st_dst.st_dev;
#endif
}

585
static int clone_local_into(git_repository *repo, git_remote *remote, const git_fetch_options *fetch_opts, const git_checkout_options *co_opts, const char *branch, int link)
586
{
587
	int error, flags;
588
	git_repository *src;
589 590
	git_str src_odb = GIT_STR_INIT, dst_odb = GIT_STR_INIT, src_path = GIT_STR_INIT;
	git_str reflog_message = GIT_STR_INIT;
591

Edward Thomson committed
592 593
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(remote);
594 595

	if (!git_repository_is_empty(repo)) {
596
		git_error_set(GIT_ERROR_INVALID, "the repository is not empty");
597 598 599 600 601 602 603 604
		return -1;
	}

	/*
	 * Let's figure out what path we should use for the source
	 * repo, if it's not rooted, the path should be relative to
	 * the repository's worktree/gitdir.
	 */
605
	if ((error = git_fs_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0)
606
		return error;
607 608

	/* Copy .git/objects/ from the source to the target */
609 610
	if ((error = git_repository_open(&src, git_str_cstr(&src_path))) < 0) {
		git_str_dispose(&src_path);
611 612 613
		return error;
	}

614 615
	if (git_repository__item_path(&src_odb, src, GIT_REPOSITORY_ITEM_OBJECTS) < 0 ||
	    git_repository__item_path(&dst_odb, repo, GIT_REPOSITORY_ITEM_OBJECTS) < 0) {
616 617 618 619
		error = -1;
		goto cleanup;
	}

620 621 622 623
	flags = 0;
	if (can_link(git_repository_path(src), git_repository_path(repo), link))
		flags |= GIT_CPDIR_LINK_FILES;

624
	error = git_futils_cp_r(git_str_cstr(&src_odb), git_str_cstr(&dst_odb),
625 626 627 628 629 630 631 632 633
				flags, GIT_OBJECT_DIR_MODE);

	/*
	 * can_link() doesn't catch all variations, so if we hit an
	 * error and did want to link, let's try again without trying
	 * to link.
	 */
	if (error < 0 && link) {
		flags &= ~GIT_CPDIR_LINK_FILES;
634
		error = git_futils_cp_r(git_str_cstr(&src_odb), git_str_cstr(&dst_odb),
635 636 637 638
					flags, GIT_OBJECT_DIR_MODE);
	}

	if (error < 0)
639 640
		goto cleanup;

641
	git_str_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
642

643
	if ((error = git_remote_fetch(remote, NULL, fetch_opts, git_str_cstr(&reflog_message))) != 0)
644 645
		goto cleanup;

646
	error = checkout_branch(repo, remote, co_opts, branch, git_str_cstr(&reflog_message));
647 648

cleanup:
649 650 651 652
	git_str_dispose(&reflog_message);
	git_str_dispose(&src_path);
	git_str_dispose(&src_odb);
	git_str_dispose(&dst_odb);
653 654 655
	git_repository_free(src);
	return error;
}