clone.c 13.5 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
#include <assert.h>
11

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

#include "remote.h"
#include "fileops.h"
23
#include "refs.h"
24
#include "path.h"
25
#include "repository.h"
26
#include "odb.h"
27

28
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);
29

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

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

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

50
	error = git_reference_create(&branch_ref, repo, git_buf_cstr(&refname), target, 0, log_message);
51
	git_buf_dispose(&refname);
Vicent Marti committed
52
	git_commit_free(head_obj);
53

54
	if (!error)
55 56 57 58
		*branch = branch_ref;
	else
		git_reference_free(branch_ref);

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	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;
	git_buf remote_key = GIT_BUF_INIT, merge_key = GIT_BUF_INIT;
	int error = -1;

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

	if (git_buf_printf(&remote_key, "branch.%s.remote", branch_name) < 0)
		goto cleanup;

	if (git_buf_printf(&merge_key, "branch.%s.merge", branch_name) < 0)
		goto cleanup;

	if (git_config_set_string(cfg, git_buf_cstr(&remote_key), remote_name) < 0)
		goto cleanup;

	if (git_config_set_string(cfg, git_buf_cstr(&merge_key), merge_target) < 0)
		goto cleanup;

	error = 0;

cleanup:
90 91
	git_buf_dispose(&remote_key);
	git_buf_dispose(&merge_key);
92 93 94 95 96 97 98
	return error;
}

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

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

	return setup_tracking_config(
		repo,
		branch_name,
		GIT_REMOTE_ORIGIN,
		git_reference_name(*branch));
112 113
}

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

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

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

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

	git_reference_free(tracking_branch);

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

139
	return error;
140 141
}

142 143 144 145
static int update_head_to_remote(
		git_repository *repo,
		git_remote *remote,
		const char *reflog_message)
146
{
147
	int error = 0;
148
	size_t refs_len;
149
	git_refspec *refspec;
150
	const git_remote_head *remote_head, **refs;
151
	const git_oid *remote_head_id;
152
	git_buf remote_master_name = GIT_BUF_INIT;
153
	git_buf branch = GIT_BUF_INIT;
Ben Straub committed
154

155 156
	if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0)
		return error;
157

158 159
	/* We cloned an empty repository or one with an unborn HEAD */
	if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE))
160
		return setup_tracking_config(
161
			repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE);
162

163
	/* We know we have HEAD, let's see where it points */
164
	remote_head = refs[0];
165 166
	assert(remote_head);

167
	remote_head_id = &remote_head->oid;
168 169 170 171

	error = git_remote_default_branch(&branch, remote);
	if (error == GIT_ENOTFOUND) {
		error = git_repository_set_head_detached(
172
			repo, remote_head_id);
173 174 175
		goto cleanup;
	}

176
	refspec = git_remote__matching_refspec(remote, git_buf_cstr(&branch));
177

178
	if (refspec == NULL) {
179 180 181
		giterr_set(GITERR_NET, "the remote's default branch does not fit the refspec configuration");
		error = GIT_EINVALIDSPEC;
		goto cleanup;
182
	}
183

184
	/* Determine the remote tracking reference name from the local master */
185
	if ((error = git_refspec_transform(
186
		&remote_master_name,
187
		refspec,
188
		git_buf_cstr(&branch))) < 0)
189
		goto cleanup;
190

191 192 193 194
	error = update_head_to_new_branch(
		repo,
		remote_head_id,
		git_buf_cstr(&branch),
195
		reflog_message);
Ben Straub committed
196

197
cleanup:
198 199
	git_buf_dispose(&remote_master_name);
	git_buf_dispose(&branch);
200

201
	return error;
202 203
}

204 205
static int update_head_to_branch(
		git_repository *repo,
206
		const char *remote_name,
207 208
		const char *branch,
		const char *reflog_message)
209 210 211 212
{
	int retcode;
	git_buf remote_branch_name = GIT_BUF_INIT;
	git_reference* remote_ref = NULL;
nulltoken committed
213

214
	assert(remote_name && branch);
215 216

	if ((retcode = git_buf_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s",
217
		remote_name, branch)) < 0 )
218 219 220 221 222
		goto cleanup;

	if ((retcode = git_reference_lookup(&remote_ref, repo, git_buf_cstr(&remote_branch_name))) < 0)
		goto cleanup;

223
	retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch,
224
			reflog_message);
225 226 227

cleanup:
	git_reference_free(remote_ref);
