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

#include <assert.h>
9

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 20 21

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

27 28
static int clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link, const git_signature *signature);

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

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

	/* Create the new branch */
46
	error = git_branch_create(&branch_ref, repo, name, head_obj, 0, signature, log_message);
Ben Straub committed
47

Vicent Marti committed
48
	git_commit_free(head_obj);
49

50
	if (!error)
51 52 53 54
		*branch = branch_ref;
	else
		git_reference_free(branch_ref);

55 56 57 58 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 90 91 92 93 94
	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:
	git_buf_free(&remote_key);
	git_buf_free(&merge_key);
	return error;
}

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

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

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

111 112 113
static int update_head_to_new_branch(
	git_repository *repo,
	const git_oid *target,
114
	const char *name,
115
	const git_signature *signature,
116
	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
			signature, reflog_message);
Ben Straub committed
126

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

	git_reference_free(tracking_branch);

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

138
	return error;
139 140
}

141 142 143
static int update_head_to_remote(
		git_repository *repo,
		git_remote *remote,
144
		const git_signature *signature,
145
		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 172 173 174 175

	error = git_remote_default_branch(&branch, remote);
	if (error == GIT_ENOTFOUND) {
		error = git_repository_set_head_detached(
			repo, remote_head_id, signature, reflog_message);
		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 195
	error = update_head_to_new_branch(
		repo,
		remote_head_id,
		git_buf_cstr(&branch),
		signature, reflog_message);
Ben Straub committed
196

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

201
	return error;
202 203
}

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

215
	assert(remote_name && branch);
216 217

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

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

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

cleanup:
	git_reference_free(remote_ref);
Sascha Cunz committed
229
	git_buf_free(&remote_branch_name);
230 231 232
	return retcode;
}

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

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static int default_remote_create(
		git_remote **out,
		git_repository *repo,
		const char *name,
		const char *url,
		void *payload)
{
	int error;
	git_remote_callbacks *callbacks = payload;

	if ((error = git_remote_create(out, repo, name, url)) < 0)
		return error;

	return git_remote_set_callbacks(*out, callbacks);
}

256 257 258 259
/*
 * submodules?
 */

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

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

280 281 282 283
	if (!remote_create) {
		remote_create = default_remote_create;
		payload = (void *)&options->remote_callbacks;
	}
284

285
	if ((error = remote_create(&origin, repo, "origin", url, payload)) < 0)
286 287
		goto on_error;

Ben Straub committed
288 289 290
	if ((error = git_remote_save(origin)) < 0)
		goto on_error;

291 292 293 294
	*out = origin;
	return 0;

on_error:
Ben Straub committed
295
	git_remote_free(origin);
296 297
	return error;
}
298

299 300 301
static bool should_checkout(
	git_repository *repo,
	bool is_bare,
302
	const git_checkout_options *opts)
303 304 305 306 307 308 309
{
	if (is_bare)
		return false;

	if (!opts)
		return false;

310
	if (opts->checkout_strategy == GIT_CHECKOUT_NONE)
311 312
		return false;

313
	return !git_repository_head_unborn(repo);
314
}
Ben Straub committed
315

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
static int checkout_branch(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, const git_signature *signature, const char *reflog_message)
{
	int error;

	if (branch)
		error = update_head_to_branch(repo, git_remote_name(remote), branch,
				signature, reflog_message);
	/* Point HEAD to the same ref as the remote's head */
	else
		error = update_head_to_remote(repo, remote, signature, reflog_message);

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

	return error;
}

333
static int clone_into(git_repository *repo, git_remote *_remote, const git_checkout_options *co_opts, const char *branch, const git_signature *signature)
334
{
335
	int error;
336
	git_buf reflog_message = GIT_BUF_INIT;
337 338
	git_remote *remote;
	const git_remote_callbacks *callbacks;
339

340
	assert(repo && _remote);
341 342 343 344 345 346

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

347
	if ((error = git_remote_dup(&remote, _remote)) < 0)
348 349
		return error;

350 351
	callbacks = git_remote_get_callbacks(_remote);
	if (!giterr__check_version(callbacks, 1, "git_remote_callbacks") &&
352
	    (error = git_remote_set_callbacks(remote, callbacks)) < 0)
353 354
		goto cleanup;

355
	if ((error = git_remote_add_fetch(remote, "refs/tags/*:refs/tags/*")) < 0)
356
		goto cleanup;
357 358

	git_remote_set_update_fetchhead(remote, 0);
359
	git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
360

361
	if ((error = git_remote_fetch(remote, NULL, signature, git_buf_cstr(&reflog_message))) != 0)
362 363
		goto cleanup;

364
	error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message));
365 366

cleanup:
367
	git_remote_free(remote);
368
	git_buf_free(&reflog_message);
369 370 371 372

	return error;
}

