clone.c 12.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
static int create_branch(
28 29 30
	git_reference **branch,
	git_repository *repo,
	const git_oid *target,
31 32 33
	const char *name,
	const git_signature *signature,
	const char *log_message)
34
{
Vicent Marti committed
35
	git_commit *head_obj = NULL;
36
	git_reference *branch_ref = NULL;
37
	int error;
Ben Straub committed
38 39

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

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

Vicent Marti committed
46
	git_commit_free(head_obj);
47

48
	if (!error)
49 50 51 52
		*branch = branch_ref;
	else
		git_reference_free(branch_ref);

53 54 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
	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,
93 94 95
	const char *branch_name,
	const git_signature *signature,
	const char *log_message)
96 97 98
{
	int error;

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

	return setup_tracking_config(
		repo,
		branch_name,
		GIT_REMOTE_ORIGIN,
		git_reference_name(*branch));
107 108
}

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

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

122
	error = create_tracking_branch(&tracking_branch, repo, target, name,
123
			signature, reflog_message);
Ben Straub committed
124

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

	git_reference_free(tracking_branch);

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

136
	return error;
137 138
}

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

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

156
	/* Did we just clone an empty repository? */
157
	if (refs_len == 0)
158
		return setup_tracking_config(
159
			repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE);
160

161 162 163 164
	error = git_remote_default_branch(&branch, remote);
	if (error == GIT_ENOTFOUND) {
		git_buf_puts(&branch, GIT_REFS_HEADS_MASTER_FILE);
	} else {
165
		found_branch = 1;
166 167
	}

168 169
	/* Get the remote's HEAD. This is always the first ref in the list. */
	remote_head = refs[0];
170 171
	assert(remote_head);

172 173
	remote_head_id = &remote_head->oid;
	refspec = git_remote__matching_refspec(remote, git_buf_cstr(&branch));
174

175
	if (refspec == NULL) {
176
		memset(&dummy_spec, 0, sizeof(git_refspec));
177
		refspec = &dummy_spec;
178
	}
179

180
	/* Determine the remote tracking reference name from the local master */
181
	if ((error = git_refspec_transform(
182
		&remote_master_name,
183
		refspec,
184
		git_buf_cstr(&branch))) < 0)
185
		return error;
186

187
	if (found_branch) {
188
		error = update_head_to_new_branch(
189
			repo,
190
			remote_head_id,
191
			git_buf_cstr(&branch),
192
			signature, reflog_message);
193
	} else {
194
		error = git_repository_set_head_detached(
195
			repo, remote_head_id, signature, reflog_message);
Ben Straub committed
196 197
	}

198
	git_buf_free(&remote_master_name);
199
	git_buf_free(&branch);
200
	return error;
201 202
}

203 204
static int update_head_to_branch(
		git_repository *repo,
205
		const char *remote_name,
206
		const char *branch,
207
		const git_signature *signature,
208
		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 224
	retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch,
			signature, reflog_message);
225 226 227

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

232 233 234 235
/*
 * submodules?
 */

236 237 238 239 240 241 242
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
243
	git_remote *origin = NULL;
244
	const char *name;
245 246 247 248 249 250 251 252 253
	char buf[GIT_PATH_MAX];

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

255 256
	name = options->remote_name ? options->remote_name : "origin";
	if ((error = git_remote_create(&origin, repo, name, url)) < 0)
257 258
		goto on_error;

259 260 261
	if (options->ignore_cert_errors)
		git_remote_check_cert(origin, 0);

262
	if ((error = git_remote_set_callbacks(origin, &options->remote_callbacks)) < 0)
263 264
		goto on_error;

Ben Straub committed
265 266 267
	if ((error = git_remote_save(origin)) < 0)
		goto on_error;

268 269 270 271
	*out = origin;
	return 0;

on_error:
Ben Straub committed
272
	git_remote_free(origin);
273 274
	return error;
}
275

276 277 278
static bool should_checkout(
	git_repository *repo,
	bool is_bare,
279
	const git_checkout_options *opts)
280 281 282 283 284 285 286
{
	if (is_bare)
		return false;

	if (!opts)
		return false;

287
	if (opts->checkout_strategy == GIT_CHECKOUT_NONE)
288 289
		return false;

290
	return !git_repository_head_unborn(repo);
291
}
Ben Straub committed
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
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;
}

