]> git.proxmox.com Git - libgit2.git/blame - src/clone.c
Checkout: only walk tree once while checking out.
[libgit2.git] / src / clone.c
CommitLineData
764df57e
BS
1/*
2 * Copyright (C) 2009-2012 the libgit2 contributors
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
BS
9
10#ifndef GIT_WIN32
acdd3d95 11#include <dirent.h>
830388a7 12#endif
764df57e
BS
13
14#include "git2/clone.h"
15#include "git2/remote.h"
bb1f6087 16#include "git2/revparse.h"
4fbc899a
BS
17#include "git2/branch.h"
18#include "git2/config.h"
14741d62 19#include "git2/checkout.h"
2b63db4c
BS
20#include "git2/commit.h"
21#include "git2/tree.h"
764df57e
BS
22
23#include "common.h"
24#include "remote.h"
25#include "fileops.h"
4fbc899a 26#include "refs.h"
764df57e
BS
27
28GIT_BEGIN_DECL
29
4fbc899a
BS
30struct HeadInfo {
31 git_repository *repo;
32 git_oid remote_head_oid;
33 git_buf branchname;
34};
bb1f6087 35
2b63db4c 36static int create_tracking_branch(git_repository *repo, const git_oid *target, const char *name)
4fbc899a
BS
37{
38 git_object *head_obj = NULL;
39 git_oid branch_oid;
40 int retcode = GIT_ERROR;
4fbc899a
BS
41
42 /* Find the target commit */
af58ec9e 43 if (git_object_lookup(&head_obj, repo, target, GIT_OBJ_ANY) < 0)
4fbc899a
BS
44 return GIT_ERROR;
45
46 /* Create the new branch */
af58ec9e 47 if (!git_branch_create(&branch_oid, repo, name, head_obj, 0)) {
4fbc899a
BS
48 /* Set up tracking */
49 git_config *cfg;
af58ec9e 50 if (!git_repository_config(&cfg, repo)) {
4fbc899a
BS
51 git_buf remote = GIT_BUF_INIT;
52 git_buf merge = GIT_BUF_INIT;
53 git_buf merge_target = GIT_BUF_INIT;
af58ec9e
BS
54 if (!git_buf_printf(&remote, "branch.%s.remote", name) &&
55 !git_buf_printf(&merge, "branch.%s.merge", name) &&
56 !git_buf_printf(&merge_target, "refs/heads/%s", name) &&
4fbc899a
BS
57 !git_config_set_string(cfg, git_buf_cstr(&remote), "origin") &&
58 !git_config_set_string(cfg, git_buf_cstr(&merge), git_buf_cstr(&merge_target))) {
59 retcode = 0;
60 }
61 git_buf_free(&remote);
62 git_buf_free(&merge);
63 git_buf_free(&merge_target);
64 git_config_free(cfg);
65 }
66 }
67
af58ec9e 68 git_object_free(head_obj);
4fbc899a
BS
69 return retcode;
70}
71
72static int reference_matches_remote_head(const char *head_name, void *payload)
73{
74 struct HeadInfo *head_info = (struct HeadInfo *)payload;
75 git_oid oid;
76
77 /* Stop looking if we've already found a match */
78 if (git_buf_len(&head_info->branchname) > 0) return 0;
79
80 if (!git_reference_name_to_oid(&oid, head_info->repo, head_name) &&
81 !git_oid_cmp(&head_info->remote_head_oid, &oid)) {
cb2dc0b0
BS
82 git_buf_puts(&head_info->branchname,
83 head_name+strlen("refs/remotes/origin/"));
4fbc899a 84 }
8340dd5d
BS
85 return 0;
86}
87
2b63db4c 88static int update_head_to_new_branch(git_repository *repo, const git_oid *target, const char *name)
af58ec9e
BS
89{
90 int retcode = GIT_ERROR;
91
92 if (!create_tracking_branch(repo, target, name)) {
93 git_reference *head;
acdd3d95 94 if (!git_reference_lookup(&head, repo, GIT_HEAD_FILE)) {
2b63db4c
BS
95 git_buf targetbuf = GIT_BUF_INIT;
96 if (!git_buf_printf(&targetbuf, "refs/heads/%s", name) &&
97 !git_reference_set_target(head, git_buf_cstr(&targetbuf))) {
98 /* Read the tree into the index */
99 git_commit *commit;
100 if (!git_commit_lookup(&commit, repo, target)) {
101 git_tree *tree;
102 if (!git_commit_tree(&tree, commit)) {
103 git_index *index;
104 if (!git_repository_index(&index, repo)) {
105 if (!git_index_read_tree(index, tree)) {
106 git_index_write(index);
107 retcode = 0;
108 }
109 git_index_free(index);
110 }
111 git_tree_free(tree);
112 }
113 git_commit_free(commit);
114 }
af58ec9e 115 }
2b63db4c 116 git_buf_free(&targetbuf);
af58ec9e
BS
117 git_reference_free(head);
118 }
119 }
120
121 return retcode;
122}
123
8340dd5d
BS
124static int update_head_to_remote(git_repository *repo, git_remote *remote)
125{
acdd3d95 126 int retcode = GIT_ERROR;
4fbc899a 127 git_remote_head *remote_head;
af58ec9e 128 git_oid oid;
4fbc899a 129 struct HeadInfo head_info;
8340dd5d
BS
130
131 /* Get the remote's HEAD. This is always the first ref in remote->refs. */
4fbc899a
BS
132 remote_head = remote->refs.contents[0];
133 git_oid_cpy(&head_info.remote_head_oid, &remote_head->oid);
134 git_buf_init(&head_info.branchname, 16);
135 head_info.repo = repo;
136
af58ec9e
BS
137 /* Check to see if "master" matches the remote head */
138 if (!git_reference_name_to_oid(&oid, repo, "refs/remotes/origin/master") &&
139 !git_oid_cmp(&remote_head->oid, &oid)) {
acdd3d95 140 retcode = update_head_to_new_branch(repo, &oid, "master");
af58ec9e
BS
141 }
142 /* Not master. Check all the other refs. */
143 else if (!git_reference_foreach(repo, GIT_REF_LISTALL,
144 reference_matches_remote_head,
145 &head_info) &&
acdd3d95
BS
146 git_buf_len(&head_info.branchname) > 0) {
147 retcode = update_head_to_new_branch(repo, &head_info.remote_head_oid,
148 git_buf_cstr(&head_info.branchname));
4fbc899a
BS
149 }
150
151 git_buf_free(&head_info.branchname);
8340dd5d 152 return retcode;
bb1f6087
BS
153}
154
764df57e
BS
155/*
156 * submodules?
157 * filemodes?
4fbc899a 158 * Line endings
764df57e
BS
159 */
160
bb1f6087
BS
161
162
f2a855d5
BS
163static int setup_remotes_and_fetch(git_repository *repo,
164 const char *origin_url,
2b63db4c 165 git_indexer_stats *stats)
764df57e 166{
8340dd5d
BS
167 int retcode = GIT_ERROR;
168 git_remote *origin = NULL;
169 git_off_t bytes = 0;
170 git_indexer_stats dummy_stats;
171
172 if (!stats) stats = &dummy_stats;
173
174 /* Create the "origin" remote */
175 if (!git_remote_add(&origin, repo, "origin", origin_url)) {
176 /* Connect and download everything */
177 if (!git_remote_connect(origin, GIT_DIR_FETCH)) {
178 if (!git_remote_download(origin, &bytes, stats)) {
179 /* Create "origin/foo" branches for all remote branches */
180 if (!git_remote_update_tips(origin, NULL)) {
181 /* Point HEAD to the same ref as the remote's head */
2b63db4c 182 if (!update_head_to_remote(repo, origin)) {
8340dd5d
BS
183 retcode = 0;
184 }
185 }
186 }
187 git_remote_disconnect(origin);
764df57e 188 }
8340dd5d
BS
189 git_remote_free(origin);
190 }
764df57e 191
8340dd5d 192 return retcode;
764df57e
BS
193}
194
acdd3d95
BS
195
196static bool is_dot_or_dotdot(const char *name)
197{
198 return (name[0] == '.' &&
199 (name[1] == '\0' ||
200 (name[1] == '.' && name[2] == '\0')));
201}
202
203/* TODO: p_opendir, p_closedir */
204static bool path_is_okay(const char *path)
205{
830388a7
BS
206#ifdef GIT_WIN32
207 HANDLE hFind = INVALID_HANDLE_VALUE;
208 wchar_t *wbuf;
209 WIN32_FIND_DATAW ffd;
210#else
211 DIR *dir = NULL;
acdd3d95 212 struct dirent *e;
830388a7
BS
213#endif
214
acdd3d95
BS
215 bool retval = true;
216
217 /* The path must either not exist, or be an empty directory */
218 if (!git_path_exists(path)) return true;
219
220 if (!git_path_isdir(path)) {
221 giterr_set(GITERR_INVALID,
222 "'%s' exists and is not an empty directory", path);
223 return false;
224 }
225
830388a7
BS
226#ifdef GIT_WIN32
227 wbuf = gitwin_to_utf16(path);
228 gitwin_append_utf16(wbuf, "\\*", 2);
229 hFind = FindFirstFileW(wbuf, &ffd);
230 if (INVALID_HANDLE_VALUE != hFind) {
231 retval = false;
232 FindClose(hFind);
233 }
234 git__free(wbuf);
235#else
acdd3d95
BS
236 dir = opendir(path);
237 if (!dir) {
238 giterr_set(GITERR_OS, "Couldn't open '%s'", path);
239 return false;
240 }
241
242 while ((e = readdir(dir)) != NULL) {
243 if (!is_dot_or_dotdot(e->d_name)) {
244 giterr_set(GITERR_INVALID,
245 "'%s' exists and is not an empty directory", path);
246 retval = false;
247 break;
248 }
249 }
acdd3d95 250 closedir(dir);
830388a7
BS
251#endif
252
acdd3d95
BS
253 return retval;
254}
255
256
f2a855d5
BS
257static int clone_internal(git_repository **out,
258 const char *origin_url,
8340dd5d 259 const char *path,
f2a855d5
BS
260 git_indexer_stats *stats,
261 int is_bare)
764df57e 262{
8340dd5d
BS
263 int retcode = GIT_ERROR;
264 git_repository *repo = NULL;
265
acdd3d95 266 if (!path_is_okay(path)) {
8340dd5d
BS
267 return GIT_ERROR;
268 }
269
270 if (!(retcode = git_repository_init(&repo, path, is_bare))) {
2b63db4c 271 if ((retcode = setup_remotes_and_fetch(repo, origin_url, stats)) < 0) {
8340dd5d
BS
272 /* Failed to fetch; clean up */
273 git_repository_free(repo);
274 git_futils_rmdir_r(path, GIT_DIRREMOVAL_FILES_AND_DIRS);
275 } else {
276 *out = repo;
277 retcode = 0;
278 }
279 }
280
281 return retcode;
764df57e
BS
282}
283
f2a855d5
BS
284int git_clone_bare(git_repository **out,
285 const char *origin_url,
286 const char *dest_path,
287 git_indexer_stats *stats)
bb1f6087 288{
14741d62 289 assert(out && origin_url && dest_path);
8340dd5d 290 return clone_internal(out, origin_url, dest_path, stats, 1);
bb1f6087 291}
764df57e 292
bb1f6087 293
f2a855d5
BS
294int git_clone(git_repository **out,
295 const char *origin_url,
296 const char *workdir_path,
297 git_indexer_stats *stats)
764df57e 298{
8340dd5d
BS
299 int retcode = GIT_ERROR;
300
14741d62
BS
301 assert(out && origin_url && workdir_path);
302
8340dd5d 303 if (!(retcode = clone_internal(out, origin_url, workdir_path, stats, 0))) {
14741d62
BS
304 git_indexer_stats checkout_stats;
305 retcode = git_checkout_force(*out, &checkout_stats);
8340dd5d
BS
306 }
307
308 return retcode;
764df57e
BS
309}
310
311
312
bb1f6087 313
764df57e 314GIT_END_DECL