]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refspec: move refspec parsing logic into its own file
authorBrandon Williams <bmwill@google.com>
Wed, 16 May 2018 22:57:48 +0000 (15:57 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 17 May 2018 21:19:41 +0000 (06:19 +0900)
In preparation for performing a refactor on refspec related code, move
the refspec parsing logic into its own file.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
17 files changed:
Makefile
branch.c
builtin/clone.c
builtin/fast-export.c
builtin/fetch.c
builtin/merge.c
builtin/pull.c
builtin/push.c
builtin/remote.c
builtin/submodule--helper.c
checkout.c
refspec.c [new file with mode: 0644]
refspec.h [new file with mode: 0644]
remote.c
remote.h
transport-helper.c
transport.c

index ad880d1fc57212fc6b47aeea792a58129b61238e..4bca65383a784dca7da0092adb6c2eb754fa83cf 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -928,6 +928,7 @@ LIB_OBJS += refs/files-backend.o
 LIB_OBJS += refs/iterator.o
 LIB_OBJS += refs/packed-backend.o
 LIB_OBJS += refs/ref-cache.o
+LIB_OBJS += refspec.o
 LIB_OBJS += ref-filter.o
 LIB_OBJS += remote.o
 LIB_OBJS += replace-object.o
index 2672054f0b5423cd09010302285935650435f1dc..32ccefc6b5d28f3c0403f06237794ca3f814810f 100644 (file)
--- a/branch.c
+++ b/branch.c
@@ -3,6 +3,7 @@
 #include "config.h"
 #include "branch.h"
 #include "refs.h"
+#include "refspec.h"
 #include "remote.h"
 #include "commit.h"
 #include "worktree.h"
index 84f1473d19dc5a521e58c0bc1a7363808888dff1..6d1614ed372fd6a0a9a3a4c930709679b555de80 100644 (file)
@@ -14,6 +14,7 @@
 #include "parse-options.h"
 #include "fetch-pack.h"
 #include "refs.h"
+#include "refspec.h"
 #include "tree.h"
 #include "tree-walk.h"
 #include "unpack-trees.h"
index 530df12f05be0e3331347d0a76306d0caa394744..a13b7c8ef307cceca471ab27bb8d490ad74c3c23 100644 (file)
@@ -7,6 +7,7 @@
 #include "cache.h"
 #include "config.h"
 #include "refs.h"
+#include "refspec.h"
 #include "commit.h"
 #include "object.h"
 #include "tag.h"
index 7ee83ac0f8170d9e68c374a21caebfe01cf64533..1fce68e9acc8152909b3bc52f2afd7442e43be45 100644 (file)
@@ -5,6 +5,7 @@
 #include "config.h"
 #include "repository.h"
 #include "refs.h"
+#include "refspec.h"
 #include "commit.h"
 #include "builtin.h"
 #include "string-list.h"
index 9db5a2cf16e189bb3bd0ceec7d34c6651d630225..c362cfe90dbaf73d755aa54aa87237d8658ca853 100644 (file)
@@ -14,6 +14,7 @@
 #include "run-command.h"
 #include "diff.h"
 #include "refs.h"
+#include "refspec.h"
 #include "commit.h"
 #include "diffcore.h"
 #include "revision.h"
index 71aac5005e0c0c8cf156cdf69621b687973f9ecc..6247c956d700ef23db9deae9a7aeba0cbfd75f49 100644 (file)
@@ -15,6 +15,7 @@
 #include "remote.h"
 #include "dir.h"
 #include "refs.h"
+#include "refspec.h"
 #include "revision.h"
 #include "submodule.h"
 #include "submodule-config.h"
index ac3705370e12fda53da086f33c6b47925a48873c..fa65999b27567d0db639cea3883a8cd846241c23 100644 (file)
@@ -4,6 +4,7 @@
 #include "cache.h"
 #include "config.h"
 #include "refs.h"
+#include "refspec.h"
 #include "run-command.h"
 #include "builtin.h"
 #include "remote.h"
index 8708e584e9e8793f3a5e44861d9fb9f049027fa3..c495139954764eb4ed5bf34ebd86ca2ed7ebfcab 100644 (file)
@@ -7,6 +7,7 @@
 #include "strbuf.h"
 #include "run-command.h"
 #include "refs.h"
+#include "refspec.h"
 #include "argv-array.h"
 
 static const char * const builtin_remote_usage[] = {
index c2403a915ffe29e152832ae16e5bc902703a9903..6ab032acb1a77b3188644393e647fec6991753c8 100644 (file)
@@ -12,6 +12,7 @@
 #include "run-command.h"
 #include "remote.h"
 #include "refs.h"
+#include "refspec.h"
 #include "connect.h"
 #include "revision.h"
 #include "diffcore.h"
index ac42630f7483e8c675c31bb7a0c58262e900b5c3..193ba856750396abfc347fe5c191aa9da7b5f952 100644 (file)
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "remote.h"
+#include "refspec.h"
 #include "checkout.h"
 
 struct tracking_name_data {
diff --git a/refspec.c b/refspec.c
new file mode 100644 (file)
index 0000000..50892f8
--- /dev/null
+++ b/refspec.c
@@ -0,0 +1,167 @@
+#include "cache.h"
+#include "refs.h"
+#include "refspec.h"
+
+static struct refspec s_tag_refspec = {
+       0,
+       1,
+       0,
+       0,
+       "refs/tags/*",
+       "refs/tags/*"
+};
+
+/* See TAG_REFSPEC for the string version */
+const struct refspec *tag_refspec = &s_tag_refspec;
+
+static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
+{
+       int i;
+       struct refspec *rs = xcalloc(nr_refspec, sizeof(*rs));
+
+       for (i = 0; i < nr_refspec; i++) {
+               size_t llen;
+               int is_glob;
+               const char *lhs, *rhs;
+               int flags;
+
+               is_glob = 0;
+
+               lhs = refspec[i];
+               if (*lhs == '+') {
+                       rs[i].force = 1;
+                       lhs++;
+               }
+
+               rhs = strrchr(lhs, ':');
+
+               /*
+                * Before going on, special case ":" (or "+:") as a refspec
+                * for pushing matching refs.
+                */
+               if (!fetch && rhs == lhs && rhs[1] == '\0') {
+                       rs[i].matching = 1;
+                       continue;
+               }
+
+               if (rhs) {
+                       size_t rlen = strlen(++rhs);
+                       is_glob = (1 <= rlen && strchr(rhs, '*'));
+                       rs[i].dst = xstrndup(rhs, rlen);
+               }
+
+               llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
+               if (1 <= llen && memchr(lhs, '*', llen)) {
+                       if ((rhs && !is_glob) || (!rhs && fetch))
+                               goto invalid;
+                       is_glob = 1;
+               } else if (rhs && is_glob) {
+                       goto invalid;
+               }
+
+               rs[i].pattern = is_glob;
+               rs[i].src = xstrndup(lhs, llen);
+               flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
+
+               if (fetch) {
+                       struct object_id unused;
+
+                       /* LHS */
+                       if (!*rs[i].src)
+                               ; /* empty is ok; it means "HEAD" */
+                       else if (llen == GIT_SHA1_HEXSZ && !get_oid_hex(rs[i].src, &unused))
+                               rs[i].exact_sha1 = 1; /* ok */
+                       else if (!check_refname_format(rs[i].src, flags))
+                               ; /* valid looking ref is ok */
+                       else
+                               goto invalid;
+                       /* RHS */
+                       if (!rs[i].dst)
+                               ; /* missing is ok; it is the same as empty */
+                       else if (!*rs[i].dst)
+                               ; /* empty is ok; it means "do not store" */
+                       else if (!check_refname_format(rs[i].dst, flags))
+                               ; /* valid looking ref is ok */
+                       else
+                               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 (!*rs[i].src)
+                               ; /* empty is ok */
+                       else if (is_glob) {
+                               if (check_refname_format(rs[i].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 (!rs[i].dst) {
+                               if (check_refname_format(rs[i].src, flags))
+                                       goto invalid;
+                       } else if (!*rs[i].dst) {
+                               goto invalid;
+                       } else {
+                               if (check_refname_format(rs[i].dst, flags))
+                                       goto invalid;
+                       }
+               }
+       }
+       return rs;
+
+ invalid:
+       if (verify) {
+               /*
+                * nr_refspec must be greater than zero and i must be valid
+                * since it is only possible to reach this point from within
+                * the for loop above.
+                */
+               free_refspec(i+1, rs);
+               return NULL;
+       }
+       die("Invalid refspec '%s'", refspec[i]);
+}
+
+int valid_fetch_refspec(const char *fetch_refspec_str)
+{
+       struct refspec *refspec;
+
+       refspec = parse_refspec_internal(1, &fetch_refspec_str, 1, 1);
+       free_refspec(1, refspec);
+       return !!refspec;
+}
+
+struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
+{
+       return parse_refspec_internal(nr_refspec, refspec, 1, 0);
+}
+
+struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
+{
+       return parse_refspec_internal(nr_refspec, refspec, 0, 0);
+}
+
+void free_refspec(int nr_refspec, struct refspec *refspec)
+{
+       int i;
+
+       if (!refspec)
+               return;
+
+       for (i = 0; i < nr_refspec; i++) {
+               free(refspec[i].src);
+               free(refspec[i].dst);
+       }
+       free(refspec);
+}
diff --git a/refspec.h b/refspec.h
new file mode 100644 (file)
index 0000000..62625c2
--- /dev/null
+++ b/refspec.h
@@ -0,0 +1,23 @@
+#ifndef REFSPEC_H
+#define REFSPEC_H
+
+#define TAG_REFSPEC "refs/tags/*:refs/tags/*"
+extern const struct refspec *tag_refspec;
+
+struct refspec {
+       unsigned force : 1;
+       unsigned pattern : 1;
+       unsigned matching : 1;
+       unsigned exact_sha1 : 1;
+
+       char *src;
+       char *dst;
+};
+
+int valid_fetch_refspec(const char *refspec);
+struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec);
+struct refspec *parse_push_refspec(int nr_refspec, const char **refspec);
+
+void free_refspec(int nr_refspec, struct refspec *refspec);
+
+#endif /* REFSPEC_H */
index 91eb010ca983c5493bbc17c5652ef31060390226..4d67c061a02c49ee6a59e0b270fed5308bfacdd3 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -2,6 +2,7 @@
 #include "config.h"
 #include "remote.h"
 #include "refs.h"
+#include "refspec.h"
 #include "commit.h"
 #include "diff.h"
 #include "revision.h"
 
 enum map_direction { FROM_SRC, FROM_DST };
 
-static struct refspec s_tag_refspec = {
-       0,
-       1,
-       0,
-       0,
-       "refs/tags/*",
-       "refs/tags/*"
-};
-
-/* See TAG_REFSPEC for the string version */
-const struct refspec *tag_refspec = &s_tag_refspec;
-
 struct counted_string {
        size_t len;
        const char *s;
@@ -499,158 +488,6 @@ static void read_config(void)
        alias_all_urls();
 }
 
-static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
-{
-       int i;
-       struct refspec *rs = xcalloc(nr_refspec, sizeof(*rs));
-
-       for (i = 0; i < nr_refspec; i++) {
-               size_t llen;
-               int is_glob;
-               const char *lhs, *rhs;
-               int flags;
-
-               is_glob = 0;
-
-               lhs = refspec[i];
-               if (*lhs == '+') {
-                       rs[i].force = 1;
-                       lhs++;
-               }
-
-               rhs = strrchr(lhs, ':');
-
-               /*
-                * Before going on, special case ":" (or "+:") as a refspec
-                * for pushing matching refs.
-                */
-               if (!fetch && rhs == lhs && rhs[1] == '\0') {
-                       rs[i].matching = 1;
-                       continue;
-               }
-
-               if (rhs) {
-                       size_t rlen = strlen(++rhs);
-                       is_glob = (1 <= rlen && strchr(rhs, '*'));
-                       rs[i].dst = xstrndup(rhs, rlen);
-               }
-
-               llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
-               if (1 <= llen && memchr(lhs, '*', llen)) {
-                       if ((rhs && !is_glob) || (!rhs && fetch))
-                               goto invalid;
-                       is_glob = 1;
-               } else if (rhs && is_glob) {
-                       goto invalid;
-               }
-
-               rs[i].pattern = is_glob;
-               rs[i].src = xstrndup(lhs, llen);
-               flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
-
-               if (fetch) {
-                       struct object_id unused;
-
-                       /* LHS */
-                       if (!*rs[i].src)
-                               ; /* empty is ok; it means "HEAD" */
-                       else if (llen == GIT_SHA1_HEXSZ && !get_oid_hex(rs[i].src, &unused))
-                               rs[i].exact_sha1 = 1; /* ok */
-                       else if (!check_refname_format(rs[i].src, flags))
-                               ; /* valid looking ref is ok */
-                       else
-                               goto invalid;
-                       /* RHS */
-                       if (!rs[i].dst)
-                               ; /* missing is ok; it is the same as empty */
-                       else if (!*rs[i].dst)
-                               ; /* empty is ok; it means "do not store" */
-                       else if (!check_refname_format(rs[i].dst, flags))
-                               ; /* valid looking ref is ok */
-                       else
-                               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 (!*rs[i].src)
-                               ; /* empty is ok */
-                       else if (is_glob) {
-                               if (check_refname_format(rs[i].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 (!rs[i].dst) {
-                               if (check_refname_format(rs[i].src, flags))
-                                       goto invalid;
-                       } else if (!*rs[i].dst) {
-                               goto invalid;
-                       } else {
-                               if (check_refname_format(rs[i].dst, flags))
-                                       goto invalid;
-                       }
-               }
-       }
-       return rs;
-
- invalid:
-       if (verify) {
-               /*
-                * nr_refspec must be greater than zero and i must be valid
-                * since it is only possible to reach this point from within
-                * the for loop above.
-                */
-               free_refspec(i+1, rs);
-               return NULL;
-       }
-       die("Invalid refspec '%s'", refspec[i]);
-}
-
-int valid_fetch_refspec(const char *fetch_refspec_str)
-{
-       struct refspec *refspec;
-
-       refspec = parse_refspec_internal(1, &fetch_refspec_str, 1, 1);
-       free_refspec(1, refspec);
-       return !!refspec;
-}
-
-struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
-{
-       return parse_refspec_internal(nr_refspec, refspec, 1, 0);
-}
-
-struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
-{
-       return parse_refspec_internal(nr_refspec, refspec, 0, 0);
-}
-
-void free_refspec(int nr_refspec, struct refspec *refspec)
-{
-       int i;
-
-       if (!refspec)
-               return;
-
-       for (i = 0; i < nr_refspec; i++) {
-               free(refspec[i].src);
-               free(refspec[i].dst);
-       }
-       free(refspec);
-}
-
 static int valid_remote_nick(const char *name)
 {
        if (!name[0] || is_dot_or_dotdot(name))
index 2b3180f94dbc185a315255938c2de6cc6aef0b13..386ced90121eb1b7c5756c48c66392fe3ced6fa4 100644 (file)
--- a/remote.h
+++ b/remote.h
@@ -68,18 +68,6 @@ int for_each_remote(each_remote_fn fn, void *priv);
 
 int remote_has_url(struct remote *remote, const char *url);
 
-struct refspec {
-       unsigned force : 1;
-       unsigned pattern : 1;
-       unsigned matching : 1;
-       unsigned exact_sha1 : 1;
-
-       char *src;
-       char *dst;
-};
-
-extern const struct refspec *tag_refspec;
-
 struct ref {
        struct ref *next;
        struct object_id old_oid;
@@ -175,12 +163,6 @@ int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid);
  */
 struct ref *ref_remove_duplicates(struct ref *ref_map);
 
-int valid_fetch_refspec(const char *refspec);
-struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec);
-extern struct refspec *parse_push_refspec(int nr_refspec, const char **refspec);
-
-void free_refspec(int nr_refspec, struct refspec *refspec);
-
 extern int query_refspecs(struct refspec *specs, int nr, struct refspec *query);
 char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
                     const char *name);
@@ -313,8 +295,6 @@ extern int parseopt_push_cas_option(const struct option *, const char *arg, int
 extern int is_empty_cas(const struct push_cas_option *);
 void apply_push_cas(struct push_cas_option *, struct remote *, struct ref *);
 
-#define TAG_REFSPEC "refs/tags/*:refs/tags/*"
-
 void add_prune_tags_to_fetch_refspec(struct remote *remote);
 
 #endif
index 11f1055b47e5e204a272e3588f74f161bdf73895..b99e1cce9e6f2ae6d32ddd215361547c50b02b6f 100644 (file)
@@ -11,6 +11,7 @@
 #include "sigchain.h"
 #include "argv-array.h"
 #include "refs.h"
+#include "refspec.h"
 #include "transport-internal.h"
 #include "protocol.h"
 
index 37410d8aad9dd70df282c133ea9939bba5589efd..2cf63d18b7c4c486fd62306a1dbbf35396ddd83f 100644 (file)
@@ -11,6 +11,7 @@
 #include "bundle.h"
 #include "dir.h"
 #include "refs.h"
+#include "refspec.h"
 #include "branch.h"
 #include "url.h"
 #include "submodule.h"