]> git.proxmox.com Git - libgit2.git/blobdiff - src/refspec.c
Revert "Update d/ch for 0.28.5+dfsg.1-1 release -- from unstable branch"
[libgit2.git] / src / refspec.c
index a9734007156b5943de11422df5e0e5c4f02a6a05..854240a846045402c39544195a83405edfcf6b84 100644 (file)
@@ -5,18 +5,18 @@
  * a Linking Exception. For full terms see the included COPYING file.
  */
 
+#include "refspec.h"
+
 #include "git2/errors.h"
 
-#include "common.h"
-#include "refspec.h"
-#include "util.h"
-#include "posix.h"
 #include "refs.h"
+#include "util.h"
 #include "vector.h"
+#include "wildmatch.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
+       /* Ported from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/remote.c#L518-636 */
 
        size_t llen;
        int is_glob = 0;
@@ -42,13 +42,21 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
         */
        if (!is_fetch && rhs == lhs && rhs[1] == '\0') {
                refspec->matching = 1;
+               refspec->string = git__strdup(input);
+               GIT_ERROR_CHECK_ALLOC(refspec->string);
+               refspec->src = git__strdup("");
+               GIT_ERROR_CHECK_ALLOC(refspec->src);
+               refspec->dst = git__strdup("");
+               GIT_ERROR_CHECK_ALLOC(refspec->dst);
                return 0;
        }
 
        if (rhs) {
                size_t rlen = strlen(++rhs);
-               is_glob = (1 <= rlen && strchr(rhs, '*'));
-               refspec->dst = git__strndup(rhs, rlen);
+               if (rlen || !is_fetch) {
+                       is_glob = (1 <= rlen && strchr(rhs, '*'));
+                       refspec->dst = git__strndup(rhs, rlen);
+               }
        }
 
        llen = (rhs ? (size_t)(rhs - lhs - 1) : strlen(lhs));
@@ -61,8 +69,9 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
 
        refspec->pattern = is_glob;
        refspec->src = git__strndup(lhs, llen);
-       flags = GIT_REF_FORMAT_ALLOW_ONELEVEL | GIT_REF_FORMAT_REFSPEC_SHORTHAND
-               | (is_glob ? GIT_REF_FORMAT_REFSPEC_PATTERN : 0);
+       flags = GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL |
+               GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND |
+               (is_glob ? GIT_REFERENCE_FORMAT_REFSPEC_PATTERN : 0);
 
        if (is_fetch) {
                /*
@@ -119,18 +128,28 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
                        if (!git_reference__is_valid_name(refspec->dst, flags))
                                goto invalid;
                }
+
+               /* if the RHS is empty, then it's a copy of the LHS */
+               if (!refspec->dst) {
+                       refspec->dst = git__strdup(refspec->src);
+                       GIT_ERROR_CHECK_ALLOC(refspec->dst);
+               }
        }
 
        refspec->string = git__strdup(input);
-       GITERR_CHECK_ALLOC(refspec->string);
+       GIT_ERROR_CHECK_ALLOC(refspec->string);
 
        return 0;
 
  invalid:
+        git_error_set(
+                GIT_ERROR_INVALID,
+                "'%s' is not a valid refspec.", input);
+        git_refspec__dispose(refspec);
        return -1;
 }
 
-void git_refspec__free(git_refspec *refspec)
+void git_refspec__dispose(git_refspec *refspec)
 {
        if (refspec == NULL)
                return;
@@ -138,6 +157,33 @@ void git_refspec__free(git_refspec *refspec)
        git__free(refspec->src);
        git__free(refspec->dst);
        git__free(refspec->string);
+
+       memset(refspec, 0x0, sizeof(git_refspec));
+}
+
+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));
+       GIT_ERROR_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)
+{
+       git_refspec__dispose(refspec);
+       git__free(refspec);
 }
 
 const char *git_refspec_src(const git_refspec *refspec)
@@ -167,7 +213,7 @@ int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
        if (refspec == NULL || refspec->src == NULL)
                return false;
 
-       return (p_fnmatch(refspec->src, refname, 0) == 0);
+       return (wildmatch(refspec->src, refname, 0) == 0);
 }
 
 int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
@@ -175,91 +221,74 @@ 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);
+       return (wildmatch(refspec->dst, refname, 0) == 0);
 }
 
