refspec.con commit multi-pack-index: load into memory (4d80560)
   1#include "cache.h"
   2#include "argv-array.h"
   3#include "refs.h"
   4#include "refspec.h"
   5
   6static struct refspec_item s_tag_refspec = {
   7        0,
   8        1,
   9        0,
  10        0,
  11        "refs/tags/*",
  12        "refs/tags/*"
  13};
  14
  15/* See TAG_REFSPEC for the string version */
  16const struct refspec_item *tag_refspec = &s_tag_refspec;
  17
  18/*
  19 * Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
  20 * Returns 1 if successful and 0 if the refspec is invalid.
  21 */
  22static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch)
  23{
  24        size_t llen;
  25        int is_glob;
  26        const char *lhs, *rhs;
  27        int flags;
  28
  29        is_glob = 0;
  30
  31        lhs = refspec;
  32        if (*lhs == '+') {
  33                item->force = 1;
  34                lhs++;
  35        }
  36
  37        rhs = strrchr(lhs, ':');
  38
  39        /*
  40         * Before going on, special case ":" (or "+:") as a refspec
  41         * for pushing matching refs.
  42         */
  43        if (!fetch && rhs == lhs && rhs[1] == '\0') {
  44                item->matching = 1;
  45                return 1;
  46        }
  47
  48        if (rhs) {
  49                size_t rlen = strlen(++rhs);
  50                is_glob = (1 <= rlen && strchr(rhs, '*'));
  51                item->dst = xstrndup(rhs, rlen);
  52        } else {
  53                item->dst = NULL;
  54        }
  55
  56        llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
  57        if (1 <= llen && memchr(lhs, '*', llen)) {
  58                if ((rhs && !is_glob) || (!rhs && fetch))
  59                        return 0;
  60                is_glob = 1;
  61        } else if (rhs && is_glob) {
  62                return 0;
  63        }
  64
  65        item->pattern = is_glob;
  66        item->src = xstrndup(lhs, llen);
  67        flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
  68
  69        if (fetch) {
  70                struct object_id unused;
  71
  72                /* LHS */
  73                if (!*item->src)
  74                        ; /* empty is ok; it means "HEAD" */
  75                else if (llen == GIT_SHA1_HEXSZ && !get_oid_hex(item->src, &unused))
  76                        item->exact_sha1 = 1; /* ok */
  77                else if (!check_refname_format(item->src, flags))
  78                        ; /* valid looking ref is ok */
  79                else
  80                        return 0;
  81                /* RHS */
  82                if (!item->dst)
  83                        ; /* missing is ok; it is the same as empty */
  84                else if (!*item->dst)
  85                        ; /* empty is ok; it means "do not store" */
  86                else if (!check_refname_format(item->dst, flags))
  87                        ; /* valid looking ref is ok */
  88                else
  89                        return 0;
  90        } else {
  91                /*
  92                 * LHS
  93                 * - empty is allowed; it means delete.
  94                 * - when wildcarded, it must be a valid looking ref.
  95                 * - otherwise, it must be an extended SHA-1, but
  96                 *   there is no existing way to validate this.
  97                 */
  98                if (!*item->src)
  99                        ; /* empty is ok */
 100                else if (is_glob) {
 101                        if (check_refname_format(item->src, flags))
 102                                return 0;
 103                }
 104                else
 105                        ; /* anything goes, for now */
 106                /*
 107                 * RHS
 108                 * - missing is allowed, but LHS then must be a
 109                 *   valid looking ref.
 110                 * - empty is not allowed.
 111                 * - otherwise it must be a valid looking ref.
 112                 */
 113                if (!item->dst) {
 114                        if (check_refname_format(item->src, flags))
 115                                return 0;
 116                } else if (!*item->dst) {
 117                        return 0;
 118                } else {
 119                        if (check_refname_format(item->dst, flags))
 120                                return 0;
 121                }
 122        }
 123
 124        return 1;
 125}
 126
 127void refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
 128{
 129        memset(item, 0, sizeof(*item));
 130
 131        if (!parse_refspec(item, refspec, fetch))
 132                die("Invalid refspec '%s'", refspec);
 133}
 134
 135void refspec_item_clear(struct refspec_item *item)
 136{
 137        FREE_AND_NULL(item->src);
 138        FREE_AND_NULL(item->dst);
 139        item->force = 0;
 140        item->pattern = 0;
 141        item->matching = 0;
 142        item->exact_sha1 = 0;
 143}
 144
 145void refspec_init(struct refspec *rs, int fetch)
 146{
 147        memset(rs, 0, sizeof(*rs));
 148        rs->fetch = fetch;
 149}
 150
 151void refspec_append(struct refspec *rs, const char *refspec)
 152{
 153        struct refspec_item item;
 154
 155        refspec_item_init(&item, refspec, rs->fetch);
 156
 157        ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
 158        rs->items[rs->nr++] = item;
 159
 160        ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
 161        rs->raw[rs->raw_nr++] = xstrdup(refspec);
 162}
 163
 164void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
 165{
 166        int i;
 167        for (i = 0; i < nr; i++)
 168                refspec_append(rs, refspecs[i]);
 169}
 170
 171void refspec_clear(struct refspec *rs)
 172{
 173        int i;
 174
 175        for (i = 0; i < rs->nr; i++)
 176                refspec_item_clear(&rs->items[i]);
 177
 178        FREE_AND_NULL(rs->items);
 179        rs->alloc = 0;
 180        rs->nr = 0;
 181
 182        for (i = 0; i < rs->raw_nr; i++)
 183                free((char *)rs->raw[i]);
 184        FREE_AND_NULL(rs->raw);
 185        rs->raw_alloc = 0;
 186        rs->raw_nr = 0;
 187
 188        rs->fetch = 0;
 189}
 190
 191int valid_fetch_refspec(const char *fetch_refspec_str)
 192{
 193        struct refspec_item refspec;
 194        int ret = parse_refspec(&refspec, fetch_refspec_str, REFSPEC_FETCH);
 195        refspec_item_clear(&refspec);
 196        return ret;
 197}
 198
 199void refspec_ref_prefixes(const struct refspec *rs,
 200                          struct argv_array *ref_prefixes)
 201{
 202        int i;
 203        for (i = 0; i < rs->nr; i++) {
 204                const struct refspec_item *item = &rs->items[i];
 205                const char *prefix = NULL;
 206
 207                if (item->exact_sha1)
 208                        continue;
 209                if (rs->fetch == REFSPEC_FETCH)
 210                        prefix = item->src;
 211                else if (item->dst)
 212                        prefix = item->dst;
 213                else if (item->src && !item->exact_sha1)
 214                        prefix = item->src;
 215
 216                if (prefix) {
 217                        if (item->pattern) {
 218                                const char *glob = strchr(prefix, '*');
 219                                argv_array_pushf(ref_prefixes, "%.*s",
 220                                                 (int)(glob - prefix),
 221                                                 prefix);
 222                        } else {
 223                                expand_ref_prefix(ref_prefixes, prefix);
 224                        }
 225                }
 226        }
 227}