]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refspec: introduce struct refspec
authorBrandon Williams <bmwill@google.com>
Wed, 16 May 2018 22:57:51 +0000 (15:57 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 17 May 2018 21:19:42 +0000 (06:19 +0900)
Introduce 'struct refspec', an abstraction around a collection of
'struct refspec_item's much like how 'struct pathspec' holds a
collection of 'struct pathspec_item's.

A refspec struct also contains an array of the original refspec strings
which will be used to facilitate the migration to using this new
abstraction throughout the code base.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refspec.c
refspec.h

index 8bf4ebbd3d67495243b2723cc7290a0d9f04081d..af9d0d4b30815b8a78b7c3c7ed12a16648e6aa18 100644 (file)
--- a/refspec.c
+++ b/refspec.c
@@ -178,3 +178,67 @@ void free_refspec(int nr_refspec, struct refspec_item *refspec)
        }
        free(refspec);
 }
+
+void refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
+{
+       memset(item, 0, sizeof(*item));
+
+       if (!parse_refspec(item, refspec, fetch))
+               die("Invalid refspec '%s'", refspec);
+}
+
+void refspec_item_clear(struct refspec_item *item)
+{
+       FREE_AND_NULL(item->src);
+       FREE_AND_NULL(item->dst);
+       item->force = 0;
+       item->pattern = 0;
+       item->matching = 0;
+       item->exact_sha1 = 0;
+}
+
+void refspec_init(struct refspec *rs, int fetch)
+{
+       memset(rs, 0, sizeof(*rs));
+       rs->fetch = fetch;
+}
+
+void refspec_append(struct refspec *rs, const char *refspec)
+{
+       struct refspec_item item;
+
+       refspec_item_init(&item, refspec, rs->fetch);
+
+       ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
+       rs->items[rs->nr++] = item;
+
+       ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
+       rs->raw[rs->raw_nr++] = xstrdup(refspec);
+}
+
+void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
+{
+       int i;
+       for (i = 0; i < nr; i++)
+               refspec_append(rs, refspecs[i]);
+}
+
+void refspec_clear(struct refspec *rs)
+{
+       int i;
+
+       for (i = 0; i < rs->nr; i++)
+               refspec_item_clear(&rs->items[i]);
+
+       FREE_AND_NULL(rs->items);
+       rs->alloc = 0;
+       rs->nr = 0;
+
+       for (i = 0; i < rs->raw_nr; i++)
+               free((char *)rs->raw[i]);
+       FREE_AND_NULL(rs->raw);
+       rs->raw_alloc = 0;
+       rs->raw_nr = 0;
+
+       rs->fetch = 0;
+}
index fc9c1af77576f882a3272bf7ca1db8f97eff5290..da3135825f93d21db7f8940799a8b2f39f577b28 100644 (file)
--- a/refspec.h
+++ b/refspec.h
@@ -20,4 +20,29 @@ struct refspec_item *parse_push_refspec(int nr_refspec, const char **refspec);
 
 void free_refspec(int nr_refspec, struct refspec_item *refspec);
 
+#define REFSPEC_FETCH 1
+#define REFSPEC_PUSH 0
+
+#define REFSPEC_INIT_FETCH { .fetch = REFSPEC_FETCH }
+#define REFSPEC_INIT_PUSH { .fetch = REFSPEC_PUSH }
+
+struct refspec {
+       struct refspec_item *items;
+       int alloc;
+       int nr;
+
+       const char **raw;
+       int raw_alloc;
+       int raw_nr;
+
+       int fetch;
+};
+
+void refspec_item_init(struct refspec_item *item, const char *refspec, int fetch);
+void refspec_item_clear(struct refspec_item *item);
+void refspec_init(struct refspec *rs, int fetch);
+void refspec_append(struct refspec *rs, const char *refspec);
+void refspec_appendn(struct refspec *rs, const char **refspecs, int nr);
+void refspec_clear(struct refspec *rs);
+
 #endif /* REFSPEC_H */