refspec.c 9.7 KB
Newer Older
Carlos Martín Nieto committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Carlos Martín Nieto committed
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
Carlos Martín Nieto committed
6 7
 */

8 9
#include "refspec.h"

10
#include "buf.h"
11
#include "refs.h"
12
#include "util.h"
13
#include "vector.h"
14
#include "wildmatch.h"
15 16 17

int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
{
18
	/* Ported from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/remote.c#L518-636 */
19 20 21 22

	size_t llen;
	int is_glob = 0;
	const char *lhs, *rhs;
23 24
	int valid = 0;
	unsigned int flags;
25

26 27
	GIT_ASSERT_ARG(refspec);
	GIT_ASSERT_ARG(input);
28 29

	memset(refspec, 0x0, sizeof(git_refspec));
30
	refspec->push = !is_fetch;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

	lhs = input;
	if (*lhs == '+') {
		refspec->force = 1;
		lhs++;
	}

	rhs = strrchr(lhs, ':');

	/*
	 * Before going on, special case ":" (or "+:") as a refspec
	 * for matching refs.
	 */
	if (!is_fetch && rhs == lhs && rhs[1] == '\0') {
		refspec->matching = 1;
46
		refspec->string = git__strdup(input);
47
		GIT_ERROR_CHECK_ALLOC(refspec->string);
48
		refspec->src = git__strdup("");
49
		GIT_ERROR_CHECK_ALLOC(refspec->src);
50
		refspec->dst = git__strdup("");
51
		GIT_ERROR_CHECK_ALLOC(refspec->dst);
52 53 54 55 56
		return 0;
	}

	if (rhs) {
		size_t rlen = strlen(++rhs);
57 58 59 60
		if (rlen || !is_fetch) {
			is_glob = (1 <= rlen && strchr(rhs, '*'));
			refspec->dst = git__strndup(rhs, rlen);
		}
61 62 63 64 65 66 67 68 69 70 71 72
	}

	llen = (rhs ? (size_t)(rhs - lhs - 1) : strlen(lhs));
	if (1 <= llen && memchr(lhs, '*', llen)) {
		if ((rhs && !is_glob) || (!rhs && is_fetch))
			goto invalid;
		is_glob = 1;
	} else if (rhs && is_glob)
		goto invalid;

	refspec->pattern = is_glob;
	refspec->src = git__strndup(lhs, llen);
73 74 75
	flags = GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL |
		GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND |
		(is_glob ? GIT_REFERENCE_FORMAT_REFSPEC_PATTERN : 0);
76 77 78

	if (is_fetch) {
		/*
79 80 81 82
		 * LHS
		 * - empty is allowed; it means HEAD.
		 * - otherwise it must be a valid looking ref.
		 */
83 84
		if (!*refspec->src)
			; /* empty is ok */
85 86 87
		else if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0)
			goto on_error;
		else if (!valid)
88
			goto invalid;
89

90
		/*
91 92 93 94 95
		 * RHS
		 * - missing is ok, and is same as empty.
		 * - empty is ok; it means not to store.
		 * - otherwise it must be a valid looking ref.
		 */
96 97 98 99
		if (!refspec->dst)
			; /* ok */
		else if (!*refspec->dst)
			; /* ok */
100 101 102
		else if (git_reference__name_is_valid(&valid, refspec->dst, flags) < 0)
			goto on_error;
		else if (!valid)
103 104 105
			goto invalid;
	} else {
		/*
106 107 108 109 110 111
		 * LHS
		 * - empty is allowed; it means delete.
		 * - when wildcarded, it must be a valid looking ref.
		 * - otherwise, it must be an extended SHA-1, but
		 *   there is no existing way to validate this.
		 */
112 113 114
		if (!*refspec->src)
			; /* empty is ok */
		else if (is_glob) {
115 116 117
			if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0)
				goto on_error;
			else if (!valid)
118 119 120 121 122
				goto invalid;
		}
		else {
			; /* anything goes, for now */
		}
123

124
		/*
125 126 127 128 129 130
		 * RHS
		 * - missing is allowed, but LHS then must be a
		 *   valid looking ref.
		 * - empty is not allowed.
		 * - otherwise it must be a valid looking ref.
		 */
131
		if (!refspec->dst) {
132 133 134
			if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0)
				goto on_error;
			else if (!valid)
135 136 137 138
				goto invalid;
		} else if (!*refspec->dst) {
			goto invalid;
		} else {
139 140 141
			if (git_reference__name_is_valid(&valid, refspec->dst, flags) < 0)
				goto on_error;
			else if (!valid)
142 143
				goto invalid;
		}
