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