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