373
int git_clone__should_clone_local(const char *url_or_path, git_clone_local_t local)
374
{
375 376 377
	git_buf fromurl = GIT_BUF_INIT;
	const char *path = url_or_path;
	bool is_url, is_local;
378 379

	if (local == GIT_CLONE_NO_LOCAL)
380
		return 0;
381

382
	if ((is_url = git_path_is_local_file_url(url_or_path)) != 0) {
383 384 385 386
		if (git_path_fromurl(&fromurl, url_or_path) < 0) {
			is_local = -1;
			goto done;
		}
387

388 389
		path = fromurl.ptr;
	}
390

391 392
	is_local = (!is_url || local != GIT_CLONE_LOCAL_AUTO) &&
		git_path_isdir(path);
393

394 395 396
done:
	git_buf_free(&fromurl);
	return is_local;
397 398
}

399
int git_clone(
400
	git_repository **out,
401 402
	const char *url,
	const char *local_path,
403
	const git_clone_options *_options)
404
{
405
	int error = 0;
Ben Straub committed
406
	git_repository *repo = NULL;
407
	git_remote *origin;
408
	git_clone_options options = GIT_CLONE_OPTIONS_INIT;
409
	uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
410
	git_repository_create_cb repository_cb;
411 412

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

414 415 416 417
	if (_options)
		memcpy(&options, _options, sizeof(git_clone_options));

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

419 420 421
	/* 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
422
			"'%s' exists and is not an empty directory", local_path);
423
		return GIT_EEXISTS;
Ben Straub committed
424 425
	}

426 427 428
	/* Only remove the root directory on failure if we create it */
	if (git_path_exists(local_path))
		rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
429

430 431 432 433 434 435
	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)
436
		return error;
437

438
	if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
439
		int clone_local = git_clone__should_clone_local(url, options.local);
440 441
		int link = options.local != GIT_CLONE_LOCAL_NO_LINKS;

442
		if (clone_local == 1)
443
			error = clone_local_into(
444
				repo, origin, &options.checkout_opts,
445
				options.checkout_branch, link, options.signature);
446
		else if (clone_local == 0)
447
			error = clone_into(
448 449
				repo, origin, &options.checkout_opts,
				options.checkout_branch, options.signature);
450 451
		else
			error = -1;
452

453 454
		git_remote_free(origin);
	}
455

456
	if (error != 0) {
457 458 459
		git_error_state last_error = {0};
		giterr_capture(&last_error, error);

460 461
		git_repository_free(repo);
		repo = NULL;
462

463
		(void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
464 465

		giterr_restore(&last_error);
466
	}
Ben Straub committed
467

468
	*out = repo;
469
	return error;
470
}
471

472
int git_clone_init_options(git_clone_options *opts, unsigned int version)
473
{
474 475 476
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT);
	return 0;
477
}
478 479 480 481 482 483 484 485 486

static const char *repository_base(git_repository *repo)
{
	if (git_repository_is_bare(repo))
		return git_repository_path(repo);

	return git_repository_workdir(repo);
}

487 488 489
static bool can_link(const char *src, const char *dst, int link)
{
#ifdef GIT_WIN32
490 491 492
	GIT_UNUSED(src);
	GIT_UNUSED(dst);
	GIT_UNUSED(link);
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	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
}

511
static int clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link, const git_signature *signature)
512
{
513
	int error, flags;
514 515 516 517
	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;

518
	assert(repo && remote);
519 520 521 522 523 524 525 526 527 528 529

	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.
	 */
530 531
	if ((error = git_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0)
		return error;
532 533 534 535 536 537 538 539 540 541 542 543 544 545

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

	git_buf_joinpath(&src_odb, git_repository_path(src), GIT_OBJECTS_DIR);
	git_buf_joinpath(&dst_odb, git_repository_path(repo), GIT_OBJECTS_DIR);
	if (git_buf_oom(&src_odb) || git_buf_oom(&dst_odb)) {
		error = -1;
		goto cleanup;
	}

546 547 548 549
	flags = 0;
	if (can_link(git_repository_path(src), git_repository_path(repo), link))
		flags |= GIT_CPDIR_LINK_FILES;

550
	if ((error = git_futils_cp_r(git_buf_cstr(&src_odb), git_buf_cstr(&dst_odb),
551
				     flags, GIT_OBJECT_DIR_MODE)) < 0)
552 553 554 555
		goto cleanup;

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

556
	if ((error = git_remote_fetch(remote, NULL, signature, git_buf_cstr(&reflog_message))) != 0)
557 558 559 560 561 562 563 564 565 566 567 568
		goto cleanup;

	error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message));

cleanup:
	git_buf_free(&reflog_message);
	git_buf_free(&src_path);
	git_buf_free(&src_odb);
	git_buf_free(&dst_odb);
	git_repository_free(src);
	return error;
}