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