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