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