| 1 |
|
| /* |
| 2 |
|
| * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
|
| * |
| 4 |
|
| * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 |
|
| * a Linking Exception. For full terms see the included COPYING file. |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include "refspec.h" |
| 9 |
|
| |
| 10 |
|
| #include "buf.h" |
| 11 |
|
| #include "refs.h" |
| 12 |
|
| #include "util.h" |
| 13 |
|
| #include "vector.h" |
| 14 |
|
| #include "wildmatch.h" |
| 15 |
|
| |
| 16 |
45 |
| int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) |
| 17 |
|
| { |
| 18 |
|
| /* Ported from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/remote.c#L518-636 */ |
| 19 |
|
| |
| 20 |
|
| size_t llen; |
| 21 |
45 |
| int is_glob = 0; |
| 22 |
|
| const char *lhs, *rhs; |
| 23 |
45 |
| int valid = 0; |
| 24 |
|
| unsigned int flags; |
| 25 |
|
| |
| 26 |
45 |
▶ | GIT_ASSERT_ARG(refspec); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(refspec); |
0 |
45 |
45 |
|
| 27 |
45 |
▶ | GIT_ASSERT_ARG(input); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(input); |
0 |
45 |
45 |
|
| 28 |
|
| |
| 29 |
45 |
| memset(refspec, 0x0, sizeof(git_refspec)); |
| 30 |
45 |
| refspec->push = !is_fetch; |
| 31 |
|
| |
| 32 |
45 |
| lhs = input; |
| 33 |
45 |
▶ | if (*lhs == '+') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*lhs == '+') { |
13 |
32 |
45 |
|
| 34 |
13 |
| refspec->force = 1; |
| 35 |
13 |
| lhs++; |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
45 |
| rhs = strrchr(lhs, ':'); |
| 39 |
|
| |
| 40 |
|
| /* |
| 41 |
|
| * Before going on, special case ":" (or "+:") as a refspec |
| 42 |
|
| * for matching refs. |
| 43 |
|
| */ |
| 44 |
45 |
▶ | if (!is_fetch && rhs == lhs && rhs[1] == '\0') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!is_fetch && rhs == lhs && rhs[1] == '\0') { |
2 |
43 |
45 |
| if (!is_fetch && rhs == lhs && rhs[1] == '\0') { |
0 |
2 |
2 |
| if (!is_fetch && rhs == lhs && rhs[1] == '\0') { |
0 |
0 |
0 |
|
| 45 |
0 |
| refspec->matching = 1; |
| 46 |
0 |
| refspec->string = git__strdup(input); |
| 47 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC(refspec->string); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(refspec->string); |
0 |
0 |
0 |
|
| 48 |
0 |
| refspec->src = git__strdup(""); |
| 49 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC(refspec->src); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(refspec->src); |
0 |
0 |
0 |
|
| 50 |
0 |
| refspec->dst = git__strdup(""); |
| 51 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC(refspec->dst); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(refspec->dst); |
0 |
0 |
0 |
|
| 52 |
0 |
| return 0; |
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
45 |
▶ | if (rhs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rhs) { |
45 |
0 |
45 |
|
| 56 |
45 |
| size_t rlen = strlen(++rhs); |
| 57 |
45 |
▶ | if (rlen || !is_fetch) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (rlen || !is_fetch) { |
0 |
45 |
45 |
| if (rlen || !is_fetch) { |
0 |
0 |
0 |
|
| 58 |
45 |
▶ | is_glob = (1 <= rlen && strchr(rhs, '*')); |
Branch: 2 branches
| Branch | True | False | Total |
| is_glob = (1 <= rlen && strchr(rhs, '*')); |
45 |
0 |
45 |
| is_glob = (1 <= rlen && strchr(rhs, '*')); |
13 |
32 |
45 |
|
| 59 |
45 |
| refspec->dst = git__strndup(rhs, rlen); |
| 60 |
|
| } |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
45 |
▶ | llen = (rhs ? (size_t)(rhs - lhs - 1) : strlen(lhs)); |
Branch: 1 branch
| Branch | True | False | Total |
| llen = (rhs ? (size_t)(rhs - lhs - 1) : strlen(lhs)); |
45 |
0 |
45 |
|
| 64 |
45 |
▶ | if (1 <= llen && memchr(lhs, '*', llen)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (1 <= llen && memchr(lhs, '*', llen)) { |
45 |
0 |
45 |
| if (1 <= llen && memchr(lhs, '*', llen)) { |
13 |
32 |
45 |
|
| 65 |
13 |
▶ | if ((rhs && !is_glob) || (!rhs && is_fetch)) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((rhs && !is_glob) || (!rhs && is_fetch)) |
13 |
0 |
13 |
| if ((rhs && !is_glob) || (!rhs && is_fetch)) |
13 |
0 |
13 |
| if ((rhs && !is_glob) || (!rhs && is_fetch)) |
0 |
13 |
13 |
| if ((rhs && !is_glob) || (!rhs && is_fetch)) |
0 |
0 |
0 |
|
| 66 |
0 |
| goto invalid; |
| 67 |
13 |
| is_glob = 1; |
| 68 |
32 |
▶ | } else if (rhs && is_glob) |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (rhs && is_glob) |
32 |
0 |
32 |
| } else if (rhs && is_glob) |
0 |
32 |
32 |
|
| 69 |
0 |
| goto invalid; |
| 70 |
|
| |
| 71 |
45 |
| refspec->pattern = is_glob; |
| 72 |
45 |
| refspec->src = git__strndup(lhs, llen); |
| 73 |
45 |
| flags = GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL | |
| 74 |
45 |
▶ | GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND | |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND | |
13 |
32 |
45 |
|
| 75 |
|
| (is_glob ? GIT_REFERENCE_FORMAT_REFSPEC_PATTERN : 0); |
| 76 |
|
| |
| 77 |
45 |
▶ | if (is_fetch) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_fetch) { |
43 |
2 |
45 |
|
| 78 |
|
| /* |
| 79 |
|
| * LHS |
| 80 |
|
| * - empty is allowed; it means HEAD. |
| 81 |
|
| * - otherwise it must be a valid looking ref. |
| 82 |
|
| */ |
| 83 |
43 |
▶ | if (!*refspec->src) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*refspec->src) |
43 |
0 |
43 |
|
| 84 |
|
| ; /* empty is ok */ |
| 85 |
43 |
▶ | else if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0) |
0 |
43 |
43 |
|
| 86 |
0 |
| goto on_error; |
| 87 |
43 |
▶ | else if (!valid) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!valid) |
0 |
43 |
43 |
|
| 88 |
0 |
| goto invalid; |
| 89 |
|
| |
| 90 |
|
| /* |
| 91 |
|
| * RHS |
| 92 |
|
| * - missing is ok, and is same as empty. |
| 93 |
|
| * - empty is ok; it means not to store. |
| 94 |
|
| * - otherwise it must be a valid looking ref. |
| 95 |
|
| */ |
| 96 |
43 |
▶ | if (!refspec->dst) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!refspec->dst) |
43 |
0 |
43 |
|
| 97 |
|
| ; /* ok */ |
| 98 |
43 |
▶ | else if (!*refspec->dst) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!*refspec->dst) |
43 |
0 |
43 |
|
| 99 |
|
| ; /* ok */ |
| 100 |
43 |
▶ | else if (git_reference__name_is_valid(&valid, refspec->dst, flags) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (git_reference__name_is_valid(&valid, refspec->dst, flags) < 0) |
0 |
43 |
43 |
|
| 101 |
0 |
| goto on_error; |
| 102 |
43 |
▶ | else if (!valid) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!valid) |
0 |
43 |
43 |
|
| 103 |
0 |
| goto invalid; |
| 104 |
|
| } else { |
| 105 |
|
| /* |
| 106 |
|
| * LHS |
| 107 |
|
| * - empty is allowed; it means delete. |
| 108 |
|
| * - when wildcarded, it must be a valid looking ref. |
| 109 |
|
| * - otherwise, it must be an extended SHA-1, but |
| 110 |
|
| * there is no existing way to validate this. |
| 111 |
|
| */ |
| 112 |
2 |
▶ | if (!*refspec->src) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*refspec->src) |
2 |
0 |
2 |
|
| 113 |
|
| ; /* empty is ok */ |
| 114 |
2 |
▶ | else if (is_glob) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (is_glob) { |
0 |
2 |
2 |
|
| 115 |
0 |
▶ | if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0) |
0 |
0 |
0 |
|
| 116 |
0 |
| goto on_error; |
| 117 |
0 |
▶ | else if (!valid) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!valid) |
0 |
0 |
0 |
|
| 118 |
0 |
| goto invalid; |
| 119 |
|
| } |
| 120 |
|
| else { |
| 121 |
|
| ; /* anything goes, for now */ |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| /* |
| 125 |
|
| * RHS |
| 126 |
|
| * - missing is allowed, but LHS then must be a |
| 127 |
|
| * valid looking ref. |
| 128 |
|
| * - empty is not allowed. |
| 129 |
|
| * - otherwise it must be a valid looking ref. |
| 130 |
|
| */ |
| 131 |
2 |
▶ | if (!refspec->dst) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!refspec->dst) { |
0 |
2 |
2 |
|
| 132 |
0 |
▶ | if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0) |
0 |
0 |
0 |
|
| 133 |
0 |
| goto on_error; |
| 134 |
0 |
▶ | else if (!valid) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!valid) |
0 |
0 |
0 |
|
| 135 |
0 |
| goto invalid; |
| 136 |
2 |
▶ | } else if (!*refspec->dst) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (!*refspec->dst) { |
0 |
2 |
2 |
|
| 137 |
0 |
| goto invalid; |
| 138 |
|
| } else { |
| 139 |
2 |
▶ | if (git_reference__name_is_valid(&valid, refspec->dst, flags) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_reference__name_is_valid(&valid, refspec->dst, flags) < 0) |
0 |
2 |
2 |
|
| 140 |
0 |
| goto on_error; |
| 141 |
2 |
▶ | else if (!valid) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!valid) |
0 |
2 |
2 |
|
| 142 |
0 |
| goto invalid; |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
|
| /* if the RHS is empty, then it's a copy of the LHS */ |
| 146 |
2 |
▶ | if (!refspec->dst) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!refspec->dst) { |
0 |
2 |
2 |
|
| 147 |
0 |
| refspec->dst = git__strdup(refspec->src); |
| 148 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC(refspec->dst); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(refspec->dst); |
0 |
0 |
0 |
|
| 149 |
|
| } |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
45 |
| refspec->string = git__strdup(input); |
| 153 |
45 |
▶ | GIT_ERROR_CHECK_ALLOC(refspec->string); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(refspec->string); |
0 |
45 |
45 |
|
| 154 |
|
| |
| 155 |
45 |
| return 0; |
| 156 |
|
| |
| 157 |
0 |
| invalid: |
| 158 |
0 |
| git_error_set(GIT_ERROR_INVALID, |
| 159 |
|
| "'%s' is not a valid refspec.", input); |
| 160 |
0 |
| git_refspec__dispose(refspec); |
| 161 |
0 |
| return GIT_EINVALIDSPEC; |
| 162 |
|
| |
| 163 |
0 |
| on_error: |
| 164 |
0 |
| git_refspec__dispose(refspec); |
| 165 |
0 |
| return -1; |
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
40 |
| void git_refspec__dispose(git_refspec *refspec) |
| 169 |
|
| { |
| 170 |
40 |
▶ | if (refspec == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (refspec == NULL) |
0 |
40 |
40 |
|
| 171 |
0 |
| return; |
| 172 |
|
| |
| 173 |
40 |
| git__free(refspec->src); |
| 174 |
40 |
| git__free(refspec->dst); |
| 175 |
40 |
| git__free(refspec->string); |
| 176 |
|
| |
| 177 |
40 |
| memset(refspec, 0x0, sizeof(git_refspec)); |
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
1 |
| int git_refspec_parse(git_refspec **out_refspec, const char *input, int is_fetch) |
| 181 |
|
| { |
| 182 |
|
| git_refspec *refspec; |
| 183 |
1 |
▶ | GIT_ASSERT_ARG(out_refspec); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(out_refspec); |
0 |
1 |
1 |
|
| 184 |
1 |
▶ | GIT_ASSERT_ARG(input); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(input); |
0 |
1 |
1 |
|
| 185 |
|
| |
| 186 |
1 |
| *out_refspec = NULL; |
| 187 |
|
| |
| 188 |
1 |
| refspec = git__malloc(sizeof(git_refspec)); |
| 189 |
1 |
▶ | GIT_ERROR_CHECK_ALLOC(refspec); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(refspec); |
0 |
1 |
1 |
|
| 190 |
|
| |
| 191 |
1 |
▶ | if (git_refspec__parse(refspec, input, !!is_fetch) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_refspec__parse(refspec, input, !!is_fetch) != 0) { |
0 |
1 |
1 |
|
| 192 |
0 |
| git__free(refspec); |
| 193 |
0 |
| return -1; |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
1 |
| *out_refspec = refspec; |
| 197 |
1 |
| return 0; |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
1 |
| void git_refspec_free(git_refspec *refspec) |
| 201 |
|
| { |
| 202 |
1 |
| git_refspec__dispose(refspec); |
| 203 |
1 |
| git__free(refspec); |
| 204 |
1 |
| } |
| 205 |
|
| |
| 206 |
3 |
| const char *git_refspec_src(const git_refspec *refspec) |
| 207 |
|
| { |
| 208 |
3 |
▶ | return refspec == NULL ? NULL : refspec->src; |
Branch: 1 branch
| Branch | True | False | Total |
| return refspec == NULL ? NULL : refspec->src; |
3 |
0 |
3 |
|
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
3 |
| const char *git_refspec_dst(const git_refspec *refspec) |
| 212 |
|
| { |
| 213 |
3 |
▶ | return refspec == NULL ? NULL : refspec->dst; |
Branch: 1 branch
| Branch | True | False | Total |
| return refspec == NULL ? NULL : refspec->dst; |
3 |
0 |
3 |
|
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
5 |
| const char *git_refspec_string(const git_refspec *refspec) |
| 217 |
|
| { |
| 218 |
5 |
▶ | return refspec == NULL ? NULL : refspec->string; |
Branch: 1 branch
| Branch | True | False | Total |
| return refspec == NULL ? NULL : refspec->string; |
5 |
0 |
5 |
|
| 219 |
|
| } |
| 220 |
|
| |
| 221 |
2 |
| int git_refspec_force(const git_refspec *refspec) |
| 222 |
|
| { |
| 223 |
2 |
▶ | GIT_ASSERT_ARG(refspec); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(refspec); |
0 |
2 |
2 |
|
| 224 |
|
| |
| 225 |
2 |
| return refspec->force; |
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
2 |
| int git_refspec_src_matches(const git_refspec *refspec, const char *refname) |
| 229 |
|
| { |
| 230 |
2 |
▶ | if (refspec == NULL || refspec->src == NULL) |
Branch: 2 branches
| Branch | True | False | Total |
| if (refspec == NULL || refspec->src == NULL) |
2 |
0 |
2 |
| if (refspec == NULL || refspec->src == NULL) |
0 |
2 |
2 |
|
| 231 |
0 |
| return false; |
| 232 |
|
| |
| 233 |
2 |
| return (wildmatch(refspec->src, refname, 0) == 0); |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
5 |
| int git_refspec_dst_matches(const git_refspec *refspec, const char *refname) |
| 237 |
|
| { |
| 238 |
5 |
▶ | if (refspec == NULL || refspec->dst == NULL) |
Branch: 2 branches
| Branch | True | False | Total |
| if (refspec == NULL || refspec->dst == NULL) |
5 |
0 |
5 |
| if (refspec == NULL || refspec->dst == NULL) |
0 |
5 |
5 |
|
| 239 |
0 |
| return false; |
| 240 |
|
| |
| 241 |
5 |
| return (wildmatch(refspec->dst, refname, 0) == 0); |
| 242 |
|
| } |
| 243 |
|
| |
| 244 |
2 |
| static int refspec_transform( |
| 245 |
|
| git_str *out, const char *from, const char *to, const char *name) |
| 246 |
|
| { |
| 247 |
|
| const char *from_star, *to_star; |
| 248 |
|
| size_t replacement_len, star_offset; |
| 249 |
|
| |
| 250 |
2 |
| git_str_clear(out); |
| 251 |
|
| |
| 252 |
|
| /* |
| 253 |
|
| * There are two parts to each side of a refspec, the bit |
| 254 |
|
| * before the star and the bit after it. The star can be in |
| 255 |
|
| * the middle of the pattern, so we need to look at each bit |
| 256 |
|
| * individually. |
| 257 |
|
| */ |
| 258 |
2 |
| from_star = strchr(from, '*'); |
| 259 |
2 |
| to_star = strchr(to, '*'); |
| 260 |
|
| |
| 261 |
2 |
▶ | GIT_ASSERT(from_star && to_star); |
Branch: 2 branches
| Branch | True | False | Total |
| GIT_ASSERT(from_star && to_star); |
2 |
0 |
2 |
| GIT_ASSERT(from_star && to_star); |
0 |
2 |
2 |
|
| 262 |
|
| |
| 263 |
|
| /* star offset, both in 'from' and in 'name' */ |
| 264 |
2 |
| star_offset = from_star - from; |
| 265 |
|
| |
| 266 |
|
| /* the first half is copied over */ |
| 267 |
2 |
| git_str_put(out, to, to_star - to); |
| 268 |
|
| |
| 269 |
|
| /* |
| 270 |
|
| * Copy over the name, but exclude the trailing part in "from" starting |
| 271 |
|
| * after the glob |
| 272 |
|
| */ |
| 273 |
2 |
| replacement_len = strlen(name + star_offset) - strlen(from_star + 1); |
| 274 |
2 |
| git_str_put(out, name + star_offset, replacement_len); |
| 275 |
|
| |
| 276 |
2 |
| return git_str_puts(out, to_star + 1); |
| 277 |
|
| } |
| 278 |
|
| |
| 279 |
1 |
| int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name) |
| 280 |
|
| { |
| 281 |
1 |
▶ | GIT_BUF_WRAP_PRIVATE(out, git_refspec__transform, spec, name); |
Branch: 2 branches
| Branch | True | False | Total |
| GIT_BUF_WRAP_PRIVATE(out, git_refspec__transform, spec, name); |
1 |
0 |
1 |
| GIT_BUF_WRAP_PRIVATE(out, git_refspec__transform, spec, name); |
1 |
0 |
1 |
|
| 282 |
|
| } |
| 283 |
|
| |
| 284 |
1 |
| int git_refspec__transform(git_str *out, const git_refspec *spec, const char *name) |
| 285 |
|
| { |
| 286 |
1 |
▶ | GIT_ASSERT_ARG(out); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(out); |
0 |
1 |
1 |
|
| 287 |
1 |
▶ | GIT_ASSERT_ARG(spec); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(spec); |
0 |
1 |
1 |
|
| 288 |
1 |
▶ | GIT_ASSERT_ARG(name); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(name); |
0 |
1 |
1 |
|
| 289 |
|
| |
| 290 |
1 |
▶ | if (!git_refspec_src_matches(spec, name)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!git_refspec_src_matches(spec, name)) { |
0 |
1 |
1 |
|
| 291 |
0 |
| git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the source", name); |
| 292 |
0 |
| return -1; |
| 293 |
|
| } |
| 294 |
|
| |
| 295 |
1 |
▶ | if (!spec->pattern) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!spec->pattern) |
0 |
1 |
1 |
|
| 296 |
0 |
▶ | return git_str_puts(out, spec->dst ? spec->dst : ""); |
Branch: 1 branch
| Branch | True | False | Total |
| return git_str_puts(out, spec->dst ? spec->dst : ""); |
0 |
0 |
0 |
|
| 297 |
|
| |
| 298 |
1 |
| return refspec_transform(out, spec->src, spec->dst, name); |
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
1 |
| int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name) |
| 302 |
|
| { |
| 303 |
1 |
▶ | GIT_BUF_WRAP_PRIVATE(out, git_refspec__rtransform, spec, name); |
Branch: 2 branches
| Branch | True | False | Total |
| GIT_BUF_WRAP_PRIVATE(out, git_refspec__rtransform, spec, name); |
1 |
0 |
1 |
| GIT_BUF_WRAP_PRIVATE(out, git_refspec__rtransform, spec, name); |
1 |
0 |
1 |
|
| 304 |
|
| } |
| 305 |
|
| |
| 306 |
1 |
| int git_refspec__rtransform(git_str *out, const git_refspec *spec, const char *name) |
| 307 |
|
| { |
| 308 |
1 |
▶ | GIT_ASSERT_ARG(out); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(out); |
0 |
1 |
1 |
|
| 309 |
1 |
▶ | GIT_ASSERT_ARG(spec); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(spec); |
0 |
1 |
1 |
|
| 310 |
1 |
▶ | GIT_ASSERT_ARG(name); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(name); |
0 |
1 |
1 |
|
| 311 |
|
| |
| 312 |
1 |
▶ | if (!git_refspec_dst_matches(spec, name)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!git_refspec_dst_matches(spec, name)) { |
0 |
1 |
1 |
|
| 313 |
0 |
| git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the destination", name); |
| 314 |
0 |
| return -1; |
| 315 |
|
| } |
| 316 |
|
| |
| 317 |
1 |
▶ | if (!spec->pattern) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!spec->pattern) |
0 |
1 |
1 |
|
| 318 |
0 |
| return git_str_puts(out, spec->src); |
| 319 |
|
| |
| 320 |
1 |
| return refspec_transform(out, spec->dst, spec->src, name); |
| 321 |
|
| } |
| 322 |
|
| |
| 323 |
0 |
| int git_refspec__serialize(git_str *out, const git_refspec *refspec) |
| 324 |
|
| { |
| 325 |
0 |
▶ | if (refspec->force) |
Branch: 1 branch
| Branch | True | False | Total |
| if (refspec->force) |
0 |
0 |
0 |
|
| 326 |
0 |
| git_str_putc(out, '+'); |
| 327 |
|
| |
| 328 |
0 |
| git_str_printf(out, "%s:%s", |
| 329 |
0 |
▶ | refspec->src != NULL ? refspec->src : "", |
Branch: 1 branch
| Branch | True | False | Total |
| refspec->src != NULL ? refspec->src : "", |
0 |
0 |
0 |
|
| 330 |
0 |
▶ | refspec->dst != NULL ? refspec->dst : ""); |
Branch: 1 branch
| Branch | True | False | Total |
| refspec->dst != NULL ? refspec->dst : ""); |
0 |
0 |
0 |
|
| 331 |
|
| |
| 332 |
0 |
| return git_str_oom(out) == false; |
| 333 |
|
| } |
| 334 |
|
| |
| 335 |
0 |
| int git_refspec_is_wildcard(const git_refspec *spec) |
| 336 |
|
| { |
| 337 |
0 |
▶ | GIT_ASSERT_ARG(spec); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(spec); |
0 |
0 |
0 |
|
| 338 |
0 |
▶ | GIT_ASSERT_ARG(spec->src); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(spec->src); |
0 |
0 |
0 |
|
| 339 |
|
| |
| 340 |
0 |
| return (spec->src[strlen(spec->src) - 1] == '*'); |
| 341 |
|
| } |
| 342 |
|
| |
| 343 |
3 |
| git_direction git_refspec_direction(const git_refspec *spec) |
| 344 |
|
| { |
| 345 |
3 |
▶ | GIT_ASSERT_ARG(spec); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(spec); |
0 |
3 |
3 |
|
| 346 |
|
| |
| 347 |
3 |
| return spec->push; |
| 348 |
|
| } |
| 349 |
|
| |
| 350 |
13 |
| int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs) |
| 351 |
|
| { |
| 352 |
13 |
| git_str buf = GIT_STR_INIT; |
| 353 |
|
| size_t j, pos; |
| 354 |
|
| git_remote_head key; |
| 355 |
|
| git_refspec *cur; |
| 356 |
|
| |
| 357 |
13 |
| const char *formatters[] = { |
| 358 |
|
| GIT_REFS_DIR "%s", |
| 359 |
|
| GIT_REFS_TAGS_DIR "%s", |
| 360 |
|
| GIT_REFS_HEADS_DIR "%s", |
| 361 |
|
| NULL |
| 362 |
|
| }; |
| 363 |
|
| |
| 364 |
13 |
▶ | GIT_ASSERT_ARG(out); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(out); |
0 |
13 |
13 |
|
| 365 |
13 |
▶ | GIT_ASSERT_ARG(spec); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(spec); |
0 |
13 |
13 |
|
| 366 |
13 |
▶ | GIT_ASSERT_ARG(refs); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(refs); |
0 |
13 |
13 |
|
| 367 |
|
| |
| 368 |
13 |
| cur = git__calloc(1, sizeof(git_refspec)); |
| 369 |
13 |
▶ | GIT_ERROR_CHECK_ALLOC(cur); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(cur); |
0 |
13 |
13 |
|
| 370 |
|
| |
| 371 |
13 |
| cur->force = spec->force; |
| 372 |
13 |
| cur->push = spec->push; |
| 373 |
13 |
| cur->pattern = spec->pattern; |
| 374 |
13 |
| cur->matching = spec->matching; |
| 375 |
13 |
| cur->string = git__strdup(spec->string); |
| 376 |
|
| |
| 377 |
|
| /* shorthand on the lhs */ |
| 378 |
13 |
▶ | if (git__prefixcmp(spec->src, GIT_REFS_DIR)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (git__prefixcmp(spec->src, GIT_REFS_DIR)) { |
0 |
13 |
13 |
|
| 379 |
0 |
▶ | for (j = 0; formatters[j]; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; formatters[j]; j++) { |
0 |
0 |
0 |
|
| 380 |
0 |
| git_str_clear(&buf); |
| 381 |
0 |
| git_str_printf(&buf, formatters[j], spec->src); |
| 382 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC_STR(&buf); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC_STR(&buf); |
0 |
0 |
0 |
|
| 383 |
|
| |
| 384 |
0 |
| key.name = (char *) git_str_cstr(&buf); |
| 385 |
0 |
▶ | if (!git_vector_search(&pos, refs, &key)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!git_vector_search(&pos, refs, &key)) { |
0 |
0 |
0 |
|
| 386 |
|
| /* we found something to match the shorthand, set src to that */ |
| 387 |
0 |
| cur->src = git_str_detach(&buf); |
| 388 |
|
| } |
| 389 |
|
| } |
| 390 |
|
| } |
| 391 |
|
| |
| 392 |
|
| /* No shorthands found, copy over the name */ |
| 393 |
13 |
▶ | if (cur->src == NULL && spec->src != NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cur->src == NULL && spec->src != NULL) { |
13 |
0 |
13 |
| if (cur->src == NULL && spec->src != NULL) { |
13 |
0 |
13 |
|
| 394 |
13 |
| cur->src = git__strdup(spec->src); |
| 395 |
13 |
▶ | GIT_ERROR_CHECK_ALLOC(cur->src); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(cur->src); |
0 |
13 |
13 |
|
| 396 |
|
| } |
| 397 |
|
| |
| 398 |
13 |
▶ | if (spec->dst && git__prefixcmp(spec->dst, GIT_REFS_DIR)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (spec->dst && git__prefixcmp(spec->dst, GIT_REFS_DIR)) { |
13 |
0 |
13 |
| if (spec->dst && git__prefixcmp(spec->dst, GIT_REFS_DIR)) { |
0 |
13 |
13 |
|
| 399 |
|
| /* if it starts with "remotes" then we just prepend "refs/" */ |
| 400 |
0 |
▶ | if (!git__prefixcmp(spec->dst, "remotes/")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!git__prefixcmp(spec->dst, "remotes/")) { |
0 |
0 |
0 |
|
| 401 |
0 |
| git_str_puts(&buf, GIT_REFS_DIR); |
| 402 |
|
| } else { |
| 403 |
0 |
| git_str_puts(&buf, GIT_REFS_HEADS_DIR); |
| 404 |
|
| } |
| 405 |
|
| |
| 406 |
0 |
| git_str_puts(&buf, spec->dst); |
| 407 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC_STR(&buf); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC_STR(&buf); |
0 |
0 |
0 |
|
| 408 |
|
| |
| 409 |
0 |
| cur->dst = git_str_detach(&buf); |
| 410 |
|
| } |
| 411 |
|
| |
| 412 |
13 |
| git_str_dispose(&buf); |
| 413 |
|
| |
| 414 |
13 |
▶ | if (cur->dst == NULL && spec->dst != NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cur->dst == NULL && spec->dst != NULL) { |
13 |
0 |
13 |
| if (cur->dst == NULL && spec->dst != NULL) { |
13 |
0 |
13 |
|
| 415 |
13 |
| cur->dst = git__strdup(spec->dst); |
| 416 |
13 |
▶ | GIT_ERROR_CHECK_ALLOC(cur->dst); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(cur->dst); |
0 |
13 |
13 |
|
| 417 |
|
| } |
| 418 |
|
| |
| 419 |
13 |
| return git_vector_insert(out, cur); |
| 420 |
|
| } |