Commit d5e5bbd7193924aa845e107f747a15814a679b10

Vicent Martí 2013-05-06T06:45:40

Merge pull request #1524 from jamill/clone_tagopts By default do not set tagopt when cloning

diff --git a/src/clone.c b/src/clone.c
index aeb7bbf..9cde6f6 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -358,31 +358,41 @@ static int setup_remotes_and_fetch(
 	git_remote *origin;
 
 	/* Construct an origin remote */
-	if (!create_and_configure_origin(&origin, repo, url, options)) {
-		git_remote_set_update_fetchhead(origin, 0);
-
-		/* Connect and download everything */
-		if (!git_remote_connect(origin, GIT_DIRECTION_FETCH)) {
-			if (!(retcode = git_remote_download(origin, options->fetch_progress_cb,
-						options->fetch_progress_payload))) {
-				/* Create "origin/foo" branches for all remote branches */
-				if (!git_remote_update_tips(origin)) {
-					/* Point HEAD to the requested branch */
-					if (options->checkout_branch) {
-						if (!update_head_to_branch(repo, options))
-							retcode = 0;
-					}
-					/* Point HEAD to the same ref as the remote's head */
-					else if (!update_head_to_remote(repo, origin)) {
-						retcode = 0;
-					}
-				}
-			}
-			git_remote_disconnect(origin);
-		}
-		git_remote_free(origin);
-	}
+	if ((retcode = create_and_configure_origin(&origin, repo, url, options)) < 0)
+		goto on_error;
+
+	git_remote_set_update_fetchhead(origin, 0);
+
+	/* If the download_tags value has not been specified, then make sure to
+		* download tags as well. It is set here because we want to download tags
+		* on the initial clone, but do not want to persist the value in the
+		* configuration file.
+		*/
+	if (origin->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_AUTO &&
+		((retcode = git_remote_add_fetch(origin, "refs/tags/*:refs/tags/*")) < 0))
+		goto on_error;
+
+	/* Connect and download everything */
+	if ((retcode = git_remote_connect(origin, GIT_DIRECTION_FETCH)) < 0)
+		goto on_error;
 
+	if ((retcode = git_remote_download(origin, options->fetch_progress_cb,
+		options->fetch_progress_payload)) < 0)
+		goto on_error;
+
+	/* Create "origin/foo" branches for all remote branches */
+	if ((retcode = git_remote_update_tips(origin)) < 0)
+		goto on_error;
+
+	/* Point HEAD to the requested branch */
+	if (options->checkout_branch)
+		retcode = update_head_to_branch(repo, options);
+	/* Point HEAD to the same ref as the remote's head */
+	else
+		retcode = update_head_to_remote(repo, origin);
+
+on_error:
+	git_remote_free(origin);
 	return retcode;
 }
 
@@ -425,7 +435,7 @@ static void normalize_options(git_clone_options *dst, const git_clone_options *s
 
 	/* Provide defaults for null pointers */
 	if (!dst->remote_name) dst->remote_name = "origin";
-	if (!dst->remote_autotag) dst->remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
+	if (!dst->remote_autotag) dst->remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_AUTO;
 }
 
 int git_clone(
diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c
index 545fe3a..8b17fd9 100644
--- a/tests-clar/clone/nonetwork.c
+++ b/tests-clar/clone/nonetwork.c
@@ -172,6 +172,7 @@ void test_clone_nonetwork__custom_push_spec(void)
 
 void test_clone_nonetwork__custom_autotag(void)
 {
+	git_remote *origin;
 	git_strarray tags = {0};
 
 	g_options.remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_NONE;
@@ -180,6 +181,23 @@ void test_clone_nonetwork__custom_autotag(void)
 	cl_git_pass(git_tag_list(&tags, g_repo));
 	cl_assert_equal_sz(0, tags.count);
 
+	cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
+	cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_NONE, origin->download_tags);
+
+	git_strarray_free(&tags);
+}
+
+void test_clone_nonetwork__custom_autotag_tags_all(void)
+{
+	git_strarray tags = {0};
+	git_remote *origin;
+
+	g_options.remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
+	cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
+
+	cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
+	cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_ALL, origin->download_tags);
+
 	git_strarray_free(&tags);
 }
 
diff --git a/tests-clar/online/clone.c b/tests-clar/online/clone.c
index c1a9a9a..aa12e47 100644
--- a/tests-clar/online/clone.c
+++ b/tests-clar/online/clone.c
@@ -3,6 +3,7 @@
 #include "git2/clone.h"
 #include "git2/cred_helpers.h"
 #include "repository.h"
+#include "remote.h"
 
 #define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
 #define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
@@ -42,6 +43,8 @@ void test_online_clone__network_full(void)
 	cl_assert(!git_repository_is_bare(g_repo));
 	cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
 
+	cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, origin->download_tags);
+
 	git_remote_free(origin);
 }