144 145 146 147

		/* if the RHS is empty, then it's a copy of the LHS */
		if (!refspec->dst) {
			refspec->dst = git__strdup(refspec->src);
148
			GIT_ERROR_CHECK_ALLOC(refspec->dst);
149
		}
150 151
	}

152
	refspec->string = git__strdup(input);
153
	GIT_ERROR_CHECK_ALLOC(refspec->string);
154

155 156
	return 0;

157
invalid:
158 159 160 161
	git_error_set(GIT_ERROR_INVALID,
	              "'%s' is not a valid refspec.", input);
	git_refspec__dispose(refspec);
	return GIT_EINVALIDSPEC;
162 163

on_error:
164
	git_refspec__dispose(refspec);
165 166
	return -1;
}
Carlos Martín Nieto committed
167

168
void git_refspec__dispose(git_refspec *refspec)
Carlos Martín Nieto committed
169
{
170 171 172
	if (refspec == NULL)
		return;

173 174
	git__free(refspec->src);
	git__free(refspec->dst);
175
	git__free(refspec->string);
176 177

	memset(refspec, 0x0, sizeof(git_refspec));
Carlos Martín Nieto committed
178 179
}

180 181 182
int git_refspec_parse(git_refspec **out_refspec, const char *input, int is_fetch)
{
	git_refspec *refspec;
183 184
	GIT_ASSERT_ARG(out_refspec);
	GIT_ASSERT_ARG(input);
185 186 187 188

	*out_refspec = NULL;

	refspec = git__malloc(sizeof(git_refspec));
189
	GIT_ERROR_CHECK_ALLOC(refspec);
190 191 192 193 194 195 196 197 198 199 200 201

	if (git_refspec__parse(refspec, input, !!is_fetch) != 0) {
		git__free(refspec);
		return -1;
	}

	*out_refspec = refspec;
	return 0;
}

void git_refspec_free(git_refspec *refspec)
{
202
	git_refspec__dispose(refspec);
203 204 205
	git__free(refspec);
}

Carlos Martín Nieto committed
206 207
const char *git_refspec_src(const git_refspec *refspec)
{
208
	return refspec == NULL ? NULL : refspec->src;
Carlos Martín Nieto committed
209 210 211 212
}

const char *git_refspec_dst(const git_refspec *refspec)
{
213
	return refspec == NULL ? NULL : refspec->dst;
Carlos Martín Nieto committed
214
}
215

216 217 218 219 220
const char *git_refspec_string(const git_refspec *refspec)
{
	return refspec == NULL ? NULL : refspec->string;
}

221 222
int git_refspec_force(const git_refspec *refspec)
{
223
	GIT_ASSERT_ARG(refspec);
224 225 226 227

	return refspec->force;
}

228
int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
229
{
230 231 232
	if (refspec == NULL || refspec->src == NULL)
		return false;

233
	return (wildmatch(refspec->src, refname, 0) == 0);
234
}
235

236 237 238 239
int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
{
	if (refspec == NULL || refspec->dst == NULL)
		return false;
240

241
	return (wildmatch(refspec->dst, refname, 0) == 0);
242 243
}

244
static int refspec_transform(
245
	git_str *out, const char *from, const char *to, const char *name)
246
{
247 248
	const char *from_star, *to_star;
	size_t replacement_len, star_offset;
249

250
	git_str_clear(out);
251

252 253 254 255 256 257 258 259
	/*
	 * There are two parts to each side of a refspec, the bit
	 * before the star and the bit after it. The star can be in
	 * the middle of the pattern, so we need to look at each bit
	 * individually.
	 */
	from_star = strchr(from, '*');
	to_star = strchr(to, '*');
260

261
	GIT_ASSERT(from_star && to_star);
262 263 264 265 266

	/* star offset, both in 'from' and in 'name' */
	star_offset = from_star - from;

	/* the first half is copied over */
267
	git_str_put(out, to, to_star - to);
268

269 270 271 272 273
	/*
	 * Copy over the name, but exclude the trailing part in "from" starting
	 * after the glob
	 */
	replacement_len = strlen(name + star_offset) - strlen(from_star + 1);
274
	git_str_put(out, name + star_offset, replacement_len);
275

276
	return git_str_puts(out, to_star + 1);
277
}
278

279
int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name)
280
{
281 282
	GIT_BUF_WRAP_PRIVATE(out, git_refspec__transform, spec, name);
}
283

