Commit fd61f05ea605155ed9e56bfad0e804c6718e0611

Vicent Marti 2014-04-03T09:53:28

Merge pull request #2238 from libgit2/cmn/upstream-for-unborn Handle an upstream branch for an unborn one

diff --git a/src/remote.c b/src/remote.c
index 62ee903..d3e81b9 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -902,19 +902,32 @@ static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *upda
 static int remote_head_for_ref(git_remote_head **out, git_refspec *spec, git_vector *update_heads, git_reference *ref)
 {
 	git_reference *resolved_ref = NULL;
-	git_reference *tracking_ref = NULL;
 	git_buf remote_name = GIT_BUF_INIT;
+	git_buf upstream_name = GIT_BUF_INIT;
+	git_repository *repo;
+	const char *ref_name;
 	int error = 0;
 
 	assert(out && spec && ref);
 
 	*out = NULL;
 
-	if ((error = git_reference_resolve(&resolved_ref, ref)) < 0 ||
-		(!git_reference_is_branch(resolved_ref)) ||
-		(error = git_branch_upstream(&tracking_ref, resolved_ref)) < 0 ||
-		(error = git_refspec_rtransform(&remote_name, spec, git_reference_name(tracking_ref))) < 0) {
-		/* Not an error if HEAD is unborn or no tracking branch */
+	repo = git_reference_owner(ref);
+
+	error = git_reference_resolve(&resolved_ref, ref);
+
+	/* If we're in an unborn branch, let's pretend nothing happened */
+	if (error == GIT_ENOTFOUND && git_reference_type(ref) == GIT_REF_SYMBOLIC) {
+		ref_name = git_reference_symbolic_target(ref);
+		error = 0;
+	} else {
+		ref_name = git_reference_name(resolved_ref);
+	}
+
+	if ((!git_reference__is_branch(ref_name)) ||
+	    (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 */
 		if (error == GIT_ENOTFOUND)
 			error = 0;
 
@@ -924,9 +937,9 @@ static int remote_head_for_ref(git_remote_head **out, git_refspec *spec, git_vec
 	error = remote_head_for_fetchspec_src(out, update_heads, git_buf_cstr(&remote_name));
 
 cleanup:
-	git_reference_free(tracking_ref);
 	git_reference_free(resolved_ref);
 	git_buf_free(&remote_name);
+	git_buf_free(&upstream_name);
 	return error;
 }
 
diff --git a/tests/clone/nonetwork.c b/tests/clone/nonetwork.c
index 68a2774..4bdc6e1 100644
--- a/tests/clone/nonetwork.c
+++ b/tests/clone/nonetwork.c
@@ -282,3 +282,37 @@ void test_clone_nonetwork__clone_into_updates_reflog_properly(void)
 	git_remote_free(remote);
 	git_signature_free(sig);
 }
+
+static void cleanup_repository(void *path)
+{
+	if (g_repo) {
+		git_repository_free(g_repo);
+		g_repo = NULL;
+	}
+
+	cl_fixture_cleanup((const char *)path);
+}
+
+void test_clone_nonetwork__clone_from_empty_sets_upstream(void)
+{
+	git_config *config;
+	git_repository *repo;
+	const char *str;
+
+	/* Create an empty repo to clone from */
+	cl_set_cleanup(&cleanup_repository, "./test1");
+	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
+	cl_set_cleanup(&cleanup_repository, "./repowithunborn");
+	cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL));
+
+	cl_git_pass(git_repository_config(&config, repo));
+
+	cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
+	cl_assert_equal_s("origin", str);
+	cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
+	cl_assert_equal_s("refs/heads/master", str);
+
+	git_config_free(config);
+	git_repository_free(repo);
+	cl_fixture_cleanup("./repowithunborn");
+}
diff --git a/tests/fetchhead/fetchhead_data.h b/tests/fetchhead/fetchhead_data.h
index 294c9fb..34adb3d 100644
--- a/tests/fetchhead/fetchhead_data.h
+++ b/tests/fetchhead/fetchhead_data.h
@@ -28,4 +28,3 @@
 
 #define FETCH_HEAD_EXPLICIT_DATA \
 	"0966a434eb1a025db6b71485ab63a3bfbea520b6\t\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n"
-
diff --git a/tests/fetchhead/nonetwork.c b/tests/fetchhead/nonetwork.c
index a68ebb0..d8c70e8 100644
--- a/tests/fetchhead/nonetwork.c
+++ b/tests/fetchhead/nonetwork.c
@@ -307,3 +307,39 @@ void test_fetchhead_nonetwork__invalid_description(void)
 	cl_assert(git__prefixcmp(giterr_last()->message, "Invalid description") == 0);
 }
 
+static int assert_master_for_merge(const char *ref, const char *url, const git_oid *id, unsigned int is_merge, void *data)
+{
+	GIT_UNUSED(url);
+	GIT_UNUSED(id);
+	GIT_UNUSED(data);
+
+	if (!strcmp("refs/heads/master", ref) && !is_merge)
+		return -1;
+
+	return 0;
+}
+
+void test_fetchhead_nonetwork__unborn_with_upstream(void)
+{
+	git_repository *repo;
+	git_remote *remote;
+
+	/* Create an empty repo to clone from */
+	cl_set_cleanup(&cleanup_repository, "./test1");
+	cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
+	cl_set_cleanup(&cleanup_repository, "./repowithunborn");
+	cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL));
+
+	/* Simulate someone pushing to it by changing to one that has stuff */
+	cl_git_pass(git_remote_load(&remote, repo, "origin"));
+	cl_git_pass(git_remote_set_url(remote, cl_fixture("testrepo.git")));
+	cl_git_pass(git_remote_save(remote));
+
+	cl_git_pass(git_remote_fetch(remote, NULL, NULL));
+	git_remote_free(remote);
+
+	cl_git_pass(git_repository_fetchhead_foreach(repo, assert_master_for_merge, NULL));
+
+	git_repository_free(repo);
+	cl_fixture_cleanup("./repowithunborn");
+}