From 7e610440194615fe5970f5f15266893f02a7f6aa Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 14 Dec 2012 12:21:59 -0800 Subject: [PATCH 1/4] Introduce git_clone_options --- include/git2/clone.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/include/git2/clone.h b/include/git2/clone.h index 8de8e0e29..5dcfeeb0b 100644 --- a/include/git2/clone.h +++ b/include/git2/clone.h @@ -22,6 +22,39 @@ */ GIT_BEGIN_DECL +/** + * Clone options structure + * + * Use zeros to indicate default settings. It's easiest to use the + * `GIT_CLONE_OPTIONS_INIT` macro: + * + * git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + * + * - `out` is a pointer that receives the resulting repository object + * - `origin_remote` is a remote which will act as the initial fetch source + * - `workdir_path` is local directory to clone to + * - `fetch_progress_cb` is optional callback for fetch progress. Be aware that + * this is called inline with network and indexing operations, so performance + * may be affected. + * - `fetch_progress_payload` is payload for fetch_progress_cb + * - `checkout_opts` is options for the checkout step. If NULL, no checkout + * is performed + */ + +typedef struct git_clone_options { + unsigned int version; + + 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; +} git_clone_options; + +#define GIT_CLONE_OPTIONS_VERSION 1 +#define GIT_CLONE_OPTIONS_INIT {GIT_CLONE_OPTIONS_VERSION} + /** * Clone a remote repository, and checkout the branch pointed to by the remote * HEAD. From 18b2d560d376da8bcb601603346b871c05958772 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 14 Dec 2012 13:03:59 -0800 Subject: [PATCH 2/4] Deploy git_clone_options; remove git_clone_bare --- include/git2/clone.h | 46 ++++++-------------------------- src/clone.c | 46 ++++++++------------------------ tests-clar/clone/network.c | 44 +++++++++++++++++-------------- tests-clar/clone/nonetwork.c | 48 ++++++++++++++++++---------------- tests-clar/fetchhead/network.c | 15 +++++++---- tests-clar/network/fetch.c | 12 ++++++--- 6 files changed, 89 insertions(+), 122 deletions(-) diff --git a/include/git2/clone.h b/include/git2/clone.h index 5dcfeeb0b..92fcf7fdd 100644 --- a/include/git2/clone.h +++ b/include/git2/clone.h @@ -32,7 +32,9 @@ GIT_BEGIN_DECL * * - `out` is a pointer that receives the resulting repository object * - `origin_remote` is a remote which will act as the initial fetch source - * - `workdir_path` is local directory to clone to + * - `local_path` is local directory to clone into + * - `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 * may be affected. @@ -46,10 +48,11 @@ typedef struct git_clone_options { git_repository **out; git_remote *origin_remote; - const char *workdir_path; - git_checkout_opts *checkout_opts; + const char *local_path; + int bare; git_transfer_progress_callback fetch_progress_cb; void *fetch_progress_payload; + git_checkout_opts *checkout_opts; } git_clone_options; #define GIT_CLONE_OPTIONS_VERSION 1 @@ -59,44 +62,11 @@ typedef struct git_clone_options { * 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 origin_remote a remote which will act as the initial fetch source - * @param workdir_path local directory to clone to - * @param fetch_progress_cb optional callback for fetch progress. Be aware that - * this is called inline with network and indexing operations, so performance - * may be affected. - * @param fetch_progress_payload payload for fetch_progress_cb - * @param checkout_opts options for the checkout step. If NULL, no checkout - * is performed + * @param options configuration options for the clone * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information * about the error) */ -GIT_EXTERN(int) git_clone( - 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); - -/** - * Create a bare clone of a remote repository. - * - * @param out pointer that will receive the resulting repository object - * @param origin_remote a remote which will act as the initial fetch source - * @param dest_path local directory to clone to - * @param fetch_progress_cb optional callback for fetch progress. Be aware that - * this is called inline with network and indexing operations, so performance - * may be affected. - * @param fetch_progress_payload payload for fetch_progress_cb - * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information about the error) - */ -GIT_EXTERN(int) git_clone_bare( - git_repository **out, - git_remote *origin_remote, - const char *dest_path, - git_transfer_progress_callback fetch_progress_cb, - void *fetch_progress_payload); +GIT_EXTERN(int) git_clone(git_clone_options *options); /** @} */ GIT_END_DECL diff --git a/src/clone.c b/src/clone.c index aa6c43f86..ab22d9ead 100644 --- a/src/clone.c +++ b/src/clone.c @@ -355,42 +355,18 @@ static int clone_internal( return retcode; } -int git_clone_bare( - git_repository **out, - git_remote *origin_remote, - const char *dest_path, - git_transfer_progress_callback fetch_progress_cb, - void *fetch_progress_payload) +int git_clone(git_clone_options *options) { - assert(out && origin_remote && dest_path); + assert(options && options->out && options->origin_remote && options->local_path); + + GITERR_CHECK_VERSION(options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options"); return clone_internal( - out, - origin_remote, - dest_path, - fetch_progress_cb, - fetch_progress_payload, - NULL, - 1); -} - - -int git_clone( - 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( - out, - origin_remote, - workdir_path, - fetch_progress_cb, - fetch_progress_payload, - checkout_opts, - 0); + options->out, + options->origin_remote, + options->local_path, + options->fetch_progress_cb, + options->fetch_progress_payload, + options->checkout_opts, + options->bare ? 1 : 0); } diff --git a/tests-clar/clone/network.c b/tests-clar/clone/network.c index 15ad95b37..49b57bbe5 100644 --- a/tests-clar/clone/network.c +++ b/tests-clar/clone/network.c @@ -9,18 +9,22 @@ CL_IN_CATEGORY("network") #define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository" static git_repository *g_repo; -static git_remote *g_origin; +static git_clone_options g_options; void test_clone_network__initialize(void) { g_repo = NULL; - cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH)); + + memset(&g_options, 0, sizeof(git_clone_options)); + g_options.version = GIT_CLONE_OPTIONS_VERSION; + g_options.out = &g_repo; + g_options.local_path = "./foo"; + cl_git_pass(git_remote_new(&g_options.origin_remote, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH)); } void test_clone_network__cleanup(void) { - git_remote_free(g_origin); - g_origin = NULL; + git_remote_free(g_options.origin_remote); } static void cleanup_repository(void *path) @@ -37,9 +41,9 @@ void test_clone_network__network_full(void) { 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_options)); cl_assert(!git_repository_is_bare(g_repo)); cl_git_pass(git_remote_load(&origin, g_repo, "origin")); @@ -51,9 +55,10 @@ void test_clone_network__network_bare(void) { 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_options)); cl_assert(git_repository_is_bare(g_repo)); cl_git_pass(git_remote_load(&origin, g_repo, "origin")); @@ -65,7 +70,7 @@ void test_clone_network__cope_with_already_existing_directory(void) cl_set_cleanup(&cleanup_repository, "./foo"); 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_options)); git_repository_free(g_repo); g_repo = NULL; } @@ -73,12 +78,12 @@ void test_clone_network__empty_repository(void) { git_reference *head; - cl_set_cleanup(&cleanup_repository, "./empty"); + cl_set_cleanup(&cleanup_repository, "./foo"); - git_remote_free(g_origin); - cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_EMPTYREPO_URL, GIT_REMOTE_DEFAULT_FETCH)); + git_remote_free(g_options.origin_remote); + cl_git_pass(git_remote_new(&g_options.origin_remote, 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_options)); cl_assert_equal_i(true, git_repository_is_empty(g_repo)); cl_assert_equal_i(true, git_repository_head_orphan(g_repo)); @@ -93,10 +98,9 @@ void test_clone_network__empty_repository(void) void test_clone_network__can_prevent_the_checkout_of_a_standard_repo(void) { 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, "./no-checkout", NULL, NULL, NULL)); + cl_git_pass(git_clone(&g_options)); 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))); @@ -129,11 +133,13 @@ void test_clone_network__can_checkout_a_cloned_repo(void) opts.checkout_strategy = GIT_CHECKOUT_SAFE; opts.progress_cb = &checkout_progress; 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, - &fetch_progress, &fetch_progress_cb_was_called)); + cl_git_pass(git_clone(&g_options)); 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))); diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c index 983f9fbc3..3626c544a 100644 --- a/tests-clar/clone/nonetwork.c +++ b/tests-clar/clone/nonetwork.c @@ -5,19 +5,23 @@ #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository" +static git_clone_options g_options; static git_repository *g_repo; -static git_remote *g_origin = NULL; void test_clone_nonetwork__initialize(void) { g_repo = NULL; - cl_git_pass(git_remote_new(&g_origin, NULL, "origin", cl_git_fixture_url("testrepo.git"), GIT_REMOTE_DEFAULT_FETCH)); + + memset(&g_options, 0, sizeof(git_clone_options)); + g_options.version = GIT_CLONE_OPTIONS_VERSION; + g_options.out = &g_repo; + g_options.local_path = "./foo"; + cl_git_pass(git_remote_new(&g_options.origin_remote, NULL, "origin", cl_git_fixture_url("testrepo.git"), GIT_REMOTE_DEFAULT_FETCH)); } void test_clone_nonetwork__cleanup(void) { - git_remote_free(g_origin); - g_origin = NULL; + git_remote_free(g_options.origin_remote); } static void cleanup_repository(void *path) @@ -33,38 +37,38 @@ static void cleanup_repository(void *path) void test_clone_nonetwork__bad_url(void) { /* Clone should clean up the mess if the URL isn't a git repository */ - git_remote_free(g_origin); - cl_git_pass(git_remote_new(&g_origin, NULL, "origin", "not_a_repo", GIT_REMOTE_DEFAULT_FETCH)); + git_remote_free(g_options.origin_remote); + cl_git_pass(git_remote_new(&g_options.origin_remote, 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_options)); + cl_assert(!git_path_exists("./foo")); + g_options.bare = true; + cl_git_fail(git_clone(&g_options)); 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) { - cl_set_cleanup(&cleanup_repository, "./local"); - - cl_git_pass(git_clone(&g_repo, g_origin, "./local", NULL, NULL, NULL)); + cl_set_cleanup(&cleanup_repository, "./foo"); + cl_git_pass(git_clone(&g_options)); } void test_clone_nonetwork__local_absolute_path(void) { const char *local_src = cl_fixture("testrepo.git"); - git_remote *local = NULL; - cl_set_cleanup(&cleanup_repository, "./local"); + git_remote_free(g_options.origin_remote); + cl_git_pass(git_remote_new(&g_options.origin_remote, NULL, "origin", local_src, GIT_REMOTE_DEFAULT_FETCH)); - cl_git_pass(git_remote_new(&local, NULL, "origin", local_src, GIT_REMOTE_DEFAULT_FETCH)); - cl_git_pass(git_clone(&g_repo, local, "./local", NULL, NULL, NULL)); - git_remote_free(local); + cl_set_cleanup(&cleanup_repository, "./foo"); + + cl_git_pass(git_clone(&g_options)); } void test_clone_nonetwork__local_bare(void) { - cl_set_cleanup(&cleanup_repository, "./local.git"); - - cl_git_pass(git_clone_bare(&g_repo, g_origin, "./local.git", NULL, NULL)); + cl_set_cleanup(&cleanup_repository, "./foo"); + g_options.bare = true; + cl_git_pass(git_clone(&g_options)); } void test_clone_nonetwork__fail_when_the_target_is_a_file(void) @@ -72,7 +76,7 @@ void test_clone_nonetwork__fail_when_the_target_is_a_file(void) cl_set_cleanup(&cleanup_repository, "./foo"); cl_git_mkfile("./foo", "Bar!"); - cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL)); + cl_git_fail(git_clone(&g_options)); } void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void) @@ -81,5 +85,5 @@ void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(vo p_mkdir("./foo", GIT_DIR_MODE); 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_options)); } diff --git a/tests-clar/fetchhead/network.c b/tests-clar/fetchhead/network.c index aed5b5ad8..b0d31c51d 100644 --- a/tests-clar/fetchhead/network.c +++ b/tests-clar/fetchhead/network.c @@ -10,17 +10,22 @@ CL_IN_CATEGORY("network") #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository" static git_repository *g_repo; -static git_remote *g_origin; +static git_clone_options g_options; void test_fetchhead_network__initialize(void) { g_repo = NULL; - cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH)); + + memset(&g_options, 0, sizeof(git_clone_options)); + g_options.version = GIT_CLONE_OPTIONS_VERSION; + g_options.out = &g_repo; + g_options.local_path = "./foo"; + cl_git_pass(git_remote_new(&g_options.origin_remote, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH)); } void test_fetchhead_network__cleanup(void) { - g_origin = NULL; + git_remote_free(g_options.origin_remote); } static void cleanup_repository(void *path) @@ -36,9 +41,9 @@ static void cleanup_repository(void *path) 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_options)); } static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead) diff --git a/tests-clar/network/fetch.c b/tests-clar/network/fetch.c index 246435b1b..83c880689 100644 --- a/tests-clar/network/fetch.c +++ b/tests-clar/network/fetch.c @@ -86,11 +86,16 @@ 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) { git_repository *_repository; - git_remote *remote; bool invoked = false; + git_remote *remote; + git_clone_options opts = GIT_CLONE_OPTIONS_INIT; - cl_git_pass(git_remote_new(&remote, NULL, "origin", "https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DEFAULT_FETCH)); - cl_git_pass(git_clone_bare(&_repository, remote, "./fetch/lg2", NULL, NULL)); + opts.out = &_repository; + opts.local_path = "./fetch/lg2"; + opts.bare = true; + cl_git_pass(git_remote_new(&opts.origin_remote, NULL, "origin", "https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DEFAULT_FETCH)); + + cl_git_pass(git_clone(&opts)); git_repository_free(_repository); cl_git_pass(git_repository_open(&_repository, "./fetch/lg2")); @@ -108,5 +113,6 @@ void test_network_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_dat git_remote_disconnect(remote); git_remote_free(remote); + git_remote_free(opts.origin_remote); git_repository_free(_repository); } From 0015b5875eda08fe1e593942710125e9db7896b5 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 14 Dec 2012 13:18:06 -0800 Subject: [PATCH 3/4] Deploy git_clone_options to network sample --- examples/network/clone.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/network/clone.c b/examples/network/clone.c index fd82f449b..740184414 100644 --- a/examples/network/clone.c +++ b/examples/network/clone.c @@ -65,8 +65,8 @@ static int cred_acquire(git_cred **out, const char *url, unsigned int allowed_ty int do_clone(git_repository *repo, int argc, char **argv) { progress_data pd; - git_remote *origin = NULL; git_repository *cloned_repo = NULL; + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT; const char *url = argv[1]; const char *path = argv[2]; @@ -87,18 +87,23 @@ int do_clone(git_repository *repo, int argc, char **argv) checkout_opts.progress_payload = &pd; // Create the origin remote, and set up for auth - error = git_remote_new(&origin, NULL, "origin", url, GIT_REMOTE_DEFAULT_FETCH); + error = git_remote_new(&clone_opts.origin_remote, NULL, "origin", url, GIT_REMOTE_DEFAULT_FETCH); if (error != 0) { const git_error *err = giterr_last(); if (err) printf("ERROR %d: %s\n", err->klass, err->message); else printf("ERROR %d: no detailed info\n", error); return error; } - git_remote_set_cred_acquire_cb(origin, cred_acquire, NULL); + git_remote_set_cred_acquire_cb(clone_opts.origin_remote, cred_acquire, NULL); // Do the clone - error = git_clone(&cloned_repo, origin, path, &checkout_opts, &fetch_progress, &pd); - git_remote_free(origin); + clone_opts.out = &cloned_repo; + clone_opts.local_path = path; + clone_opts.checkout_opts = &checkout_opts; + clone_opts.fetch_progress_cb = &fetch_progress; + clone_opts.fetch_progress_payload = &pd; + error = git_clone(&clone_opts); + git_remote_free(clone_opts.origin_remote); printf("\n"); if (error != 0) { const git_error *err = giterr_last(); From b9e7e2b4e148deb90119d4c2c52d8d9e889527cd Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 14 Dec 2012 13:46:45 -0800 Subject: [PATCH 4/4] Move non-options back out of options struct --- examples/network/clone.c | 11 +++++------ include/git2/clone.h | 18 ++++++++++-------- src/clone.c | 17 ++++++++++++----- tests-clar/clone/network.c | 24 +++++++++++------------- tests-clar/clone/nonetwork.c | 29 ++++++++++++++--------------- tests-clar/fetchhead/network.c | 9 ++++----- tests-clar/network/fetch.c | 10 ++++------ 7 files changed, 60 insertions(+), 58 deletions(-) diff --git a/examples/network/clone.c b/examples/network/clone.c index 740184414..8d598de41 100644 --- a/examples/network/clone.c +++ b/examples/network/clone.c @@ -66,6 +66,7 @@ int do_clone(git_repository *repo, int argc, char **argv) { progress_data pd; 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; const char *url = argv[1]; @@ -87,23 +88,21 @@ int do_clone(git_repository *repo, int argc, char **argv) checkout_opts.progress_payload = &pd; // Create the origin remote, and set up for auth - error = git_remote_new(&clone_opts.origin_remote, NULL, "origin", url, GIT_REMOTE_DEFAULT_FETCH); + error = git_remote_new(&origin, NULL, "origin", url, GIT_REMOTE_DEFAULT_FETCH); if (error != 0) { const git_error *err = giterr_last(); if (err) printf("ERROR %d: %s\n", err->klass, err->message); else printf("ERROR %d: no detailed info\n", error); return error; } - git_remote_set_cred_acquire_cb(clone_opts.origin_remote, cred_acquire, NULL); + git_remote_set_cred_acquire_cb(origin, cred_acquire, NULL); // Do the clone - clone_opts.out = &cloned_repo; - clone_opts.local_path = path; clone_opts.checkout_opts = &checkout_opts; clone_opts.fetch_progress_cb = &fetch_progress; clone_opts.fetch_progress_payload = &pd; - error = git_clone(&clone_opts); - git_remote_free(clone_opts.origin_remote); + error = git_clone(&cloned_repo, origin, path, &clone_opts); + git_remote_free(origin); printf("\n"); if (error != 0) { const git_error *err = giterr_last(); diff --git a/include/git2/clone.h b/include/git2/clone.h index 92fcf7fdd..2b5381e4d 100644 --- a/include/git2/clone.h +++ b/include/git2/clone.h @@ -30,9 +30,6 @@ GIT_BEGIN_DECL * * git_clone_options opts = GIT_CLONE_OPTIONS_INIT; * - * - `out` is a pointer that receives the resulting repository object - * - `origin_remote` is a remote which will act as the initial fetch source - * - `local_path` is local directory to clone into * - `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 @@ -46,9 +43,6 @@ GIT_BEGIN_DECL typedef struct git_clone_options { unsigned int version; - git_repository **out; - git_remote *origin_remote; - const char *local_path; int bare; git_transfer_progress_callback fetch_progress_cb; void *fetch_progress_payload; @@ -62,11 +56,19 @@ typedef struct git_clone_options { * Clone a remote repository, and checkout the branch pointed to by the remote * HEAD. * - * @param options configuration options for the clone + * @param out pointer that will receive the resulting repository object + * @param origin_remote a remote which will act as the initial fetch source + * @param local_path local directory to clone to + * @param options configuration options for the clone. If NULL, the function + * works as though GIT_OPTIONS_INIT were passed. * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information * about the error) */ -GIT_EXTERN(int) git_clone(git_clone_options *options); +GIT_EXTERN(int) git_clone( + git_repository **out, + git_remote *origin, + const char *local_path, + const git_clone_options *options); /** @} */ GIT_END_DECL diff --git a/src/clone.c b/src/clone.c index ab22d9ead..865521bce 100644 --- a/src/clone.c +++ b/src/clone.c @@ -355,16 +355,23 @@ static int clone_internal( return retcode; } -int git_clone(git_clone_options *options) +int git_clone( + git_repository **out, + git_remote *origin, + const char *local_path, + const git_clone_options *options) { - assert(options && options->out && options->origin_remote && options->local_path); + git_clone_options dummy_options = GIT_CLONE_OPTIONS_INIT; + + assert(out && origin && local_path); + if (!options) options = &dummy_options; GITERR_CHECK_VERSION(options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options"); return clone_internal( - options->out, - options->origin_remote, - options->local_path, + out, + origin, + local_path, options->fetch_progress_cb, options->fetch_progress_payload, options->checkout_opts, diff --git a/tests-clar/clone/network.c b/tests-clar/clone/network.c index 49b57bbe5..d3009660e 100644 --- a/tests-clar/clone/network.c +++ b/tests-clar/clone/network.c @@ -9,6 +9,7 @@ CL_IN_CATEGORY("network") #define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository" static git_repository *g_repo; +static git_remote *g_origin; static git_clone_options g_options; void test_clone_network__initialize(void) @@ -17,14 +18,12 @@ void test_clone_network__initialize(void) memset(&g_options, 0, sizeof(git_clone_options)); g_options.version = GIT_CLONE_OPTIONS_VERSION; - g_options.out = &g_repo; - g_options.local_path = "./foo"; - cl_git_pass(git_remote_new(&g_options.origin_remote, 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) { - git_remote_free(g_options.origin_remote); + git_remote_free(g_origin); } static void cleanup_repository(void *path) @@ -43,7 +42,7 @@ void test_clone_network__network_full(void) cl_set_cleanup(&cleanup_repository, "./foo"); - cl_git_pass(git_clone(&g_options)); + cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options)); cl_assert(!git_repository_is_bare(g_repo)); cl_git_pass(git_remote_load(&origin, g_repo, "origin")); @@ -58,7 +57,7 @@ void test_clone_network__network_bare(void) cl_set_cleanup(&cleanup_repository, "./foo"); g_options.bare = true; - cl_git_pass(git_clone(&g_options)); + cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options)); cl_assert(git_repository_is_bare(g_repo)); cl_git_pass(git_remote_load(&origin, g_repo, "origin")); @@ -70,8 +69,7 @@ void test_clone_network__cope_with_already_existing_directory(void) cl_set_cleanup(&cleanup_repository, "./foo"); p_mkdir("./foo", GIT_DIR_MODE); - cl_git_pass(git_clone(&g_options)); - git_repository_free(g_repo); g_repo = NULL; + cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options)); } void test_clone_network__empty_repository(void) @@ -80,10 +78,10 @@ void test_clone_network__empty_repository(void) cl_set_cleanup(&cleanup_repository, "./foo"); - git_remote_free(g_options.origin_remote); - cl_git_pass(git_remote_new(&g_options.origin_remote, NULL, "origin", LIVE_EMPTYREPO_URL, GIT_REMOTE_DEFAULT_FETCH)); + 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_clone(&g_options)); + 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_head_orphan(g_repo)); @@ -100,7 +98,7 @@ void test_clone_network__can_prevent_the_checkout_of_a_standard_repo(void) git_buf path = GIT_BUF_INIT; cl_set_cleanup(&cleanup_repository, "./foo"); - cl_git_pass(git_clone(&g_options)); + cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options)); 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))); @@ -139,7 +137,7 @@ void test_clone_network__can_checkout_a_cloned_repo(void) cl_set_cleanup(&cleanup_repository, "./foo"); - cl_git_pass(git_clone(&g_options)); + cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options)); 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))); diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c index 3626c544a..623a0683f 100644 --- a/tests-clar/clone/nonetwork.c +++ b/tests-clar/clone/nonetwork.c @@ -6,6 +6,7 @@ #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; void test_clone_nonetwork__initialize(void) @@ -14,14 +15,12 @@ void test_clone_nonetwork__initialize(void) memset(&g_options, 0, sizeof(git_clone_options)); g_options.version = GIT_CLONE_OPTIONS_VERSION; - g_options.out = &g_repo; - g_options.local_path = "./foo"; - cl_git_pass(git_remote_new(&g_options.origin_remote, 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) { - git_remote_free(g_options.origin_remote); + git_remote_free(g_origin); } static void cleanup_repository(void *path) @@ -37,38 +36,38 @@ static void cleanup_repository(void *path) void test_clone_nonetwork__bad_url(void) { /* Clone should clean up the mess if the URL isn't a git repository */ - git_remote_free(g_options.origin_remote); - cl_git_pass(git_remote_new(&g_options.origin_remote, NULL, "origin", "not_a_repo", GIT_REMOTE_DEFAULT_FETCH)); + git_remote_free(g_origin); + cl_git_pass(git_remote_new(&g_origin, NULL, "origin", "not_a_repo", GIT_REMOTE_DEFAULT_FETCH)); - cl_git_fail(git_clone(&g_options)); + 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_options)); + cl_git_fail(git_clone(&g_repo, g_origin, "./foo", &g_options)); cl_assert(!git_path_exists("./foo")); } void test_clone_nonetwork__local(void) { cl_set_cleanup(&cleanup_repository, "./foo"); - cl_git_pass(git_clone(&g_options)); + cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options)); } void test_clone_nonetwork__local_absolute_path(void) { const char *local_src = cl_fixture("testrepo.git"); - git_remote_free(g_options.origin_remote); - cl_git_pass(git_remote_new(&g_options.origin_remote, NULL, "origin", local_src, GIT_REMOTE_DEFAULT_FETCH)); + git_remote_free(g_origin); + cl_git_pass(git_remote_new(&g_origin, NULL, "origin", local_src, GIT_REMOTE_DEFAULT_FETCH)); cl_set_cleanup(&cleanup_repository, "./foo"); - cl_git_pass(git_clone(&g_options)); + cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options)); } void test_clone_nonetwork__local_bare(void) { cl_set_cleanup(&cleanup_repository, "./foo"); g_options.bare = true; - cl_git_pass(git_clone(&g_options)); + cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options)); } void test_clone_nonetwork__fail_when_the_target_is_a_file(void) @@ -76,7 +75,7 @@ void test_clone_nonetwork__fail_when_the_target_is_a_file(void) cl_set_cleanup(&cleanup_repository, "./foo"); cl_git_mkfile("./foo", "Bar!"); - cl_git_fail(git_clone(&g_options)); + 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) @@ -85,5 +84,5 @@ void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(vo p_mkdir("./foo", GIT_DIR_MODE); cl_git_mkfile("./foo/bar", "Baz!"); - cl_git_fail(git_clone(&g_options)); + cl_git_fail(git_clone(&g_repo, g_origin, "./foo", &g_options)); } diff --git a/tests-clar/fetchhead/network.c b/tests-clar/fetchhead/network.c index b0d31c51d..d10c891c6 100644 --- a/tests-clar/fetchhead/network.c +++ b/tests-clar/fetchhead/network.c @@ -10,6 +10,7 @@ CL_IN_CATEGORY("network") #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository" static git_repository *g_repo; +static git_remote *g_origin; static git_clone_options g_options; void test_fetchhead_network__initialize(void) @@ -18,14 +19,12 @@ void test_fetchhead_network__initialize(void) memset(&g_options, 0, sizeof(git_clone_options)); g_options.version = GIT_CLONE_OPTIONS_VERSION; - g_options.out = &g_repo; - g_options.local_path = "./foo"; - cl_git_pass(git_remote_new(&g_options.origin_remote, 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) { - git_remote_free(g_options.origin_remote); + git_remote_free(g_origin); } static void cleanup_repository(void *path) @@ -43,7 +42,7 @@ static void fetchhead_test_clone(void) { cl_set_cleanup(&cleanup_repository, "./foo"); - cl_git_pass(git_clone(&g_options)); + cl_git_pass(git_clone(&g_repo, g_origin, "./foo", &g_options)); } static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead) diff --git a/tests-clar/network/fetch.c b/tests-clar/network/fetch.c index 83c880689..4cc23318d 100644 --- a/tests-clar/network/fetch.c +++ b/tests-clar/network/fetch.c @@ -87,15 +87,13 @@ void test_network_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_dat { git_repository *_repository; bool invoked = false; - git_remote *remote; + git_remote *remote, *origin; git_clone_options opts = GIT_CLONE_OPTIONS_INIT; - opts.out = &_repository; - opts.local_path = "./fetch/lg2"; opts.bare = true; - cl_git_pass(git_remote_new(&opts.origin_remote, NULL, "origin", "https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DEFAULT_FETCH)); + cl_git_pass(git_remote_new(&origin, NULL, "origin", "https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DEFAULT_FETCH)); - cl_git_pass(git_clone(&opts)); + cl_git_pass(git_clone(&_repository, origin, "./fetch/lg2", &opts)); git_repository_free(_repository); cl_git_pass(git_repository_open(&_repository, "./fetch/lg2")); @@ -113,6 +111,6 @@ void test_network_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_dat git_remote_disconnect(remote); git_remote_free(remote); - git_remote_free(opts.origin_remote); + git_remote_free(origin); git_repository_free(_repository); }