284 285
int git_refspec__transform(git_str *out, const git_refspec *spec, const char *name)
{
286 287 288
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(spec);
	GIT_ASSERT_ARG(name);
289

290
	if (!git_refspec_src_matches(spec, name)) {
291
		git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the source", name);
292 293 294
		return -1;
	}

295
	if (!spec->pattern)
296
		return git_str_puts(out, spec->dst ? spec->dst : "");
297

298 299 300
	return refspec_transform(out, spec->src, spec->dst, name);
}

301
int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name)
302
{
303 304
	GIT_BUF_WRAP_PRIVATE(out, git_refspec__rtransform, spec, name);
}
305

306 307
int git_refspec__rtransform(git_str *out, const git_refspec *spec, const char *name)
{
308 309 310
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(spec);
	GIT_ASSERT_ARG(name);
311

312
	if (!git_refspec_dst_matches(spec, name)) {
313
		git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the destination", name);
314 315 316
		return -1;
	}

317
	if (!spec->pattern)
318
		return git_str_puts(out, spec->src);
319

320 321 322
	return refspec_transform(out, spec->dst, spec->src, name);
}

323
int git_refspec__serialize(git_str *out, const git_refspec *refspec)
324 325
{
	if (refspec->force)
326
		git_str_putc(out, '+');
327

328
	git_str_printf(out, "%s:%s",
329 330 331
		refspec->src != NULL ? refspec->src : "",
		refspec->dst != NULL ? refspec->dst : "");

332
	return git_str_oom(out) == false;
333
}
334 335 336

int git_refspec_is_wildcard(const git_refspec *spec)
{
337 338
	GIT_ASSERT_ARG(spec);
	GIT_ASSERT_ARG(spec->src);
339 340 341

	return (spec->src[strlen(spec->src) - 1] == '*');
}
342 343 344

git_direction git_refspec_direction(const git_refspec *spec)
{
345
	GIT_ASSERT_ARG(spec);
346 347 348

	return spec->push;
}
349 350 351

int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs)
{
352
	git_str buf = GIT_STR_INIT;
353 354
	size_t j, pos;
	git_remote_head key;
355
	git_refspec *cur;
356

357
	const char *formatters[] = {
358 359 360 361 362 363
		GIT_REFS_DIR "%s",
		GIT_REFS_TAGS_DIR "%s",
		GIT_REFS_HEADS_DIR "%s",
		NULL
	};

364 365 366
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(spec);
	GIT_ASSERT_ARG(refs);
367 368

	cur = git__calloc(1, sizeof(git_refspec));
369
	GIT_ERROR_CHECK_ALLOC(cur);
370 371 372 373 374 375 376 377 378 379

	cur->force = spec->force;
	cur->push = spec->push;
	cur->pattern = spec->pattern;
	cur->matching = spec->matching;
	cur->string = git__strdup(spec->string);

	/* shorthand on the lhs */
	if (git__prefixcmp(spec->src, GIT_REFS_DIR)) {
		for (j = 0; formatters[j]; j++) {
380 381 382
			git_str_clear(&buf);
			git_str_printf(&buf, formatters[j], spec->src);
			GIT_ERROR_CHECK_ALLOC_STR(&buf);
383

384
			key.name = (char *) git_str_cstr(&buf);
385 386
			if (!git_vector_search(&pos, refs, &key)) {
				/* we found something to match the shorthand, set src to that */
387
				cur->src = git_str_detach(&buf);
388 389 390 391 392 393 394
			}
		}
	}

	/* No shorthands found, copy over the name */
	if (cur->src == NULL && spec->src != NULL) {
		cur->src = git__strdup(spec->src);
395
		GIT_ERROR_CHECK_ALLOC(cur->src);
396 397 398 399 400
	}

	if (spec->dst && git__prefixcmp(spec->dst, GIT_REFS_DIR)) {
		/* if it starts with "remotes" then we just prepend "refs/" */
		if (!git__prefixcmp(spec->dst, "remotes/")) {
401
			git_str_puts(&buf, GIT_REFS_DIR);
402
		} else {
403
			git_str_puts(&buf, GIT_REFS_HEADS_DIR);
404 405
		}

406 407
		git_str_puts(&buf, spec->dst);
		GIT_ERROR_CHECK_ALLOC_STR(&buf);
408

409
		cur->dst = git_str_detach(&buf);
410 411
	}

412
	git_str_dispose(&buf);
413 414 415

	if (cur->dst == NULL && spec->dst != NULL) {
		cur->dst = git__strdup(spec->dst);
416
		GIT_ERROR_CHECK_ALLOC(cur->dst);
417 418 419 420
	}

	return git_vector_insert(out, cur);
}