summaryrefslogtreecommitdiff
path: root/src/refspec.c
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2012-09-11 11:42:13 +0200
committernulltoken <emeric.fermas@gmail.com>2012-09-25 07:49:15 +0200
commit0adfa20aefcd18262214a22042d303721cc7d23a (patch)
tree11fbf058764125e09358fdcc4f778b1cbb3bdf51 /src/refspec.c
parent77e06d7e8547e82336489b7cdeb04294ed3d6015 (diff)
downloadlibgit2-0adfa20aefcd18262214a22042d303721cc7d23a.tar.gz
refspec: introduce git_refspec__parse()
Diffstat (limited to 'src/refspec.c')
-rw-r--r--src/refspec.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/refspec.c b/src/refspec.c
index b6b1158b7..1265c566c 100644
--- a/src/refspec.c
+++ b/src/refspec.c
@@ -11,6 +11,119 @@
#include "refspec.h"
#include "util.h"
#include "posix.h"
+#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));
+
+ 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);
+ 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;
+}
int git_refspec_parse(git_refspec *refspec, const char *str)
{