228
	git_buf_dispose(&remote_branch_name);
229 230 231
	return retcode;
}

232 233 234 235 236 237 238
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);
}

239 240 241 242 243 244 245
static int default_remote_create(
		git_remote **out,
		git_repository *repo,
		const char *name,
		const char *url,
		void *payload)
{
246
	GIT_UNUSED(payload);
247

248
	return git_remote_create(out, repo, name, url);
249 250
}

251 252 253 254
/*
 * submodules?
 */

255 256 257 258 259 260 261
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
262
	git_remote *origin = NULL;
263
	char buf[GIT_PATH_MAX];
264 265
	git_remote_create_cb remote_create = options->remote_cb;
	void *payload = options->remote_cb_payload;
266 267 268 269 270 271 272 273

	/* If the path exists and is a dir, the url should be the absolute path */
	if (git_path_root(url) < 0 && git_path_exists(url) && git_path_isdir(url)) {
		if (p_realpath(url, buf) == NULL)
			return -1;

		url = buf;
	}
274

275 276
	if (!remote_create) {
		remote_create = default_remote_create;
277
		payload = NULL;
278
	}
279

280
	if ((error = remote_create(&origin, repo, "origin", url, payload)) < 0)
281 282 283 284 285 286
		goto on_error;

	*out = origin;
	return 0;

on_error:
Ben Straub committed
287
	git_remote_free(origin);
288 289
	return error;
}
290

291 292 293
static bool should_checkout(
	git_repository *repo,
	bool is_bare,
294
	const git_checkout_options *opts)
295 296 297 298 299 300 301
{
	if (is_bare)
		return false;

	if (!opts)
		return false;

302
	if (opts->checkout_strategy == GIT_CHECKOUT_NONE)
303 304
		return false;

305
	return !git_repository_head_unborn(repo);
306
}
Ben Straub committed
307

308
static int checkout_branch(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, const char *reflog_message)
309 310 311 312 313
{
	int error;

	if (branch)
		error = update_head_to_branch(repo, git_remote_name(remote), branch,
314
				reflog_message);
315 316
	/* Point HEAD to the same ref as the remote's head */
	else
317
		error = update_head_to_remote(repo, remote, reflog_message);
318 319 320 321 322 323 324

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

	return error;
}

325
static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch_options *opts, const git_checkout_options *co_opts, const char *branch)
326
{
327
	int error;
328
	git_buf reflog_message = GIT_BUF_INIT;
329
	git_fetch_options fetch_opts;
330
	git_remote *remote;
331

332
	assert(repo && _remote);
333 334 335 336 337 338

	if (!git_repository_is_empty(repo)) {
		giterr_set(GITERR_INVALID, "the repository is not empty");
		return -1;
	}

339
	if ((error = git_remote_dup(&remote, _remote)) < 0)
340 341
		return error;

342 343
	memcpy(&fetch_opts, opts, sizeof(git_fetch_options));
	fetch_opts.update_fetchhead = 0;
344
	fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
345
	git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
346

347
	if ((error = git_remote_fetch(remote, NULL, &fetch_opts, git_buf_cstr(&reflog_message))) != 0)
348 349
		goto cleanup;

350
	error = checkout_branch(repo, remote, co_opts, branch, git_buf_cstr(&reflog_message));
351 352

cleanup:
353
	git_remote_free(remote);
354
	git_buf_dispose(&reflog_message);
355 356 357 358

	return error;
}

359
int git_clone__should_clone_local(const char *url_or_path, git_clone_local_t local)
360
{
361 362 363
	git_buf fromurl = GIT_BUF_INIT;
	const char *path = url_or_path;
	bool is_url, is_local;
364 365

	if (local == GIT_CLONE_NO_LOCAL)
366
		return 0;
367

368
	if ((is_url = git_path_is_local_file_url(url_or_path)) != 0) {
369 370 371 372
		if (git_path_fromurl(&fromurl, url_or_path) < 0) {
			is_local = -1;
			goto done;
		}
373

374 375
		path = fromurl.ptr;
	}
376

377 378
	is_local = (!is_url || local != GIT_CLONE_LOCAL_AUTO) &&
		git_path_isdir(path);
379

380
done:
381
	git_buf_dispose(&fromurl);
382
	return is_local;
383 384
}

385
int git_clone(
386
	git_repository **out,
387 388
	const char *url,
	const char *local_path,
389
	const git_clone_options *_options)
