|
5 | 5 | #include "gettext.h"
|
6 | 6 | #include "hash.h"
|
7 | 7 | #include "hex.h"
|
| 8 | +#include "string-list.h" |
8 | 9 | #include "strvec.h"
|
9 | 10 | #include "refs.h"
|
10 | 11 | #include "refspec.h"
|
| 12 | +#include "remote.h" |
11 | 13 | #include "strbuf.h"
|
12 | 14 |
|
13 | 15 | /*
|
@@ -276,3 +278,204 @@ void refspec_ref_prefixes(const struct refspec *rs,
|
276 | 278 | }
|
277 | 279 | }
|
278 | 280 | }
|
| 281 | + |
| 282 | +int match_name_with_pattern(const char *key, const char *name, |
| 283 | + const char *value, char **result) |
| 284 | +{ |
| 285 | + const char *kstar = strchr(key, '*'); |
| 286 | + size_t klen; |
| 287 | + size_t ksuffixlen; |
| 288 | + size_t namelen; |
| 289 | + int ret; |
| 290 | + if (!kstar) |
| 291 | + die(_("key '%s' of pattern had no '*'"), key); |
| 292 | + klen = kstar - key; |
| 293 | + ksuffixlen = strlen(kstar + 1); |
| 294 | + namelen = strlen(name); |
| 295 | + ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen && |
| 296 | + !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen); |
| 297 | + if (ret && value) { |
| 298 | + struct strbuf sb = STRBUF_INIT; |
| 299 | + const char *vstar = strchr(value, '*'); |
| 300 | + if (!vstar) |
| 301 | + die(_("value '%s' of pattern has no '*'"), value); |
| 302 | + strbuf_add(&sb, value, vstar - value); |
| 303 | + strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen); |
| 304 | + strbuf_addstr(&sb, vstar + 1); |
| 305 | + *result = strbuf_detach(&sb, NULL); |
| 306 | + } |
| 307 | + return ret; |
| 308 | +} |
| 309 | + |
| 310 | +static int refspec_match(const struct refspec_item *refspec, |
| 311 | + const char *name) |
| 312 | +{ |
| 313 | + if (refspec->pattern) |
| 314 | + return match_name_with_pattern(refspec->src, name, NULL, NULL); |
| 315 | + |
| 316 | + return !strcmp(refspec->src, name); |
| 317 | +} |
| 318 | + |
| 319 | +int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs) |
| 320 | +{ |
| 321 | + int i; |
| 322 | + |
| 323 | + for (i = 0; i < rs->nr; i++) { |
| 324 | + if (rs->items[i].negative && refspec_match(&rs->items[i], refname)) |
| 325 | + return 1; |
| 326 | + } |
| 327 | + return 0; |
| 328 | +} |
| 329 | + |
| 330 | +static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query) |
| 331 | +{ |
| 332 | + int i, matched_negative = 0; |
| 333 | + int find_src = !query->src; |
| 334 | + struct string_list reversed = STRING_LIST_INIT_DUP; |
| 335 | + const char *needle = find_src ? query->dst : query->src; |
| 336 | + |
| 337 | + /* |
| 338 | + * Check whether the queried ref matches any negative refpsec. If so, |
| 339 | + * then we should ultimately treat this as not matching the query at |
| 340 | + * all. |
| 341 | + * |
| 342 | + * Note that negative refspecs always match the source, but the query |
| 343 | + * item uses the destination. To handle this, we apply pattern |
| 344 | + * refspecs in reverse to figure out if the query source matches any |
| 345 | + * of the negative refspecs. |
| 346 | + * |
| 347 | + * The first loop finds and expands all positive refspecs |
| 348 | + * matched by the queried ref. |
| 349 | + * |
| 350 | + * The second loop checks if any of the results of the first loop |
| 351 | + * match any negative refspec. |
| 352 | + */ |
| 353 | + for (i = 0; i < rs->nr; i++) { |
| 354 | + struct refspec_item *refspec = &rs->items[i]; |
| 355 | + char *expn_name; |
| 356 | + |
| 357 | + if (refspec->negative) |
| 358 | + continue; |
| 359 | + |
| 360 | + /* Note the reversal of src and dst */ |
| 361 | + if (refspec->pattern) { |
| 362 | + const char *key = refspec->dst ? refspec->dst : refspec->src; |
| 363 | + const char *value = refspec->src; |
| 364 | + |
| 365 | + if (match_name_with_pattern(key, needle, value, &expn_name)) |
| 366 | + string_list_append_nodup(&reversed, expn_name); |
| 367 | + } else if (refspec->matching) { |
| 368 | + /* For the special matching refspec, any query should match */ |
| 369 | + string_list_append(&reversed, needle); |
| 370 | + } else if (!refspec->src) { |
| 371 | + BUG("refspec->src should not be null here"); |
| 372 | + } else if (!strcmp(needle, refspec->src)) { |
| 373 | + string_list_append(&reversed, refspec->src); |
| 374 | + } |
| 375 | + } |
| 376 | + |
| 377 | + for (i = 0; !matched_negative && i < reversed.nr; i++) { |
| 378 | + if (refname_matches_negative_refspec_item(reversed.items[i].string, rs)) |
| 379 | + matched_negative = 1; |
| 380 | + } |
| 381 | + |
| 382 | + string_list_clear(&reversed, 0); |
| 383 | + |
| 384 | + return matched_negative; |
| 385 | +} |
| 386 | + |
| 387 | +void refspec_find_all_matches(struct refspec *rs, |
| 388 | + struct refspec_item *query, |
| 389 | + struct string_list *results) |
| 390 | +{ |
| 391 | + int i; |
| 392 | + int find_src = !query->src; |
| 393 | + |
| 394 | + if (find_src && !query->dst) |
| 395 | + BUG("refspec_find_all_matches: need either src or dst"); |
| 396 | + |
| 397 | + if (refspec_find_negative_match(rs, query)) |
| 398 | + return; |
| 399 | + |
| 400 | + for (i = 0; i < rs->nr; i++) { |
| 401 | + struct refspec_item *refspec = &rs->items[i]; |
| 402 | + const char *key = find_src ? refspec->dst : refspec->src; |
| 403 | + const char *value = find_src ? refspec->src : refspec->dst; |
| 404 | + const char *needle = find_src ? query->dst : query->src; |
| 405 | + char **result = find_src ? &query->src : &query->dst; |
| 406 | + |
| 407 | + if (!refspec->dst || refspec->negative) |
| 408 | + continue; |
| 409 | + if (refspec->pattern) { |
| 410 | + if (match_name_with_pattern(key, needle, value, result)) |
| 411 | + string_list_append_nodup(results, *result); |
| 412 | + } else if (!strcmp(needle, key)) { |
| 413 | + string_list_append(results, value); |
| 414 | + } |
| 415 | + } |
| 416 | +} |
| 417 | + |
| 418 | +int refspec_find_match(struct refspec *rs, struct refspec_item *query) |
| 419 | +{ |
| 420 | + int i; |
| 421 | + int find_src = !query->src; |
| 422 | + const char *needle = find_src ? query->dst : query->src; |
| 423 | + char **result = find_src ? &query->src : &query->dst; |
| 424 | + |
| 425 | + if (find_src && !query->dst) |
| 426 | + BUG("refspec_find_match: need either src or dst"); |
| 427 | + |
| 428 | + if (refspec_find_negative_match(rs, query)) |
| 429 | + return -1; |
| 430 | + |
| 431 | + for (i = 0; i < rs->nr; i++) { |
| 432 | + struct refspec_item *refspec = &rs->items[i]; |
| 433 | + const char *key = find_src ? refspec->dst : refspec->src; |
| 434 | + const char *value = find_src ? refspec->src : refspec->dst; |
| 435 | + |
| 436 | + if (!refspec->dst || refspec->negative) |
| 437 | + continue; |
| 438 | + if (refspec->pattern) { |
| 439 | + if (match_name_with_pattern(key, needle, value, result)) { |
| 440 | + query->force = refspec->force; |
| 441 | + return 0; |
| 442 | + } |
| 443 | + } else if (!strcmp(needle, key)) { |
| 444 | + *result = xstrdup(value); |
| 445 | + query->force = refspec->force; |
| 446 | + return 0; |
| 447 | + } |
| 448 | + } |
| 449 | + return -1; |
| 450 | +} |
| 451 | + |
| 452 | +struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs) |
| 453 | +{ |
| 454 | + struct ref **tail; |
| 455 | + |
| 456 | + for (tail = &ref_map; *tail; ) { |
| 457 | + struct ref *ref = *tail; |
| 458 | + |
| 459 | + if (refname_matches_negative_refspec_item(ref->name, rs)) { |
| 460 | + *tail = ref->next; |
| 461 | + free(ref->peer_ref); |
| 462 | + free(ref); |
| 463 | + } else |
| 464 | + tail = &ref->next; |
| 465 | + } |
| 466 | + |
| 467 | + return ref_map; |
| 468 | +} |
| 469 | + |
| 470 | +char *apply_refspecs(struct refspec *rs, const char *name) |
| 471 | +{ |
| 472 | + struct refspec_item query; |
| 473 | + |
| 474 | + memset(&query, 0, sizeof(struct refspec_item)); |
| 475 | + query.src = (char *)name; |
| 476 | + |
| 477 | + if (refspec_find_match(rs, &query)) |
| 478 | + return NULL; |
| 479 | + |
| 480 | + return query.dst; |
| 481 | +} |
0 commit comments