Commit 44f36f6e3ba40420d0a8bb2977e6ada2b735bc2b

Ben Straub 2012-12-12T19:48:44

Convert clone to use dangling remotes

diff --git a/include/git2/clone.h b/include/git2/clone.h
index 2a02723..8de8e0e 100644
--- a/include/git2/clone.h
+++ b/include/git2/clone.h
@@ -27,7 +27,7 @@ GIT_BEGIN_DECL
  * HEAD.
  *
  * @param out pointer that will receive the resulting repository object
- * @param origin_url repository to clone from
+ * @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
@@ -40,7 +40,7 @@ GIT_BEGIN_DECL
  */
 GIT_EXTERN(int) git_clone(
 		git_repository **out,
-		const char *origin_url,
+		git_remote *origin_remote,
 		const char *workdir_path,
 		git_checkout_opts *checkout_opts,
 		git_transfer_progress_callback fetch_progress_cb,
@@ -50,7 +50,7 @@ GIT_EXTERN(int) git_clone(
  * Create a bare clone of a remote repository.
  *
  * @param out pointer that will receive the resulting repository object
- * @param origin_url repository to clone from
+ * @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
@@ -60,7 +60,7 @@ GIT_EXTERN(int) git_clone(
  */
 GIT_EXTERN(int) git_clone_bare(
 		git_repository **out,
-		const char *origin_url,
+		git_remote *origin_remote,
 		const char *dest_path,
 		git_transfer_progress_callback fetch_progress_cb,
 		void *fetch_progress_payload);
diff --git a/src/clone.c b/src/clone.c
index 7d49b39..55638fd 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -262,15 +262,14 @@ cleanup:
 
 static int setup_remotes_and_fetch(
 		git_repository *repo,
-		const char *origin_url,
+		git_remote *origin,
 		git_transfer_progress_callback progress_cb,
 		void *progress_payload)
 {
 	int retcode = GIT_ERROR;
-	git_remote *origin = NULL;
 
-	/* Create the "origin" remote */
-	if (!git_remote_add(&origin, repo, GIT_REMOTE_ORIGIN, origin_url)) {
+	/* Add the origin remote */
+	if (!git_remote_set_repository(origin, repo) && !git_remote_save(origin)) {
 		/*
 		 * Don't write FETCH_HEAD, we'll check out the remote tracking
 		 * branch ourselves based on the server's default.
@@ -325,7 +324,7 @@ static bool should_checkout(
 
 static int clone_internal(
 	git_repository **out,
-	const char *origin_url,
+	git_remote *origin_remote,
 	const char *path,
 	git_transfer_progress_callback fetch_progress_cb,
 	void *fetch_progress_payload,
@@ -340,7 +339,7 @@ static int clone_internal(
 	}
 
 	if (!(retcode = git_repository_init(&repo, path, is_bare))) {
-		if ((retcode = setup_remotes_and_fetch(repo, origin_url,
+		if ((retcode = setup_remotes_and_fetch(repo, origin_remote,
 						fetch_progress_cb, fetch_progress_payload)) < 0) {
 			/* Failed to fetch; clean up */
 			git_repository_free(repo);
@@ -359,16 +358,16 @@ static int clone_internal(
 
 int git_clone_bare(
 		git_repository **out,
-		const char *origin_url,
+		git_remote *origin_remote,
 		const char *dest_path,
 		git_transfer_progress_callback fetch_progress_cb,
 		void *fetch_progress_payload)
 {
-	assert(out && origin_url && dest_path);
+	assert(out && origin_remote && dest_path);
 
 	return clone_internal(
 		out,
-		origin_url,
+		origin_remote,
 		dest_path,
 		fetch_progress_cb,
 		fetch_progress_payload,
@@ -379,17 +378,17 @@ int git_clone_bare(
 
 int git_clone(
 		git_repository **out,
-		const char *origin_url,
+		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_url && workdir_path);
+	assert(out && origin_remote && workdir_path);
 
 	return clone_internal(
 		out,
-		origin_url,
+		origin_remote,
 		workdir_path,
 		fetch_progress_cb,
 		fetch_progress_payload,
diff --git a/src/remote.c b/src/remote.c
index f430cd0..24a821e 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -86,6 +86,8 @@ cleanup:
 int git_remote_new(git_remote **out, git_repository *repo, const char *name, const char *url, const char *fetch)
 {
 	git_remote *remote;
+	git_buf fetchbuf = GIT_BUF_INIT;
+	int error = -1;
 
 	/* name is optional */
 	assert(out && url);
@@ -98,20 +100,26 @@ int git_remote_new(git_remote **out, git_repository *repo, const char *name, con
 	remote->update_fetchhead = 1;
 
 	if (git_vector_init(&remote->refs, 32, NULL) < 0)
-		return -1;
+		goto on_error;
 
 	remote->url = git__strdup(url);
 	GITERR_CHECK_ALLOC(remote->url);
 
 	if (name != NULL) {
-		int error;
 		if ((error = ensure_remote_name_is_valid(name)) < 0) {
-			git_remote_free(remote);
-			return error;
+			error = GIT_EINVALIDSPEC;
+			goto on_error;
 		}
 
 		remote->name = git__strdup(name);
 		GITERR_CHECK_ALLOC(remote->name);
+
+		/* An empty name indicates to use a sensible default for the fetchspec. */
+		if (fetch && strlen(fetch) == 0) {
+			if (git_buf_printf(&fetchbuf, "+refs/heads/*:refs/remotes/%s/*", remote->name) < 0)
+				goto on_error;
+			fetch = git_buf_cstr(&fetchbuf);
+		}
 	}
 
 	if (fetch != NULL) {
@@ -125,11 +133,13 @@ int git_remote_new(git_remote **out, git_repository *repo, const char *name, con
 	}
 
 	*out = remote;
+	git_buf_free(&fetchbuf);
 	return 0;
 
 on_error:
 	git_remote_free(remote);
-	return -1;
+	git_buf_free(&fetchbuf);
+	return error;
 }
 
 int git_remote_set_repository(git_remote *remote, git_repository *repo)
diff --git a/tests-clar/clone/network.c b/tests-clar/clone/network.c
index d519e45..5d564f9 100644
--- a/tests-clar/clone/network.c
+++ b/tests-clar/clone/network.c
@@ -5,14 +5,16 @@
 
 CL_IN_CATEGORY("network")
 
-#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
-#define LIVE_EMPTYREPO_URL "git://github.com/libgit2/TestEmptyRepository"
+#define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
+#define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
 
 static git_repository *g_repo;
+static git_remote *g_origin;
 
 void test_clone_network__initialize(void)
 {
 	g_repo = NULL;
+	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, ""));
 }
 
 static void cleanup_repository(void *path)
@@ -31,7 +33,7 @@ void test_clone_network__network_full(void)
 
 	cl_set_cleanup(&cleanup_repository, "./test2");
 
-	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./test2", NULL, NULL, NULL));
+	cl_git_pass(git_clone(&g_repo, g_origin, "./test2", NULL, NULL, NULL));
 	cl_assert(!git_repository_is_bare(g_repo));
 	cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
 
@@ -45,7 +47,7 @@ void test_clone_network__network_bare(void)
 
 	cl_set_cleanup(&cleanup_repository, "./test");
 
-	cl_git_pass(git_clone_bare(&g_repo, LIVE_REPO_URL, "./test", NULL, NULL));
+	cl_git_pass(git_clone_bare(&g_repo, g_origin, "./test", NULL, NULL));
 	cl_assert(git_repository_is_bare(g_repo));
 	cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
 
@@ -57,7 +59,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, LIVE_REPO_URL, "./foo", NULL, NULL, NULL));
+	cl_git_pass(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL));
 	git_repository_free(g_repo); g_repo = NULL;
 }
 
@@ -67,7 +69,10 @@ void test_clone_network__empty_repository(void)
 
 	cl_set_cleanup(&cleanup_repository, "./empty");
 
-	cl_git_pass(git_clone(&g_repo, LIVE_EMPTYREPO_URL, "./empty", NULL, NULL, NULL));
+	git_remote_free(g_origin);
+	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_EMPTYREPO_URL, ""));
+
+	cl_git_pass(git_clone(&g_repo, g_origin, "./empty", NULL, NULL, NULL));
 
 	cl_assert_equal_i(true, git_repository_is_empty(g_repo));
 	cl_assert_equal_i(true, git_repository_head_orphan(g_repo));
@@ -85,7 +90,7 @@ void test_clone_network__can_prevent_the_checkout_of_a_standard_repo(void)
 
 	cl_set_cleanup(&cleanup_repository, "./no-checkout");
 
-	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./no-checkout", NULL, NULL, NULL));
+	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_assert_equal_i(false, git_path_isfile(git_buf_cstr(&path)));
@@ -121,7 +126,7 @@ void test_clone_network__can_checkout_a_cloned_repo(void)
 
 	cl_set_cleanup(&cleanup_repository, "./default-checkout");
 
-	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./default-checkout", &opts,
+	cl_git_pass(git_clone(&g_repo, g_origin, "./default-checkout", &opts,
 				&fetch_progress, &fetch_progress_cb_was_called));
 
 	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c
index fbebe54..9a1eed7 100644
--- a/tests-clar/clone/nonetwork.c
+++ b/tests-clar/clone/nonetwork.c
@@ -6,10 +6,12 @@
 #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
 
 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"), ""));
 }
 
 static void cleanup_repository(void *path)
@@ -25,26 +27,27 @@ 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 */
-	cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", NULL, NULL, NULL));
+	git_remote_free(g_origin);
+	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", "not_a_repo", NULL));
+
+	cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL));
 	cl_assert(!git_path_exists("./foo"));
-	cl_git_fail(git_clone_bare(&g_repo, "not_a_repo", "./foo.git", NULL, NULL));
+	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)
 {
-	const char *src = cl_git_fixture_url("testrepo.git");
 	cl_set_cleanup(&cleanup_repository, "./local");
 
-	cl_git_pass(git_clone(&g_repo, src, "./local", NULL, NULL, NULL));
+	cl_git_pass(git_clone(&g_repo, g_origin, "./local", NULL, NULL, NULL));
 }
 
 void test_clone_nonetwork__local_bare(void)
 {
-	const char *src = cl_git_fixture_url("testrepo.git");
 	cl_set_cleanup(&cleanup_repository, "./local.git");
 
-	cl_git_pass(git_clone_bare(&g_repo, src, "./local.git", NULL, NULL));
+	cl_git_pass(git_clone_bare(&g_repo, g_origin, "./local.git", NULL, NULL));
 }
 
 void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
@@ -52,7 +55,8 @@ 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, LIVE_REPO_URL, "./foo", NULL, NULL, NULL));
+	cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL));
+	git_remote_free(g_origin);
 }
 
 void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
