diff options
author | Vicent Marti <tanoku@gmail.com> | 2013-04-16 19:46:41 +0400 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2013-04-16 19:46:41 +0400 |
commit | a50086d174658914d4d6462afbc83b02825b1f5b (patch) | |
tree | e8daa1c7bf678222cf351445179837bed7db3a72 /src/refspec.c | |
parent | 5b9fac39d8a76b9139667c26a63e6b3f204b3977 (diff) | |
parent | f124ebd457bfbf43de3516629aaba5a279636e04 (diff) |
Merge branch 'development'v0.18.0
Diffstat (limited to 'src/refspec.c')
-rw-r--r-- | src/refspec.c | 196 |
1 files changed, 170 insertions, 26 deletions
diff --git a/src/refspec.c b/src/refspec.c index 697b1bf87..a51b0cfab 100644 --- a/src/refspec.c +++ b/src/refspec.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2012 the libgit2 contributors + * Copyright (C) the libgit2 contributors. All rights reserved. * * This file is part of libgit2, distributed under the GNU GPL v2 with * a Linking Exception. For full terms see the included COPYING file. @@ -11,36 +11,127 @@ #include "refspec.h" #include "util.h" #include "posix.h" +#include "refs.h" -int git_refspec_parse(git_refspec *refspec, const char *str) +int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) { - char *delim; + // 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)); - if (*str == '+') { + lhs = input; + if (*lhs == '+') { refspec->force = 1; - str++; + lhs++; } - delim = strchr(str, ':'); - if (delim == NULL) { - refspec->src = git__strdup(str); - GITERR_CHECK_ALLOC(refspec->src); + 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; } - refspec->src = git__strndup(str, delim - str); - GITERR_CHECK_ALLOC(refspec->src); + 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->dst = git__strdup(delim + 1); - if (refspec->dst == NULL) { - git__free(refspec->src); - refspec->src = NULL; - return -1; + refspec->pattern = is_glob; + refspec->src = git__strndup(lhs, llen); + flags = GIT_REF_FORMAT_ALLOW_ONELEVEL + | (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; + } } return 0; + + invalid: + return -1; +} + +void git_refspec__free(git_refspec *refspec) +{ + if (refspec == NULL) + return; + + git__free(refspec->src); + git__free(refspec->dst); } const char *git_refspec_src(const git_refspec *refspec) @@ -53,6 +144,13 @@ const char *git_refspec_dst(const git_refspec *refspec) return refspec == NULL ? NULL : refspec->dst; } +int git_refspec_force(const git_refspec *refspec) +{ + assert(refspec); + + return refspec->force; +} + int git_refspec_src_matches(const git_refspec *refspec, const char *refname) { if (refspec == NULL || refspec->src == NULL) @@ -61,11 +159,19 @@ int git_refspec_src_matches(const git_refspec *refspec, const char *refname) return (p_fnmatch(refspec->src, refname, 0) == 0); } -int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name) +int git_refspec_dst_matches(const git_refspec *refspec, const char *refname) +{ + if (refspec == NULL || refspec->dst == NULL) + return false; + + return (p_fnmatch(refspec->dst, refname, 0) == 0); +} + +static int refspec_transform_internal(char *out, size_t outlen, const char *from, const char *to, const char *name) { size_t baselen, namelen; - baselen = strlen(spec->dst); + baselen = strlen(to); if (outlen <= baselen) { giterr_set(GITERR_INVALID, "Reference name too long"); return GIT_EBUFS; @@ -75,8 +181,8 @@ int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, con * No '*' at the end means that it's mapped to one specific local * branch, so no actual transformation is needed. */ - if (spec->dst[baselen - 1] != '*') { - memcpy(out, spec->dst, baselen + 1); /* include '\0' */ + if (to[baselen - 1] != '*') { + memcpy(out, to, baselen + 1); /* include '\0' */ return 0; } @@ -84,7 +190,7 @@ int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, con baselen--; /* skip the prefix, -1 is for the '*' */ - name += strlen(spec->src) - 1; + name += strlen(from) - 1; namelen = strlen(name); @@ -93,26 +199,36 @@ int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, con return GIT_EBUFS; } - memcpy(out, spec->dst, baselen); + memcpy(out, to, baselen); memcpy(out + baselen, name, namelen + 1); return 0; } -int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *name) +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); +} + +static int refspec_transform(git_buf *out, const char *from, const char *to, const char *name) { - if (git_buf_sets(out, spec->dst) < 0) + if (git_buf_sets(out, to) < 0) return -1; /* - * No '*' at the end means that it's mapped to one specific local + * No '*' at the end means that it's mapped to one specific * branch, so no actual transformation is needed. */ if (git_buf_len(out) > 0 && out->ptr[git_buf_len(out) - 1] != '*') return 0; git_buf_truncate(out, git_buf_len(out) - 1); /* remove trailing '*' */ - git_buf_puts(out, name + strlen(spec->src) - 1); + git_buf_puts(out, name + strlen(from) - 1); if (git_buf_oom(out)) return -1; @@ -120,3 +236,31 @@ int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *n return 0; } +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); +} + +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; +} + +int git_refspec_is_wildcard(const git_refspec *spec) +{ + assert(spec && spec->src); + + return (spec->src[strlen(spec->src) - 1] == '*'); +} |