refspec.c 9.21 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 11
#include "git2/errors.h"

12
#include "util.h"
13
#include "posix.h"
14
#include "refs.h"
15
#include "vector.h"
16 17 18

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

	size_t llen;
	int is_glob = 0;
	const char *lhs, *rhs;
	int flags;

	assert(refspec && input);

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

	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;
45 46 47 48 49 50
		refspec->string = git__strdup(input);
		GITERR_CHECK_ALLOC(refspec->string);
		refspec->src = git__strdup("");
		GITERR_CHECK_ALLOC(refspec->src);
		refspec->dst = git__strdup("");
		GITERR_CHECK_ALLOC(refspec->dst);
51 52 53 54 55
		return 0;
	}

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

	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);
72
	flags = GIT_REF_FORMAT_ALLOW_ONELEVEL | GIT_REF_FORMAT_REFSPEC_SHORTHAND
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		| (is_glob ? GIT_REF_FORMAT_REFSPEC_PATTERN : 0);

	if (is_fetch) {
		/*
			* LHS
			* - empty is allowed; it means HEAD.
			* - otherwise it must be a valid looking ref.
			*/
		if (!*refspec->src)
			; /* empty is ok */
		else if (!git_reference__is_valid_name(refspec->src, flags))
			goto invalid;
		/*
			* 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.
			*/
		if (!refspec->dst)
			; /* ok */
		else if (!*refspec->dst)
			; /* ok */
		else if (!git_reference__is_valid_name(refspec->dst, flags))
			goto invalid;
	} else {
		/*
			* 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.
			*/
		if (!*refspec->src)
			; /* empty is ok */
		else if (is_glob) {
			if (!git_reference__is_valid_name(refspec->src, flags))
				goto invalid;
		}
		else {
			; /* anything goes, for now */
		}
		/*
			* 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.
			*/
		if (!refspec->dst) {
			if (!git_reference__is_valid_name(refspec->src, flags))
				goto invalid;
		} else if (!*refspec->dst) {
			goto invalid;
		} else {
			if (!git_reference__is_valid_name(refspec->dst, flags))
				goto invalid;
		}
130 131 132 133 134 135

		/* if the RHS is empty, then it's a copy of the LHS */
		if (!refspec->dst) {
			refspec->dst = git__strdup(refspec->src);
			GITERR_CHECK_ALLOC(refspec->dst);
		}
136 137
	}

138 139 140
	refspec->string = git__strdup(input);
	GITERR_CHECK_ALLOC(refspec->string);

141 142 143
	return 0;

 invalid:
144 145 146
        giterr_set(
                GITERR_INVALID,
                "'%s' is not a valid refspec.", input);
147
        git_refspec__dispose(refspec);
148 149
	return -1;
}
Carlos Martín Nieto committed
150

151
void git_refspec__dispose(git_refspec *refspec)
Carlos Martín Nieto committed
152
{
153 154 155
	if (refspec == NULL)
		return;

156 157
	git__free(refspec->src);
	git__free(refspec->dst);
158
	git__free(refspec->string);
159 160

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

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
int git_refspec_parse(git_refspec **out_refspec, const char *input, int is_fetch)
{
	git_refspec *refspec;
	assert(out_refspec && input);

	*out_refspec = NULL;

	refspec = git__malloc(sizeof(git_refspec));
	GITERR_CHECK_ALLOC(refspec);

	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)
{
184
	git_refspec__dispose(refspec);
185 186 187
	git__free(refspec);
}

Carlos Martín Nieto committed
188 189
const char *git_refspec_src(const git_refspec *refspec)
{
190
	return refspec == NULL ? NULL : refspec->src;
Carlos Martín Nieto committed
191 192 193 194
}

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

198 199 200 201 202
const char *git_refspec_string(const git_refspec *refspec)
{
	return refspec == NULL ? NULL : refspec->string;
}

203 204 205 206 207 208 209
int git_refspec_force(const git_refspec *refspec)
{
	assert(refspec);

	return refspec->force;
}

210
int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
211
{
212 213 214 215
	if (refspec == NULL || refspec->src == NULL)
		return false;

	return (p_fnmatch(refspec->src, refname, 0) == 0);
216
}
217

218 219 220 221
int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
{
	if (refspec == NULL || refspec->dst == NULL)
		return false;
222

223 224 225
	return (p_fnmatch(refspec->dst, refname, 0) == 0);
}

226 227
static int refspec_transform(
	git_buf *out, const char *from, const char *to, const char *name)
228
{
229 230 231
	const char *from_star, *to_star;
	const char *name_slash, *from_slash;
	size_t replacement_len, star_offset;
232

233
	git_buf_sanitize(out);
234
	git_buf_clear(out);
235

236 237 238 239 240 241 242 243
	/*
	 * 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, '*');
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	assert(from_star && to_star);

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

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

	/* then we copy over the replacement, from the star's offset to the next slash in 'name' */
	name_slash = strchr(name + star_offset, '/');
	if (!name_slash)
		name_slash = strrchr(name, '\0');

	/* if there is no slash after the star in 'from', we want to copy everything over */
	from_slash = strchr(from + star_offset, '/');
	if (!from_slash)
		name_slash = strrchr(name, '\0');
262

263 264
	replacement_len = (name_slash - name) - star_offset;
	git_buf_put(out, name + star_offset, replacement_len);
265

266
	return git_buf_puts(out, to_star + 1);
267
}
268

269
int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name)
270
{
271 272
	assert(out && spec && name);
	git_buf_sanitize(out);
273

274 275 276 277 278
	if (!git_refspec_src_matches(spec, name)) {
		giterr_set(GITERR_INVALID, "ref '%s' doesn't match the source", name);
		return -1;
	}

279
	if (!spec->pattern)
280
		return git_buf_puts(out, spec->dst ? spec->dst : "");
281

282 283 284
	return refspec_transform(out, spec->src, spec->dst, name);
}

