Hash :
0c71e4cb
Author :
Date :
2019-04-26T10:38:02
refspec: fix transforming nested stars When we transform a refspec with a component containing a glob, then we simply copy over the component until the next separator from the matching ref. E.g. if we have a ref "refs/heads/foo/bar" and a refspec "refs/heads/*/bar:refs/remotes/origin/*/bar", we: 1. Copy over everything until hitting the glob from the <dst> part: "refs/remotes/origin/". 2. Strip the common prefix of ref and <src> part until the glob, which is "refs/heads/". This leaves us with a ref of "foo/bar". 3. Copy from the ref until the next "/" separator, resulting in "refs/remotes/origin/foo". 4. Copy over the remaining part of the <dst> spec, which is "bar": "refs/remotes/origin/foo/bar". This worked just fine in a world where globs in refspecs were restricted such that a globbing component may only contain a single "*", only. But this restriction has been lifted, so that a glob component may be nested between other characters, causing the above algorithm to fail. Most notably the third step, where we copy until hitting the next "/" separator, might result in a wrong transformation. Given e.g. a ref "refs/gbranchg/head" and a refspec "refs/g*g/head:refs/remotes/origin/*", we'd also be copying the "g" between "branch" and "/" and end up with the wrong transformed ref "refs/remotes/origin/branchg". Instead of copying until the next component separator, we should copy until we hit the pattern after the "*". So in the above example, we'd copy until hitting the string "g/head".
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
#include "clar_libgit2.h"
#include "refspec.h"
#include "remote.h"
static void assert_refspec(unsigned int direction, const char *input, bool is_expected_to_be_valid)
{
git_refspec refspec;
int error;
error = git_refspec__parse(&refspec, input, direction == GIT_DIRECTION_FETCH);
git_refspec__dispose(&refspec);
if (is_expected_to_be_valid)
cl_assert_equal_i(0, error);
else
cl_assert_equal_i(GIT_ERROR, error);
}
void test_network_refspecs__parsing(void)
{
/* 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);
assert_refspec(GIT_DIRECTION_PUSH, "::", false);
assert_refspec(GIT_DIRECTION_PUSH, "+:", true);
assert_refspec(GIT_DIRECTION_FETCH, "", true);
assert_refspec(GIT_DIRECTION_PUSH, ":", true);
assert_refspec(GIT_DIRECTION_FETCH, "::", false);
assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*:refs/remotes/frotz/*", true);
assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*:refs/remotes/frotz", false);
assert_refspec(GIT_DIRECTION_PUSH, "refs/heads:refs/remotes/frotz/*", false);
assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master:refs/remotes/frotz/xyzzy", true);
/*
* These have invalid LHS, but we do not have a formal "valid sha-1
* expression syntax checker" so they are not checked with the current
* 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_FETCH, "refs/heads/*:refs/remotes/frotz/*", true);
assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz", false);
assert_refspec(GIT_DIRECTION_FETCH, "refs/heads:refs/remotes/frotz/*", false);
assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/master:refs/remotes/frotz/xyzzy", true);
assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/master::refs/remotes/frotz/xyzzy", false);
assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false);
assert_refspec(GIT_DIRECTION_PUSH, "master~1:refs/remotes/frotz/backup", true);
assert_refspec(GIT_DIRECTION_FETCH, "master~1:refs/remotes/frotz/backup", false);
assert_refspec(GIT_DIRECTION_PUSH, "HEAD~4:refs/remotes/frotz/new", true);
assert_refspec(GIT_DIRECTION_FETCH, "HEAD~4:refs/remotes/frotz/new", false);
assert_refspec(GIT_DIRECTION_PUSH, "HEAD", true);
assert_refspec(GIT_DIRECTION_FETCH, "HEAD", true);
assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/ nitfol", false);
assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/ nitfol", false);
assert_refspec(GIT_DIRECTION_PUSH, "HEAD:", false);
assert_refspec(GIT_DIRECTION_FETCH, "HEAD:", true);
assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/ nitfol:", false);
assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/ nitfol:", false);
assert_refspec(GIT_DIRECTION_PUSH, ":refs/remotes/frotz/deleteme", true);
assert_refspec(GIT_DIRECTION_FETCH, ":refs/remotes/frotz/HEAD-to-me", true);
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", 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/*", 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);
assert_refspec(GIT_DIRECTION_FETCH, "master", true);
assert_refspec(GIT_DIRECTION_PUSH, "master", true);
assert_refspec(GIT_DIRECTION_FETCH, "refs/pull/*/head:refs/remotes/origin/pr/*", true);
}
static void assert_valid_transform(const char *refspec, const char *name, const char *result)
{
git_refspec spec;
git_buf buf = GIT_BUF_INIT;
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_dispose(&buf);
git_refspec__dispose(&spec);
}
void test_network_refspecs__transform_mid_star(void)
{
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;
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);
}