Blame src/refspec.c

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#include "git2/errors.h"
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "refspec.h"
Packit ae9e2a
#include "util.h"
Packit ae9e2a
#include "posix.h"
Packit ae9e2a
#include "refs.h"
Packit ae9e2a
#include "vector.h"
Packit ae9e2a
Packit ae9e2a
int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
Packit ae9e2a
{
Packit ae9e2a
	// Ported from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/remote.c#L518-636
Packit ae9e2a
Packit ae9e2a
	size_t llen;
Packit ae9e2a
	int is_glob = 0;
Packit ae9e2a
	const char *lhs, *rhs;
Packit ae9e2a
	int flags;
Packit ae9e2a
Packit ae9e2a
	assert(refspec && input);
Packit ae9e2a
Packit ae9e2a
	memset(refspec, 0x0, sizeof(git_refspec));
Packit ae9e2a
	refspec->push = !is_fetch;
Packit ae9e2a
Packit ae9e2a
	lhs = input;
Packit ae9e2a
	if (*lhs == '+') {
Packit ae9e2a
		refspec->force = 1;
Packit ae9e2a
		lhs++;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	rhs = strrchr(lhs, ':');
Packit ae9e2a
Packit ae9e2a
	/*
Packit ae9e2a
	 * Before going on, special case ":" (or "+:") as a refspec
Packit ae9e2a
	 * for matching refs.
Packit ae9e2a
	 */
Packit ae9e2a
	if (!is_fetch && rhs == lhs && rhs[1] == '\0') {
Packit ae9e2a
		refspec->matching = 1;
Packit ae9e2a
		refspec->string = git__strdup(input);
Packit ae9e2a
		GITERR_CHECK_ALLOC(refspec->string);
Packit ae9e2a
		refspec->src = git__strdup("");
Packit ae9e2a
		GITERR_CHECK_ALLOC(refspec->src);
Packit ae9e2a
		refspec->dst = git__strdup("");
Packit ae9e2a
		GITERR_CHECK_ALLOC(refspec->dst);
Packit ae9e2a
		return 0;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (rhs) {
Packit ae9e2a
		size_t rlen = strlen(++rhs);
Packit ae9e2a
		if (rlen || !is_fetch) {
Packit ae9e2a
			is_glob = (1 <= rlen && strchr(rhs, '*'));
Packit ae9e2a
			refspec->dst = git__strndup(rhs, rlen);
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	llen = (rhs ? (size_t)(rhs - lhs - 1) : strlen(lhs));
Packit ae9e2a
	if (1 <= llen && memchr(lhs, '*', llen)) {
Packit ae9e2a
		if ((rhs && !is_glob) || (!rhs && is_fetch))
Packit ae9e2a
			goto invalid;
Packit ae9e2a
		is_glob = 1;
Packit ae9e2a
	} else if (rhs && is_glob)
Packit ae9e2a
		goto invalid;
Packit ae9e2a
Packit ae9e2a
	refspec->pattern = is_glob;
Packit ae9e2a
	refspec->src = git__strndup(lhs, llen);
Packit ae9e2a
	flags = GIT_REF_FORMAT_ALLOW_ONELEVEL | GIT_REF_FORMAT_REFSPEC_SHORTHAND
Packit ae9e2a
		| (is_glob ? GIT_REF_FORMAT_REFSPEC_PATTERN : 0);
Packit ae9e2a
Packit ae9e2a
	if (is_fetch) {
Packit ae9e2a
		/*
Packit ae9e2a
			* LHS
Packit ae9e2a
			* - empty is allowed; it means HEAD.
Packit ae9e2a
			* - otherwise it must be a valid looking ref.
Packit ae9e2a
			*/
Packit ae9e2a
		if (!*refspec->src)
Packit ae9e2a
			; /* empty is ok */
Packit ae9e2a
		else if (!git_reference__is_valid_name(refspec->src, flags))
Packit ae9e2a
			goto invalid;
Packit ae9e2a
		/*
Packit ae9e2a
			* RHS
Packit ae9e2a
			* - missing is ok, and is same as empty.
Packit ae9e2a
			* - empty is ok; it means not to store.
Packit ae9e2a
			* - otherwise it must be a valid looking ref.
Packit ae9e2a
			*/
Packit ae9e2a
		if (!refspec->dst)
Packit ae9e2a
			; /* ok */
Packit ae9e2a
		else if (!*refspec->dst)
Packit ae9e2a
			; /* ok */
Packit ae9e2a
		else if (!git_reference__is_valid_name(refspec->dst, flags))
Packit ae9e2a
			goto invalid;
Packit ae9e2a
	} else {
Packit ae9e2a
		/*
Packit ae9e2a
			* LHS
Packit ae9e2a
			* - empty is allowed; it means delete.
Packit ae9e2a
			* - when wildcarded, it must be a valid looking ref.
Packit ae9e2a
			* - otherwise, it must be an extended SHA-1, but
Packit ae9e2a
			*   there is no existing way to validate this.
Packit ae9e2a
			*/
Packit ae9e2a
		if (!*refspec->src)
Packit ae9e2a
			; /* empty is ok */
Packit ae9e2a
		else if (is_glob) {
Packit ae9e2a
			if (!git_reference__is_valid_name(refspec->src, flags))
Packit ae9e2a
				goto invalid;
Packit ae9e2a
		}
Packit ae9e2a
		else {
Packit ae9e2a
			; /* anything goes, for now */
Packit ae9e2a
		}
Packit ae9e2a
		/*
Packit ae9e2a
			* RHS
Packit ae9e2a
			* - missing is allowed, but LHS then must be a
Packit ae9e2a
			*   valid looking ref.
Packit ae9e2a
			* - empty is not allowed.
Packit ae9e2a
			* - otherwise it must be a valid looking ref.
Packit ae9e2a
			*/
Packit ae9e2a
		if (!refspec->dst) {
Packit ae9e2a
			if (!git_reference__is_valid_name(refspec->src, flags))
Packit ae9e2a
				goto invalid;
Packit ae9e2a
		} else if (!*refspec->dst) {
Packit ae9e2a
			goto invalid;
Packit ae9e2a
		} else {
Packit ae9e2a
			if (!git_reference__is_valid_name(refspec->dst, flags))
Packit ae9e2a
				goto invalid;
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		/* if the RHS is empty, then it's a copy of the LHS */
Packit ae9e2a
		if (!refspec->dst) {
Packit ae9e2a
			refspec->dst = git__strdup(refspec->src);
Packit ae9e2a
			GITERR_CHECK_ALLOC(refspec->dst);
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	refspec->string = git__strdup(input);
Packit ae9e2a
	GITERR_CHECK_ALLOC(refspec->string);
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
Packit ae9e2a
 invalid:
Packit ae9e2a
        giterr_set(
Packit ae9e2a
                GITERR_INVALID,
Packit ae9e2a
                "'%s' is not a valid refspec.", input);
Packit ae9e2a
        git_refspec__free(refspec);
Packit ae9e2a
	return -1;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void git_refspec__free(git_refspec *refspec)
Packit ae9e2a
{
Packit ae9e2a
	if (refspec == NULL)
Packit ae9e2a
		return;
Packit ae9e2a
Packit ae9e2a
	git__free(refspec->src);
Packit ae9e2a
	git__free(refspec->dst);
Packit ae9e2a
	git__free(refspec->string);
Packit ae9e2a
Packit ae9e2a
	memset(refspec, 0x0, sizeof(git_refspec));
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
const char *git_refspec_src(const git_refspec *refspec)
Packit ae9e2a
{
Packit ae9e2a
	return refspec == NULL ? NULL : refspec->src;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
const char *git_refspec_dst(const git_refspec *refspec)
Packit ae9e2a
{
Packit ae9e2a
	return refspec == NULL ? NULL : refspec->dst;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
const char *git_refspec_string(const git_refspec *refspec)
Packit ae9e2a
{
Packit ae9e2a
	return refspec == NULL ? NULL : refspec->string;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_refspec_force(const git_refspec *refspec)
Packit ae9e2a
{
Packit ae9e2a
	assert(refspec);
Packit ae9e2a
Packit ae9e2a
	return refspec->force;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
Packit ae9e2a
{
Packit ae9e2a
	if (refspec == NULL || refspec->src == NULL)
Packit ae9e2a
		return false;
Packit ae9e2a
Packit ae9e2a
	return (p_fnmatch(refspec->src, refname, 0) == 0);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
Packit ae9e2a
{
Packit ae9e2a
	if (refspec == NULL || refspec->dst == NULL)
Packit ae9e2a
		return false;
Packit ae9e2a
Packit ae9e2a
	return (p_fnmatch(refspec->dst, refname, 0) == 0);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int refspec_transform(
Packit ae9e2a
	git_buf *out, const char *from, const char *to, const char *name)
Packit ae9e2a
{
Packit ae9e2a
	const char *from_star, *to_star;
Packit ae9e2a
	const char *name_slash, *from_slash;
Packit ae9e2a
	size_t replacement_len, star_offset;
Packit ae9e2a
Packit ae9e2a
	git_buf_sanitize(out);
Packit ae9e2a
	git_buf_clear(out);
Packit ae9e2a
Packit ae9e2a
	/*
Packit ae9e2a
	 * There are two parts to each side of a refspec, the bit
Packit ae9e2a
	 * before the star and the bit after it. The star can be in
Packit ae9e2a
	 * the middle of the pattern, so we need to look at each bit
Packit ae9e2a
	 * individually.
Packit ae9e2a
	 */
Packit ae9e2a
	from_star = strchr(from, '*');
Packit ae9e2a
	to_star = strchr(to, '*');
Packit ae9e2a
Packit ae9e2a
	assert(from_star && to_star);
Packit ae9e2a
Packit ae9e2a
	/* star offset, both in 'from' and in 'name' */
Packit ae9e2a
	star_offset = from_star - from;
Packit ae9e2a
Packit ae9e2a
	/* the first half is copied over */
Packit ae9e2a
	git_buf_put(out, to, to_star - to);
Packit ae9e2a
Packit ae9e2a
	/* then we copy over the replacement, from the star's offset to the next slash in 'name' */
Packit ae9e2a
	name_slash = strchr(name + star_offset, '/');
Packit ae9e2a
	if (!name_slash)
Packit ae9e2a
		name_slash = strrchr(name, '\0');
Packit ae9e2a
Packit ae9e2a
	/* if there is no slash after the star in 'from', we want to copy everything over */
Packit ae9e2a
	from_slash = strchr(from + star_offset, '/');
Packit ae9e2a
	if (!from_slash)
Packit ae9e2a
		name_slash = strrchr(name, '\0');
Packit ae9e2a
Packit ae9e2a
	replacement_len = (name_slash - name) - star_offset;
Packit ae9e2a
	git_buf_put(out, name + star_offset, replacement_len);
Packit ae9e2a
Packit ae9e2a
	return git_buf_puts(out, to_star + 1);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name)
Packit ae9e2a
{
Packit ae9e2a
	assert(out && spec && name);
Packit ae9e2a
	git_buf_sanitize(out);
Packit ae9e2a
Packit ae9e2a
	if (!git_refspec_src_matches(spec, name)) {
Packit ae9e2a
		giterr_set(GITERR_INVALID, "ref '%s' doesn't match the source", name);
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!spec->pattern)
Packit ae9e2a
		return git_buf_puts(out, spec->dst);
Packit ae9e2a
Packit ae9e2a
	return refspec_transform(out, spec->src, spec->dst, name);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name)
Packit ae9e2a
{
Packit ae9e2a
	assert(out && spec && name);
Packit ae9e2a
	git_buf_sanitize(out);
Packit ae9e2a
Packit ae9e2a
	if (!git_refspec_dst_matches(spec, name)) {
Packit ae9e2a
		giterr_set(GITERR_INVALID, "ref '%s' doesn't match the destination", name);
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!spec->pattern)
Packit ae9e2a
		return git_buf_puts(out, spec->src);
Packit ae9e2a
Packit ae9e2a
	return refspec_transform(out, spec->dst, spec->src, name);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_refspec__serialize(git_buf *out, const git_refspec *refspec)
Packit ae9e2a
{
Packit ae9e2a
	if (refspec->force)
Packit ae9e2a
		git_buf_putc(out, '+');
Packit ae9e2a
Packit ae9e2a
	git_buf_printf(out, "%s:%s",
Packit ae9e2a
		refspec->src != NULL ? refspec->src : "",
Packit ae9e2a
		refspec->dst != NULL ? refspec->dst : "");
Packit ae9e2a
Packit ae9e2a
	return git_buf_oom(out) == false;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_refspec_is_wildcard(const git_refspec *spec)
Packit ae9e2a
{
Packit ae9e2a
	assert(spec && spec->src);
Packit ae9e2a
Packit ae9e2a
	return (spec->src[strlen(spec->src) - 1] == '*');
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
git_direction git_refspec_direction(const git_refspec *spec)
Packit ae9e2a
{
Packit ae9e2a
	assert(spec);
Packit ae9e2a
Packit ae9e2a
	return spec->push;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs)
Packit ae9e2a
{
Packit ae9e2a
	git_buf buf = GIT_BUF_INIT;
Packit ae9e2a
	size_t j, pos;
Packit ae9e2a
	git_remote_head key;
Packit ae9e2a
Packit ae9e2a
	const char* formatters[] = {
Packit ae9e2a
		GIT_REFS_DIR "%s",
Packit ae9e2a
		GIT_REFS_TAGS_DIR "%s",
Packit ae9e2a
		GIT_REFS_HEADS_DIR "%s",
Packit ae9e2a
		NULL
Packit ae9e2a
	};
Packit ae9e2a
Packit ae9e2a
	git_refspec *cur = git__calloc(1, sizeof(git_refspec));
Packit ae9e2a
	GITERR_CHECK_ALLOC(cur);
Packit ae9e2a
Packit ae9e2a
	cur->force = spec->force;
Packit ae9e2a
	cur->push = spec->push;
Packit ae9e2a
	cur->pattern = spec->pattern;
Packit ae9e2a
	cur->matching = spec->matching;
Packit ae9e2a
	cur->string = git__strdup(spec->string);
Packit ae9e2a
Packit ae9e2a
	/* shorthand on the lhs */
Packit ae9e2a
	if (git__prefixcmp(spec->src, GIT_REFS_DIR)) {
Packit ae9e2a
		for (j = 0; formatters[j]; j++) {
Packit ae9e2a
			git_buf_clear(&buf;;
Packit ae9e2a
			git_buf_printf(&buf, formatters[j], spec->src);
Packit ae9e2a
			GITERR_CHECK_ALLOC_BUF(&buf;;
Packit ae9e2a
Packit ae9e2a
			key.name = (char *) git_buf_cstr(&buf;;
Packit ae9e2a
			if (!git_vector_search(&pos, refs, &key)) {
Packit ae9e2a
				/* we found something to match the shorthand, set src to that */
Packit ae9e2a
				cur->src = git_buf_detach(&buf;;
Packit ae9e2a
			}
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	/* No shorthands found, copy over the name */
Packit ae9e2a
	if (cur->src == NULL && spec->src != NULL) {
Packit ae9e2a
		cur->src = git__strdup(spec->src);
Packit ae9e2a
		GITERR_CHECK_ALLOC(cur->src);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (spec->dst && git__prefixcmp(spec->dst, GIT_REFS_DIR)) {
Packit ae9e2a
		/* if it starts with "remotes" then we just prepend "refs/" */
Packit ae9e2a
		if (!git__prefixcmp(spec->dst, "remotes/")) {
Packit ae9e2a
			git_buf_puts(&buf, GIT_REFS_DIR);
Packit ae9e2a
		} else {
Packit ae9e2a
			git_buf_puts(&buf, GIT_REFS_HEADS_DIR);
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		git_buf_puts(&buf, spec->dst);
Packit ae9e2a
		GITERR_CHECK_ALLOC_BUF(&buf;;
Packit ae9e2a
Packit ae9e2a
		cur->dst = git_buf_detach(&buf;;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_buf_free(&buf;;
Packit ae9e2a
Packit ae9e2a
	if (cur->dst == NULL && spec->dst != NULL) {
Packit ae9e2a
		cur->dst = git__strdup(spec->dst);
Packit ae9e2a
		GITERR_CHECK_ALLOC(cur->dst);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return git_vector_insert(out, cur);
Packit ae9e2a
}