]> git.proxmox.com Git - libgit2.git/blame - src/clone.c
push: remove own copy of callbacks
[libgit2.git] / src / clone.c
CommitLineData
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
659cf202 27static int clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link);
6812afaf 28
bf0e62a2 29static 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
61static 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
88cleanup:
89 git_buf_free(&remote_key);
90 git_buf_free(&merge_key);
91 return error;
92}
93
94static 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 113static 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
141static 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 196cleanup:
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
203static 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
225cleanup:
226 git_reference_free(remote_ref);
1265b51f 227 git_buf_free(&remote_branch_name);
88aef766
SC
228 return retcode;
229}
230
d58a64e9
CMN
231static 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
238static 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
764df57e
BS
254/*
255 * submodules?
764df57e
BS
256 */
257
b412d563
BS
258static 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;
00998a12 265 git_remote *origin = NULL;
a0b5f785 266 char buf[GIT_PATH_MAX];
1697cd6f
PK
267 git_remote_create_cb remote_create = options->remote_cb;
268 void *payload = options->remote_cb_payload;
a0b5f785
CMN
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 }
b412d563 277
1697cd6f
PK
278 if (!remote_create) {
279 remote_create = default_remote_create;
280 payload = (void *)&options->remote_callbacks;
281 }
b9bf5d70 282
1697cd6f 283 if ((error = remote_create(&origin, repo, "origin", url, payload)) < 0)
b412d563
BS
284 goto on_error;
285
621b50e4
BS
286 if ((error = git_remote_save(origin)) < 0)
287 goto on_error;
288
b412d563
BS
289 *out = origin;
290 return 0;
291
292on_error:
00998a12 293 git_remote_free(origin);
b412d563
BS
294 return error;
295}
bb1f6087 296
4d968f13 297static bool should_checkout(
298 git_repository *repo,
299 bool is_bare,
6affd71f 300 const git_checkout_options *opts)
4d968f13 301{
302 if (is_bare)
303 return false;
304
305 if (!opts)
306 return false;
307
2850252a 308 if (opts->checkout_strategy == GIT_CHECKOUT_NONE)
730df6d0
BS
309 return false;
310
605da51a 311 return !git_repository_head_unborn(repo);
4d968f13 312}
acdd3d95 313
659cf202 314static int checkout_branch(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, const char *reflog_message)
4386d80b
CMN
315{
316 int error;
317
318 if (branch)
319 error = update_head_to_branch(repo, git_remote_name(remote), branch,
659cf202 320 reflog_message);
4386d80b
CMN
321 /* Point HEAD to the same ref as the remote's head */
322 else
659cf202 323 error = update_head_to_remote(repo, remote, reflog_message);
4386d80b
CMN
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
659cf202 331static int clone_into(git_repository *repo, git_remote *_remote, const git_checkout_options *co_opts, const char *branch)
d19870d9 332{
60cdf495 333 int error;
94f263f5 334 git_buf reflog_message = GIT_BUF_INIT;
3c607685
CMN
335 git_remote *remote;
336 const git_remote_callbacks *callbacks;
d19870d9 337
3c607685 338 assert(repo && _remote);
d19870d9
CMN
339
340 if (!git_repository_is_empty(repo)) {
341 giterr_set(GITERR_INVALID, "the repository is not empty");
342 return -1;
343 }
344
3c607685 345 if ((error = git_remote_dup(&remote, _remote)) < 0)
266af6d8
CMN
346 return error;
347
3c607685
CMN
348 callbacks = git_remote_get_callbacks(_remote);
349 if (!giterr__check_version(callbacks, 1, "git_remote_callbacks") &&
60cdf495 350 (error = git_remote_set_callbacks(remote, callbacks)) < 0)
3c607685
CMN
351 goto cleanup;
352
d19870d9 353 if ((error = git_remote_add_fetch(remote, "refs/tags/*:refs/tags/*")) < 0)
3c607685 354 goto cleanup;
d19870d9 355
d19870d9 356 git_remote_set_update_fetchhead(remote, 0);
94f263f5 357 git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
d19870d9 358
659cf202 359 if ((error = git_remote_fetch(remote, NULL, git_buf_cstr(&reflog_message))) != 0)
d19870d9
CMN
360 goto cleanup;
361
659cf202 362 error = checkout_branch(repo, remote, co_opts, branch, git_buf_cstr(&reflog_message));
d19870d9
CMN
363
364cleanup:
3c607685 365 git_remote_free(remote);
94f263f5 366 git_buf_free(&reflog_message);
d19870d9
CMN
367
368 return error;
369}
370
529fd30d 371int git_clone__should_clone_local(const char *url_or_path, git_clone_local_t local)
121b2673 372{
529fd30d
ET
373 git_buf fromurl = GIT_BUF_INIT;
374 const char *path = url_or_path;
375 bool is_url, is_local;
121b2673
CMN
376
377 if (local == GIT_CLONE_NO_LOCAL)
529fd30d 378 return 0;
121b2673 379
84a85d1b 380 if ((is_url = git_path_is_local_file_url(url_or_path)) != 0) {
529fd30d
ET
381 if (git_path_fromurl(&fromurl, url_or_path) < 0) {
382 is_local = -1;
383 goto done;
384 }
121b2673 385
529fd30d
ET
386 path = fromurl.ptr;
387 }
121b2673 388
529fd30d
ET
389 is_local = (!is_url || local != GIT_CLONE_LOCAL_AUTO) &&
390 git_path_isdir(path);
121b2673 391
529fd30d
ET
392done:
393 git_buf_free(&fromurl);
394 return is_local;
121b2673
CMN
395}
396
b412d563 397int git_clone(
bf0e62a2 398 git_repository **out,
b412d563
BS
399 const char *url,
400 const char *local_path,
fdc7e5e3 401 const git_clone_options *_options)
764df57e 402{
219d3457 403 int error = 0;
ea817863 404 git_repository *repo = NULL;
e3a92f0d 405 git_remote *origin;
fdc7e5e3 406 git_clone_options options = GIT_CLONE_OPTIONS_INIT;
219d3457 407 uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
d58a64e9 408 git_repository_create_cb repository_cb;
b412d563
BS
409
410 assert(out && url && local_path);
ea817863 411
fdc7e5e3
CMN
412 if (_options)
413 memcpy(&options, _options, sizeof(git_clone_options));
414
415 GITERR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
b412d563 416
f6f48f90
RB
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,
46779411 420 "'%s' exists and is not an empty directory", local_path);
219d3457 421 return GIT_EEXISTS;
ea817863
BS
422 }
423
219d3457
RB
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;
926acbcf 427
d58a64e9
CMN
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)
219d3457 434 return error;
e3a92f0d 435
219d3457 436 if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
84a85d1b 437 int clone_local = git_clone__should_clone_local(url, options.local);
529fd30d
ET
438 int link = options.local != GIT_CLONE_LOCAL_NO_LINKS;
439
84a85d1b 440 if (clone_local == 1)
6812afaf 441 error = clone_local_into(
4386d80b 442 repo, origin, &options.checkout_opts,
659cf202 443 options.checkout_branch, link);
84a85d1b 444 else if (clone_local == 0)
6812afaf 445 error = clone_into(
4386d80b 446 repo, origin, &options.checkout_opts,
659cf202 447 options.checkout_branch);
529fd30d
ET
448 else
449 error = -1;
926acbcf 450
219d3457
RB
451 git_remote_free(origin);
452 }
926acbcf 453
8f1066a0 454 if (error != 0) {
1df8ad01
ET
455 git_error_state last_error = {0};
456 giterr_capture(&last_error, error);
457
219d3457
RB
458 git_repository_free(repo);
459 repo = NULL;
8f1066a0 460
219d3457 461 (void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
1df8ad01
ET
462
463 giterr_restore(&last_error);
219d3457 464 }
ea817863 465
e3a92f0d 466 *out = repo;
219d3457 467 return error;
764df57e 468}
b9f81997 469
702efc89 470int git_clone_init_options(git_clone_options *opts, unsigned int version)
b9f81997 471{
702efc89
RB
472 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
473 opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT);
474 return 0;
b9f81997 475}
4386d80b
CMN
476
477static 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
2614819c
CMN
485static bool can_link(const char *src, const char *dst, int link)
486{
487#ifdef GIT_WIN32
959a93e7
JG
488 GIT_UNUSED(src);
489 GIT_UNUSED(dst);
490 GIT_UNUSED(link);
2614819c
CMN
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
659cf202 509static int clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link)
4386d80b 510{
18d7896c 511 int error, flags;
4386d80b
CMN
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;
4386d80b 515
2614819c 516 assert(repo && remote);
4386d80b
CMN
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 */
18d7896c
CMN
528 if ((error = git_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0)
529 return error;
4386d80b
CMN
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
2614819c
CMN
544 flags = 0;
545 if (can_link(git_repository_path(src), git_repository_path(repo), link))
546 flags |= GIT_CPDIR_LINK_FILES;
547
4386d80b 548 if ((error = git_futils_cp_r(git_buf_cstr(&src_odb), git_buf_cstr(&dst_odb),
2614819c 549 flags, GIT_OBJECT_DIR_MODE)) < 0)
4386d80b
CMN
550 goto cleanup;
551
552 git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
553
659cf202 554 if ((error = git_remote_fetch(remote, NULL, 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
559cleanup:
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}