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