Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 2 | #include "strvec.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, |
Jacob Keller | c0192df | 2020-09-30 14:25:29 -0700 | [diff] [blame] | 11 | 0, |
Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 12 | "refs/tags/*", |
| 13 | "refs/tags/*" |
| 14 | }; |
| 15 | |
| 16 | /* See TAG_REFSPEC for the string version */ |
Brandon Williams | 0ad4a5f | 2018-05-16 15:57:49 -0700 | [diff] [blame] | 17 | const struct refspec_item *tag_refspec = &s_tag_refspec; |
Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 18 | |
Brandon Williams | 3eec370 | 2018-05-16 15:57:50 -0700 | [diff] [blame] | 19 | /* |
| 20 | * Parses the provided refspec 'refspec' and populates the refspec_item 'item'. |
| 21 | * Returns 1 if successful and 0 if the refspec is invalid. |
| 22 | */ |
| 23 | static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch) |
| 24 | { |
| 25 | size_t llen; |
| 26 | int is_glob; |
| 27 | const char *lhs, *rhs; |
| 28 | int flags; |
| 29 | |
| 30 | is_glob = 0; |
| 31 | |
| 32 | lhs = refspec; |
| 33 | if (*lhs == '+') { |
| 34 | item->force = 1; |
| 35 | lhs++; |
Jacob Keller | c0192df | 2020-09-30 14:25:29 -0700 | [diff] [blame] | 36 | } else if (*lhs == '^') { |
| 37 | item->negative = 1; |
| 38 | lhs++; |
Brandon Williams | 3eec370 | 2018-05-16 15:57:50 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | rhs = strrchr(lhs, ':'); |
| 42 | |
Jacob Keller | c0192df | 2020-09-30 14:25:29 -0700 | [diff] [blame] | 43 | /* negative refspecs only have one side */ |
| 44 | if (item->negative && rhs) |
| 45 | return 0; |
| 46 | |
Brandon Williams | 3eec370 | 2018-05-16 15:57:50 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Before going on, special case ":" (or "+:") as a refspec |
| 49 | * for pushing matching refs. |
| 50 | */ |
| 51 | if (!fetch && rhs == lhs && rhs[1] == '\0') { |
| 52 | item->matching = 1; |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | if (rhs) { |
| 57 | size_t rlen = strlen(++rhs); |
| 58 | is_glob = (1 <= rlen && strchr(rhs, '*')); |
| 59 | item->dst = xstrndup(rhs, rlen); |
Junio C Hamano | c3072c6 | 2018-06-01 11:33:19 +0900 | [diff] [blame] | 60 | } else { |
| 61 | item->dst = NULL; |
Brandon Williams | 3eec370 | 2018-05-16 15:57:50 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | llen = (rhs ? (rhs - lhs - 1) : strlen(lhs)); |
| 65 | if (1 <= llen && memchr(lhs, '*', llen)) { |
Jacob Keller | c0192df | 2020-09-30 14:25:29 -0700 | [diff] [blame] | 66 | if ((rhs && !is_glob) || (!rhs && !item->negative && fetch)) |
Brandon Williams | 3eec370 | 2018-05-16 15:57:50 -0700 | [diff] [blame] | 67 | return 0; |
| 68 | is_glob = 1; |
| 69 | } else if (rhs && is_glob) { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | item->pattern = is_glob; |
Felipe Contreras | 374fbae | 2020-11-25 18:16:16 -0600 | [diff] [blame] | 74 | if (llen == 1 && *lhs == '@') |
| 75 | item->src = xstrdup("HEAD"); |
| 76 | else |
| 77 | item->src = xstrndup(lhs, llen); |
Brandon Williams | 3eec370 | 2018-05-16 15:57:50 -0700 | [diff] [blame] | 78 | flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0); |
| 79 | |
Jacob Keller | c0192df | 2020-09-30 14:25:29 -0700 | [diff] [blame] | 80 | if (item->negative) { |
| 81 | struct object_id unused; |
| 82 | |
| 83 | /* |
| 84 | * Negative refspecs only have a LHS, which indicates a ref |
| 85 | * (or pattern of refs) to exclude from other matches. This |
| 86 | * can either be a simple ref, or a glob pattern. Exact sha1 |
| 87 | * match is not currently supported. |
| 88 | */ |
| 89 | if (!*item->src) |
| 90 | return 0; /* negative refspecs must not be empty */ |
| 91 | else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused)) |
| 92 | return 0; /* negative refpsecs cannot be exact sha1 */ |
| 93 | else if (!check_refname_format(item->src, flags)) |
| 94 | ; /* valid looking ref is ok */ |
| 95 | else |
| 96 | return 0; |
| 97 | |
| 98 | /* the other rules below do not apply to negative refspecs */ |
| 99 | return 1; |
| 100 | } |
| 101 | |
Brandon Williams | 3eec370 | 2018-05-16 15:57:50 -0700 | [diff] [blame] | 102 | if (fetch) { |
| 103 | struct object_id unused; |
| 104 | |
| 105 | /* LHS */ |
| 106 | if (!*item->src) |
| 107 | ; /* empty is ok; it means "HEAD" */ |
brian m. carlson | b8d45d0 | 2019-02-19 00:05:21 +0000 | [diff] [blame] | 108 | 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] | 109 | item->exact_sha1 = 1; /* ok */ |
| 110 | else if (!check_refname_format(item->src, flags)) |
| 111 | ; /* valid looking ref is ok */ |
| 112 | else |
| 113 | return 0; |
| 114 | /* RHS */ |
| 115 | if (!item->dst) |
| 116 | ; /* missing is ok; it is the same as empty */ |
| 117 | else if (!*item->dst) |
| 118 | ; /* empty is ok; it means "do not store" */ |
| 119 | else if (!check_refname_format(item->dst, flags)) |
| 120 | ; /* valid looking ref is ok */ |
| 121 | else |
| 122 | return 0; |
| 123 | } else { |
| 124 | /* |
| 125 | * LHS |
| 126 | * - empty is allowed; it means delete. |
| 127 | * - when wildcarded, it must be a valid looking ref. |
| 128 | * - otherwise, it must be an extended SHA-1, but |
| 129 | * there is no existing way to validate this. |
| 130 | */ |
| 131 | if (!*item->src) |
| 132 | ; /* empty is ok */ |
| 133 | else if (is_glob) { |
| 134 | if (check_refname_format(item->src, flags)) |
| 135 | return 0; |
| 136 | } |
| 137 | else |
| 138 | ; /* anything goes, for now */ |
| 139 | /* |
| 140 | * RHS |
| 141 | * - missing is allowed, but LHS then must be a |
| 142 | * valid looking ref. |
| 143 | * - empty is not allowed. |
| 144 | * - otherwise it must be a valid looking ref. |
| 145 | */ |
| 146 | if (!item->dst) { |
| 147 | if (check_refname_format(item->src, flags)) |
| 148 | return 0; |
| 149 | } else if (!*item->dst) { |
| 150 | return 0; |
| 151 | } else { |
| 152 | if (check_refname_format(item->dst, flags)) |
| 153 | return 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return 1; |
| 158 | } |
| 159 | |
Ævar Arnfjörð Bjarmason | c495fd3 | 2018-06-05 19:54:39 +0000 | [diff] [blame] | 160 | 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] | 161 | { |
| 162 | memset(item, 0, sizeof(*item)); |
Ævar Arnfjörð Bjarmason | c495fd3 | 2018-06-05 19:54:39 +0000 | [diff] [blame] | 163 | return parse_refspec(item, refspec, fetch); |
| 164 | } |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 165 | |
Ævar Arnfjörð Bjarmason | dc06422 | 2018-06-05 19:54:38 +0000 | [diff] [blame] | 166 | void refspec_item_init_or_die(struct refspec_item *item, const char *refspec, |
| 167 | int fetch) |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 168 | { |
Ævar Arnfjörð Bjarmason | c495fd3 | 2018-06-05 19:54:39 +0000 | [diff] [blame] | 169 | if (!refspec_item_init(item, refspec, fetch)) |
Nguyễn Thái Ngọc Duy | 1b5e07b | 2018-07-21 09:49:36 +0200 | [diff] [blame] | 170 | die(_("invalid refspec '%s'"), refspec); |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void refspec_item_clear(struct refspec_item *item) |
| 174 | { |
| 175 | FREE_AND_NULL(item->src); |
| 176 | FREE_AND_NULL(item->dst); |
| 177 | item->force = 0; |
| 178 | item->pattern = 0; |
| 179 | item->matching = 0; |
| 180 | item->exact_sha1 = 0; |
| 181 | } |
| 182 | |
| 183 | void refspec_init(struct refspec *rs, int fetch) |
| 184 | { |
| 185 | memset(rs, 0, sizeof(*rs)); |
| 186 | rs->fetch = fetch; |
| 187 | } |
| 188 | |
René Scharfe | 1af8b8c | 2020-09-05 16:49:30 +0200 | [diff] [blame] | 189 | static void refspec_append_nodup(struct refspec *rs, char *refspec) |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 190 | { |
| 191 | struct refspec_item item; |
| 192 | |
Ævar Arnfjörð Bjarmason | dc06422 | 2018-06-05 19:54:38 +0000 | [diff] [blame] | 193 | refspec_item_init_or_die(&item, refspec, rs->fetch); |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 194 | |
| 195 | ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc); |
| 196 | rs->items[rs->nr++] = item; |
| 197 | |
| 198 | ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc); |
René Scharfe | 1af8b8c | 2020-09-05 16:49:30 +0200 | [diff] [blame] | 199 | rs->raw[rs->raw_nr++] = refspec; |
| 200 | } |
| 201 | |
| 202 | void refspec_append(struct refspec *rs, const char *refspec) |
| 203 | { |
| 204 | refspec_append_nodup(rs, xstrdup(refspec)); |
| 205 | } |
| 206 | |
| 207 | void refspec_appendf(struct refspec *rs, const char *fmt, ...) |
| 208 | { |
| 209 | va_list ap; |
| 210 | |
| 211 | va_start(ap, fmt); |
| 212 | refspec_append_nodup(rs, xstrvfmt(fmt, ap)); |
| 213 | va_end(ap); |
Brandon Williams | 6d4c057 | 2018-05-16 15:57:51 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void refspec_appendn(struct refspec *rs, const char **refspecs, int nr) |
| 217 | { |
| 218 | int i; |
| 219 | for (i = 0; i < nr; i++) |
| 220 | refspec_append(rs, refspecs[i]); |
| 221 | } |
| 222 | |
| 223 | void refspec_clear(struct refspec *rs) |
| 224 | { |
| 225 | int i; |
| 226 | |
| 227 | for (i = 0; i < rs->nr; i++) |
| 228 | refspec_item_clear(&rs->items[i]); |
| 229 | |
| 230 | FREE_AND_NULL(rs->items); |
| 231 | rs->alloc = 0; |
| 232 | rs->nr = 0; |
| 233 | |
| 234 | for (i = 0; i < rs->raw_nr; i++) |
| 235 | free((char *)rs->raw[i]); |
| 236 | FREE_AND_NULL(rs->raw); |
| 237 | rs->raw_alloc = 0; |
| 238 | rs->raw_nr = 0; |
| 239 | |
| 240 | rs->fetch = 0; |
| 241 | } |
Brandon Williams | c8fa9ef | 2018-05-16 15:57:52 -0700 | [diff] [blame] | 242 | |
| 243 | int valid_fetch_refspec(const char *fetch_refspec_str) |
| 244 | { |
| 245 | struct refspec_item refspec; |
Martin Ågren | 7865d15 | 2018-06-05 19:54:40 +0000 | [diff] [blame] | 246 | int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH); |
Brandon Williams | c8fa9ef | 2018-05-16 15:57:52 -0700 | [diff] [blame] | 247 | refspec_item_clear(&refspec); |
| 248 | return ret; |
| 249 | } |
Brandon Williams | 6373cb5 | 2018-05-16 16:48:21 -0700 | [diff] [blame] | 250 | |
Sean Barag | f2c6fda | 2020-10-01 03:46:13 +0000 | [diff] [blame] | 251 | int valid_remote_name(const char *name) |
| 252 | { |
| 253 | int result; |
| 254 | struct strbuf refspec = STRBUF_INIT; |
| 255 | strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name); |
| 256 | result = valid_fetch_refspec(refspec.buf); |
| 257 | strbuf_release(&refspec); |
| 258 | return result; |
| 259 | } |
| 260 | |
Brandon Williams | 6373cb5 | 2018-05-16 16:48:21 -0700 | [diff] [blame] | 261 | void refspec_ref_prefixes(const struct refspec *rs, |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 262 | struct strvec *ref_prefixes) |
Brandon Williams | 6373cb5 | 2018-05-16 16:48:21 -0700 | [diff] [blame] | 263 | { |
| 264 | int i; |
| 265 | for (i = 0; i < rs->nr; i++) { |
| 266 | const struct refspec_item *item = &rs->items[i]; |
| 267 | const char *prefix = NULL; |
| 268 | |
Jacob Keller | c0192df | 2020-09-30 14:25:29 -0700 | [diff] [blame] | 269 | if (item->exact_sha1 || item->negative) |
Jonathan Nieder | 6c301ad | 2018-05-31 00:23:39 -0700 | [diff] [blame] | 270 | continue; |
Brandon Williams | 6373cb5 | 2018-05-16 16:48:21 -0700 | [diff] [blame] | 271 | if (rs->fetch == REFSPEC_FETCH) |
| 272 | prefix = item->src; |
| 273 | else if (item->dst) |
| 274 | prefix = item->dst; |
| 275 | else if (item->src && !item->exact_sha1) |
| 276 | prefix = item->src; |
| 277 | |
Felipe Contreras | bfded87 | 2020-11-30 18:46:46 -0600 | [diff] [blame] | 278 | if (!prefix) |
| 279 | continue; |
| 280 | |
| 281 | if (item->pattern) { |
| 282 | const char *glob = strchr(prefix, '*'); |
| 283 | strvec_pushf(ref_prefixes, "%.*s", |
| 284 | (int)(glob - prefix), |
| 285 | prefix); |
| 286 | } else { |
| 287 | expand_ref_prefix(ref_prefixes, prefix); |
Brandon Williams | 6373cb5 | 2018-05-16 16:48:21 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | } |