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 
git_refspec__parse(git_refspec * refspec,const char * input,bool is_fetch)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 		GIT_ERROR_CHECK_ALLOC(refspec->string);
47 		refspec->src = git__strdup("");
48 		GIT_ERROR_CHECK_ALLOC(refspec->src);
49 		refspec->dst = git__strdup("");
50 		GIT_ERROR_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_REFERENCE_FORMAT_ALLOW_ONELEVEL |
73 		GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND |
74 		(is_glob ? GIT_REFERENCE_FORMAT_REFSPEC_PATTERN : 0);
75 
76 	if (is_fetch) {
77 		/*
78 			* LHS
79 			* - empty is allowed; it means HEAD.
80 			* - otherwise it must be a valid looking ref.
81 			*/
82 		if (!*refspec->src)
83 			; /* empty is ok */
84 		else if (!git_reference__is_valid_name(refspec->src, flags))
85 			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 		if (!refspec->dst)
93 			; /* ok */
94 		else if (!*refspec->dst)
95 			; /* ok */
96 		else if (!git_reference__is_valid_name(refspec->dst, flags))
97 			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 		if (!*refspec->src)
107 			; /* empty is ok */
108 		else if (is_glob) {
109 			if (!git_reference__is_valid_name(refspec->src, flags))
110 				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 		if (!refspec->dst) {
123 			if (!git_reference__is_valid_name(refspec->src, flags))
124 				goto invalid;
125 		} else if (!*refspec->dst) {
126 			goto invalid;
127 		} else {
128 			if (!git_reference__is_valid_name(refspec->dst, flags))
129 				goto invalid;
130 		}
131 
132 		/* if the RHS is empty, then it's a copy of the LHS */
133 		if (!refspec->dst) {
134 			refspec->dst = git__strdup(refspec->src);
135 			GIT_ERROR_CHECK_ALLOC(refspec->dst);
136 		}
137 	}
138 
139 	refspec->string = git__strdup(input);
140 	GIT_ERROR_CHECK_ALLOC(refspec->string);
141 
142 	return 0;
143 
144  invalid:
145         git_error_set(
146                 GIT_ERROR_INVALID,
147                 "'%s' is not a valid refspec.", input);
148         git_refspec__dispose(refspec);
149 	return -1;
150 }
151 
git_refspec__dispose(git_refspec * refspec)152 void git_refspec__dispose(git_refspec *refspec)
153 {
154 	if (refspec == NULL)
155 		return;
156 
157 	git__free(refspec->src);
158 	git__free(refspec->dst);
159 	git__free(refspec->string);
160 
161 	memset(refspec, 0x0, sizeof(git_refspec));
162 }
163 
git_refspec_parse(git_refspec ** out_refspec,const char * input,int is_fetch)164 int git_refspec_parse(git_refspec **out_refspec, const char *input, int is_fetch)
165 {
166 	git_refspec *refspec;
167 	assert(out_refspec && input);
168 
169 	*out_refspec = NULL;
170 
171 	refspec = git__malloc(sizeof(git_refspec));
172 	GIT_ERROR_CHECK_ALLOC(refspec);
173 
174 	if (git_refspec__parse(refspec, input, !!is_fetch) != 0) {
175 		git__free(refspec);
176 		return -1;
177 	}
178 
179 	*out_refspec = refspec;
180 	return 0;
181 }
182 
git_refspec_free(git_refspec * refspec)183 void git_refspec_free(git_refspec *refspec)
184 {
185 	git_refspec__dispose(refspec);
186 	git__free(refspec);
187 }
188 
git_refspec_src(const git_refspec * refspec)189 const char *git_refspec_src(const git_refspec *refspec)
190 {
191 	return refspec == NULL ? NULL : refspec->src;
192 }
193 
git_refspec_dst(const git_refspec * refspec)194 const char *git_refspec_dst(const git_refspec *refspec)
195 {
196 	return refspec == NULL ? NULL : refspec->dst;
197 }
198 
git_refspec_string(const git_refspec * refspec)199 const char *git_refspec_string(const git_refspec *refspec)
200 {
201 	return refspec == NULL ? NULL : refspec->string;
202 }
203 
git_refspec_force(const git_refspec * refspec)204 int git_refspec_force(const git_refspec *refspec)
205 {
206 	assert(refspec);
207 
208 	return refspec->force;
209 }
210 
git_refspec_src_matches(const git_refspec * refspec,const char * refname)211 int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
212 {
213 	if (refspec == NULL || refspec->src == NULL)
214 		return false;
215 
216 	return (wildmatch(refspec->src, refname, 0) == 0);
217 }
218 
git_refspec_dst_matches(const git_refspec * refspec,const char * refname)219 int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
220 {
221 	if (refspec == NULL || refspec->dst == NULL)
222 		return false;
223 
224 	return (wildmatch(refspec->dst, refname, 0) == 0);
225 }
226 
refspec_transform(git_buf * out,const char * from,const char * to,const char * name)227 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 	git_buf_sanitize(out);
234 	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 	from_star = strchr(from, '*');
243 	to_star = strchr(to, '*');
244 
245 	assert(from_star && to_star);
246 
247 	/* star offset, both in 'from' and in 'name' */
248 	star_offset = from_star - from;
249 
250 	/* the first half is copied over */
251 	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 	replacement_len = strlen(name + star_offset) - strlen(from_star + 1);
258 	git_buf_put(out, name + star_offset, replacement_len);
259 
260 	return git_buf_puts(out, to_star + 1);
261 }
262 
git_refspec_transform(git_buf * out,const git_refspec * spec,const char * name)263 int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name)
264 {
265 	assert(out && spec && name);
266 	git_buf_sanitize(out);
267 
268 	if (!git_refspec_src_matches(spec, name)) {
269 		git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the source", name);
270 		return -1;
271 	}
272 
273 	if (!spec->pattern)
274 		return git_buf_puts(out, spec->dst ? spec->dst : "");
275 
276 	return refspec_transform(out, spec->src, spec->dst, name);
277 }
278 
git_refspec_rtransform(git_buf * out,const git_refspec * spec,const char * name)279 int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name)
280 {
281 	assert(out && spec && name);
282 	git_buf_sanitize(out);
283 
284 	if (!git_refspec_dst_matches(spec, name)) {
285 		git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the destination", name);
286 		return -1;
287 	}
288 
289 	if (!spec->pattern)
290 		return git_buf_puts(out, spec->src);
291 
292 	return refspec_transform(out, spec->dst, spec->src, name);
293 }
294 
git_refspec__serialize(git_buf * out,const git_refspec * refspec)295 int git_refspec__serialize(git_buf *out, const git_refspec *refspec)
296 {
297 	if (refspec->force)
298 		git_buf_putc(out, '+');
299 
300 	git_buf_printf(out, "%s:%s",
301 		refspec->src != NULL ? refspec->src : "",
302 		refspec->dst != NULL ? refspec->dst : "");
303 
304 	return git_buf_oom(out) == false;
305 }
306 
git_refspec_is_wildcard(const git_refspec * spec)307 int git_refspec_is_wildcard(const git_refspec *spec)
308 {
309 	assert(spec && spec->src);
310 
311 	return (spec->src[strlen(spec->src) - 1] == '*');
312 }
313 
git_refspec_direction(const git_refspec * spec)314 git_direction git_refspec_direction(const git_refspec *spec)
315 {
316 	assert(spec);
317 
318 	return spec->push;
319 }
320 
git_refspec__dwim_one(git_vector * out,git_refspec * spec,git_vector * refs)321 int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs)
322 {
323 	git_buf buf = GIT_BUF_INIT;
324 	size_t j, pos;
325 	git_remote_head key;
326 	git_refspec *cur;
327 
328 	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 	assert(out && spec && refs);
336 
337 	cur = git__calloc(1, sizeof(git_refspec));
338 	GIT_ERROR_CHECK_ALLOC(cur);
339 
340 	cur->force = spec->force;
341 	cur->push = spec->push;
342 	cur->pattern = spec->pattern;
343 	cur->matching = spec->matching;
344 	cur->string = git__strdup(spec->string);
345 
346 	/* shorthand on the lhs */
347 	if (git__prefixcmp(spec->src, GIT_REFS_DIR)) {
348 		for (j = 0; formatters[j]; j++) {
349 			git_buf_clear(&buf);
350 			git_buf_printf(&buf, formatters[j], spec->src);
351 			GIT_ERROR_CHECK_ALLOC_BUF(&buf);
352 
353 			key.name = (char *) git_buf_cstr(&buf);
354 			if (!git_vector_search(&pos, refs, &key)) {
355 				/* we found something to match the shorthand, set src to that */
356 				cur->src = git_buf_detach(&buf);
357 			}
358 		}
359 	}
360 
361 	/* No shorthands found, copy over the name */
362 	if (cur->src == NULL && spec->src != NULL) {
363 		cur->src = git__strdup(spec->src);
364 		GIT_ERROR_CHECK_ALLOC(cur->src);
365 	}
366 
367 	if (spec->dst && git__prefixcmp(spec->dst, GIT_REFS_DIR)) {
368 		/* if it starts with "remotes" then we just prepend "refs/" */
369 		if (!git__prefixcmp(spec->dst, "remotes/")) {
370 			git_buf_puts(&buf, GIT_REFS_DIR);
371 		} else {
372 			git_buf_puts(&buf, GIT_REFS_HEADS_DIR);
373 		}
374 
375 		git_buf_puts(&buf, spec->dst);
376 		GIT_ERROR_CHECK_ALLOC_BUF(&buf);
377 
378 		cur->dst = git_buf_detach(&buf);
379 	}
380 
381 	git_buf_dispose(&buf);
382 
383 	if (cur->dst == NULL && spec->dst != NULL) {
384 		cur->dst = git__strdup(spec->dst);
385 		GIT_ERROR_CHECK_ALLOC(cur->dst);
386 	}
387 
388 	return git_vector_insert(out, cur);
389 }
390