]> git.proxmox.com Git - libgit2.git/blobdiff - tests/network/refspecs.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / network / refspecs.c
index aa9b36e587c91e936e184804c6e739b7595f1cad..d9e0d9e8dd7a08a7fbebec1be84df784c21b8c87 100644 (file)
@@ -8,17 +8,17 @@ static void assert_refspec(unsigned int direction, const char *input, bool is_ex
        int error;
 
        error = git_refspec__parse(&refspec, input, direction == GIT_DIRECTION_FETCH);
-       git_refspec__free(&refspec);
+       git_refspec__dispose(&refspec);
 
        if (is_expected_to_be_valid)
                cl_assert_equal_i(0, error);
        else
-               cl_assert_equal_i(GIT_ERROR, error);
+               cl_assert_equal_i(GIT_EINVALIDSPEC, error);
 }
 
 void test_network_refspecs__parsing(void)
 {
-       // Ported from https://github.com/git/git/blob/abd2bde78bd994166900290434a2048e660dabed/t/t5511-refspec.sh
+       /* Ported from https://github.com/git/git/blob/abd2bde78bd994166900290434a2048e660dabed/t/t5511-refspec.sh */
 
        assert_refspec(GIT_DIRECTION_PUSH, "", false);
        assert_refspec(GIT_DIRECTION_PUSH, ":", true);
@@ -40,8 +40,8 @@ void test_network_refspecs__parsing(void)
         * code.  They will be caught downstream anyway, but we may want to
         * have tighter check later...
         */
-       //assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master::refs/remotes/frotz/xyzzy", false);
-       //assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false);
+       /*assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master::refs/remotes/frotz/xyzzy", false); */
+       /*assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false); */
 
        assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz/*", true);
        assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz", false);
@@ -70,15 +70,18 @@ void test_network_refspecs__parsing(void)
        assert_refspec(GIT_DIRECTION_PUSH, ":refs/remotes/frotz/delete me", false);
        assert_refspec(GIT_DIRECTION_FETCH, ":refs/remotes/frotz/HEAD to me", false);
 
-       assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", false);
-       assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", false);
+       assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", true);
+       assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", true);
 
-       assert_refspec(GIT_DIRECTION_FETCH, "refs/heads*/for-linus:refs/remotes/mine/*", false);
-       assert_refspec(GIT_DIRECTION_PUSH, "refs/heads*/for-linus:refs/remotes/mine/*", false);
+       assert_refspec(GIT_DIRECTION_FETCH, "refs/heads*/for-linus:refs/remotes/mine/*", true);
+       assert_refspec(GIT_DIRECTION_PUSH, "refs/heads*/for-linus:refs/remotes/mine/*", true);
 
        assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/*/for-linus:refs/remotes/mine/*", false);
        assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/*/for-linus:refs/remotes/mine/*", false);
 
+       assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*g*/for-linus:refs/remotes/mine/*", false);
+       assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*g*/for-linus:refs/remotes/mine/*", false);
+
        assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*", true);
        assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*", true);
 
@@ -88,23 +91,101 @@ void test_network_refspecs__parsing(void)
        assert_refspec(GIT_DIRECTION_FETCH, "refs/pull/*/head:refs/remotes/origin/pr/*", true);
 }
 
-void assert_transform(const char *refspec, const char *name, const char *result)
+static void assert_valid_transform(const char *refspec, const char *name, const char *result)
 {
        git_refspec spec;
        git_buf buf = GIT_BUF_INIT;
 
-       git_refspec__parse(&spec, refspec, true);
+       cl_git_pass(git_refspec__parse(&spec, refspec, true));
        cl_git_pass(git_refspec_transform(&buf, &spec, name));
        cl_assert_equal_s(result, buf.ptr);
 
-       git_buf_free(&buf);
-       git_refspec__free(&spec);
+       git_buf_dispose(&buf);
+       git_refspec__dispose(&spec);
 }
 
 void test_network_refspecs__transform_mid_star(void)
 {
-       assert_transform("refs/pull/*/head:refs/remotes/origin/pr/*", "refs/pull/23/head", "refs/remotes/origin/pr/23");
-       assert_transform("refs/heads/*:refs/remotes/origin/*", "refs/heads/master", "refs/remotes/origin/master");
-       assert_transform("refs/heads/*:refs/heads/*", "refs/heads/master", "refs/heads/master");
-       assert_transform("refs/*:refs/*", "refs/heads/master", "refs/heads/master");
+       assert_valid_transform("refs/pull/*/head:refs/remotes/origin/pr/*", "refs/pull/23/head", "refs/remotes/origin/pr/23");
+       assert_valid_transform("refs/heads/*:refs/remotes/origin/*", "refs/heads/master", "refs/remotes/origin/master");
+       assert_valid_transform("refs/heads/*:refs/remotes/origin/*", "refs/heads/user/feature", "refs/remotes/origin/user/feature");
+       assert_valid_transform("refs/heads/*:refs/heads/*", "refs/heads/master", "refs/heads/master");
+       assert_valid_transform("refs/heads/*:refs/heads/*", "refs/heads/user/feature", "refs/heads/user/feature");
+       assert_valid_transform("refs/*:refs/*", "refs/heads/master", "refs/heads/master");
+}
+
+void test_network_refspecs__transform_loosened_star(void)
+{
+       assert_valid_transform("refs/heads/branch-*:refs/remotes/origin/branch-*", "refs/heads/branch-a", "refs/remotes/origin/branch-a");
+       assert_valid_transform("refs/heads/branch-*/head:refs/remotes/origin/branch-*/head", "refs/heads/branch-a/head", "refs/remotes/origin/branch-a/head");
+}
+
+void test_network_refspecs__transform_nested_star(void)
+{
+       assert_valid_transform("refs/heads/x*x/for-linus:refs/remotes/mine/*", "refs/heads/xbranchx/for-linus", "refs/remotes/mine/branch");
+}
+
+void test_network_refspecs__no_dst(void)
+{
+       assert_valid_transform("refs/heads/master:", "refs/heads/master", "");
+}
+
+static void assert_invalid_transform(const char *refspec, const char *name)
+{
+       git_refspec spec;
+       git_buf buf = GIT_BUF_INIT;
+
+       git_refspec__parse(&spec, refspec, true);
+       cl_git_fail(git_refspec_transform(&buf, &spec, name));
+
+       git_buf_dispose(&buf);
+       git_refspec__dispose(&spec);
+}
+
+void test_network_refspecs__invalid(void)
+{
+       assert_invalid_transform("refs/heads/*:refs/remotes/origin/*", "master");
+       assert_invalid_transform("refs/heads/*:refs/remotes/origin/*", "refs/headz/master");
+}
+
+static void assert_invalid_rtransform(const char *refspec, const char *name)
+{
+       git_refspec spec;
+       git_buf buf = GIT_BUF_INIT;
+
+       cl_git_pass(git_refspec__parse(&spec, refspec, true));
+       cl_git_fail(git_refspec_rtransform(&buf, &spec, name));
+
+       git_buf_dispose(&buf);
+       git_refspec__dispose(&spec);
+}
+
+void test_network_refspecs__invalid_reverse(void)
+{
+       assert_invalid_rtransform("refs/heads/*:refs/remotes/origin/*", "master");
+       assert_invalid_rtransform("refs/heads/*:refs/remotes/origin/*", "refs/remotes/o/master");
+}
+
+void test_network_refspecs__matching(void)
+{
+       git_refspec spec;
+
+       cl_git_pass(git_refspec__parse(&spec, ":", false));
+       cl_assert_equal_s(":", spec.string);
+       cl_assert_equal_s("", spec.src);
+       cl_assert_equal_s("", spec.dst);
+
+       git_refspec__dispose(&spec);
+}
+
+void test_network_refspecs__parse_free(void)
+{
+       git_refspec *spec = NULL;
+
+       cl_git_fail(git_refspec_parse(&spec, "", 0));
+       cl_git_fail(git_refspec_parse(&spec, ":::", 0));
+       cl_git_pass(git_refspec_parse(&spec, "HEAD:", 1));
+
+       cl_assert(spec != NULL);
+       git_refspec_free(spec);
 }