]>
Commit | Line | Data |
---|---|---|
764df57e | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
764df57e BS |
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> | |
830388a7 | 9 | |
764df57e BS |
10 | #include "git2/clone.h" |
11 | #include "git2/remote.h" | |
bb1f6087 | 12 | #include "git2/revparse.h" |
4fbc899a BS |
13 | #include "git2/branch.h" |
14 | #include "git2/config.h" | |
14741d62 | 15 | #include "git2/checkout.h" |
2b63db4c BS |
16 | #include "git2/commit.h" |
17 | #include "git2/tree.h" | |
764df57e BS |
18 | |
19 | #include "common.h" | |
20 | #include "remote.h" | |
21 | #include "fileops.h" | |
4fbc899a | 22 | #include "refs.h" |
c3b5099f | 23 | #include "path.h" |
114f5a6c | 24 | #include "repository.h" |
4386d80b | 25 | #include "odb.h" |
764df57e | 26 | |
8f0104ec | 27 | 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); |
6812afaf | 28 | |
bf0e62a2 | 29 | static int create_branch( |
7eca3c56 | 30 | git_reference **branch, |
31 | git_repository *repo, | |
32 | const git_oid *target, | |
1cc974ab | 33 | const char *name, |
1cc974ab | 34 | const char *log_message) |
4fbc899a | 35 | { |
cfbe4be3 | 36 | git_commit *head_obj = NULL; |
132c2db6 | 37 | git_reference *branch_ref = NULL; |
6bfb990d | 38 | git_buf refname = GIT_BUF_INIT; |
bf0e62a2 | 39 | int error; |
ea817863 BS |
40 | |
41 | /* Find the target commit */ | |
cfbe4be3 | 42 | if ((error = git_commit_lookup(&head_obj, repo, target)) < 0) |
bf0e62a2 | 43 | return error; |
ea817863 BS |
44 | |
45 | /* Create the new branch */ | |
6bfb990d CMN |
46 | if ((error = git_buf_printf(&refname, GIT_REFS_HEADS_DIR "%s", name)) < 0) |
47 | return error; | |
ea817863 | 48 | |
6bfb990d CMN |
49 | error = git_reference_create(&branch_ref, repo, git_buf_cstr(&refname), target, 0, log_message); |
50 | git_buf_free(&refname); | |
cfbe4be3 | 51 | git_commit_free(head_obj); |
7eca3c56 | 52 | |
bf0e62a2 | 53 | if (!error) |
7eca3c56 | 54 | *branch = branch_ref; |
55 | else | |
56 | git_reference_free(branch_ref); | |
57 | ||
bf0e62a2 | 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, | |
1cc974ab | 98 | const char *branch_name, |
1cc974ab | 99 | const char *log_message) |
bf0e62a2 | 100 | { |
101 | int error; | |
102 | ||
659cf202 | 103 | if ((error = create_branch(branch, repo, target, branch_name, log_message)) < 0) |
bf0e62a2 | 104 | return error; |
105 | ||
106 | return setup_tracking_config( | |
107 | repo, | |
108 | branch_name, | |
109 | GIT_REMOTE_ORIGIN, | |
110 | git_reference_name(*branch)); | |
4fbc899a BS |
111 | } |
112 | ||
bf0e62a2 | 113 | static int update_head_to_new_branch( |
114 | git_repository *repo, | |
115 | const git_oid *target, | |
94f263f5 BS |
116 | const char *name, |
117 | const char *reflog_message) | |
af58ec9e | 118 | { |
aa4437f6 | 119 | git_reference *tracking_branch = NULL; |
4c4408c3 | 120 | int error; |
cdb8a608 CMN |
121 | |
122 | if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR)) | |
123 | name += strlen(GIT_REFS_HEADS_DIR); | |
124 | ||
4c4408c3 | 125 | error = create_tracking_branch(&tracking_branch, repo, target, name, |
659cf202 | 126 | reflog_message); |
ea817863 | 127 | |
dab89f9b RB |
128 | if (!error) |
129 | error = git_repository_set_head( | |
4e498646 | 130 | repo, git_reference_name(tracking_branch)); |
7eca3c56 | 131 | |
132 | git_reference_free(tracking_branch); | |
133 | ||
32332fcc CMN |
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 | ||
7eca3c56 | 138 | return error; |
af58ec9e BS |
139 | } |
140 | ||
94f263f5 BS |
141 | static int update_head_to_remote( |
142 | git_repository *repo, | |
143 | git_remote *remote, | |
144 | const char *reflog_message) | |
8340dd5d | 145 | { |
15c30b72 | 146 | int error = 0; |
359dce72 | 147 | size_t refs_len; |
15c30b72 | 148 | git_refspec *refspec; |
359dce72 | 149 | const git_remote_head *remote_head, **refs; |
2a597116 | 150 | const git_oid *remote_head_id; |
d280c71b | 151 | git_buf remote_master_name = GIT_BUF_INIT; |
cdb8a608 | 152 | git_buf branch = GIT_BUF_INIT; |
ea817863 | 153 | |
dab89f9b RB |
154 | if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0) |
155 | return error; | |
359dce72 | 156 | |
e128a1af CMN |
157 | /* We cloned an empty repository or one with an unborn HEAD */ |
158 | if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE)) | |
bf0e62a2 | 159 | return setup_tracking_config( |
dab89f9b | 160 | repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE); |
bf0e62a2 | 161 | |
15c30b72 | 162 | /* We know we have HEAD, let's see where it points */ |
359dce72 | 163 | remote_head = refs[0]; |
41fb1ca0 PK |
164 | assert(remote_head); |
165 | ||
2a597116 | 166 | remote_head_id = &remote_head->oid; |
15c30b72 CMN |
167 | |
168 | error = git_remote_default_branch(&branch, remote); | |
169 | if (error == GIT_ENOTFOUND) { | |
170 | error = git_repository_set_head_detached( | |
4e498646 | 171 | repo, remote_head_id); |
15c30b72 CMN |
172 | goto cleanup; |
173 | } | |
174 | ||
2a597116 | 175 | refspec = git_remote__matching_refspec(remote, git_buf_cstr(&branch)); |
4330ab26 | 176 | |
2a597116 | 177 | if (refspec == NULL) { |
15c30b72 CMN |
178 | giterr_set(GITERR_NET, "the remote's default branch does not fit the refspec configuration"); |
179 | error = GIT_EINVALIDSPEC; | |
180 | goto cleanup; | |
4330ab26 | 181 | } |
55ededfd | 182 | |
d280c71b | 183 | /* Determine the remote tracking reference name from the local master */ |
bf522e08 | 184 | if ((error = git_refspec_transform( |
d280c71b | 185 | &remote_master_name, |
2a597116 | 186 | refspec, |
cdb8a608 | 187 | git_buf_cstr(&branch))) < 0) |
15c30b72 | 188 | goto cleanup; |
d280c71b | 189 | |
15c30b72 CMN |
190 | error = update_head_to_new_branch( |
191 | repo, | |
192 | remote_head_id, | |
193 | git_buf_cstr(&branch), | |
659cf202 | 194 | reflog_message); |
ea817863 | 195 | |
15c30b72 | 196 | cleanup: |
d280c71b | 197 | git_buf_free(&remote_master_name); |
cdb8a608 | 198 | git_buf_free(&branch); |
15c30b72 | 199 | |
dab89f9b | 200 | return error; |
bb1f6087 BS |
201 | } |
202 | ||
88aef766 SC |
203 | static int update_head_to_branch( |
204 | git_repository *repo, | |
d19870d9 | 205 | const char *remote_name, |
94f263f5 BS |
206 | const char *branch, |
207 | const char *reflog_message) | |
88aef766 SC |
208 | { |
209 | int retcode; | |
210 | git_buf remote_branch_name = GIT_BUF_INIT; | |
211 | git_reference* remote_ref = NULL; | |
1fed6b07 | 212 | |
d19870d9 | 213 | assert(remote_name && branch); |
88aef766 SC |
214 | |
215 | if ((retcode = git_buf_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s", | |
d19870d9 | 216 | remote_name, branch)) < 0 ) |
88aef766 SC |
217 | goto cleanup; |
218 | ||
219 | if ((retcode = git_reference_lookup(&remote_ref, repo, git_buf_cstr(&remote_branch_name))) < 0) | |
220 | goto cleanup; | |
221 | ||
1cc974ab | 222 | retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch, |
659cf202 | 223 | reflog_message); |
88aef766 SC |
224 | |
225 | cleanup: | |
226 | git_reference_free(remote_ref); | |
1265b51f | 227 | git_buf_free(&remote_branch_name); |
88aef766 SC |
228 | return retcode; |
229 | } | |
230 | ||
d58a64e9 CMN |
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 | ||
1697cd6f PK |
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 | { | |
8f0104ec | 245 | GIT_UNUSED(payload); |
1697cd6f | 246 | |
8f0104ec | 247 | return git_remote_create(out, repo, name, url); |
1697cd6f PK |
248 | } |
249 | ||
764df57e BS |
250 | /* |
251 | * submodules? | |
764df57e BS |
252 | */ |
253 | ||
b412d563 BS |
254 | static int create_and_configure_origin( |
255 | git_remote **out, | |
256 | git_repository *repo, | |
257 | const char *url, | |
258 | const git_clone_options *options) | |
259 | { | |
260 | int error; | |
00998a12 | 261 | git_remote *origin = NULL; |
a0b5f785 | 262 | char buf[GIT_PATH_MAX]; |
1697cd6f PK |
263 | git_remote_create_cb remote_create = options->remote_cb; |
264 | void *payload = options->remote_cb_payload; | |
a0b5f785 CMN |
265 | |
266 | /* If the path exists and is a dir, the url should be the absolute path */ | |
267 | if (git_path_root(url) < 0 && git_path_exists(url) && git_path_isdir(url)) { | |
268 | if (p_realpath(url, buf) == NULL) | |
269 | return -1; | |
270 | ||
271 | url = buf; | |
272 | } | |
b412d563 | 273 | |
1697cd6f PK |
274 | if (!remote_create) { |
275 | remote_create = default_remote_create; | |
8f0104ec | 276 | payload = NULL; |
1697cd6f | 277 | } |
b9bf5d70 | 278 | |
1697cd6f | 279 | if ((error = remote_create(&origin, repo, "origin", url, payload)) < 0) |
b412d563 BS |
280 | goto on_error; |
281 | ||
b412d563 BS |
282 | *out = origin; |
283 | return 0; | |
284 | ||
285 | on_error: | |
00998a12 | 286 | git_remote_free(origin); |
b412d563 BS |
287 | return error; |
288 | } | |
bb1f6087 | 289 | |
4d968f13 | 290 | static bool should_checkout( |
291 | git_repository *repo, | |
292 | bool is_bare, | |
6affd71f | 293 | const git_checkout_options *opts) |
4d968f13 | 294 | { |
295 | if (is_bare) | |
296 | return false; | |
297 | ||
298 | if (!opts) | |
299 | return false; | |
300 | ||
2850252a | 301 | if (opts->checkout_strategy == GIT_CHECKOUT_NONE) |
730df6d0 BS |
302 | return false; |
303 | ||
605da51a | 304 | return !git_repository_head_unborn(repo); |
4d968f13 | 305 | } |
acdd3d95 | 306 | |
659cf202 | 307 | static int checkout_branch(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, const char *reflog_message) |
4386d80b CMN |
308 | { |
309 | int error; | |
310 | ||
311 | if (branch) | |
312 | error = update_head_to_branch(repo, git_remote_name(remote), branch, | |
659cf202 | 313 | reflog_message); |
4386d80b CMN |
314 | /* Point HEAD to the same ref as the remote's head */ |
315 | else | |
659cf202 | 316 | error = update_head_to_remote(repo, remote, reflog_message); |
4386d80b CMN |
317 | |
318 | if (!error && should_checkout(repo, git_repository_is_bare(repo), co_opts)) | |
319 | error = git_checkout_head(repo, co_opts); | |
320 | ||
321 | return error; | |
322 | } | |
323 | ||
8f0104ec | 324 | static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch_options *opts, const git_checkout_options *co_opts, const char *branch) |
d19870d9 | 325 | { |
60cdf495 | 326 | int error; |
94f263f5 | 327 | git_buf reflog_message = GIT_BUF_INIT; |
3eff2a57 | 328 | git_fetch_options fetch_opts; |
3c607685 | 329 | git_remote *remote; |
d19870d9 | 330 | |
3c607685 | 331 | assert(repo && _remote); |
d19870d9 CMN |
332 | |
333 | if (!git_repository_is_empty(repo)) { | |
334 | giterr_set(GITERR_INVALID, "the repository is not empty"); | |
335 | return -1; | |
336 | } | |
337 | ||
3c607685 | 338 | if ((error = git_remote_dup(&remote, _remote)) < 0) |
266af6d8 CMN |
339 | return error; |
340 | ||
3eff2a57 CMN |
341 | memcpy(&fetch_opts, opts, sizeof(git_fetch_options)); |
342 | fetch_opts.update_fetchhead = 0; | |
77254990 | 343 | fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; |
94f263f5 | 344 | git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); |
d19870d9 | 345 | |
3eff2a57 | 346 | if ((error = git_remote_fetch(remote, NULL, &fetch_opts, git_buf_cstr(&reflog_message))) != 0) |
d19870d9 CMN |
347 | goto cleanup; |
348 | ||
659cf202 | 349 | error = checkout_branch(repo, remote, co_opts, branch, git_buf_cstr(&reflog_message)); |
d19870d9 CMN |
350 | |
351 | cleanup: | |
3c607685 | 352 | git_remote_free(remote); |
94f263f5 | 353 | git_buf_free(&reflog_message); |
d19870d9 CMN |
354 | |
355 | return error; | |
356 | } | |
357 | ||
529fd30d | 358 | int git_clone__should_clone_local(const char *url_or_path, git_clone_local_t local) |
121b2673 | 359 | { |
529fd30d ET |
360 | git_buf fromurl = GIT_BUF_INIT; |
361 | const char *path = url_or_path; | |
362 | bool is_url, is_local; | |
121b2673 CMN |
363 | |
364 | if (local == GIT_CLONE_NO_LOCAL) | |
529fd30d | 365 | return 0; |
121b2673 | 366 | |
84a85d1b | 367 | if ((is_url = git_path_is_local_file_url(url_or_path)) != 0) { |
529fd30d ET |
368 | if (git_path_fromurl(&fromurl, url_or_path) < 0) { |
369 | is_local = -1; | |
370 | goto done; | |
371 | } | |
121b2673 | 372 | |
529fd30d ET |
373 | path = fromurl.ptr; |
374 | } | |
121b2673 | 375 | |
529fd30d ET |
376 | is_local = (!is_url || local != GIT_CLONE_LOCAL_AUTO) && |
377 | git_path_isdir(path); | |
121b2673 | 378 | |
529fd30d ET |
379 | done: |
380 | git_buf_free(&fromurl); | |
381 | return is_local; | |
121b2673 CMN |
382 | } |
383 | ||
b412d563 | 384 | int git_clone( |
bf0e62a2 | 385 | git_repository **out, |
b412d563 BS |
386 | const char *url, |
387 | const char *local_path, | |
fdc7e5e3 | 388 | const git_clone_options *_options) |
764df57e | 389 | { |
219d3457 | 390 | int error = 0; |
ea817863 | 391 | git_repository *repo = NULL; |
e3a92f0d | 392 | git_remote *origin; |
fdc7e5e3 | 393 | git_clone_options options = GIT_CLONE_OPTIONS_INIT; |
219d3457 | 394 | uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES; |
d58a64e9 | 395 | git_repository_create_cb repository_cb; |
b412d563 BS |
396 | |
397 | assert(out && url && local_path); | |
ea817863 | 398 | |
fdc7e5e3 CMN |
399 | if (_options) |
400 | memcpy(&options, _options, sizeof(git_clone_options)); | |
401 | ||
402 | GITERR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options"); | |
b412d563 | 403 | |
f6f48f90 RB |
404 | /* Only clone to a new directory or an empty directory */ |
405 | if (git_path_exists(local_path) && !git_path_is_empty_dir(local_path)) { | |
406 | giterr_set(GITERR_INVALID, | |
46779411 | 407 | "'%s' exists and is not an empty directory", local_path); |
219d3457 | 408 | return GIT_EEXISTS; |
ea817863 BS |
409 | } |
410 | ||
219d3457 RB |
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; | |
926acbcf | 414 | |
d58a64e9 CMN |
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) | |
219d3457 | 421 | return error; |
e3a92f0d | 422 | |
219d3457 | 423 | if (!(error = create_and_configure_origin(&origin, repo, url, &options))) { |
84a85d1b | 424 | int clone_local = git_clone__should_clone_local(url, options.local); |
529fd30d ET |
425 | int link = options.local != GIT_CLONE_LOCAL_NO_LINKS; |
426 | ||
84a85d1b | 427 | if (clone_local == 1) |
6812afaf | 428 | error = clone_local_into( |
8f0104ec | 429 | repo, origin, &options.fetch_opts, &options.checkout_opts, |
659cf202 | 430 | options.checkout_branch, link); |
84a85d1b | 431 | else if (clone_local == 0) |
6812afaf | 432 | error = clone_into( |
8f0104ec | 433 | repo, origin, &options.fetch_opts, &options.checkout_opts, |
659cf202 | 434 | options.checkout_branch); |
529fd30d ET |
435 | else |
436 | error = -1; | |
926acbcf | 437 | |
219d3457 RB |
438 | git_remote_free(origin); |
439 | } | |
926acbcf | 440 | |
8f1066a0 | 441 | if (error != 0) { |
1df8ad01 ET |
442 | git_error_state last_error = {0}; |
443 | giterr_capture(&last_error, error); | |
444 | ||
219d3457 RB |
445 | git_repository_free(repo); |
446 | repo = NULL; | |
8f1066a0 | 447 | |
219d3457 | 448 | (void)git_futils_rmdir_r(local_path, NULL, rmdir_flags); |
1df8ad01 ET |
449 | |
450 | giterr_restore(&last_error); | |
219d3457 | 451 | } |
ea817863 | 452 | |
e3a92f0d | 453 | *out = repo; |
219d3457 | 454 | return error; |
764df57e | 455 | } |
b9f81997 | 456 | |
702efc89 | 457 | int git_clone_init_options(git_clone_options *opts, unsigned int version) |
b9f81997 | 458 | { |
702efc89 RB |
459 | GIT_INIT_STRUCTURE_FROM_TEMPLATE( |
460 | opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT); | |
461 | return 0; | |
b9f81997 | 462 | } |
4386d80b CMN |
463 | |
464 | static const char *repository_base(git_repository *repo) | |
465 | { | |
466 | if (git_repository_is_bare(repo)) | |
467 | return git_repository_path(repo); | |
468 | ||
469 | return git_repository_workdir(repo); | |
470 | } | |
471 | ||
2614819c CMN |
472 | static bool can_link(const char *src, const char *dst, int link) |
473 | { | |
474 | #ifdef GIT_WIN32 | |
959a93e7 JG |
475 | GIT_UNUSED(src); |
476 | GIT_UNUSED(dst); | |
477 | GIT_UNUSED(link); | |
2614819c CMN |
478 | return false; |
479 | #else | |
480 | ||
481 | struct stat st_src, st_dst; | |
482 | ||
483 | if (!link) | |
484 | return false; | |
485 | ||
486 | if (p_stat(src, &st_src) < 0) | |
487 | return false; | |
488 | ||
489 | if (p_stat(dst, &st_dst) < 0) | |
490 | return false; | |
491 | ||
492 | return st_src.st_dev == st_dst.st_dev; | |
493 | #endif | |
494 | } | |
495 | ||
8f0104ec | 496 | 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) |
4386d80b | 497 | { |
18d7896c | 498 | int error, flags; |
4386d80b CMN |
499 | git_repository *src; |
500 | git_buf src_odb = GIT_BUF_INIT, dst_odb = GIT_BUF_INIT, src_path = GIT_BUF_INIT; | |
501 | git_buf reflog_message = GIT_BUF_INIT; | |
4386d80b | 502 | |
2614819c | 503 | assert(repo && remote); |
4386d80b CMN |
504 | |
505 | if (!git_repository_is_empty(repo)) { | |
506 | giterr_set(GITERR_INVALID, "the repository is not empty"); | |
507 | return -1; | |
508 | } | |
509 | ||
510 | /* | |
511 | * Let's figure out what path we should use for the source | |
512 | * repo, if it's not rooted, the path should be relative to | |
513 | * the repository's worktree/gitdir. | |
514 | */ | |
18d7896c CMN |
515 | if ((error = git_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0) |
516 | return error; | |
4386d80b CMN |
517 | |
518 | /* Copy .git/objects/ from the source to the target */ | |
519 | if ((error = git_repository_open(&src, git_buf_cstr(&src_path))) < 0) { | |
520 | git_buf_free(&src_path); | |
521 | return error; | |
522 | } | |
523 | ||
524 | git_buf_joinpath(&src_odb, git_repository_path(src), GIT_OBJECTS_DIR); | |
525 | git_buf_joinpath(&dst_odb, git_repository_path(repo), GIT_OBJECTS_DIR); | |
526 | if (git_buf_oom(&src_odb) || git_buf_oom(&dst_odb)) { | |
527 | error = -1; | |
528 | goto cleanup; | |
529 | } | |
530 | ||
2614819c CMN |
531 | flags = 0; |
532 | if (can_link(git_repository_path(src), git_repository_path(repo), link)) | |
533 | flags |= GIT_CPDIR_LINK_FILES; | |
534 | ||
10940736 CMN |
535 | error = git_futils_cp_r(git_buf_cstr(&src_odb), git_buf_cstr(&dst_odb), |
536 | flags, GIT_OBJECT_DIR_MODE); | |
537 | ||
538 | /* | |
539 | * can_link() doesn't catch all variations, so if we hit an | |
540 | * error and did want to link, let's try again without trying | |
541 | * to link. | |
542 | */ | |
543 | if (error < 0 && link) { | |
544 | flags &= ~GIT_CPDIR_LINK_FILES; | |
545 | error = git_futils_cp_r(git_buf_cstr(&src_odb), git_buf_cstr(&dst_odb), | |
546 | flags, GIT_OBJECT_DIR_MODE); | |
547 | } | |
548 | ||
549 | if (error < 0) | |
4386d80b CMN |
550 | goto cleanup; |
551 | ||
552 | git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); | |
553 | ||
8f0104ec | 554 | if ((error = git_remote_fetch(remote, NULL, fetch_opts, git_buf_cstr(&reflog_message))) != 0) |
4386d80b CMN |
555 | goto cleanup; |
556 | ||
659cf202 | 557 | error = checkout_branch(repo, remote, co_opts, branch, git_buf_cstr(&reflog_message)); |
4386d80b CMN |
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 | } |