310
int git_clone_into(git_repository *repo, git_remote *_remote, const git_checkout_options *co_opts, const char *branch, const git_signature *signature)
311
{
312
	int error;
313
	git_buf reflog_message = GIT_BUF_INIT;
314 315
	git_remote *remote;
	const git_remote_callbacks *callbacks;
316

317
	assert(repo && _remote);
318 319 320 321 322 323

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

324
	if ((error = git_remote_dup(&remote, _remote)) < 0)
325 326
		return error;

327 328
	callbacks = git_remote_get_callbacks(_remote);
	if (!giterr__check_version(callbacks, 1, "git_remote_callbacks") &&
329
	    (error = git_remote_set_callbacks(remote, callbacks)) < 0)
330 331
		goto cleanup;

332
	if ((error = git_remote_add_fetch(remote, "refs/tags/*:refs/tags/*")) < 0)
333
		goto cleanup;
334 335

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

338
	if ((error = git_remote_fetch(remote, signature, git_buf_cstr(&reflog_message))) != 0)
339 340
		goto cleanup;

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

cleanup:
344
	git_remote_free(remote);
345
	git_buf_free(&reflog_message);
346 347 348 349

	return error;
}

350 351 352 353 354 355 356 357 358 359
int git_clone__should_clone_local(const char *url, git_clone_local_t local)
{
	const char *path;
	int is_url;

	if (local == GIT_CLONE_NO_LOCAL)
		return false;

	is_url = !git__prefixcmp(url, "file://");

360
	if (is_url && local != GIT_CLONE_LOCAL && local != GIT_CLONE_LOCAL_NO_LINKS )
361 362 363 364 365 366 367 368 369 370 371 372
		return false;

	path = url;
	if (is_url)
		path = url + strlen("file://");

	if ((git_path_exists(path) && git_path_isdir(path)) && local != GIT_CLONE_NO_LOCAL)
		return true;

	return false;
}

373
int git_clone(
374
	git_repository **out,
375 376
	const char *url,
	const char *local_path,
377
	const git_clone_options *_options)
378
{
379
	int error = 0;
Ben Straub committed
380
	git_repository *repo = NULL;
381
	git_remote *origin;
382
	git_clone_options options = GIT_CLONE_OPTIONS_INIT;
383
	uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
384 385

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

387 388 389 390
	if (_options)
		memcpy(&options, _options, sizeof(git_clone_options));

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

392 393 394
	/* 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
395
			"'%s' exists and is not an empty directory", local_path);
396
		return GIT_EEXISTS;
Ben Straub committed
397 398
	}

399 400 401
	/* Only remove the root directory on failure if we create it */
	if (git_path_exists(local_path))
		rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
402

403 404
	if ((error = git_repository_init(&repo, local_path, options.bare)) < 0)
		return error;
405

406
	if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
407
		if (git_clone__should_clone_local(url, options.local)) {
408
			int link = options.local != GIT_CLONE_LOCAL_NO_LINKS;
409 410
			error = git_clone_local_into(
				repo, origin, &options.checkout_opts,
411
				options.checkout_branch, link, options.signature);
412 413 414 415 416
		} else {
			error = git_clone_into(
				repo, origin, &options.checkout_opts,
				options.checkout_branch, options.signature);
		}
417

418 419
		git_remote_free(origin);
	}
420

421
	if (error != 0) {
422 423 424
		git_error_state last_error = {0};
		giterr_capture(&last_error, error);

425 426
		git_repository_free(repo);
		repo = NULL;
427

428
		(void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
429 430

		giterr_restore(&last_error);
431
	}
Ben Straub committed
432

433
	*out = repo;
434
	return error;
435
}
436

437
int git_clone_init_options(git_clone_options *opts, unsigned int version)
438
{
439 440 441
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT);
	return 0;
442
}
443 444 445 446 447 448 449 450 451

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

	return git_repository_workdir(repo);
}

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
static bool can_link(const char *src, const char *dst, int link)
{
#ifdef GIT_WIN32
	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
}

int git_clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link, const git_signature *signature)
474
{
475
	int error, flags;
476 477 478 479
	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;

480
	assert(repo && remote);
481 482 483 484 485 486 487 488 489 490 491

	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.
	 */
492 493
	if ((error = git_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0)
		return error;
494 495 496 497 498 499 500 501 502 503 504 505 506 507

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

508 509 510 511
	flags = 0;
	if (can_link(git_repository_path(src), git_repository_path(repo), link))
		flags |= GIT_CPDIR_LINK_FILES;

512
	if ((error = git_futils_cp_r(git_buf_cstr(&src_odb), git_buf_cstr(&dst_odb),
513
				     flags, GIT_OBJECT_DIR_MODE)) < 0)
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
		goto cleanup;

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

	if ((error = git_remote_fetch(remote, signature, git_buf_cstr(&reflog_message))) != 0)
		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;
}