285
int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name)
286
{
287 288
	assert(out && spec && name);
	git_buf_sanitize(out);
289

290 291 292 293 294
	if (!git_refspec_dst_matches(spec, name)) {
		giterr_set(GITERR_INVALID, "ref '%s' doesn't match the destination", name);
		return -1;
	}

295 296 297
	if (!spec->pattern)
		return git_buf_puts(out, spec->src);

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

301 302 303 304 305 306 307 308 309 310 311
int git_refspec__serialize(git_buf *out, const git_refspec *refspec)
{
	if (refspec->force)
		git_buf_putc(out, '+');

	git_buf_printf(out, "%s:%s",
		refspec->src != NULL ? refspec->src : "",
		refspec->dst != NULL ? refspec->dst : "");

	return git_buf_oom(out) == false;
}
312 313 314 315 316 317 318

int git_refspec_is_wildcard(const git_refspec *spec)
{
	assert(spec && spec->src);

	return (spec->src[strlen(spec->src) - 1] == '*');
}
319 320 321 322 323 324 325

git_direction git_refspec_direction(const git_refspec *spec)
{
	assert(spec);

	return spec->push;
}
326 327 328 329 330 331

int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs)
{
	git_buf buf = GIT_BUF_INIT;
	size_t j, pos;
	git_remote_head key;
332
	git_refspec *cur;
333 334 335 336 337 338 339 340

	const char* formatters[] = {
		GIT_REFS_DIR "%s",
		GIT_REFS_TAGS_DIR "%s",
		GIT_REFS_HEADS_DIR "%s",
		NULL
	};

341 342 343
	assert(out && spec && refs);

	cur = git__calloc(1, sizeof(git_refspec));
344 345 346 347 348 349 350 351 352 353 354 355
	GITERR_CHECK_ALLOC(cur);

	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++) {
			git_buf_clear(&buf);
356 357
			git_buf_printf(&buf, formatters[j], spec->src);
			GITERR_CHECK_ALLOC_BUF(&buf);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

			key.name = (char *) git_buf_cstr(&buf);
			if (!git_vector_search(&pos, refs, &key)) {
				/* we found something to match the shorthand, set src to that */
				cur->src = git_buf_detach(&buf);
			}
		}
	}

	/* No shorthands found, copy over the name */
	if (cur->src == NULL && spec->src != NULL) {
		cur->src = git__strdup(spec->src);
		GITERR_CHECK_ALLOC(cur->src);
	}

	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/")) {
			git_buf_puts(&buf, GIT_REFS_DIR);
		} else {
			git_buf_puts(&buf, GIT_REFS_HEADS_DIR);
		}

381 382
		git_buf_puts(&buf, spec->dst);
		GITERR_CHECK_ALLOC_BUF(&buf);
383 384 385 386

		cur->dst = git_buf_detach(&buf);
	}

387
	git_buf_dispose(&buf);
388 389 390 391 392 393 394 395

	if (cur->dst == NULL && spec->dst != NULL) {
		cur->dst = git__strdup(spec->dst);
		GITERR_CHECK_ALLOC(cur->dst);
	}

	return git_vector_insert(out, cur);
}