1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 
8 #include "clone.h"
9 
10 #include "git2/clone.h"
11 #include "git2/remote.h"
12 #include "git2/revparse.h"
13 #include "git2/branch.h"
14 #include "git2/config.h"
15 #include "git2/checkout.h"
16 #include "git2/commit.h"
17 #include "git2/tree.h"
18 
19 #include "remote.h"
20 #include "futils.h"
21 #include "refs.h"
22 #include "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 
create_branch(git_reference ** branch,git_repository * repo,const git_oid * target,const char * name,const char * log_message)28 static int create_branch(
29 	git_reference **branch,
30 	git_repository *repo,
31 	const git_oid *target,
32 	const char *name,
33 	const char *log_message)
34 {
35 	git_commit *head_obj = NULL;
36 	git_reference *branch_ref = NULL;
37 	git_buf refname = GIT_BUF_INIT;
38 	int error;
39 
40 	/* Find the target commit */
41 	if ((error = git_commit_lookup(&head_obj, repo, target)) < 0)
42 		return error;
43 
44 	/* Create the new branch */
45 	if ((error = git_buf_printf(&refname, GIT_REFS_HEADS_DIR "%s", name)) < 0)
46 		return error;
47 
48 	error = git_reference_create(&branch_ref, repo, git_buf_cstr(&refname), target, 0, log_message);
49 	git_buf_dispose(&refname);
50 	git_commit_free(head_obj);
51 
52 	if (!error)
53 		*branch = branch_ref;
54 	else
55 		git_reference_free(branch_ref);
56 
57 	return error;
58 }
59 
setup_tracking_config(git_repository * repo,const char * branch_name,const char * remote_name,const char * merge_target)60 static int setup_tracking_config(
61 	git_repository *repo,
62 	const char *branch_name,
63 	const char *remote_name,
64 	const char *merge_target)
65 {
66 	git_config *cfg;
67 	git_buf remote_key = GIT_BUF_INIT, merge_key = GIT_BUF_INIT;
68 	int error = -1;
69 
70 	if (git_repository_config__weakptr(&cfg, repo) < 0)
71 		return -1;
72 
73 	if (git_buf_printf(&remote_key, "branch.%s.remote", branch_name) < 0)
74 		goto cleanup;
75 
76 	if (git_buf_printf(&merge_key, "branch.%s.merge", branch_name) < 0)
77 		goto cleanup;
78 
79 	if (git_config_set_string(cfg, git_buf_cstr(&remote_key), remote_name) < 0)
80 		goto cleanup;
81 
82 	if (git_config_set_string(cfg, git_buf_cstr(&merge_key), merge_target) < 0)
83 		goto cleanup;
84 
85 	error = 0;
86 
87 cleanup:
88 	git_buf_dispose(&remote_key);
89 	git_buf_dispose(&merge_key);
90 	return error;
91 }
92 
create_tracking_branch(git_reference ** branch,git_repository * repo,const git_oid * target,const char * branch_name,const char * log_message)93 static int create_tracking_branch(
94 	git_reference **branch,
95 	git_repository *repo,
96 	const git_oid *target,
97 	const char *branch_name,
98 	const char *log_message)
99 {
100 	int error;
101 
102 	if ((error = create_branch(branch, repo, target, branch_name, log_message)) < 0)
103 		return error;
104 
105 	return setup_tracking_config(
106 		repo,
107 		branch_name,
108 		GIT_REMOTE_ORIGIN,
109 		git_reference_name(*branch));
110 }
111 
update_head_to_new_branch(git_repository * repo,const git_oid * target,const char * name,const char * reflog_message)112 static int update_head_to_new_branch(
113 	git_repository *repo,
114 	const git_oid *target,
115 	const char *name,
116 	const char *reflog_message)
117 {
118 	git_reference *tracking_branch = NULL;
119 	int error;
120 
121 	if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR))
122 		name += strlen(GIT_REFS_HEADS_DIR);
123 
124 	error = create_tracking_branch(&tracking_branch, repo, target, name,
125 			reflog_message);
126 
127 	if (!error)
128 		error = git_repository_set_head(
129 			repo, git_reference_name(tracking_branch));
130 
131 	git_reference_free(tracking_branch);
132 
133 	/* if it already existed, then the user's refspec created it for us, ignore it' */
134 	if (error == GIT_EEXISTS)
135 		error = 0;
136 
137 	return error;
138 }
139 
update_head_to_remote(git_repository * repo,git_remote * remote,const char * reflog_message)140 static int update_head_to_remote(
141 		git_repository *repo,
142 		git_remote *remote,
143 		const char *reflog_message)
144 {
145 	int error = 0;
146 	size_t refs_len;
147 	git_refspec *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;
152 
153 	if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0)
154 		return error;
155 
156 	/* We cloned an empty repository or one with an unborn HEAD */
157 	if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE))
158 		return setup_tracking_config(
159 			repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE);
160 
161 	/* We know we have HEAD, let's see where it points */
162 	remote_head = refs[0];
163 	assert(remote_head);
164 
165 	remote_head_id = &remote_head->oid;
166 
167 	error = git_remote_default_branch(&branch, remote);
168 	if (error == GIT_ENOTFOUND) {
169 		error = git_repository_set_head_detached(
170 			repo, remote_head_id);
171 		goto cleanup;
172 	}
173 
174 	refspec = git_remote__matching_refspec(remote, git_buf_cstr(&branch));
175 
176 	if (refspec == NULL) {
177 		git_error_set(GIT_ERROR_NET, "the remote's default branch does not fit the refspec configuration");
178 		error = GIT_EINVALIDSPEC;
179 		goto cleanup;
180 	}
181 
182 	/* Determine the remote tracking reference name from the local master */
183 	if ((error = git_refspec_transform(
184 		&remote_master_name,
185 		refspec,
186 		git_buf_cstr(&branch))) < 0)
187 		goto cleanup;
188 
189 	error = update_head_to_new_branch(
190 		repo,
191 		remote_head_id,
192 		git_buf_cstr(&branch),
193 		reflog_message);
194 
195 cleanup:
196 	git_buf_dispose(&remote_master_name);
197 	git_buf_dispose(&branch);
198 
199 	return error;
200 }
201 
update_head_to_branch(git_repository * repo,const char * remote_name,const char * branch,const char * reflog_message)202 static int update_head_to_branch(
203 		git_repository *repo,
204 		const char *remote_name,
205 		const char *branch,
206 		const char *reflog_message)
207 {
208 	int retcode;
209 	git_buf remote_branch_name = GIT_BUF_INIT;
210 	git_reference* remote_ref = NULL;
211 
212 	assert(remote_name && branch);
213 
214 	if ((retcode = git_buf_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s",
215 		remote_name, branch)) < 0 )
216 		goto cleanup;
217 
218 	if ((retcode = git_reference_lookup(&remote_ref, repo, git_buf_cstr(&remote_branch_name))) < 0)
219 		goto cleanup;
220 
221 	retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch,
222 			reflog_message);
223 
224 cleanup:
225 	git_reference_free(remote_ref);
226 	git_buf_dispose(&remote_branch_name);
227 	return retcode;
228 }
229 
default_repository_create(git_repository ** out,const char * path,int bare,void * payload)230 static int default_repository_create(git_repository **out, const char *path, int bare, void *payload)
231 {
232 	GIT_UNUSED(payload);
233 
234 	return git_repository_init(out, path, bare);
235 }
236 
default_remote_create(git_remote ** out,git_repository * repo,const char * name,const char * url,void * payload)237 static int default_remote_create(
238 		git_remote **out,
239 		git_repository *repo,
240 		const char *name,
241 		const char *url,
242 		void *payload)
243 {
244 	GIT_UNUSED(payload);
245 
246 	return git_remote_create(out, repo, name, url);
247 }
248 
249 /*
250  * submodules?
251  */
252 
create_and_configure_origin(git_remote ** out,git_repository * repo,const char * url,const git_clone_options * options)253 static int create_and_configure_origin(
254 		git_remote **out,
255 		git_repository *repo,
256 		const char *url,
257 		const git_clone_options *options)
258 {
259 	int error;
260 	git_remote *origin = NULL;
261 	char buf[GIT_PATH_MAX];
262 	git_remote_create_cb remote_create = options->remote_cb;
263 	void *payload = options->remote_cb_payload;
264 
265 	/* If the path exists and is a dir, the url should be the absolute path */
266 	if (git_path_root(url) < 0 && git_path_exists(url) && git_path_isdir(url)) {
267 		if (p_realpath(url, buf) == NULL)
268 			return -1;
269 
270 		url = buf;
271 	}
272 
273 	if (!remote_create) {
274 		remote_create = default_remote_create;
275 		payload = NULL;
276 	}
277 
278 	if ((error = remote_create(&origin, repo, "origin", url, payload)) < 0)
279 		goto on_error;
280 
281 	*out = origin;
282 	return 0;
283 
284 on_error:
285 	git_remote_free(origin);
286 	return error;
287 }
288 
should_checkout(git_repository * repo,bool is_bare,const git_checkout_options * opts)289 static bool should_checkout(
290 	git_repository *repo,
291 	bool is_bare,
292 	const git_checkout_options *opts)
293 {
294 	if (is_bare)
295 		return false;
296 
297 	if (!opts)
298 		return false;
299 
300 	if (opts->checkout_strategy == GIT_CHECKOUT_NONE)
301 		return false;
302 
303 	return !git_repository_head_unborn(repo);
304 }
305 
checkout_branch(git_repository * repo,git_remote * remote,const git_checkout_options * co_opts,const char * branch,const char * reflog_message)306 static int checkout_branch(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, const char *reflog_message)
307 {
308 	int error;
309 
310 	if (branch)
311 		error = update_head_to_branch(repo, git_remote_name(remote), branch,
312 				reflog_message);
313 	/* Point HEAD to the same ref as the remote's head */
314 	else
315 		error = update_head_to_remote(repo, remote, reflog_message);
316 
317 	if (!error && should_checkout(repo, git_repository_is_bare(repo), co_opts))
318 		error = git_checkout_head(repo, co_opts);
319 
320 	return error;
321 }
322 
clone_into(git_repository * repo,git_remote * _remote,const git_fetch_options * opts,const git_checkout_options * co_opts,const char * branch)323 static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch_options *opts, const git_checkout_options *co_opts, const char *branch)
324 {
325 	int error;
326 	git_buf reflog_message = GIT_BUF_INIT;
327 	git_fetch_options fetch_opts;
328 	git_remote *remote;
329 
330 	assert(repo && _remote);
331 
332 	if (!git_repository_is_empty(repo)) {
333 		git_error_set(GIT_ERROR_INVALID, "the repository is not empty");
334 		return -1;
335 	}
336 
337 	if ((error = git_remote_dup(&remote, _remote)) < 0)
338 		return error;
339 
340 	memcpy(&fetch_opts, opts, sizeof(git_fetch_options));
341 	fetch_opts.update_fetchhead = 0;
342 	fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
343 	git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
344 
345 	if ((error = git_remote_fetch(remote, NULL, &fetch_opts, git_buf_cstr(&reflog_message))) != 0)
346 		goto cleanup;
347 
348 	error = checkout_branch(repo, remote, co_opts, branch, git_buf_cstr(&reflog_message));
349 
350 cleanup:
351 	git_remote_free(remote);
352 	git_buf_dispose(&reflog_message);
353 
354 	return error;
355 }
356 
git_clone__should_clone_local(const char * url_or_path,git_clone_local_t local)357 int git_clone__should_clone_local(const char *url_or_path, git_clone_local_t local)
358 {
359 	git_buf fromurl = GIT_BUF_INIT;
360 	const char *path = url_or_path;
361 	bool is_url, is_local;
362 
363 	if (local == GIT_CLONE_NO_LOCAL)
364 		return 0;
365 
366 	if ((is_url = git_path_is_local_file_url(url_or_path)) != 0) {
367 		if (git_path_fromurl(&fromurl, url_or_path) < 0) {
368 			is_local = -1;
369 			goto done;
370 		}
371 
372 		path = fromurl.ptr;
373 	}
374 
375 	is_local = (!is_url || local != GIT_CLONE_LOCAL_AUTO) &&
376 		git_path_isdir(path);
377 
378 done:
379 	git_buf_dispose(&fromurl);
380 	return is_local;
381 }
382 
git__clone(git_repository ** out,const char * url,const char * local_path,const git_clone_options * _options,int use_existing)383 static int git__clone(
384 	git_repository **out,
385 	const char *url,
386 	const char *local_path,
387 	const git_clone_options *_options,
388 	int use_existing)
389 {
390 	int error = 0;
391 	git_repository *repo = NULL;
392 	git_remote *origin;
393 	git_clone_options options = GIT_CLONE_OPTIONS_INIT;
394 	uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
395 	git_repository_create_cb repository_cb;
396 
397 	assert(out && url && local_path);
398 
399 	if (_options)
400 		memcpy(&options, _options, sizeof(git_clone_options));
401 
402 	GIT_ERROR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
403 
404 	/* Only clone to a new directory or an empty directory */
405 	if (git_path_exists(local_path) && !use_existing && !git_path_is_empty_dir(local_path)) {
406 		git_error_set(GIT_ERROR_INVALID,
407 			"'%s' exists and is not an empty directory", local_path);
408 		return GIT_EEXISTS;
409 	}
410 
411 	/* Only remove the root directory on failure if we create it */
412 	if (git_path_exists(local_path))
413 		rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
414 
415 	if (options.repository_cb)
416 		repository_cb = options.repository_cb;
417 	else
418 		repository_cb = default_repository_create;
419 
420 	if ((error = repository_cb(&repo, local_path, options.bare, options.repository_cb_payload)) < 0)
421 		return error;
422 
423 	if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
424 		int clone_local = git_clone__should_clone_local(url, options.local);
425 		int link = options.local != GIT_CLONE_LOCAL_NO_LINKS;
426 
427 		if (clone_local == 1)
428 			error = clone_local_into(
429 				repo, origin, &options.fetch_opts, &options.checkout_opts,
430 				options.checkout_branch, link);
431 		else if (clone_local == 0)
432 			error = clone_into(
433 				repo, origin, &options.fetch_opts, &options.checkout_opts,
434 				options.checkout_branch);
435 		else
436 			error = -1;
437 
438 		git_remote_free(origin);
439 	}
440 
441 	if (error != 0) {
442 		git_error_state last_error = {0};
443 		git_error_state_capture(&last_error, error);
444 
445 		git_repository_free(repo);
446 		repo = NULL;
447 
448 		(void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
449 
450 		git_error_state_restore(&last_error);
451 	}
452 
453 	*out = repo;
454 	return error;
455 }
456 
git_clone(git_repository ** out,const char * url,const char * local_path,const git_clone_options * _options)457 int git_clone(
458 	git_repository **out,
459 	const char *url,
460 	const char *local_path,
461 	const git_clone_options *_options)
462 {
463 	return git__clone(out, url, local_path, _options, 0);
464 }
465 
git_clone__submodule(git_repository ** out,const char * url,const char * local_path,const git_clone_options * _options)466 int git_clone__submodule(
467 	git_repository **out,
468 	const char *url,
469 	const char *local_path,
470 	const git_clone_options *_options)
471 {
472 	return git__clone(out, url, local_path, _options, 1);
473 }
474 
git_clone_options_init(git_clone_options * opts,unsigned int version)475 int git_clone_options_init(git_clone_options *opts, unsigned int version)
476 {
477 	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
478 		opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT);
479 	return 0;
480 }
481 
482 #ifndef GIT_DEPRECATE_HARD
git_clone_init_options(git_clone_options * opts,unsigned int version)483 int git_clone_init_options(git_clone_options *opts, unsigned int version)
484 {
485 	return git_clone_options_init(opts, version);
486 }
487 #endif
488 
can_link(const char * src,const char * dst,int link)489 static bool can_link(const char *src, const char *dst, int link)
490 {
491 #ifdef GIT_WIN32
492 	GIT_UNUSED(src);
493 	GIT_UNUSED(dst);
494 	GIT_UNUSED(link);
495 	return false;
496 #else
497 
498 	struct stat st_src, st_dst;
499 
500 	if (!link)
501 		return false;
502 
503 	if (p_stat(src, &st_src) < 0)
504 		return false;
505 
506 	if (p_stat(dst, &st_dst) < 0)
507 		return false;
508 
509 	return st_src.st_dev == st_dst.st_dev;
510 #endif
511 }
512 
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)513 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)
514 {
515 	int error, flags;
516 	git_repository *src;
517 	git_buf src_odb = GIT_BUF_INIT, dst_odb = GIT_BUF_INIT, src_path = GIT_BUF_INIT;
518 	git_buf reflog_message = GIT_BUF_INIT;
519 
520 	assert(repo && remote);
521 
522 	if (!git_repository_is_empty(repo)) {
523 		git_error_set(GIT_ERROR_INVALID, "the repository is not empty");
524 		return -1;
525 	}
526 
527 	/*
528 	 * Let's figure out what path we should use for the source
529 	 * repo, if it's not rooted, the path should be relative to
530 	 * the repository's worktree/gitdir.
531 	 */
532 	if ((error = git_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0)
533 		return error;
534 
535 	/* Copy .git/objects/ from the source to the target */
536 	if ((error = git_repository_open(&src, git_buf_cstr(&src_path))) < 0) {
537 		git_buf_dispose(&src_path);
538 		return error;
539 	}
540 
541 	if (git_repository_item_path(&src_odb, src, GIT_REPOSITORY_ITEM_OBJECTS) < 0
542 		|| git_repository_item_path(&dst_odb, repo, GIT_REPOSITORY_ITEM_OBJECTS) < 0) {
543 		error = -1;
544 		goto cleanup;
545 	}
546 
547 	flags = 0;
548 	if (can_link(git_repository_path(src), git_repository_path(repo), link))
549 		flags |= GIT_CPDIR_LINK_FILES;
550 
551 	error = git_futils_cp_r(git_buf_cstr(&src_odb), git_buf_cstr(&dst_odb),
552 				flags, GIT_OBJECT_DIR_MODE);
553 
554 	/*
555 	 * can_link() doesn't catch all variations, so if we hit an
556 	 * error and did want to link, let's try again without trying
557 	 * to link.
558 	 */
559 	if (error < 0 && link) {
560 		flags &= ~GIT_CPDIR_LINK_FILES;
561 		error = git_futils_cp_r(git_buf_cstr(&src_odb), git_buf_cstr(&dst_odb),
562 					flags, GIT_OBJECT_DIR_MODE);
563 	}
564 
565 	if (error < 0)
566 		goto cleanup;
567 
568 	git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
569 
570 	if ((error = git_remote_fetch(remote, NULL, fetch_opts, git_buf_cstr(&reflog_message))) != 0)
571 		goto cleanup;
572 
573 	error = checkout_branch(repo, remote, co_opts, branch, git_buf_cstr(&reflog_message));
574 
575 cleanup:
576 	git_buf_dispose(&reflog_message);
577 	git_buf_dispose(&src_path);
578 	git_buf_dispose(&src_odb);
579 	git_buf_dispose(&dst_odb);
580 	git_repository_free(src);
581 	return error;
582 }
583