-static int refspec_transform_internal(char *out, size_t outlen, const char *from, const char *to, const char *name)
+static int refspec_transform(
+       git_buf *out, const char *from, const char *to, const char *name)
 {
-       size_t baselen, namelen;
+       const char *from_star, *to_star;
+       size_t replacement_len, star_offset;
 
-       baselen = strlen(to);
-       if (outlen <= baselen) {
-               giterr_set(GITERR_INVALID, "Reference name too long");
-               return GIT_EBUFS;
-       }
+       git_buf_sanitize(out);
+       git_buf_clear(out);
 
        /*
-        * No '*' at the end means that it's mapped to one specific local
-        * branch, so no actual transformation is needed.
+        * 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.
         */
-       if (to[baselen - 1] != '*') {
-               memcpy(out, to, baselen + 1); /* include '\0' */
-               return 0;
-       }
+       from_star = strchr(from, '*');
+       to_star = strchr(to, '*');
 
-       /* There's a '*' at the end, so remove its length */
-       baselen--;
+       assert(from_star && to_star);
 
-       /* skip the prefix, -1 is for the '*' */
-       name += strlen(from) - 1;
+       /* star offset, both in 'from' and in 'name' */
+       star_offset = from_star - from;
 
-       namelen = strlen(name);
+       /* the first half is copied over */
+       git_buf_put(out, to, to_star - to);
 
-       if (outlen <= baselen + namelen) {
-               giterr_set(GITERR_INVALID, "Reference name too long");
-               return GIT_EBUFS;
-       }
-
-       memcpy(out, to, baselen);
-       memcpy(out + baselen, name, namelen + 1);
-
-       return 0;
-}
-
-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);
-}
+       /*
+        * Copy over the name, but exclude the trailing part in "from" starting
+        * after the glob
+        */
+       replacement_len = strlen(name + star_offset) - strlen(from_star + 1);
+       git_buf_put(out, name + star_offset, replacement_len);
 
-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);
+       return git_buf_puts(out, to_star + 1);
 }
 
-static int refspec_transform(
-       git_buf *out, const char *from, const char *to, const char *name)
+int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name)
 {
-       size_t to_len   = to   ? strlen(to)   : 0;
-       size_t from_len = from ? strlen(from) : 0;
-       size_t name_len = name ? strlen(name) : 0;
+       assert(out && spec && name);
+       git_buf_sanitize(out);
 
-       if (git_buf_set(out, to, to_len) < 0)
+       if (!git_refspec_src_matches(spec, name)) {
+               git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the source", name);
                return -1;
-
-       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' */
        }
 
-       if (from_len > 0) /* ignore trailing '*' from 'from' */
-               from_len--;
-       if (from_len > name_len)
-               from_len = name_len;
+       if (!spec->pattern)
+               return git_buf_puts(out, spec->dst ? spec->dst : "");
 
-       return git_buf_put(out, name + from_len, name_len - from_len);
-}
-
-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)
+int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name)
 {
+       assert(out && spec && name);
+       git_buf_sanitize(out);
+
+       if (!git_refspec_dst_matches(spec, name)) {
+               git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the destination", name);
+               return -1;
+       }
+
+       if (!spec->pattern)
+               return git_buf_puts(out, spec->src);
+
        return refspec_transform(out, spec->dst, spec->src, name);
 }
 
@@ -294,6 +323,7 @@ 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;
+       git_refspec *cur;
 
        const char* formatters[] = {
                GIT_REFS_DIR "%s",
@@ -302,8 +332,10 @@ int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs)
                NULL
        };
 
-       git_refspec *cur = git__calloc(1, sizeof(git_refspec));
-       GITERR_CHECK_ALLOC(cur);
+       assert(out && spec && refs);
+
+       cur = git__calloc(1, sizeof(git_refspec));
+       GIT_ERROR_CHECK_ALLOC(cur);
 
        cur->force = spec->force;
        cur->push = spec->push;
@@ -315,8 +347,8 @@ int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs)
        if (git__prefixcmp(spec->src, GIT_REFS_DIR)) {
                for (j = 0; formatters[j]; j++) {
                        git_buf_clear(&buf);
-                       if (git_buf_printf(&buf, formatters[j], spec->src) < 0)
-                               return -1;
+                       git_buf_printf(&buf, formatters[j], spec->src);
+                       GIT_ERROR_CHECK_ALLOC_BUF(&buf);
 
                        key.name = (char *) git_buf_cstr(&buf);
                        if (!git_vector_search(&pos, refs, &key)) {
@@ -329,7 +361,7 @@ int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs)
        /* 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);
+               GIT_ERROR_CHECK_ALLOC(cur->src);
        }
 
        if (spec->dst && git__prefixcmp(spec->dst, GIT_REFS_DIR)) {
@@ -340,17 +372,17 @@ int git_refspec__dwim_one(git_vector *out, git_refspec *spec, git_vector *refs)
                        git_buf_puts(&buf, GIT_REFS_HEADS_DIR);
                }
 
-               if (git_buf_puts(&buf, spec->dst) < 0)
-                       return -1;
+               git_buf_puts(&buf, spec->dst);
+               GIT_ERROR_CHECK_ALLOC_BUF(&buf);
 
                cur->dst = git_buf_detach(&buf);
        }
 
-       git_buf_free(&buf);
+       git_buf_dispose(&buf);
 
        if (cur->dst == NULL && spec->dst != NULL) {
                cur->dst = git__strdup(spec->dst);
-               GITERR_CHECK_ALLOC(cur->dst);
+               GIT_ERROR_CHECK_ALLOC(cur->dst);
        }
 
        return git_vector_insert(out, cur);