Merge pull request #1083 from nulltoken/fix/tracking Fix git_branch_tracking() for branches with empty merge and/or remote config entries
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
diff --git a/src/branch.c b/src/branch.c
index 5d7d443..7f73af8 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -257,8 +257,8 @@ int git_branch_tracking(
if ((error = retrieve_tracking_configuration(&merge_name, branch, "branch.%s.merge")) < 0)
goto cleanup;
-
- if (remote_name == NULL || merge_name == NULL) {
+
+ if (!*remote_name || !*merge_name) {
error = GIT_ENOTFOUND;
goto cleanup;
}
diff --git a/tests-clar/refs/branches/tracking.c b/tests-clar/refs/branches/tracking.c
index 9378eca..e8b2f24 100644
--- a/tests-clar/refs/branches/tracking.c
+++ b/tests-clar/refs/branches/tracking.c
@@ -2,17 +2,19 @@
#include "refs.h"
static git_repository *repo;
-static git_reference *branch;
+static git_reference *branch, *tracking;
void test_refs_branches_tracking__initialize(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
branch = NULL;
+ tracking = NULL;
}
void test_refs_branches_tracking__cleanup(void)
{
+ git_reference_free(tracking);
git_reference_free(branch);
branch = NULL;
@@ -22,61 +24,71 @@ void test_refs_branches_tracking__cleanup(void)
void test_refs_branches_tracking__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)
{
- git_reference *branch, *tracking;
-
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_git_pass(git_branch_tracking(&tracking, branch));
cl_assert_equal_s("refs/remotes/test/master", git_reference_name(tracking));
-
- git_reference_free(branch);
- git_reference_free(tracking);
}
void test_refs_branches_tracking__can_retrieve_the_local_tracking_reference_of_a_local_branch(void)
{
- git_reference *branch, *tracking;
-
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/track-local"));
cl_git_pass(git_branch_tracking(&tracking, branch));
cl_assert_equal_s("refs/heads/master", git_reference_name(tracking));
-
- git_reference_free(branch);
- git_reference_free(tracking);
}
void test_refs_branches_tracking__cannot_retrieve_a_remote_tracking_reference_from_a_non_branch(void)
{
- git_reference *branch, *tracking;
-
cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));
cl_git_fail(git_branch_tracking(&tracking, branch));
-
- git_reference_free(branch);
}
void test_refs_branches_tracking__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void)
{
- git_reference *branch, *tracking;
-
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees"));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_tracking(&tracking, branch));
-
- git_reference_free(branch);
}
void test_refs_branches_tracking__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void)
{
- git_reference *branch, *tracking;
-
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/cannot-fetch"));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_tracking(&tracking, branch));
+}
+
+static void assert_merge_and_or_remote_key_missing(git_repository *repository, git_object *target, const char *entry_name)
+{
+ git_reference *branch;
+
+ cl_git_pass(git_branch_create(&branch, repository, entry_name, target, 0));
+
+ cl_assert_equal_i(GIT_ENOTFOUND, git_branch_tracking(&tracking, branch));
git_reference_free(branch);
}
+
+void test_refs_branches_tracking__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void)
+{
+ git_reference *head;
+ git_repository *repository;
+ git_object *target;
+
+ repository = cl_git_sandbox_init("testrepo.git");
+
+ cl_git_pass(git_repository_head(&head, repository));
+ cl_git_pass(git_reference_peel(&target, head, GIT_OBJ_COMMIT));
+ git_reference_free(head);
+
+ assert_merge_and_or_remote_key_missing(repository, target, "remoteless");
+ assert_merge_and_or_remote_key_missing(repository, target, "mergeless");
+ assert_merge_and_or_remote_key_missing(repository, target, "mergeandremoteless");
+
+ git_object_free(target);
+
+ cl_git_sandbox_cleanup();
+}
diff --git a/tests-clar/resources/testrepo.git/config b/tests-clar/resources/testrepo.git/config
index 3801ce0..904a4e3 100644
--- a/tests-clar/resources/testrepo.git/config
+++ b/tests-clar/resources/testrepo.git/config
@@ -25,3 +25,12 @@
[branch "cannot-fetch"]
remote = joshaber
merge = refs/heads/cannot-fetch
+[branch "remoteless"]
+ remote =
+ merge = refs/heads/master
+[branch "mergeless"]
+ remote = test
+ merge =
+[branch "mergeandremoteless"]
+ remote =
+ merge =