Merge pull request #3882 from pks-t/pks/fix-fetch-refspec-dst-parsing refspec: do not set empty rhs for fetch refspecs
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
diff --git a/src/refspec.c b/src/refspec.c
index debde86..d200e56 100644
--- a/src/refspec.c
+++ b/src/refspec.c
@@ -53,8 +53,10 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
if (rhs) {
size_t rlen = strlen(++rhs);
- is_glob = (1 <= rlen && strchr(rhs, '*'));
- refspec->dst = git__strndup(rhs, rlen);
+ if (rlen || !is_fetch) {
+ is_glob = (1 <= rlen && strchr(rhs, '*'));
+ refspec->dst = git__strndup(rhs, rlen);
+ }
}
llen = (rhs ? (size_t)(rhs - lhs - 1) : strlen(lhs));
diff --git a/tests/online/fetchhead.c b/tests/online/fetchhead.c
index 200edac..9aaad25 100644
--- a/tests/online/fetchhead.c
+++ b/tests/online/fetchhead.c
@@ -35,6 +35,19 @@ static void fetchhead_test_clone(void)
cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
}
+static int count_references(void)
+{
+ git_strarray array;
+ int refs;
+
+ cl_git_pass(git_reference_list(&array, g_repo));
+ refs = array.count;
+
+ git_strarray_free(&array);
+
+ return refs;
+}
+
static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead)
{
git_remote *remote;
@@ -101,3 +114,41 @@ void test_online_fetchhead__no_merges(void)
cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA3);
}
+
+void test_online_fetchhead__explicit_dst_refspec_creates_branch(void)
+{
+ git_reference *ref;
+ int refs;
+
+ fetchhead_test_clone();
+ refs = count_references();
+ fetchhead_test_fetch("refs/heads/first-merge:refs/heads/explicit-refspec", FETCH_HEAD_EXPLICIT_DATA);
+
+ cl_git_pass(git_branch_lookup(&ref, g_repo, "explicit-refspec", GIT_BRANCH_ALL));
+ cl_assert_equal_i(refs + 1, count_references());
+}
+
+void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void)
+{
+ git_reference *ref;
+ int refs;
+
+ fetchhead_test_clone();
+ refs = count_references();
+
+ fetchhead_test_fetch("refs/heads/first-merge", FETCH_HEAD_EXPLICIT_DATA);
+ cl_git_fail(git_branch_lookup(&ref, g_repo, "first-merge", GIT_BRANCH_ALL));
+
+ cl_assert_equal_i(refs, count_references());
+}
+
+void test_online_fetchhead__colon_only_dst_refspec_creates_no_branch(void)
+{
+ int refs;
+
+ fetchhead_test_clone();
+ refs = count_references();
+ fetchhead_test_fetch("refs/heads/first-merge:", FETCH_HEAD_EXPLICIT_DATA);
+
+ cl_assert_equal_i(refs, count_references());
+}