From 730df6d0f70a343ade75ef9411fe0435b0afd5a9 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 2 Jan 2013 13:43:54 -0800 Subject: [PATCH] Include checkout options inline --- examples/network/clone.c | 2 +- include/git2/clone.h | 8 ++++---- src/clone.c | 7 +++++-- tests-clar/clone/network.c | 13 ++++++++----- tests-clar/clone/nonetwork.c | 4 ++++ 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/examples/network/clone.c b/examples/network/clone.c index 2bfad44e0..9b323ff73 100644 --- a/examples/network/clone.c +++ b/examples/network/clone.c @@ -94,10 +94,10 @@ int do_clone(git_repository *repo, int argc, char **argv) } // Set up options - clone_opts.checkout_opts = &checkout_opts; checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; checkout_opts.progress_cb = checkout_progress; checkout_opts.progress_payload = &pd; + clone_opts.checkout_opts = checkout_opts; clone_opts.fetch_progress_cb = &fetch_progress; clone_opts.fetch_progress_payload = &pd; clone_opts.cred_acquire_cb = cred_acquire; diff --git a/include/git2/clone.h b/include/git2/clone.h index 72fba0ee2..c6ab8032b 100644 --- a/include/git2/clone.h +++ b/include/git2/clone.h @@ -31,14 +31,14 @@ GIT_BEGIN_DECL * * git_clone_options opts = GIT_CLONE_OPTIONS_INIT; * + * - `checkout_opts` is options for the checkout step. To disable checkout, + * set the `checkout_strategy` to GIT_CHECKOUT_DEFAULT. * - `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. * - `fetch_progress_payload` is payload for fetch_progress_cb - * - `checkout_opts` is options for the checkout step. If NULL, no checkout is - * performed * * ** "origin" remote options: ** * - `remote_name` is the name given to the "origin" remote. The default is @@ -62,10 +62,10 @@ GIT_BEGIN_DECL typedef struct git_clone_options { unsigned int version; + git_checkout_opts checkout_opts; int bare; git_transfer_progress_callback fetch_progress_cb; void *fetch_progress_payload; - git_checkout_opts *checkout_opts; const char *remote_name; const char *pushurl; @@ -79,7 +79,7 @@ typedef struct git_clone_options { } git_clone_options; #define GIT_CLONE_OPTIONS_VERSION 1 -#define GIT_CLONE_OPTIONS_INIT {GIT_CLONE_OPTIONS_VERSION} +#define GIT_CLONE_OPTIONS_INIT {GIT_CLONE_OPTIONS_VERSION, {GIT_CHECKOUT_OPTS_VERSION, GIT_CHECKOUT_SAFE}} /** * Clone a remote repository, and checkout the branch pointed to by the remote diff --git a/src/clone.c b/src/clone.c index 9c4a5d9a1..39c0ba26c 100644 --- a/src/clone.c +++ b/src/clone.c @@ -362,6 +362,9 @@ static bool should_checkout( if (!opts) return false; + if (opts->checkout_strategy == GIT_CHECKOUT_DEFAULT) + return false; + return !git_repository_head_orphan(repo); } @@ -406,8 +409,8 @@ int git_clone( } } - if (!retcode && should_checkout(repo, normOptions.bare, normOptions.checkout_opts)) - retcode = git_checkout_head(*out, normOptions.checkout_opts); + if (!retcode && should_checkout(repo, normOptions.bare, &normOptions.checkout_opts)) + retcode = git_checkout_head(*out, &normOptions.checkout_opts); return retcode; } diff --git a/tests-clar/clone/network.c b/tests-clar/clone/network.c index 154fbe829..885098779 100644 --- a/tests-clar/clone/network.c +++ b/tests-clar/clone/network.c @@ -13,10 +13,14 @@ static git_clone_options g_options; void test_clone_network__initialize(void) { + git_checkout_opts dummy_opts = GIT_CHECKOUT_OPTS_INIT; + g_repo = NULL; memset(&g_options, 0, sizeof(git_clone_options)); g_options.version = GIT_CLONE_OPTIONS_VERSION; + g_options.checkout_opts = dummy_opts; + g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; } static void cleanup_repository(void *path) @@ -88,6 +92,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"); + g_options.checkout_opts.checkout_strategy = 0; cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options)); cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt")); @@ -112,16 +117,14 @@ static void fetch_progress(const git_transfer_progress *stats, void *payload) void test_clone_network__can_checkout_a_cloned_repo(void) { - git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; git_buf path = GIT_BUF_INIT; git_reference *head; bool checkout_progress_cb_was_called = false, fetch_progress_cb_was_called = false; - 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.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; + g_options.checkout_opts.progress_cb = &checkout_progress; + g_options.checkout_opts.progress_payload = &checkout_progress_cb_was_called; g_options.fetch_progress_cb = &fetch_progress; g_options.fetch_progress_payload = &fetch_progress_cb_was_called; diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c index 919680317..51fedabb3 100644 --- a/tests-clar/clone/nonetwork.c +++ b/tests-clar/clone/nonetwork.c @@ -10,10 +10,14 @@ static git_repository *g_repo; void test_clone_nonetwork__initialize(void) { + git_checkout_opts dummy_opts = GIT_CHECKOUT_OPTS_INIT; + g_repo = NULL; memset(&g_options, 0, sizeof(git_clone_options)); g_options.version = GIT_CLONE_OPTIONS_VERSION; + g_options.checkout_opts = dummy_opts; + g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; } static void cleanup_repository(void *path)