deps/libgit2/src/libgit2/clone.c

filter: Statementstmt 0.0% 0 / 291 Branchbran 0.0% 0 / 160 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 451 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/apply.c ↑ Summary deps/libgit2/src/libgit2/common.h »
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 "fs_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  
28 0 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 0 git_commit *head_obj = NULL;
36 0 git_reference *branch_ref = NULL;
37 0 git_str refname = GIT_STR_INIT;
38 int error;
39  
40 /* Find the target commit */
41 0 ▶ if ((error = git_commit_lookup(&head_obj, repo, target)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_lookup(&head_obj, repo, target)) < 0) 0 0 0
42 0 return error;
43  
44 /* Create the new branch */
45 0 ▶ if ((error = git_str_printf(&refname, GIT_REFS_HEADS_DIR "%s", name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_printf(&refname, GIT_REFS_HEADS_DIR "%s", name)) < 0) 0 0 0
46 0 return error;
47  
48 0 error = git_reference_create(&branch_ref, repo, git_str_cstr(&refname), target, 0, log_message);
49 0 git_str_dispose(&refname);
50 0 git_commit_free(head_obj);
51  
52 0 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 0 0 0
53 0 *branch = branch_ref;
54 else
55 0 git_reference_free(branch_ref);
56  
57 0 return error;
58 }
59  
60 0 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 0 git_str remote_key = GIT_STR_INIT, merge_key = GIT_STR_INIT;
68 0 int error = -1;
69  
70 0 ▶ if (git_repository_config__weakptr(&cfg, repo) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository_config__weakptr(&cfg, repo) < 0) 0 0 0
71 0 return -1;
72  
73 0 ▶ if (git_str_printf(&remote_key, "branch.%s.remote", branch_name) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_printf(&remote_key, "branch.%s.remote", branch_name) < 0) 0 0 0
74 0 goto cleanup;
75  
76 0 ▶ if (git_str_printf(&merge_key, "branch.%s.merge", branch_name) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_printf(&merge_key, "branch.%s.merge", branch_name) < 0) 0 0 0
77 0 goto cleanup;
78  
79 0 ▶ if (git_config_set_string(cfg, git_str_cstr(&remote_key), remote_name) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_config_set_string(cfg, git_str_cstr(&remote_key), remote_name) < 0) 0 0 0
80 0 goto cleanup;
81  
82 0 ▶ if (git_config_set_string(cfg, git_str_cstr(&merge_key), merge_target) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_config_set_string(cfg, git_str_cstr(&merge_key), merge_target) < 0) 0 0 0
83 0 goto cleanup;
84  
85 0 error = 0;
86  
87 0 cleanup:
88 0 git_str_dispose(&remote_key);
89 0 git_str_dispose(&merge_key);
90 0 return error;
91 }
92  
93 0 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 0 ▶ if ((error = create_branch(branch, repo, target, branch_name, log_message)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = create_branch(branch, repo, target, branch_name, log_message)) < 0) 0 0 0
103 0 return error;
104  
105 0 return setup_tracking_config(
106 repo,
107 branch_name,
108 GIT_REMOTE_ORIGIN,
109 git_reference_name(*branch));
110 }
111  
112 0 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 0 git_reference *tracking_branch = NULL;
119 int error;
120  
121 0 ▶ if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR))
Branch: 1 branch
BranchTrueFalseTotal
if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR)) 0 0 0
122 0 name += strlen(GIT_REFS_HEADS_DIR);
123  
124 0 error = create_tracking_branch(&tracking_branch, repo, target, name,
125 reflog_message);
126  
127 0 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 0 0 0
128 0 error = git_repository_set_head(
129 repo, git_reference_name(tracking_branch));
130  
131 0 git_reference_free(tracking_branch);
132  
133 /* if it already existed, then the user's refspec created it for us, ignore it' */
134 0 ▶ if (error == GIT_EEXISTS)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_EEXISTS) 0 0 0
135 0 error = 0;
136  
137 0 return error;
138 }
139  
140 0 static int update_head_to_default(git_repository *repo)
141 {
142 0 git_str initialbranch = GIT_STR_INIT;
143 const char *branch_name;
144 0 int error = 0;
145  
146 0 ▶ if ((error = git_repository_initialbranch(&initialbranch, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository_initialbranch(&initialbranch, repo)) < 0) 0 0 0
147 0 goto done;
148  
149 0 ▶ if (git__prefixcmp(initialbranch.ptr, GIT_REFS_HEADS_DIR) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixcmp(initialbranch.ptr, GIT_REFS_HEADS_DIR) != 0) { 0 0 0
150 0 git_error_set(GIT_ERROR_INVALID, "invalid initial branch '%s'", initialbranch.ptr);
151 0 error = -1;
152 0 goto done;
153 }
154  
155 0 branch_name = initialbranch.ptr + strlen(GIT_REFS_HEADS_DIR);
156  
157 0 error = setup_tracking_config(repo, branch_name, GIT_REMOTE_ORIGIN,
158 0 initialbranch.ptr);
159  
160 0 done:
161 0 git_str_dispose(&initialbranch);
162 0 return error;
163 }
164  
165 0 static int update_remote_head(
166 git_repository *repo,
167 git_remote *remote,
168 git_str *target,
169 const char *reflog_message)
170 {
171 git_refspec *refspec;
172 0 git_reference *remote_head = NULL;
173 0 git_str remote_head_name = GIT_STR_INIT;
174 0 git_str remote_branch_name = GIT_STR_INIT;
175 int error;
176  
177 /* Determine the remote tracking ref name from the local branch */
178 0 refspec = git_remote__matching_refspec(remote, git_str_cstr(target));
179  
180 0 ▶ if (refspec == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (refspec == NULL) { 0 0 0
181 0 git_error_set(GIT_ERROR_NET, "the remote's default branch does not fit the refspec configuration");
182 0 error = GIT_EINVALIDSPEC;
183 0 goto cleanup;
184 }
185  
186 0 ▶ if ((error = git_refspec__transform(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_refspec__transform( 0 0 0
187 &remote_branch_name,
188 refspec,
189 git_str_cstr(target))) < 0)
190 0 goto cleanup;
191  
192 0 ▶ if ((error = git_str_printf(&remote_head_name,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_printf(&remote_head_name, 0 0 0
193 "%s%s/%s",
194 GIT_REFS_REMOTES_DIR,
195 git_remote_name(remote),
196 GIT_HEAD_FILE)) < 0)
197 0 goto cleanup;
198  
199 0 error = git_reference_symbolic_create(
200 &remote_head,
201 repo,
202 git_str_cstr(&remote_head_name),
203 git_str_cstr(&remote_branch_name),
204 true,
205 reflog_message);
206  
207 0 cleanup:
208 0 git_reference_free(remote_head);
209 0 git_str_dispose(&remote_branch_name);
210 0 git_str_dispose(&remote_head_name);
211 0 return error;
212 }
213  
214 0 static int update_head_to_remote(
215 git_repository *repo,
216 git_remote *remote,
217 const char *reflog_message)
218 {
219 0 int error = 0;
220 size_t refs_len;
221 const git_remote_head *remote_head, **refs;
222 const git_oid *remote_head_id;
223 0 git_str branch = GIT_STR_INIT;
224  
225 0 ▶ if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0) 0 0 0
226 0 return error;
227  
228 /* We cloned an empty repository or one with an unborn HEAD */
229 0 ▶ if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE))
Branch: 2 branches
BranchTrueFalseTotal
if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE)) 0 0 0
if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE)) 0 0 0
230 0 return update_head_to_default(repo);
231  
232 /* We know we have HEAD, let's see where it points */
233 0 remote_head = refs[0];
234 0 ▶ GIT_ASSERT(remote_head);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(remote_head); 0 0 0
235  
236 0 remote_head_id = &remote_head->oid;
237  
238 0 error = git_remote__default_branch(&branch, remote);
239 0 ▶ if (error == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) { 0 0 0
240 0 error = git_repository_set_head_detached(
241 repo, remote_head_id);
242 0 goto cleanup;
243 }
244  
245 0 ▶ if ((error = update_remote_head(repo, remote, &branch, reflog_message)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = update_remote_head(repo, remote, &branch, reflog_message)) < 0) 0 0 0
246 0 goto cleanup;
247  
248 0 error = update_head_to_new_branch(
249 repo,
250 remote_head_id,
251 git_str_cstr(&branch),
252 reflog_message);
253  
254 0 cleanup:
255 0 git_str_dispose(&branch);
256  
257 0 return error;
258 }
259  
260 0 static int update_head_to_branch(
261 git_repository *repo,
262 git_remote *remote,
263 const char *branch,
264 const char *reflog_message)
265 {
266 int retcode;
267 0 git_str remote_branch_name = GIT_STR_INIT;
268 0 git_reference *remote_ref = NULL;
269 0 git_str default_branch = GIT_STR_INIT;
270  
271 0 ▶ GIT_ASSERT_ARG(remote);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(remote); 0 0 0
272 0 ▶ GIT_ASSERT_ARG(branch);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(branch); 0 0 0
273  
274 0 ▶ if ((retcode = git_str_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s",
Branch: 1 branch
BranchTrueFalseTotal
if ((retcode = git_str_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s", 0 0 0
275 git_remote_name(remote), branch)) < 0 )
276 0 goto cleanup;
277  
278 0 ▶ if ((retcode = git_reference_lookup(&remote_ref, repo, git_str_cstr(&remote_branch_name))) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((retcode = git_reference_lookup(&remote_ref, repo, git_str_cstr(&remote_branch_name))) < 0) 0 0 0
279 0 goto cleanup;
280  
281 0 ▶ if ((retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch,
Branch: 1 branch
BranchTrueFalseTotal
if ((retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch, 0 0 0
282 reflog_message)) < 0)
283 0 goto cleanup;
284  
285 0 ▶ if ((retcode = git_remote__default_branch(&default_branch, remote)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((retcode = git_remote__default_branch(&default_branch, remote)) < 0) 0 0 0
286 0 goto cleanup;
287  
288 0 ▶ if (!git_remote__matching_refspec(remote, git_str_cstr(&default_branch)))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_remote__matching_refspec(remote, git_str_cstr(&default_branch))) 0 0 0
289 0 goto cleanup;
290  
291 0 retcode = update_remote_head(repo, remote, &default_branch, reflog_message);
292  
293 0 cleanup:
294 0 git_reference_free(remote_ref);
295 0 git_str_dispose(&remote_branch_name);
296 0 git_str_dispose(&default_branch);
297 0 return retcode;
298 }
299  
300 0 static int default_repository_create(git_repository **out, const char *path, int bare, void *payload)
301 {
302 0 GIT_UNUSED(payload);
303  
304 0 return git_repository_init(out, path, bare);
305 }
306  
307 0 static int default_remote_create(
308 git_remote **out,
309 git_repository *repo,
310 const char *name,
311 const char *url,
312 void *payload)
313 {
314 0 GIT_UNUSED(payload);
315  
316 0 return git_remote_create(out, repo, name, url);
317 }
318  
319 /*
320 * submodules?
321 */
322  
323 0 static int create_and_configure_origin(
324 git_remote **out,
325 git_repository *repo,
326 const char *url,
327 const git_clone_options *options)
328 {
329 int error;
330 0 git_remote *origin = NULL;
331 char buf[GIT_PATH_MAX];
332 0 git_remote_create_cb remote_create = options->remote_cb;
333 0 void *payload = options->remote_cb_payload;
334  
335 /* If the path exists and is a dir, the url should be the absolute path */
336 0 ▶ if (git_fs_path_root(url) < 0 && git_fs_path_exists(url) && git_fs_path_isdir(url)) {
Branch: 3 branches
BranchTrueFalseTotal
if (git_fs_path_root(url) < 0 && git_fs_path_exists(url) && git_fs_path_isdir(url)) { 0 0 0
if (git_fs_path_root(url) < 0 && git_fs_path_exists(url) && git_fs_path_isdir(url)) { 0 0 0
if (git_fs_path_root(url) < 0 && git_fs_path_exists(url) && git_fs_path_isdir(url)) { 0 0 0
337 0 ▶ if (p_realpath(url, buf) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (p_realpath(url, buf) == NULL) 0 0 0
338 0 return -1;
339  
340 0 url = buf;
341 }
342  
343 0 ▶ if (!remote_create) {
Branch: 1 branch
BranchTrueFalseTotal
if (!remote_create) { 0 0 0
344 0 remote_create = default_remote_create;
345 0 payload = NULL;
346 }
347  
348 0 ▶ if ((error = remote_create(&origin, repo, "origin", url, payload)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = remote_create(&origin, repo, "origin", url, payload)) < 0) 0 0 0
349 0 goto on_error;
350  
351 0 *out = origin;
352 0 return 0;
353  
354 0 on_error:
355 0 git_remote_free(origin);
356 0 return error;
357 }
358  
359 0 static bool should_checkout(
360 git_repository *repo,
361 bool is_bare,
362 const git_checkout_options *opts)
363 {
364 0 ▶ if (is_bare)
Branch: 1 branch
BranchTrueFalseTotal
if (is_bare) 0 0 0
365 0 return false;
366  
367 0 ▶ if (!opts)
Branch: 1 branch
BranchTrueFalseTotal
if (!opts) 0 0 0
368 0 return false;
369  
370 0 ▶ if (opts->checkout_strategy == GIT_CHECKOUT_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if (opts->checkout_strategy == GIT_CHECKOUT_NONE) 0 0 0
371 0 return false;
372  
373 0 return !git_repository_head_unborn(repo);
374 }
375  
376 0 static int checkout_branch(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, const char *reflog_message)
377 {
378 int error;
379  
380 0 ▶ if (branch)
Branch: 1 branch
BranchTrueFalseTotal
if (branch) 0 0 0
381 0 error = update_head_to_branch(repo, remote, branch, reflog_message);
382 /* Point HEAD to the same ref as the remote's head */
383 else
384 0 error = update_head_to_remote(repo, remote, reflog_message);
385  
386 0 ▶ if (!error && should_checkout(repo, git_repository_is_bare(repo), co_opts))
Branch: 2 branches
BranchTrueFalseTotal
if (!error && should_checkout(repo, git_repository_is_bare(repo), co_opts)) 0 0 0
if (!error && should_checkout(repo, git_repository_is_bare(repo), co_opts)) 0 0 0
387 0 error = git_checkout_head(repo, co_opts);
388  
389 0 return error;
390 }
391  
392 0 static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch_options *opts, const git_checkout_options *co_opts, const char *branch)
393 {
394 int error;
395 0 git_str reflog_message = GIT_STR_INIT;
396 git_fetch_options fetch_opts;
397 git_remote *remote;
398  
399 0 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 0 0
400 0 ▶ GIT_ASSERT_ARG(_remote);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(_remote); 0 0 0
401  
402 0 ▶ if (!git_repository_is_empty(repo)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_repository_is_empty(repo)) { 0 0 0
403 0 git_error_set(GIT_ERROR_INVALID, "the repository is not empty");
404 0 return -1;
405 }
406  
407 0 ▶ if ((error = git_remote_dup(&remote, _remote)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_remote_dup(&remote, _remote)) < 0) 0 0 0
408 0 return error;
409  
410 0 memcpy(&fetch_opts, opts, sizeof(git_fetch_options));
411 0 fetch_opts.update_fetchhead = 0;
412 0 fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
413 0 git_str_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
414  
415 0 ▶ if ((error = git_remote_fetch(remote, NULL, &fetch_opts, git_str_cstr(&reflog_message))) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_remote_fetch(remote, NULL, &fetch_opts, git_str_cstr(&reflog_message))) != 0) 0 0 0
416 0 goto cleanup;
417  
418 0 error = checkout_branch(repo, remote, co_opts, branch, git_str_cstr(&reflog_message));
419  
420 0 cleanup:
421 0 git_remote_free(remote);
422 0 git_str_dispose(&reflog_message);
423  
424 0 return error;
425 }
426  
427 0 int git_clone__should_clone_local(const char *url_or_path, git_clone_local_t local)
428 {
429 0 git_str fromurl = GIT_STR_INIT;
430 0 const char *path = url_or_path;
431 bool is_url, is_local;
432  
433 0 ▶ if (local == GIT_CLONE_NO_LOCAL)
Branch: 1 branch
BranchTrueFalseTotal
if (local == GIT_CLONE_NO_LOCAL) 0 0 0
434 0 return 0;
435  
436 0 ▶ if ((is_url = git_fs_path_is_local_file_url(url_or_path)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((is_url = git_fs_path_is_local_file_url(url_or_path)) != 0) { 0 0 0
437 0 ▶ if (git_fs_path_fromurl(&fromurl, url_or_path) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_fromurl(&fromurl, url_or_path) < 0) { 0 0 0
438 0 is_local = -1;
439 0 goto done;
440 }
441  
442 0 path = fromurl.ptr;
443 }
444  
445 0 ▶ is_local = (!is_url || local != GIT_CLONE_LOCAL_AUTO) &&
Branch: 1 branch
BranchTrueFalseTotal
is_local = (!is_url || local != GIT_CLONE_LOCAL_AUTO) && 0 0 0
446 0 git_fs_path_isdir(path);
447  
448 0 done:
449 0 git_str_dispose(&fromurl);
450 0 return is_local;
451 }
452  
453 0 static int git__clone(
454 git_repository **out,
455 const char *url,
456 const char *local_path,
457 const git_clone_options *_options,
458 int use_existing)
459 {
460 0 int error = 0;
461 0 git_repository *repo = NULL;
462 git_remote *origin;
463 0 git_clone_options options = GIT_CLONE_OPTIONS_INIT;
464 0 uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
465 git_repository_create_cb repository_cb;
466  
467 0 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 0 0
468 0 ▶ GIT_ASSERT_ARG(url);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(url); 0 0 0
469 0 ▶ GIT_ASSERT_ARG(local_path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(local_path); 0 0 0
470  
471 0 ▶ if (_options)
Branch: 1 branch
BranchTrueFalseTotal
if (_options) 0 0 0
472 0 memcpy(&options, _options, sizeof(git_clone_options));
473  
474 0 ▶ GIT_ERROR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options"); 0 0 0
475  
476 /* Only clone to a new directory or an empty directory */
477 0 ▶ if (git_fs_path_exists(local_path) && !use_existing && !git_fs_path_is_empty_dir(local_path)) {
Branch: 3 branches
BranchTrueFalseTotal
if (git_fs_path_exists(local_path) && !use_existing && !git_fs_path_is_empty_dir(local_path)) { 0 0 0
if (git_fs_path_exists(local_path) && !use_existing && !git_fs_path_is_empty_dir(local_path)) { 0 0 0
if (git_fs_path_exists(local_path) && !use_existing && !git_fs_path_is_empty_dir(local_path)) { 0 0 0
478 0 git_error_set(GIT_ERROR_INVALID,
479 "'%s' exists and is not an empty directory", local_path);
480 0 return GIT_EEXISTS;
481 }
482  
483 /* Only remove the root directory on failure if we create it */
484 0 ▶ if (git_fs_path_exists(local_path))
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_exists(local_path)) 0 0 0
485 0 rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
486  
487 0 ▶ if (options.repository_cb)
Branch: 1 branch
BranchTrueFalseTotal
if (options.repository_cb) 0 0 0
488 0 repository_cb = options.repository_cb;
489 else
490 0 repository_cb = default_repository_create;
491  
492 0 ▶ if ((error = repository_cb(&repo, local_path, options.bare, options.repository_cb_payload)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = repository_cb(&repo, local_path, options.bare, options.repository_cb_payload)) < 0) 0 0 0
493 0 return error;
494  
495 0 ▶ if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(error = create_and_configure_origin(&origin, repo, url, &options))) { 0 0 0
496 0 int clone_local = git_clone__should_clone_local(url, options.local);
497 0 int link = options.local != GIT_CLONE_LOCAL_NO_LINKS;
498  
499 0 ▶ if (clone_local == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (clone_local == 1) 0 0 0
500 0 error = clone_local_into(
501 repo, origin, &options.fetch_opts, &options.checkout_opts,
502 options.checkout_branch, link);
503 0 ▶ else if (clone_local == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (clone_local == 0) 0 0 0
504 0 error = clone_into(
505 repo, origin, &options.fetch_opts, &options.checkout_opts,
506 options.checkout_branch);
507 else
508 0 error = -1;
509  
510 0 git_remote_free(origin);
511 }
512  
513 0 ▶ if (error != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error != 0) { 0 0 0
514 0 git_error_state last_error = {0};
515 0 git_error_state_capture(&last_error, error);
516  
517 0 git_repository_free(repo);
518 0 repo = NULL;
519  
520 0 (void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
521  
522 0 git_error_state_restore(&last_error);
523 }
524  
525 0 *out = repo;
526 0 return error;
527 }
528  
529 0 int git_clone(
530 git_repository **out,
531 const char *url,
532 const char *local_path,
533 const git_clone_options *_options)
534 {
535 0 return git__clone(out, url, local_path, _options, 0);
536 }
537  
538 0 int git_clone__submodule(
539 git_repository **out,
540 const char *url,
541 const char *local_path,
542 const git_clone_options *_options)
543 {
544 0 return git__clone(out, url, local_path, _options, 1);
545 }
546  
547 0 int git_clone_options_init(git_clone_options *opts, unsigned int version)
548 {
549 0 ▶ GIT_INIT_STRUCTURE_FROM_TEMPLATE(
Branch: 1 branch
BranchTrueFalseTotal
GIT_INIT_STRUCTURE_FROM_TEMPLATE( 0 0 0
550 opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT);
551 0 return 0;
552 }
553  
554 #ifndef GIT_DEPRECATE_HARD
555 0 int git_clone_init_options(git_clone_options *opts, unsigned int version)
556 {
557 0 return git_clone_options_init(opts, version);
558 }
559 #endif
560  
561 0 static bool can_link(const char *src, const char *dst, int link)
562 {
563 #ifdef GIT_WIN32
564 GIT_UNUSED(src);
565 GIT_UNUSED(dst);
566 GIT_UNUSED(link);
567 return false;
568 #else
569  
570 struct stat st_src, st_dst;
571  
572 0 ▶ if (!link)
Branch: 1 branch
BranchTrueFalseTotal
if (!link) 0 0 0
573 0 return false;
574  
575 0 ▶ if (p_stat(src, &st_src) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p_stat(src, &st_src) < 0) 0 0 0
576 0 return false;
577  
578 0 ▶ if (p_stat(dst, &st_dst) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p_stat(dst, &st_dst) < 0) 0 0 0
579 0 return false;
580  
581 0 return st_src.st_dev == st_dst.st_dev;
582 #endif
583 }
584  
585 0 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)
586 {
587 int error, flags;
588 git_repository *src;
589 0 git_str src_odb = GIT_STR_INIT, dst_odb = GIT_STR_INIT, src_path = GIT_STR_INIT;
590 0 git_str reflog_message = GIT_STR_INIT;
591  
592 0 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 0 0
593 0 ▶ GIT_ASSERT_ARG(remote);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(remote); 0 0 0
594  
595 0 ▶ if (!git_repository_is_empty(repo)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_repository_is_empty(repo)) { 0 0 0
596 0 git_error_set(GIT_ERROR_INVALID, "the repository is not empty");
597 0 return -1;
598 }
599  
600 /*
601 * Let's figure out what path we should use for the source
602 * repo, if it's not rooted, the path should be relative to
603 * the repository's worktree/gitdir.
604 */
605 0 ▶ if ((error = git_fs_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_fs_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0) 0 0 0
606 0 return error;
607  
608 /* Copy .git/objects/ from the source to the target */
609 0 ▶ if ((error = git_repository_open(&src, git_str_cstr(&src_path))) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository_open(&src, git_str_cstr(&src_path))) < 0) { 0 0 0
610 0 git_str_dispose(&src_path);
611 0 return error;
612 }
613  
614 0 if (git_repository__item_path(&src_odb, src, GIT_REPOSITORY_ITEM_OBJECTS) < 0 ||
615 0 git_repository__item_path(&dst_odb, repo, GIT_REPOSITORY_ITEM_OBJECTS) < 0) {
616 0 error = -1;
617 0 goto cleanup;
618 }
619  
620 0 flags = 0;
621 0 ▶ if (can_link(git_repository_path(src), git_repository_path(repo), link))
Branch: 1 branch
BranchTrueFalseTotal
if (can_link(git_repository_path(src), git_repository_path(repo), link)) 0 0 0
622 0 flags |= GIT_CPDIR_LINK_FILES;
623  
624 0 error = git_futils_cp_r(git_str_cstr(&src_odb), git_str_cstr(&dst_odb),
625 flags, GIT_OBJECT_DIR_MODE);
626  
627 /*
628 * can_link() doesn't catch all variations, so if we hit an
629 * error and did want to link, let's try again without trying
630 * to link.
631 */
632 0 ▶ if (error < 0 && link) {
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && link) { 0 0 0
if (error < 0 && link) { 0 0 0
633 0 flags &= ~GIT_CPDIR_LINK_FILES;
634 0 error = git_futils_cp_r(git_str_cstr(&src_odb), git_str_cstr(&dst_odb),
635 flags, GIT_OBJECT_DIR_MODE);
636 }
637  
638 0 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 0 0
639 0 goto cleanup;
640  
641 0 git_str_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
642  
643 0 ▶ if ((error = git_remote_fetch(remote, NULL, fetch_opts, git_str_cstr(&reflog_message))) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_remote_fetch(remote, NULL, fetch_opts, git_str_cstr(&reflog_message))) != 0) 0 0 0
644 0 goto cleanup;
645  
646 0 error = checkout_branch(repo, remote, co_opts, branch, git_str_cstr(&reflog_message));
647  
648 0 cleanup:
649 0 git_str_dispose(&reflog_message);
650 0 git_str_dispose(&src_path);
651 0 git_str_dispose(&src_odb);
652 0 git_str_dispose(&dst_odb);
653 0 git_repository_free(src);
654 0 return error;
655 }
« deps/libgit2/src/libgit2/apply.c ↑ Summary deps/libgit2/src/libgit2/common.h »