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