refspec.c 6.66 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 "git2/errors.h"

Carlos Martín Nieto committed
10 11
#include "common.h"
#include "refspec.h"
12
#include "util.h"
13
#include "posix.h"
14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include "refs.h"

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

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

	assert(refspec && input);

	memset(refspec, 0x0, sizeof(git_refspec));
28
	refspec->push = !is_fetch;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

	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;
		return 0;
	}

	if (rhs) {
		size_t rlen = strlen(++rhs);
		is_glob = (1 <= rlen && strchr(rhs, '*'));
		refspec->dst = git__strndup(rhs, rlen);
	}

	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);
63
	flags = GIT_REF_FORMAT_ALLOW_ONELEVEL | GIT_REF_FORMAT_REFSPEC_SHORTHAND
64 65 66 67 68 69 70 71 72 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
		| (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;
		}
	}

123 124 125
	refspec->string = git__strdup(input);
	GITERR_CHECK_ALLOC(refspec->string);

126 127 128 129 130
	return 0;

 invalid:
	return -1;
}
Carlos Martín Nieto committed
131

132
void git_refspec__free(git_refspec *refspec)
Carlos Martín Nieto committed
133
{
134 135 136
	if (refspec == NULL)
		return;

137 138
	git__free(refspec->src);
	git__free(refspec->dst);
139
	git__free(refspec->string);
Carlos Martín Nieto committed
140 141 142 143
}

const char *git_refspec_src(const git_refspec *refspec)
{
144
	return refspec == NULL ? NULL : refspec->src;
Carlos Martín Nieto committed
145 146 147 148
}

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

152 153 154 155 156
const char *git_refspec_string(const git_refspec *refspec)
{
	return refspec == NULL ? NULL : refspec->string;
}

157 158 159 160 161 162 163
int git_refspec_force(const git_refspec *refspec)
{
	assert(refspec);

	return refspec->force;
}

164
int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
165
{
166 167 168 169
	if (refspec == NULL || refspec->src == NULL)
		return false;

	return (p_fnmatch(refspec->src, refname, 0) == 0);
170
}
171

172 173 174 175
int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
{
	if (refspec == NULL || refspec->dst == NULL)
		return false;
176

177 178 179
	return (p_fnmatch(refspec->dst, refname, 0) == 0);
}

180
static int refspec_transform_internal(char *out, size_t outlen, const char *from, const char *to, const char *name)
181 182 183
{
	size_t baselen, namelen;

184
	baselen = strlen(to);
185 186
	if (outlen <= baselen) {
		giterr_set(GITERR_INVALID, "Reference name too long");
187
		return GIT_EBUFS;
188
	}
189 190 191 192 193

	/*
	 * No '*' at the end means that it's mapped to one specific local
	 * branch, so no actual transformation is needed.
	 */
194 195
	if (to[baselen - 1] != '*') {
		memcpy(out, to, baselen + 1); /* include '\0' */
196
		return 0;
197 198 199 200 201 202
	}

	/* There's a '*' at the end, so remove its length */
	baselen--;

	/* skip the prefix, -1 is for the '*' */
203
	name += strlen(from) - 1;
204 205 206

	namelen = strlen(name);

207 208
	if (outlen <= baselen + namelen) {
		giterr_set(GITERR_INVALID, "Reference name too long");
209
		return GIT_EBUFS;
210
	}
211

212
	memcpy(out, to, baselen);
213 214
	memcpy(out + baselen, name, namelen + 1);

215
	return 0;
216
}
217

218 219 220 221 222 223 224 225 226 227
int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name)
{
	return refspec_transform_internal(out, outlen, spec->src, spec->dst, name);
}

int git_refspec_rtransform(char *out, size_t outlen, const git_refspec *spec, const char *name)
{
	return refspec_transform_internal(out, outlen, spec->dst, spec->src, name);
}

228 229
static int refspec_transform(
	git_buf *out, const char *from, const char *to, const char *name)
230
{
231 232 233
	size_t to_len   = to   ? strlen(to)   : 0;
	size_t from_len = from ? strlen(from) : 0;
	size_t name_len = name ? strlen(name) : 0;
234

235 236
	if (git_buf_set(out, to, to_len) < 0)
		return -1;
237

238 239 240 241 242 243 244 245
	if (to_len > 0) {
		/* No '*' at the end of 'to' means that refspec is mapped to one
		 * specific branch, so no actual transformation is needed.
		 */
		if (out->ptr[to_len - 1] != '*')
			return 0;
		git_buf_shorten(out, 1); /* remove trailing '*' copied from 'to' */
	}
246

247 248 249 250
	if (from_len > 0) /* ignore trailing '*' from 'from' */
		from_len--;
	if (from_len > name_len)
		from_len = name_len;
251

252
	return git_buf_put(out, name + from_len, name_len - from_len);
253
}
254

255 256 257 258 259 260 261 262 263 264
int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *name)
{
	return refspec_transform(out, spec->src, spec->dst, name);
}

int git_refspec_transform_l(git_buf *out, const git_refspec *spec, const char *name)
{
	return refspec_transform(out, spec->dst, spec->src, name);
}

265 266 267 268 269 270 271 272 273 274 275
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;
}
276 277 278 279 280 281 282

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

	return (spec->src[strlen(spec->src) - 1] == '*');
}
283 284 285 286 287 288 289

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

	return spec->push;
}