@@ -61,5 +65,6 @@ 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, LIVE_REPO_URL, "./foo", NULL, NULL, NULL));
+	cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL));
+	git_remote_free(g_origin);
 }
diff --git a/tests-clar/fetchhead/network.c b/tests-clar/fetchhead/network.c
index 46cb977..0eeac9e 100644
--- a/tests-clar/fetchhead/network.c
+++ b/tests-clar/fetchhead/network.c
@@ -10,10 +10,17 @@ CL_IN_CATEGORY("network")
 #define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
 
 static git_repository *g_repo;
+static git_remote *g_origin;
 
 void test_fetchhead_network__initialize(void)
 {
 	g_repo = NULL;
+	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, ""));
+}
+
+void test_fetchhead_network__cleanup(void)
+{
+	g_origin = NULL;
 }
 
 static void cleanup_repository(void *path)
@@ -31,7 +38,7 @@ static void fetchhead_test_clone(void)
 {
 	cl_set_cleanup(&cleanup_repository, "./test1");
 
-	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./test1", NULL, NULL, NULL));
+	cl_git_pass(git_clone(&g_repo, g_origin, "./test1", NULL, NULL, NULL));
 }
 
 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 a5e2c02..fa3199f 100644
--- a/tests-clar/network/fetch.c
+++ b/tests-clar/network/fetch.c
@@ -89,7 +89,8 @@ void test_network_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_dat
 	git_remote *remote;
 	bool invoked = false;
 
-	cl_git_pass(git_clone_bare(&_repository, "https://github.com/libgit2/TestGitRepository.git", "./fetch/lg2", NULL, NULL));
+	cl_git_pass(git_remote_new(&remote, NULL, "origin", "https://github.com/libgit2/TestGitRepository.git", ""));
+	cl_git_pass(git_clone_bare(&_repository, remote, "./fetch/lg2", NULL, NULL));
 	git_repository_free(_repository);
 
 	cl_git_pass(git_repository_open(&_repository, "./fetch/lg2"));