]> git.proxmox.com Git - libgit2.git/blob - src/refspec.c
Merge pull request #1956 from libgit2/cmn/fetch-default-head
[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
124 refspec->string = git__strdup(input);
125 GITERR_CHECK_ALLOC(refspec->string);
126
127 return 0;
128
129 invalid:
130 return -1;
131 }
132
133 void git_refspec__free(git_refspec *refspec)
134 {
135 if (refspec == NULL)
136 return;
137
138 git__free(refspec->src);
139 git__free(refspec->dst);
140 git__free(refspec->string);
141 }
142
143 const char *git_refspec_src(const git_refspec *refspec)
144 {
145 return refspec == NULL ? NULL : refspec->src;
146 }
147
148 const char *git_refspec_dst(const git_refspec *refspec)
149 {
150 return refspec == NULL ? NULL : refspec->dst;
151 }
152
153 const char *git_refspec_string(const git_refspec *refspec)
154 {
155 return refspec == NULL ? NULL : refspec->string;
156 }
157
158 int git_refspec_force(const git_refspec *refspec)
159 {
160 assert(refspec);
161
162 return refspec->force;
163 }
164
165 int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
166 {
167 if (refspec == NULL || refspec->src == NULL)
168 return false;
169
170 return (p_fnmatch(refspec->src, refname, 0) == 0);
171 }
172
173 int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
174 {
175 if (refspec == NULL || refspec->dst == NULL)
176 return false;
177
178 return (p_fnmatch(refspec->dst, refname, 0) == 0);
179 }
180
181 static int refspec_transform_internal(char *out, size_t outlen, const char *from, const char *to, const char *name)
182 {
183 size_t baselen, namelen;
184
185 baselen = strlen(to);
186 if (outlen <= baselen) {
187 giterr_set(GITERR_INVALID, "Reference name too long");
188 return GIT_EBUFS;
189 }
190
191 /*
192 * No '*' at the end means that it's mapped to one specific local
193 * branch, so no actual transformation is needed.
194 */
195 if (to[baselen - 1] != '*') {
196 memcpy(out, to, baselen + 1); /* include '\0' */
197 return 0;
198 }
199
200 /* There's a '*' at the end, so remove its length */
201 baselen--;
202
203 /* skip the prefix, -1 is for the '*' */
204 name += strlen(from) - 1;
205
206 namelen = strlen(name);
207
208 if (outlen <= baselen + namelen) {
209 giterr_set(GITERR_INVALID, "Reference name too long");
210 return GIT_EBUFS;
211 }
212
213 memcpy(out, to, baselen);
214 memcpy(out + baselen, name, namelen + 1);
215
216 return 0;
217 }
218
219 int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name)
220 {
221 return refspec_transform_internal(out, outlen, spec->src, spec->dst, name);
222 }
223
224 int git_refspec_rtransform(char *out, size_t outlen, const git_refspec *spec, const char *name)
225 {
226 return refspec_transform_internal(out, outlen, spec->dst, spec->src, name);
227 }
228
229 static int refspec_transform(
230 git_buf *out, const char *from, const char *to, const char *name)
231 {
232 size_t to_len = to ? strlen(to) : 0;
233 size_t from_len = from ? strlen(from) : 0;
234 size_t name_len = name ? strlen(name) : 0;
235
236 if (git_buf_set(out, to, to_len) < 0)
237 return -1;
238
239 if (to_len > 0) {
240 /* No '*' at the end of 'to' means that refspec is mapped to one
241 * specific branch, so no actual transformation is needed.
242 */
243 if (out->ptr[to_len - 1] != '*')
244 return 0;
245 git_buf_shorten(out, 1); /* remove trailing '*' copied from 'to' */
246 }
247
248 if (from_len > 0) /* ignore trailing '*' from 'from' */
249 from_len--;
250 if (from_len > name_len)
251 from_len = name_len;
252
253 return git_buf_put(out, name + from_len, name_len - from_len);
254 }
255
256 int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *name)
257 {
258 return refspec_transform(out, spec->src, spec->dst, name);
259 }
260
261 int git_refspec_transform_l(git_buf *out, const git_refspec *spec, const char *name)
262 {
263 return refspec_transform(out, spec->dst, spec->src, name);
264 }
265
266 int git_refspec__serialize(git_buf *out, const git_refspec *refspec)
267 {
268 if (refspec->force)
269 git_buf_putc(out, '+');
270
271 git_buf_printf(out, "%s:%s",
272 refspec->src != NULL ? refspec->src : "",
273 refspec->dst != NULL ? refspec->dst : "");
274
275 return git_buf_oom(out) == false;
276 }
277
278 int git_refspec_is_wildcard(const git_refspec *spec)
279 {
280 assert(spec && spec->src);
281
282 return (spec->src[strlen(spec->src) - 1] == '*');
283 }
284
285 git_direction git_refspec_direction(const git_refspec *spec)
286 {
287 assert(spec);
288
289 return spec->push;
290 }
291
292 int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs)
293 {
294 git_buf buf = GIT_BUF_INIT;
295 size_t j, pos;
296 git_remote_head key;
297
298 const char* formatters[] = {
299 GIT_REFS_DIR "%s",
300 GIT_REFS_TAGS_DIR "%s",
301 GIT_REFS_HEADS_DIR "%s",
302 NULL
303 };
304
305 git_refspec *cur = git__calloc(1, sizeof(git_refspec));
306 GITERR_CHECK_ALLOC(cur);
307
308 cur->force = spec->force;
309 cur->push = spec->push;
310 cur->pattern = spec->pattern;
311 cur->matching = spec->matching;
312 cur->string = git__strdup(spec->string);
313
314 /* shorthand on the lhs */
315 if (git__prefixcmp(spec->src, GIT_REFS_DIR)) {
316 for (j = 0; formatters[j]; j++) {
317 git_buf_clear(&buf);
318 if (git_buf_printf(&buf, formatters[j], spec->src) < 0)
319 return -1;
320
321 key.name = (char *) git_buf_cstr(&buf);
322 if (!git_vector_search(&pos, refs, &key)) {
323 /* we found something to match the shorthand, set src to that */
324 cur->src = git_buf_detach(&buf);
325 }
326 }
327 }
328
329 /* No shorthands found, copy over the name */
330 if (cur->src == NULL && spec->src != NULL) {
331 cur->src = git__strdup(spec->src);
332 GITERR_CHECK_ALLOC(cur->src);
333 }
334
335 if (spec->dst && git__prefixcmp(spec->dst, GIT_REFS_DIR)) {
336 /* if it starts with "remotes" then we just prepend "refs/" */
337 if (!git__prefixcmp(spec->dst, "remotes/")) {
338 git_buf_puts(&buf, GIT_REFS_DIR);
339 } else {
340 git_buf_puts(&buf, GIT_REFS_HEADS_DIR);
341 }
342
343 if (git_buf_puts(&buf, spec->dst) < 0)
344 return -1;
345
346 cur->dst = git_buf_detach(&buf);
347 }
348
349 git_buf_free(&buf);
350
351 if (cur->dst == NULL && spec->dst != NULL) {
352 cur->dst = git__strdup(spec->dst);
353 GITERR_CHECK_ALLOC(cur->dst);
354 }
355
356 return git_vector_insert(out, cur);
357 }