]> git.proxmox.com Git - libgit2.git/blame - src/clone.c
Add git_clone and git_clone_bare.
[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>
9
10#include "git2/clone.h"
11#include "git2/remote.h"
12
13#include "common.h"
14#include "remote.h"
15#include "fileops.h"
16// TODO #include "checkout.h"
17
18GIT_BEGIN_DECL
19
20/*
21 * submodules?
22 * filemodes?
23 */
24
25static int setup_remotes_and_fetch(git_repository *repo, const char *origin_url)
26{
27 int retcode = GIT_ERROR;
28 git_remote *origin = NULL;
29 git_off_t bytes = 0;
30 git_indexer_stats stats = {0};
31
32 if (!git_remote_new(&origin, repo, "origin", origin_url, NULL)) {
33 if (!git_remote_save(origin)) {
34 if (!git_remote_connect(origin, GIT_DIR_FETCH)) {
35 if (!git_remote_download(origin, &bytes, &stats)) {
36 if (!git_remote_update_tips(origin, NULL)) {
37 // TODO
38 // if (!git_checkout(...)) {
39 retcode = 0;
40 // }
41 }
42 }
43 git_remote_disconnect(origin);
44 }
45 }
46 git_remote_free(origin);
47 }
48
49 return retcode;
50}
51
52int git_clone(git_repository **out, const char *origin_url, const char *dest_path)
53{
54 int retcode = GIT_ERROR;
55 git_repository *repo = NULL;
56 char fullpath[512] = {0};
57
58 p_realpath(dest_path, fullpath);
59 if (git_path_exists(fullpath)) {
60 giterr_set(GITERR_INVALID, "Destination already exists: %s", fullpath);
61 return GIT_ERROR;
62 }
63
64 /* Initialize the dest/.git directory */
65 if (!(retcode = git_repository_init(&repo, fullpath, 0))) {
66 if ((retcode = setup_remotes_and_fetch(repo, origin_url)) < 0) {
67 /* Failed to fetch; clean up */
68 git_repository_free(repo);
69 git_futils_rmdir_r(fullpath, GIT_DIRREMOVAL_FILES_AND_DIRS);
70 } else {
71 /* Fetched successfully, do a checkout */
72 /* if (!(retcode = git_checkout(...))) {} */
73 *out = repo;
74 retcode = 0;
75 }
76 }
77
78 return retcode;
79}
80
81
82int git_clone_bare(git_repository **out, const char *origin_url, const char *dest_path)
83{
84 int retcode = GIT_ERROR;
85 git_repository *repo = NULL;
86 char fullpath[512] = {0};
87
88 p_realpath(dest_path, fullpath);
89 if (git_path_exists(fullpath)) {
90 giterr_set(GITERR_INVALID, "Destination already exists: %s", fullpath);
91 return GIT_ERROR;
92 }
93
94 if (!(retcode = git_repository_init(&repo, fullpath, 1))) {
95 if ((retcode = setup_remotes_and_fetch(repo, origin_url)) < 0) {
96 /* Failed to fetch; clean up */
97 git_repository_free(repo);
98 git_futils_rmdir_r(fullpath, GIT_DIRREMOVAL_FILES_AND_DIRS);
99 } else {
100 /* Fetched successfully, do a checkout */
101 /* if (!(retcode = git_checkout(...))) {} */
102 *out = repo;
103 retcode = 0;
104 }
105 }
106
107 return retcode;
108}
109
110
111
112GIT_END_DECL