Commit be5869fc by Vicent Martí

Merge pull request #1143 from ben/clone-options

Options structure for git_clone
parents 37ac4436 b9e7e2b4
...@@ -65,8 +65,9 @@ static int cred_acquire(git_cred **out, const char *url, unsigned int allowed_ty ...@@ -65,8 +65,9 @@ static int cred_acquire(git_cred **out, const char *url, unsigned int allowed_ty
int do_clone(git_repository *repo, int argc, char **argv) int do_clone(git_repository *repo, int argc, char **argv)
{ {
progress_data pd; progress_data pd;
git_remote *origin = NULL;
git_repository *cloned_repo = NULL; git_repository *cloned_repo = NULL;
git_remote *origin;
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT; git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT;
const char *url = argv[1]; const char *url = argv[1];
const char *path = argv[2]; const char *path = argv[2];
...@@ -97,7 +98,10 @@ int do_clone(git_repository *repo, int argc, char **argv) ...@@ -97,7 +98,10 @@ int do_clone(git_repository *repo, int argc, char **argv)
git_remote_set_cred_acquire_cb(origin, cred_acquire, NULL); git_remote_set_cred_acquire_cb(origin, cred_acquire, NULL);
// Do the clone // Do the clone
error = git_clone(&cloned_repo, origin, path, &checkout_opts, &fetch_progress, &pd); clone_opts.checkout_opts = &checkout_opts;
clone_opts.fetch_progress_cb = &fetch_progress;
clone_opts.fetch_progress_payload = &pd;
error = git_clone(&cloned_repo, origin, path, &clone_opts);
git_remote_free(origin); git_remote_free(origin);
printf("\n"); printf("\n");
if (error != 0) { if (error != 0) {
......
...@@ -23,47 +23,52 @@ ...@@ -23,47 +23,52 @@
GIT_BEGIN_DECL GIT_BEGIN_DECL
/** /**
* Clone a remote repository, and checkout the branch pointed to by the remote * Clone options structure
* HEAD.
* *
* @param out pointer that will receive the resulting repository object * Use zeros to indicate default settings. It's easiest to use the
* @param origin_remote a remote which will act as the initial fetch source * `GIT_CLONE_OPTIONS_INIT` macro:
* @param workdir_path local directory to clone to *
* @param fetch_progress_cb optional callback for fetch progress. Be aware that * git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
*
* - `bare` should be set to zero to create a standard repo, non-zero for
* a bare repo
* - `fetch_progress_cb` is optional callback for fetch progress. Be aware that
* this is called inline with network and indexing operations, so performance * this is called inline with network and indexing operations, so performance
* may be affected. * may be affected.
* @param fetch_progress_payload payload for fetch_progress_cb * - `fetch_progress_payload` is payload for fetch_progress_cb
* @param checkout_opts options for the checkout step. If NULL, no checkout * - `checkout_opts` is options for the checkout step. If NULL, no checkout
* is performed * is performed
* @return 0 on success, GIT_ERROR otherwise (use giterr_last for information
* about the error)
*/ */
GIT_EXTERN(int) git_clone(
git_repository **out, typedef struct git_clone_options {
git_remote *origin_remote, unsigned int version;
const char *workdir_path,
git_checkout_opts *checkout_opts, int bare;
git_transfer_progress_callback fetch_progress_cb, git_transfer_progress_callback fetch_progress_cb;
void *fetch_progress_payload); void *fetch_progress_payload;
git_checkout_opts *checkout_opts;
} git_clone_options;
#define GIT_CLONE_OPTIONS_VERSION 1
#define GIT_CLONE_OPTIONS_INIT {GIT_CLONE_OPTIONS_VERSION}
/** /**
* Create a bare clone of a remote repository. * Clone a remote repository, and checkout the branch pointed to by the remote
* HEAD.
* *
* @param out pointer that will receive the resulting repository object * @param out pointer that will receive the resulting repository object
* @param origin_remote a remote which will act as the initial fetch source * @param origin_remote a remote which will act as the initial fetch source
* @param dest_path local directory to clone to * @param local_path local directory to clone to
* @param fetch_progress_cb optional callback for fetch progress. Be aware that * @param options configuration options for the clone. If NULL, the function
* this is called inline with network and indexing operations, so performance * works as though GIT_OPTIONS_INIT were passed.
* may be affected. * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information
* @param fetch_progress_payload payload for fetch_progress_cb * about the error)
* @return 0 on success, GIT_ERROR otherwise (use giterr_last for information about the error)
*/ */
GIT_EXTERN(int) git_clone_bare( GIT_EXTERN(int) git_clone(
git_repository **out, git_repository **out,
git_remote *origin_remote, git_remote *origin,
const char *dest_path, const char *local_path,
git_transfer_progress_callback fetch_progress_cb, const git_clone_options *options);
void *fetch_progress_payload);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
......
...@@ -355,42 +355,25 @@ static int clone_internal( ...@@ -355,42 +355,25 @@ static int clone_internal(
return retcode; return retcode;
} }
int git_clone_bare( int git_clone(
git_repository **out, git_repository **out,
git_remote *origin_remote, git_remote *origin,
const char *dest_path, const char *local_path,
git_transfer_progress_callback fetch_progress_cb, const git_clone_options *options)
void *fetch_progress_payload)
{ {
assert(out && origin_remote && dest_path); git_clone_options dummy_options = GIT_CLONE_OPTIONS_INIT;
return clone_internal(
out,
origin_remote,
dest_path,
fetch_progress_cb,
fetch_progress_payload,
NULL,
1);
}
assert(out && origin && local_path);
if (!options) options = &dummy_options;
int git_clone( GITERR_CHECK_VERSION(options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
git_repository **out,
git_remote *origin_remote,
const char *workdir_path,
git_checkout_opts *checkout_opts,
git_transfer_progress_callback fetch_progress_cb,
void *fetch_progress_payload)
{
assert(out && origin_remote && workdir_path);
return clone_internal( return clone_internal(
out, out,
origin_remote, origin,
workdir_path, local_path,
fetch_progress_cb, options->fetch_progress_cb,
fetch_progress_payload, options->fetch_progress_payload,
checkout_opts, options->checkout_opts,
0); options->bare ? 1 : 0);
} }
...@@ -10,17 +10,20 @@ CL_IN_CATEGORY("network") ...@@ -10,17 +10,20 @@ CL_IN_CATEGORY("network")
static git_repository *g_repo; static git_repository *g_repo;
static git_remote *g_origin; static git_remote *g_origin;
static git_clone_options g_options;
void test_clone_network__initialize(void) void test_clone_network__initialize(void)
{ {
g_repo = NULL; g_repo = NULL;
memset(&g_options, 0, sizeof(git_clone_options));
g_options.version = GIT_CLONE_OPTIONS_VERSION;
cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH)); cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH));
} }
void test_clone_network__cleanup(void) void test_clone_network__cleanup(void)
{ {
git_remote_free(g_origin); git_remote_free(g_origin);
g_origin = NULL;
} }
static void cleanup_repository(void *path) static void cleanup_repository(void *path)
...@@ -37,9 +40,9 @@ void test_clone_network__network_full(void) ...@@ -37,9 +40,9 @@ void test_clone_network__network_full(void)
{ {
git_remote *origin; git_remote *origin;
cl_set_cleanup(&cleanup_repository, "./test2"); cl_set_cleanup(&cleanup_repository, "./foo");
cl_git_pass(git_clone(&g_repo, g_origin, "./test2", NULL, NULL, NULL)); cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options));
cl_assert(!git_repository_is_bare(g_repo)); cl_assert(!git_repository_is_bare(g_repo));
cl_git_pass(git_remote_load(&origin, g_repo, "origin")); cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
...@@ -51,9 +54,10 @@ void test_clone_network__network_bare(void) ...@@ -51,9 +54,10 @@ void test_clone_network__network_bare(void)
{ {
git_remote *origin; git_remote *origin;
cl_set_cleanup(&cleanup_repository, "./test"); cl_set_cleanup(&cleanup_repository, "./foo");
g_options.bare = true;
cl_git_pass(git_clone_bare(&g_repo, g_origin, "./test", NULL, NULL)); cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options));
cl_assert(git_repository_is_bare(g_repo)); cl_assert(git_repository_is_bare(g_repo));
cl_git_pass(git_remote_load(&origin, g_repo, "origin")); cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
...@@ -65,20 +69,19 @@ void test_clone_network__cope_with_already_existing_directory(void) ...@@ -65,20 +69,19 @@ void test_clone_network__cope_with_already_existing_directory(void)
cl_set_cleanup(&cleanup_repository, "./foo"); cl_set_cleanup(&cleanup_repository, "./foo");
p_mkdir("./foo", GIT_DIR_MODE); p_mkdir("./foo", GIT_DIR_MODE);
cl_git_pass(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL)); cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options));
git_repository_free(g_repo); g_repo = NULL;
} }
void test_clone_network__empty_repository(void) void test_clone_network__empty_repository(void)
{ {
git_reference *head; git_reference *head;
cl_set_cleanup(&cleanup_repository, "./empty"); cl_set_cleanup(&cleanup_repository, "./foo");
git_remote_free(g_origin); git_remote_free(g_origin);
cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_EMPTYREPO_URL, GIT_REMOTE_DEFAULT_FETCH)); cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_EMPTYREPO_URL, GIT_REMOTE_DEFAULT_FETCH));
cl_git_pass(git_clone(&g_repo, g_origin, "./empty", NULL, NULL, NULL)); cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options));
cl_assert_equal_i(true, git_repository_is_empty(g_repo)); cl_assert_equal_i(true, git_repository_is_empty(g_repo));
cl_assert_equal_i(true, git_repository_head_orphan(g_repo)); cl_assert_equal_i(true, git_repository_head_orphan(g_repo));
...@@ -93,10 +96,9 @@ void test_clone_network__empty_repository(void) ...@@ -93,10 +96,9 @@ void test_clone_network__empty_repository(void)
void test_clone_network__can_prevent_the_checkout_of_a_standard_repo(void) void test_clone_network__can_prevent_the_checkout_of_a_standard_repo(void)
{ {
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
cl_set_cleanup(&cleanup_repository, "./foo");
cl_set_cleanup(&cleanup_repository, "./no-checkout"); cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options));
cl_git_pass(git_clone(&g_repo, g_origin, "./no-checkout", NULL, NULL, NULL));
cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt")); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path))); cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path)));
...@@ -129,11 +131,13 @@ void test_clone_network__can_checkout_a_cloned_repo(void) ...@@ -129,11 +131,13 @@ void test_clone_network__can_checkout_a_cloned_repo(void)
opts.checkout_strategy = GIT_CHECKOUT_SAFE; opts.checkout_strategy = GIT_CHECKOUT_SAFE;
opts.progress_cb = &checkout_progress; opts.progress_cb = &checkout_progress;
opts.progress_payload = &checkout_progress_cb_was_called; opts.progress_payload = &checkout_progress_cb_was_called;
g_options.checkout_opts = &opts;
g_options.fetch_progress_cb = &fetch_progress;
g_options.fetch_progress_payload = &fetch_progress_cb_was_called;
cl_set_cleanup(&cleanup_repository, "./default-checkout"); cl_set_cleanup(&cleanup_repository, "./foo");
cl_git_pass(git_clone(&g_repo, g_origin, "./default-checkout", &opts, cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options));
&fetch_progress, &fetch_progress_cb_was_called));
cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt")); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&path))); cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&path)));
......
...@@ -5,19 +5,22 @@ ...@@ -5,19 +5,22 @@
#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository" #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
static git_clone_options g_options;
static git_remote *g_origin;
static git_repository *g_repo; static git_repository *g_repo;
static git_remote *g_origin = NULL;
void test_clone_nonetwork__initialize(void) void test_clone_nonetwork__initialize(void)
{ {
g_repo = NULL; g_repo = NULL;
memset(&g_options, 0, sizeof(git_clone_options));
g_options.version = GIT_CLONE_OPTIONS_VERSION;
cl_git_pass(git_remote_new(&g_origin, NULL, "origin", cl_git_fixture_url("testrepo.git"), GIT_REMOTE_DEFAULT_FETCH)); cl_git_pass(git_remote_new(&g_origin, NULL, "origin", cl_git_fixture_url("testrepo.git"), GIT_REMOTE_DEFAULT_FETCH));
} }
void test_clone_nonetwork__cleanup(void) void test_clone_nonetwork__cleanup(void)
{ {
git_remote_free(g_origin); git_remote_free(g_origin);
g_origin = NULL;
} }
static void cleanup_repository(void *path) static void cleanup_repository(void *path)
...@@ -36,35 +39,35 @@ void test_clone_nonetwork__bad_url(void) ...@@ -36,35 +39,35 @@ void test_clone_nonetwork__bad_url(void)
git_remote_free(g_origin); git_remote_free(g_origin);
cl_git_pass(git_remote_new(&g_origin, NULL, "origin", "not_a_repo", GIT_REMOTE_DEFAULT_FETCH)); cl_git_pass(git_remote_new(&g_origin, NULL, "origin", "not_a_repo", GIT_REMOTE_DEFAULT_FETCH));
cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL)); cl_git_fail(git_clone(&g_repo, g_origin, "./foo", &g_options));
cl_assert(!git_path_exists("./foo"));
g_options.bare = true;
cl_git_fail(git_clone(&g_repo, g_origin, "./foo", &g_options));
cl_assert(!git_path_exists("./foo")); cl_assert(!git_path_exists("./foo"));
cl_git_fail(git_clone_bare(&g_repo, g_origin, "./foo.git", NULL, NULL));
cl_assert(!git_path_exists("./foo.git"));
} }
void test_clone_nonetwork__local(void) void test_clone_nonetwork__local(void)
{ {
cl_set_cleanup(&cleanup_repository, "./local"); cl_set_cleanup(&cleanup_repository, "./foo");
cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options));
cl_git_pass(git_clone(&g_repo, g_origin, "./local", NULL, NULL, NULL));
} }
void test_clone_nonetwork__local_absolute_path(void) void test_clone_nonetwork__local_absolute_path(void)
{ {
const char *local_src = cl_fixture("testrepo.git"); const char *local_src = cl_fixture("testrepo.git");
git_remote *local = NULL; git_remote_free(g_origin);
cl_set_cleanup(&cleanup_repository, "./local"); cl_git_pass(git_remote_new(&g_origin, NULL, "origin", local_src, GIT_REMOTE_DEFAULT_FETCH));
cl_git_pass(git_remote_new(&local, NULL, "origin", local_src, GIT_REMOTE_DEFAULT_FETCH)); cl_set_cleanup(&cleanup_repository, "./foo");
cl_git_pass(git_clone(&g_repo, local, "./local", NULL, NULL, NULL));
git_remote_free(local); cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options));
} }
void test_clone_nonetwork__local_bare(void) void test_clone_nonetwork__local_bare(void)
{ {
cl_set_cleanup(&cleanup_repository, "./local.git"); cl_set_cleanup(&cleanup_repository, "./foo");
g_options.bare = true;
cl_git_pass(git_clone_bare(&g_repo, g_origin, "./local.git", NULL, NULL)); cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options));
} }
void test_clone_nonetwork__fail_when_the_target_is_a_file(void) void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
...@@ -72,7 +75,7 @@ void test_clone_nonetwork__fail_when_the_target_is_a_file(void) ...@@ -72,7 +75,7 @@ void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
cl_set_cleanup(&cleanup_repository, "./foo"); cl_set_cleanup(&cleanup_repository, "./foo");
cl_git_mkfile("./foo", "Bar!"); cl_git_mkfile("./foo", "Bar!");
cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL)); cl_git_fail(git_clone(&g_repo, g_origin, "./foo", &g_options));
} }
void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void) void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
...@@ -81,5 +84,5 @@ void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(vo ...@@ -81,5 +84,5 @@ void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(vo
p_mkdir("./foo", GIT_DIR_MODE); p_mkdir("./foo", GIT_DIR_MODE);
cl_git_mkfile("./foo/bar", "Baz!"); cl_git_mkfile("./foo/bar", "Baz!");
cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL)); cl_git_fail(git_clone(&g_repo, g_origin, "./foo", &g_options));
} }
...@@ -11,16 +11,20 @@ CL_IN_CATEGORY("network") ...@@ -11,16 +11,20 @@ CL_IN_CATEGORY("network")
static git_repository *g_repo; static git_repository *g_repo;
static git_remote *g_origin; static git_remote *g_origin;
static git_clone_options g_options;
void test_fetchhead_network__initialize(void) void test_fetchhead_network__initialize(void)
{ {
g_repo = NULL; g_repo = NULL;
memset(&g_options, 0, sizeof(git_clone_options));
g_options.version = GIT_CLONE_OPTIONS_VERSION;
cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH)); cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH));
} }
void test_fetchhead_network__cleanup(void) void test_fetchhead_network__cleanup(void)
{ {
g_origin = NULL; git_remote_free(g_origin);
} }
static void cleanup_repository(void *path) static void cleanup_repository(void *path)
...@@ -36,9 +40,9 @@ static void cleanup_repository(void *path) ...@@ -36,9 +40,9 @@ static void cleanup_repository(void *path)
static void fetchhead_test_clone(void) static void fetchhead_test_clone(void)
{ {
cl_set_cleanup(&cleanup_repository, "./test1"); cl_set_cleanup(&cleanup_repository, "./foo");
cl_git_pass(git_clone(&g_repo, g_origin, "./test1", NULL, NULL, NULL)); cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options));
} }
static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead) static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead)
......
...@@ -86,11 +86,14 @@ static void transferProgressCallback(const git_transfer_progress *stats, void *p ...@@ -86,11 +86,14 @@ static void transferProgressCallback(const git_transfer_progress *stats, void *p
void test_network_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date(void) void test_network_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date(void)
{ {
git_repository *_repository; git_repository *_repository;
git_remote *remote;
bool invoked = false; bool invoked = false;
git_remote *remote, *origin;
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
opts.bare = true;
cl_git_pass(git_remote_new(&origin, NULL, "origin", "https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DEFAULT_FETCH));
cl_git_pass(git_remote_new(&remote, NULL, "origin", "https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DEFAULT_FETCH)); cl_git_pass(git_clone(&_repository, origin, "./fetch/lg2", &opts));
cl_git_pass(git_clone_bare(&_repository, remote, "./fetch/lg2", NULL, NULL));
git_repository_free(_repository); git_repository_free(_repository);
cl_git_pass(git_repository_open(&_repository, "./fetch/lg2")); cl_git_pass(git_repository_open(&_repository, "./fetch/lg2"));
...@@ -108,5 +111,6 @@ void test_network_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_dat ...@@ -108,5 +111,6 @@ void test_network_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_dat
git_remote_disconnect(remote); git_remote_disconnect(remote);
git_remote_free(remote); git_remote_free(remote);
git_remote_free(origin);
git_repository_free(_repository); git_repository_free(_repository);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment