Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | 6373cb5 | 2018-05-16 16:48:21 -0700 | [diff] [blame] | 2 | #include "argv-array.h" |
Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 3 | #include "refs.h" |
| 4 | #include "refspec.h" |
| 5 | |
Brandon Williams | 0ad4a5f | 2018-05-16 15:57:49 -0700 | [diff] [blame] | 6 | static struct refspec_item s_tag_refspec = { |
Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 7 | 0, |
| 8 | 1, |
| 9 | 0, |
| 10 | 0, |
| 11 | "refs/tags/*", |
| 12 | "refs/tags/*" |
| 13 | }; |
| 14 | |
| 15 | /* See TAG_REFSPEC for the string version */ |
Brandon Williams | 0ad4a5f | 2018-05-16 15:57:49 -0700 | [diff] [blame] | 16 | const struct refspec_item *tag_refspec = &s_tag_refspec; |
Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 17 | |
Brandon Williams | 3eec370 | 2018-05-16 15:57:50 -0700 | [diff] [blame] | 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 | */ |
| 22 | static 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); |
Junio C Hamano | c3072c6 | 2018-06-01 11:33:19 +0900 | [diff] [blame] | 52 | } else { |
| 53 | item->dst = NULL; |
Brandon Williams | 3eec370 | 2018-05-16 15:57:50 -0700 | [diff] [blame] | 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" */ |
brian m. carlson | b8d45d0 | 2019-02-19 00:05:21 +0000 | [diff] [blame] | 75 | else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused)) |
Brandon Williams | 3eec370 | 2018-05-16 15:57:50 -0700 | [diff] [blame] | 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 | |
Ævar Arnfjörð Bjarmason | c495fd3 | 2018-06-05 19:54:39 +0000 | [diff] [blame] | 127 | int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch) |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 128 | { |
| 129 | memset(item, 0, sizeof(*item)); |
Ævar Arnfjörð Bjarmason | c495fd3 | 2018-06-05 19:54:39 +0000 | [diff] [blame] | 130 | return parse_refspec(item, refspec, fetch); |
| 131 | } |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 132 | |
Ævar Arnfjörð Bjarmason | dc06422 | 2018-06-05 19:54:38 +0000 | [diff] [blame] | 133 | void refspec_item_init_or_die(struct refspec_item *item, const char *refspec, |
| 134 | int fetch) |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 135 | { |
Ævar Arnfjörð Bjarmason | c495fd3 | 2018-06-05 19:54:39 +0000 | [diff] [blame] | 136 | if (!refspec_item_init(item, refspec, fetch)) |
Nguyễn Thái Ngọc Duy | 1b5e07b | 2018-07-21 09:49:36 +0200 | [diff] [blame] | 137 | die(_("invalid refspec '%s'"), refspec); |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void refspec_item_clear(struct refspec_item *item) |
| 141 | { |
| 142 | FREE_AND_NULL(item->src); |
| 143 | FREE_AND_NULL(item->dst); |
| 144 | item->force = 0; |
| 145 | item->pattern = 0; |
| 146 | item->matching = 0; |
| 147 | item->exact_sha1 = 0; |
| 148 | } |
| 149 | |
| 150 | void refspec_init(struct refspec *rs, int fetch) |
| 151 | { |
| 152 | memset(rs, 0, sizeof(*rs)); |
| 153 | rs->fetch = fetch; |
| 154 | } |
| 155 | |
| 156 | void refspec_append(struct refspec *rs, const char *refspec) |
| 157 | { |
| 158 | struct refspec_item item; |
| 159 | |
Ævar Arnfjörð Bjarmason | dc06422 | 2018-06-05 19:54:38 +0000 | [diff] [blame] | 160 | refspec_item_init_or_die(&item, refspec, rs->fetch); |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 161 | |
| 162 | ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc); |
| 163 | rs->items[rs->nr++] = item; |
| 164 | |
| 165 | ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc); |
| 166 | rs->raw[rs->raw_nr++] = xstrdup(refspec); |
| 167 | } |
| 168 | |
| 169 | void refspec_appendn(struct refspec *rs, const char **refspecs, int nr) |
| 170 | { |
| 171 | int i; |
| 172 | for (i = 0; i < nr; i++) |
| 173 | refspec_append(rs, refspecs[i]); |
| 174 | } |
| 175 | |
| 176 | void refspec_clear(struct refspec *rs) |
| 177 | { |
| 178 | int i; |
| 179 | |
| 180 | for (i = 0; i < rs->nr; i++) |
| 181 | refspec_item_clear(&rs->items[i]); |
| 182 | |
| 183 | FREE_AND_NULL(rs->items); |
| 184 | rs->alloc = 0; |
| 185 | rs->nr = 0; |
| 186 | |
| 187 | for (i = 0; i < rs->raw_nr; i++) |
| 188 | free((char *)rs->raw[i]); |
| 189 | FREE_AND_NULL(rs->raw); |
| 190 | rs->raw_alloc = 0; |
| 191 | rs->raw_nr = 0; |
| 192 | |
| 193 | rs->fetch = 0; |
| 194 | } |
Brandon Williams | c8fa9ef | 2018-05-16 15:57:52 -0700 | [diff] [blame] | 195 | |
| 196 | int valid_fetch_refspec(const char *fetch_refspec_str) |
| 197 | { |
| 198 | struct refspec_item refspec; |
Martin Ågren | 7865d15 | 2018-06-05 19:54:40 +0000 | [diff] [blame] | 199 | int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH); |
Brandon Williams | c8fa9ef | 2018-05-16 15:57:52 -0700 | [diff] [blame] | 200 | refspec_item_clear(&refspec); |
| 201 | return ret; |
| 202 | } |
Brandon Williams | 6373cb5 | 2018-05-16 16:48:21 -0700 | [diff] [blame] | 203 | |
| 204 | void refspec_ref_prefixes(const struct refspec *rs, |
| 205 | struct argv_array *ref_prefixes) |
| 206 | { |
| 207 | int i; |
| 208 | for (i = 0; i < rs->nr; i++) { |
| 209 | const struct refspec_item *item = &rs->items[i]; |
| 210 | const char *prefix = NULL; |
| 211 | |
Jonathan Nieder | 6c301ad | 2018-05-31 00:23:39 -0700 | [diff] [blame] | 212 | if (item->exact_sha1) |
| 213 | continue; |
Brandon Williams | 6373cb5 | 2018-05-16 16:48:21 -0700 | [diff] [blame] | 214 | if (rs->fetch == REFSPEC_FETCH) |
| 215 | prefix = item->src; |
| 216 | else if (item->dst) |
| 217 | prefix = item->dst; |
| 218 | else if (item->src && !item->exact_sha1) |
| 219 | prefix = item->src; |
| 220 | |
| 221 | if (prefix) { |
| 222 | if (item->pattern) { |
| 223 | const char *glob = strchr(prefix, '*'); |
| 224 | argv_array_pushf(ref_prefixes, "%.*s", |
| 225 | (int)(glob - prefix), |
| 226 | prefix); |
| 227 | } else { |
| 228 | expand_ref_prefix(ref_prefixes, prefix); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |