Commit 2139c9b76c57db4745a9eefa2bb85d3e9ab7c1b4

Carlos Martín Nieto 2014-10-10T17:50:28

Merge pull request #2542 from linquize/fetch-head Do not error out when fetching from second remote

diff --git a/src/remote.c b/src/remote.c
index c659c55..10b5624 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -927,13 +927,15 @@ static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *upda
 	return 0;
 }
 
-static int remote_head_for_ref(git_remote_head **out, git_refspec *spec, git_vector *update_heads, git_reference *ref)
+static int remote_head_for_ref(git_remote_head **out, git_remote *remote, git_refspec *spec, git_vector *update_heads, git_reference *ref)
 {
 	git_reference *resolved_ref = NULL;
 	git_buf remote_name = GIT_BUF_INIT;
 	git_buf upstream_name = GIT_BUF_INIT;
+	git_buf config_key = GIT_BUF_INIT;
 	git_repository *repo;
-	const char *ref_name;
+	git_config *config = NULL;
+	const char *ref_name, *branch_remote;
 	int error = 0;
 
 	assert(out && spec && ref);
@@ -953,6 +955,11 @@ static int remote_head_for_ref(git_remote_head **out, git_refspec *spec, git_vec
 	}
 
 	if ((!git_reference__is_branch(ref_name)) ||
+	    (error = git_repository_config_snapshot(&config, repo)) < 0 ||
+	    (error = git_buf_printf(&config_key, "branch.%s.remote",
+	        ref_name + strlen(GIT_REFS_HEADS_DIR))) < 0 ||
+	    (error = git_config_get_string(&branch_remote, config, git_buf_cstr(&config_key))) < 0 ||
+	    git__strcmp(git_remote_name(remote), branch_remote) ||
 	    (error = git_branch_upstream_name(&upstream_name, repo, ref_name)) < 0 ||
 	    (error = git_refspec_rtransform(&remote_name, spec, upstream_name.ptr)) < 0) {
 		/* Not an error if there is no upstream */
@@ -968,6 +975,8 @@ cleanup:
 	git_reference_free(resolved_ref);
 	git_buf_free(&remote_name);
 	git_buf_free(&upstream_name);
+	git_buf_free(&config_key);
+	git_config_free(config);
 	return error;
 }
 
@@ -996,7 +1005,7 @@ static int git_remote_write_fetchhead(git_remote *remote, git_refspec *spec, git
 	/* Determine what to merge: if refspec was a wildcard, just use HEAD */
 	if (git_refspec_is_wildcard(spec)) {
 		if ((error = git_reference_lookup(&head_ref, remote->repo, GIT_HEAD_FILE)) < 0 ||
-			(error = remote_head_for_ref(&merge_remote_ref, spec, update_heads, head_ref)) < 0)
+			(error = remote_head_for_ref(&merge_remote_ref, remote, spec, update_heads, head_ref)) < 0)
 				goto cleanup;
 	} else {
 		/* If we're fetching a single refspec, that's the only thing that should be in FETCH_HEAD. */
diff --git a/tests/network/fetchlocal.c b/tests/network/fetchlocal.c
index bf0d087..fb266f3 100644
--- a/tests/network/fetchlocal.c
+++ b/tests/network/fetchlocal.c
@@ -127,3 +127,37 @@ void test_network_fetchlocal__clone_into_mirror(void)
 	git_repository_free(repo);
 	cl_fixture_cleanup("./foo.git");
 }
+
+void test_network_fetchlocal__multi_remotes(void)
+{
+	git_repository *repo = cl_git_sandbox_init("testrepo.git");
+	git_remote *test, *test2;
+	git_strarray refnames = {0};
+	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
+
+	callbacks.transfer_progress = transfer_cb;
+	cl_git_pass(git_remote_load(&test, repo, "test"));
+	cl_git_pass(git_remote_set_url(test, cl_git_fixture_url("testrepo.git")));
+	git_remote_set_callbacks(test, &callbacks);
+	cl_git_pass(git_remote_connect(test, GIT_DIRECTION_FETCH));
+	cl_git_pass(git_remote_download(test));
+	cl_git_pass(git_remote_update_tips(test, NULL, NULL));
+
+	cl_git_pass(git_reference_list(&refnames, repo));
+	cl_assert_equal_i(32, (int)refnames.count);
+
+	cl_git_pass(git_remote_load(&test2, repo, "test_with_pushurl"));
+	cl_git_pass(git_remote_set_url(test2, cl_git_fixture_url("testrepo.git")));
+	git_remote_set_callbacks(test2, &callbacks);
+	cl_git_pass(git_remote_connect(test2, GIT_DIRECTION_FETCH));
+	cl_git_pass(git_remote_download(test2));
+	cl_git_pass(git_remote_update_tips(test2, NULL, NULL));
+
+	cl_git_pass(git_reference_list(&refnames, repo));
+	cl_assert_equal_i(44, (int)refnames.count);
+
+	git_strarray_free(&refnames);
+	git_remote_free(test);
+	git_remote_free(test2);
+	git_repository_free(repo);
+}