390
{
391
	int error = 0;
Ben Straub committed
392
	git_repository *repo = NULL;
393
	git_remote *origin;
394
	git_clone_options options = GIT_CLONE_OPTIONS_INIT;
395
	uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
396
	git_repository_create_cb repository_cb;
397 398

	assert(out && url && local_path);
Ben Straub committed
399

400 401 402 403
	if (_options)
		memcpy(&options, _options, sizeof(git_clone_options));

	GITERR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
404

405 406 407
	/* Only clone to a new directory or an empty directory */
	if (git_path_exists(local_path) && !git_path_is_empty_dir(local_path)) {
		giterr_set(GITERR_INVALID,
Russell Belfer committed
408
			"'%s' exists and is not an empty directory", local_path);
409
		return GIT_EEXISTS;
Ben Straub committed
410 411
	}

412 413 414
	/* Only remove the root directory on failure if we create it */
	if (git_path_exists(local_path))
		rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
415

416 417 418 419 420 421
	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)
422
		return error;
423

424
	if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
425
		int clone_local = git_clone__should_clone_local(url, options.local);
426 427
		int link = options.local != GIT_CLONE_LOCAL_NO_LINKS;

428
		if (clone_local == 1)
429
			error = clone_local_into(
430
				repo, origin, &options.fetch_opts, &options.checkout_opts,
431
				options.checkout_branch, link);
432
		else if (clone_local == 0)
433
			error = clone_into(
434
				repo, origin, &options.fetch_opts, &options.checkout_opts,
435
				options.checkout_branch);
436 437
		else
			error = -1;
438

439 440
		git_remote_free(origin);
	}
441

442
	if (error != 0) {
443
		git_error_state last_error = {0};
444
		giterr_state_capture(&last_error, error);
445

446 447
		git_repository_free(repo);
		repo = NULL;
448

449
		(void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
450

451
		giterr_state_restore(&last_error);
452
	}
Ben Straub committed
453

454
	*out = repo;
455
	return error;
456
}
457

458
int git_clone_init_options(git_clone_options *opts, unsigned int version)
459
{
460 461 462
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT);
	return 0;
463
}
464

465 466 467
static bool can_link(const char *src, const char *dst, int link)
{
#ifdef GIT_WIN32
468 469 470
	GIT_UNUSED(src);
	GIT_UNUSED(dst);
	GIT_UNUSED(link);
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
	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
}

489
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)
490
{
491
	int error, flags;
492 493 494 495
	git_repository *src;
	git_buf src_odb = GIT_BUF_INIT, dst_odb = GIT_BUF_INIT, src_path = GIT_BUF_INIT;
	git_buf reflog_message = GIT_BUF_INIT;

496
	assert(repo && remote);
497 498 499 500 501 502 503 504 505 506 507

	if (!git_repository_is_empty(repo)) {
		giterr_set(GITERR_INVALID, "the repository is not empty");
		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.
	 */
508 509
	if ((error = git_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0)
		return error;
510 511 512

	/* Copy .git/objects/ from the source to the target */
	if ((error = git_repository_open(&src, git_buf_cstr(&src_path))) < 0) {
513
		git_buf_dispose(&src_path);
514 515 516
		return error;
	}

517 518
	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) {
519 520 521 522
		error = -1;
		goto cleanup;
	}

523 524 525 526
	flags = 0;
	if (can_link(git_repository_path(src), git_repository_path(repo), link))
		flags |= GIT_CPDIR_LINK_FILES;

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	error = git_futils_cp_r(git_buf_cstr(&src_odb), git_buf_cstr(&dst_odb),
				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;
		error = git_futils_cp_r(git_buf_cstr(&src_odb), git_buf_cstr(&dst_odb),
					flags, GIT_OBJECT_DIR_MODE);
	}

	if (error < 0)
542 543 544 545
		goto cleanup;

	git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));

546
	if ((error = git_remote_fetch(remote, NULL, fetch_opts, git_buf_cstr(&reflog_message))) != 0)
547 548
		goto cleanup;

549
	error = checkout_branch(repo, remote, co_opts, branch, git_buf_cstr(&reflog_message));
550 551

cleanup:
552 553 554 555
	git_buf_dispose(&reflog_message);
	git_buf_dispose(&src_path);
	git_buf_dispose(&src_odb);
	git_buf_dispose(&dst_odb);
556 557 558
	git_repository_free(src);
	return error;
}