branch: error out if we cannot find the remote When we look for which remote corresponds to a remote-tracking branch, we look in the refspecs to see which ones matches. If none do, we should abort. We currently ignore the error message from this operation, so let's not do that anymore. As part of the test we're writing, let's test for the expected behaviour if we cannot find a refspec which tells us what the remote-tracking branch for a remote would look like.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
diff --git a/src/branch.c b/src/branch.c
index 10be6f7..791d551 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -551,7 +551,7 @@ int git_branch_set_upstream(git_reference *branch, const char *upstream_name)
git_remote *remote = NULL;
git_config *config;
const char *name, *shortname;
- int local;
+ int local, error;
const git_refspec *fetchspec;
name = git_reference_name(branch);
@@ -586,9 +586,12 @@ int git_branch_set_upstream(git_reference *branch, const char *upstream_name)
* that.
*/
if (local)
- git_buf_puts(&value, ".");
+ error = git_buf_puts(&value, ".");
else
- git_branch_remote_name(&value, repo, git_reference_name(upstream));
+ error = git_branch_remote_name(&value, repo, git_reference_name(upstream));
+
+ if (error < 0)
+ goto on_error;
if (git_buf_printf(&key, "branch.%s.remote", shortname) < 0)
goto on_error;
diff --git a/tests/refs/branches/upstream.c b/tests/refs/branches/upstream.c
index 3514494..8f2e7a2 100644
--- a/tests/refs/branches/upstream.c
+++ b/tests/refs/branches/upstream.c
@@ -159,3 +159,35 @@ void test_refs_branches_upstream__set_unset_upstream(void)
cl_git_sandbox_cleanup();
}
+
+void test_refs_branches_upstream__no_fetch_refspec(void)
+{
+ git_reference *ref, *branch;
+ git_repository *repo;
+ git_remote *remote;
+ git_config *cfg;
+
+ repo = cl_git_sandbox_init("testrepo.git");
+
+ cl_git_pass(git_remote_create_with_fetchspec(&remote, repo, "matching", ".", NULL));
+ cl_git_pass(git_remote_add_push(repo, "matching", ":"));
+
+ cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/test"));
+ cl_git_pass(git_reference_create(&ref, repo, "refs/remotes/matching/master", git_reference_target(branch), 1, "fetch"));
+ cl_git_fail(git_branch_set_upstream(branch, "matching/master"));
+ cl_assert_equal_s("Could not determine remote for 'refs/remotes/matching/master'",
+ giterr_last()->message);
+
+ /* we can't set it automatically, so let's test the user setting it by hand */
+ cl_git_pass(git_repository_config(&cfg, repo));
+ cl_git_pass(git_config_set_string(cfg, "branch.test.remote", "matching"));
+ cl_git_pass(git_config_set_string(cfg, "branch.test.merge", "refs/heads/master"));
+ /* we still can't find it because there is no rule for that reference */
+ cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream(&ref, branch));
+
+ git_reference_free(ref);
+ git_reference_free(branch);
+ git_remote_free(remote);
+
+ cl_git_sandbox_cleanup();
+}