]> git.proxmox.com Git - libgit2.git/blame - src/clone.c
clone: perform a "local clone" when given a local path
[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
bf0e62a2 27static int create_branch(
7eca3c56 28 git_reference **branch,
29 git_repository *repo,
30 const git_oid *target,
1cc974ab
BS
31 const char *name,
32 const git_signature *signature,
33 const char *log_message)
4fbc899a 34{
cfbe4be3 35 git_commit *head_obj = NULL;
132c2db6 36 git_reference *branch_ref = NULL;
bf0e62a2 37 int error;
ea817863
BS
38
39 /* Find the target commit */
cfbe4be3 40 if ((error = git_commit_lookup(&head_obj, repo, target)) < 0)
bf0e62a2 41 return error;
ea817863
BS
42
43 /* Create the new branch */
1cc974ab 44 error = git_branch_create(&branch_ref, repo, name, head_obj, 0, signature, log_message);
ea817863 45
cfbe4be3 46 git_commit_free(head_obj);
7eca3c56 47
bf0e62a2 48 if (!error)
7eca3c56 49 *branch = branch_ref;
50 else
51 git_reference_free(branch_ref);
52
bf0e62a2 53 return error;
54}
55
56static int setup_tracking_config(
57 git_repository *repo,
58 const char *branch_name,
59 const char *remote_name,
60 const char *merge_target)
61{
62 git_config *cfg;
63 git_buf remote_key = GIT_BUF_INIT, merge_key = GIT_BUF_INIT;
64 int error = -1;
65
66 if (git_repository_config__weakptr(&cfg, repo) < 0)
67 return -1;
68
69 if (git_buf_printf(&remote_key, "branch.%s.remote", branch_name) < 0)
70 goto cleanup;
71
72 if (git_buf_printf(&merge_key, "branch.%s.merge", branch_name) < 0)
73 goto cleanup;
74
75 if (git_config_set_string(cfg, git_buf_cstr(&remote_key), remote_name) < 0)
76 goto cleanup;
77
78 if (git_config_set_string(cfg, git_buf_cstr(&merge_key), merge_target) < 0)
79 goto cleanup;
80
81 error = 0;
82
83cleanup:
84 git_buf_free(&remote_key);
85 git_buf_free(&merge_key);
86 return error;
87}
88
89static int create_tracking_branch(
90 git_reference **branch,
91 git_repository *repo,
92 const git_oid *target,
1cc974ab
BS
93 const char *branch_name,
94 const git_signature *signature,
95 const char *log_message)
bf0e62a2 96{
97 int error;
98
1cc974ab 99 if ((error = create_branch(branch, repo, target, branch_name, signature, log_message)) < 0)
bf0e62a2 100 return error;
101
102 return setup_tracking_config(
103 repo,
104 branch_name,
105 GIT_REMOTE_ORIGIN,
106 git_reference_name(*branch));
4fbc899a
BS
107}
108
bf0e62a2 109static int update_head_to_new_branch(
110 git_repository *repo,
111 const git_oid *target,
94f263f5 112 const char *name,
1cc974ab 113 const git_signature *signature,
94f263f5 114 const char *reflog_message)
af58ec9e 115{
aa4437f6 116 git_reference *tracking_branch = NULL;
4c4408c3 117 int error;
cdb8a608
CMN
118
119 if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR))
120 name += strlen(GIT_REFS_HEADS_DIR);
121
4c4408c3 122 error = create_tracking_branch(&tracking_branch, repo, target, name,
1cc974ab 123 signature, reflog_message);
ea817863 124
dab89f9b
RB
125 if (!error)
126 error = git_repository_set_head(
94f263f5 127 repo, git_reference_name(tracking_branch),
1cc974ab 128 signature, reflog_message);
7eca3c56 129
130 git_reference_free(tracking_branch);
131
32332fcc
CMN
132 /* if it already existed, then the user's refspec created it for us, ignore it' */
133 if (error == GIT_EEXISTS)
134 error = 0;
135
7eca3c56 136 return error;
af58ec9e
BS
137}
138
94f263f5
BS
139static int update_head_to_remote(
140 git_repository *repo,
141 git_remote *remote,
1cc974ab 142 const git_signature *signature,
94f263f5 143 const char *reflog_message)
8340dd5d 144{
2a597116 145 int error = 0, found_branch = 0;
359dce72 146 size_t refs_len;
2a597116 147 git_refspec dummy_spec, *refspec;
359dce72 148 const git_remote_head *remote_head, **refs;
2a597116 149 const git_oid *remote_head_id;
d280c71b 150 git_buf remote_master_name = GIT_BUF_INIT;
cdb8a608 151 git_buf branch = GIT_BUF_INIT;
ea817863 152
dab89f9b
RB
153 if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0)
154 return error;
359dce72 155
bf0e62a2 156 /* Did we just clone an empty repository? */
dab89f9b 157 if (refs_len == 0)
bf0e62a2 158 return setup_tracking_config(
dab89f9b 159 repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE);
bf0e62a2 160
cdb8a608
CMN
161 error = git_remote_default_branch(&branch, remote);
162 if (error == GIT_ENOTFOUND) {
163 git_buf_puts(&branch, GIT_REFS_HEADS_MASTER_FILE);
164 } else {
2a597116 165 found_branch = 1;
cdb8a608
CMN
166 }
167
359dce72
CMN
168 /* Get the remote's HEAD. This is always the first ref in the list. */
169 remote_head = refs[0];
41fb1ca0
PK
170 assert(remote_head);
171
2a597116
CMN
172 remote_head_id = &remote_head->oid;
173 refspec = git_remote__matching_refspec(remote, git_buf_cstr(&branch));
4330ab26 174
2a597116 175 if (refspec == NULL) {
4330ab26 176 memset(&dummy_spec, 0, sizeof(git_refspec));
2a597116 177 refspec = &dummy_spec;
4330ab26 178 }
55ededfd 179
d280c71b 180 /* Determine the remote tracking reference name from the local master */
bf522e08 181 if ((error = git_refspec_transform(
d280c71b 182 &remote_master_name,
2a597116 183 refspec,
cdb8a608 184 git_buf_cstr(&branch))) < 0)
dab89f9b 185 return error;
d280c71b 186
2a597116 187 if (found_branch) {
dab89f9b 188 error = update_head_to_new_branch(
d280c71b 189 repo,
2a597116 190 remote_head_id,
cdb8a608 191 git_buf_cstr(&branch),
1cc974ab 192 signature, reflog_message);
d280c71b 193 } else {
dab89f9b 194 error = git_repository_set_head_detached(
2a597116 195 repo, remote_head_id, signature, reflog_message);
ea817863
BS
196 }
197
d280c71b 198 git_buf_free(&remote_master_name);
cdb8a608 199 git_buf_free(&branch);
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 206 const char *branch,
1cc974ab 207 const git_signature *signature,
94f263f5 208 const char *reflog_message)
88aef766
SC
209{
210 int retcode;
211 git_buf remote_branch_name = GIT_BUF_INIT;
212 git_reference* remote_ref = NULL;
1fed6b07 213
d19870d9 214 assert(remote_name && branch);
88aef766
SC
215
216 if ((retcode = git_buf_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s",
d19870d9 217 remote_name, branch)) < 0 )
88aef766
SC
218 goto cleanup;
219
220 if ((retcode = git_reference_lookup(&remote_ref, repo, git_buf_cstr(&remote_branch_name))) < 0)
221 goto cleanup;
222
1cc974ab
BS
223 retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch,
224 signature, reflog_message);
88aef766
SC
225
226cleanup:
227 git_reference_free(remote_ref);
1265b51f 228 git_buf_free(&remote_branch_name);
88aef766
SC
229 return retcode;
230}
231
764df57e
BS
232/*
233 * submodules?
764df57e
BS
234 */
235
b412d563
BS
236static int create_and_configure_origin(
237 git_remote **out,
238 git_repository *repo,
239 const char *url,
240 const git_clone_options *options)
241{
242 int error;
00998a12 243 git_remote *origin = NULL;
c833893c 244 const char *name;
b412d563 245
c833893c
CMN
246 name = options->remote_name ? options->remote_name : "origin";
247 if ((error = git_remote_create(&origin, repo, name, url)) < 0)
b412d563
BS
248 goto on_error;
249
b9bf5d70
CMN
250 if (options->ignore_cert_errors)
251 git_remote_check_cert(origin, 0);
252
0e0cf787 253 if ((error = git_remote_set_callbacks(origin, &options->remote_callbacks)) < 0)
b412d563
BS
254 goto on_error;
255
621b50e4
BS
256 if ((error = git_remote_save(origin)) < 0)
257 goto on_error;
258
b412d563
BS
259 *out = origin;
260 return 0;
261
262on_error:
00998a12 263 git_remote_free(origin);
b412d563
BS
264 return error;
265}
bb1f6087 266
4d968f13 267static bool should_checkout(
268 git_repository *repo,
269 bool is_bare,
6affd71f 270 const git_checkout_options *opts)
4d968f13 271{
272 if (is_bare)
273 return false;
274
275 if (!opts)
276 return false;
277
2850252a 278 if (opts->checkout_strategy == GIT_CHECKOUT_NONE)
730df6d0
BS
279 return false;
280
605da51a 281 return !git_repository_head_unborn(repo);
4d968f13 282}
acdd3d95 283
4386d80b
CMN
284static 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)
285{
286 int error;
287
288 if (branch)
289 error = update_head_to_branch(repo, git_remote_name(remote), branch,
290 signature, reflog_message);
291 /* Point HEAD to the same ref as the remote's head */
292 else
293 error = update_head_to_remote(repo, remote, signature, reflog_message);
294
295 if (!error && should_checkout(repo, git_repository_is_bare(repo), co_opts))
296 error = git_checkout_head(repo, co_opts);
297
298 return error;
299}
300
3c607685 301int git_clone_into(git_repository *repo, git_remote *_remote, const git_checkout_options *co_opts, const char *branch, const git_signature *signature)
d19870d9 302{
60cdf495 303 int error;
94f263f5 304 git_buf reflog_message = GIT_BUF_INIT;
3c607685
CMN
305 git_remote *remote;
306 const git_remote_callbacks *callbacks;
d19870d9 307
3c607685 308 assert(repo && _remote);
d19870d9
CMN
309
310 if (!git_repository_is_empty(repo)) {
311 giterr_set(GITERR_INVALID, "the repository is not empty");
312 return -1;
313 }
314
3c607685 315 if ((error = git_remote_dup(&remote, _remote)) < 0)
266af6d8
CMN
316 return error;
317
3c607685
CMN
318 callbacks = git_remote_get_callbacks(_remote);
319 if (!giterr__check_version(callbacks, 1, "git_remote_callbacks") &&
60cdf495 320 (error = git_remote_set_callbacks(remote, callbacks)) < 0)
3c607685
CMN
321 goto cleanup;
322
d19870d9 323 if ((error = git_remote_add_fetch(remote, "refs/tags/*:refs/tags/*")) < 0)
3c607685 324 goto cleanup;
d19870d9 325
d19870d9 326 git_remote_set_update_fetchhead(remote, 0);
94f263f5 327 git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
d19870d9 328
c3ab1e5a 329 if ((error = git_remote_fetch(remote, signature, git_buf_cstr(&reflog_message))) != 0)
d19870d9
CMN
330 goto cleanup;
331
4386d80b 332 error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message));
d19870d9
CMN
333
334cleanup:
3c607685 335 git_remote_free(remote);
94f263f5 336 git_buf_free(&reflog_message);
d19870d9
CMN
337
338 return error;
339}
340
b412d563 341int git_clone(
bf0e62a2 342 git_repository **out,
b412d563
BS
343 const char *url,
344 const char *local_path,
fdc7e5e3 345 const git_clone_options *_options)
764df57e 346{
219d3457 347 int error = 0;
ea817863 348 git_repository *repo = NULL;
e3a92f0d 349 git_remote *origin;
fdc7e5e3 350 git_clone_options options = GIT_CLONE_OPTIONS_INIT;
219d3457 351 uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
b412d563
BS
352
353 assert(out && url && local_path);
ea817863 354
fdc7e5e3
CMN
355 if (_options)
356 memcpy(&options, _options, sizeof(git_clone_options));
357
358 GITERR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
b412d563 359
f6f48f90
RB
360 /* Only clone to a new directory or an empty directory */
361 if (git_path_exists(local_path) && !git_path_is_empty_dir(local_path)) {
362 giterr_set(GITERR_INVALID,
46779411 363 "'%s' exists and is not an empty directory", local_path);
219d3457 364 return GIT_EEXISTS;
ea817863
BS
365 }
366
219d3457
RB
367 /* Only remove the root directory on failure if we create it */
368 if (git_path_exists(local_path))
369 rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
926acbcf 370
219d3457
RB
371 if ((error = git_repository_init(&repo, local_path, options.bare)) < 0)
372 return error;
e3a92f0d 373
219d3457 374 if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
4386d80b
CMN
375 if (git__prefixcmp(url, "file://")) {
376 error = git_clone_local_into(
377 repo, origin, &options.checkout_opts,
378 options.checkout_branch, options.signature);
379 } else {
380 error = git_clone_into(
381 repo, origin, &options.checkout_opts,
382 options.checkout_branch, options.signature);
383 }
926acbcf 384
219d3457
RB
385 git_remote_free(origin);
386 }
926acbcf 387
8f1066a0 388 if (error != 0) {
1df8ad01
ET
389 git_error_state last_error = {0};
390 giterr_capture(&last_error, error);
391
219d3457
RB
392 git_repository_free(repo);
393 repo = NULL;
8f1066a0 394
219d3457 395 (void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
1df8ad01
ET
396
397 giterr_restore(&last_error);
219d3457 398 }
ea817863 399
e3a92f0d 400 *out = repo;
219d3457 401 return error;
764df57e 402}
b9f81997 403
702efc89 404int git_clone_init_options(git_clone_options *opts, unsigned int version)
b9f81997 405{
702efc89
RB
406 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
407 opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT);
408 return 0;
b9f81997 409}
4386d80b
CMN
410
411static const char *repository_base(git_repository *repo)
412{
413 if (git_repository_is_bare(repo))
414 return git_repository_path(repo);
415
416 return git_repository_workdir(repo);
417}
418
419int git_clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, const git_signature *signature)
420{
421 int error, root;
422 git_repository *src;
423 git_buf src_odb = GIT_BUF_INIT, dst_odb = GIT_BUF_INIT, src_path = GIT_BUF_INIT;
424 git_buf reflog_message = GIT_BUF_INIT;
425 const char *url;
426
427 assert(repo && remote && co_opts);
428
429 if (!git_repository_is_empty(repo)) {
430 giterr_set(GITERR_INVALID, "the repository is not empty");
431 return -1;
432 }
433
434 /*
435 * Let's figure out what path we should use for the source
436 * repo, if it's not rooted, the path should be relative to
437 * the repository's worktree/gitdir.
438 */
439 url = git_remote_url(remote);
440 if (!git__prefixcmp(url, "file://"))
441 root = strlen("file://");
442 else
443 root = git_path_root(url);
444
445 if (root >= 0)
446 git_buf_puts(&src_path, url + root);
447 else
448 git_buf_joinpath(&src_path, repository_base(repo), url);
449
450 if (git_buf_oom(&src_path))
451 return -1;
452
453 /* Copy .git/objects/ from the source to the target */
454 if ((error = git_repository_open(&src, git_buf_cstr(&src_path))) < 0) {
455 git_buf_free(&src_path);
456 return error;
457 }
458
459 git_buf_joinpath(&src_odb, git_repository_path(src), GIT_OBJECTS_DIR);
460 git_buf_joinpath(&dst_odb, git_repository_path(repo), GIT_OBJECTS_DIR);
461 if (git_buf_oom(&src_odb) || git_buf_oom(&dst_odb)) {
462 error = -1;
463 goto cleanup;
464 }
465
466 if ((error = git_futils_cp_r(git_buf_cstr(&src_odb), git_buf_cstr(&dst_odb),
467 0, GIT_OBJECT_DIR_MODE)) < 0)
468 goto cleanup;
469
470 git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
471
472 if ((error = git_remote_fetch(remote, signature, git_buf_cstr(&reflog_message))) != 0)
473 goto cleanup;
474
475 error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message));
476
477cleanup:
478 git_buf_free(&reflog_message);
479 git_buf_free(&src_path);
480 git_buf_free(&src_odb);
481 git_buf_free(&dst_odb);
482 git_repository_free(src);
483 return error;
484}