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