@matheusbsbs / matheus-bagliano / commits / 54a8ad925c
Fork 0

remote.c: refactor match_explicit_refs()

This does not change functionality; just splits one block that is deeply nested and indented out of a huge loop into a separate function. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 8, 2007 at 23:22 UTC 54a8ad925cfac90bb4141c9904b1f97f0c5b83d4
1 file changed +86 -73
remote.c
+86 -73
@@ -406,90 +406,98 @@ static struct ref *try_explicit_object_name(const char *name)
406 return ref;
407 }
408
409 -static int match_explicit_refs(struct ref *src, struct ref *dst,
410 - struct ref ***dst_tail, struct refspec *rs,
411 - int rs_nr)
409 +static int match_explicit(struct ref *src, struct ref *dst,
410 + struct ref ***dst_tail,
411 + struct refspec *rs,
412 + int errs)
413 {
413 - int i, errs;
414 - for (i = errs = 0; i < rs_nr; i++) {
415 - struct ref *matched_src, *matched_dst;
414 + struct ref *matched_src, *matched_dst;
415
417 - const char *dst_value = rs[i].dst;
416 + const char *dst_value = rs->dst;
417
419 - if (rs[i].pattern)
420 - continue;
418 + if (rs->pattern)
419 + return errs;
420
422 - if (dst_value == NULL)
423 - dst_value = rs[i].src;
421 + if (dst_value == NULL)
422 + dst_value = rs->src;
423
425 - matched_src = matched_dst = NULL;
426 - switch (count_refspec_match(rs[i].src, src, &matched_src)) {
427 - case 1:
428 - break;
429 - case 0:
430 - /* The source could be in the get_sha1() format
431 - * not a reference name. :refs/other is a
432 - * way to delete 'other' ref at the remote end.
433 - */
434 - matched_src = try_explicit_object_name(rs[i].src);
435 - if (matched_src)
436 - break;
437 - errs = 1;
438 - error("src refspec %s does not match any.",
439 - rs[i].src);
440 - break;
441 - default:
442 - errs = 1;
443 - error("src refspec %s matches more than one.",
444 - rs[i].src);
445 - break;
446 - }
447 - switch (count_refspec_match(dst_value, dst, &matched_dst)) {
448 - case 1:
449 - break;
450 - case 0:
451 - if (!memcmp(dst_value, "refs/", 5)) {
452 - int len = strlen(dst_value) + 1;
453 - matched_dst = xcalloc(1, sizeof(*dst) + len);
454 - memcpy(matched_dst->name, dst_value, len);
455 - link_dst_tail(matched_dst, dst_tail);
456 - }
457 - else if (!strcmp(rs[i].src, dst_value) &&
458 - matched_src) {
459 - /* pushing "master:master" when
460 - * remote does not have master yet.
461 - */
462 - int len = strlen(matched_src->name) + 1;
463 - matched_dst = xcalloc(1, sizeof(*dst) + len);
464 - memcpy(matched_dst->name, matched_src->name,
465 - len);
466 - link_dst_tail(matched_dst, dst_tail);
467 - }
468 - else {
469 - errs = 1;
470 - error("dst refspec %s does not match any "
471 - "existing ref on the remote and does "
472 - "not start with refs/.", dst_value);
473 - }
474 - break;
475 - default:
476 - errs = 1;
477 - error("dst refspec %s matches more than one.",
478 - dst_value);
424 + matched_src = matched_dst = NULL;
425 + switch (count_refspec_match(rs->src, src, &matched_src)) {
426 + case 1:
427 + break;
428 + case 0:
429 + /* The source could be in the get_sha1() format
430 + * not a reference name. :refs/other is a
431 + * way to delete 'other' ref at the remote end.
432 + */
433 + matched_src = try_explicit_object_name(rs->src);
434 + if (matched_src)
435 break;
436 + errs = 1;
437 + error("src refspec %s does not match any.",
438 + rs->src);
439 + break;
440 + default:
441 + errs = 1;
442 + error("src refspec %s matches more than one.",
443 + rs->src);
444 + break;
445 + }
446 + switch (count_refspec_match(dst_value, dst, &matched_dst)) {
447 + case 1:
448 + break;
449 + case 0:
450 + if (!memcmp(dst_value, "refs/", 5)) {
451 + int len = strlen(dst_value) + 1;
452 + matched_dst = xcalloc(1, sizeof(*dst) + len);
453 + memcpy(matched_dst->name, dst_value, len);
454 + link_dst_tail(matched_dst, dst_tail);
455 }
481 - if (errs)
482 - continue;
483 - if (matched_dst->peer_ref) {
484 - errs = 1;
485 - error("dst ref %s receives from more than one src.",
486 - matched_dst->name);
456 + else if (!strcmp(rs->src, dst_value) &&
457 + matched_src) {
458 + /* pushing "master:master" when
459 + * remote does not have master yet.
460 + */
461 + int len = strlen(matched_src->name) + 1;
462 + matched_dst = xcalloc(1, sizeof(*dst) + len);
463 + memcpy(matched_dst->name, matched_src->name,
464 + len);
465 + link_dst_tail(matched_dst, dst_tail);
466 }
467 else {
489 - matched_dst->peer_ref = matched_src;
490 - matched_dst->force = rs[i].force;
468 + errs = 1;
469 + error("dst refspec %s does not match any "
470 + "existing ref on the remote and does "
471 + "not start with refs/.", dst_value);
472 }
473 + break;
474 + default:
475 + errs = 1;
476 + error("dst refspec %s matches more than one.",
477 + dst_value);
478 + break;
479 + }
480 + if (errs)
481 + return errs;
482 + if (matched_dst->peer_ref) {
483 + errs = 1;
484 + error("dst ref %s receives from more than one src.",
485 + matched_dst->name);
486 }
487 + else {
488 + matched_dst->peer_ref = matched_src;
489 + matched_dst->force = rs->force;
490 + }
491 + return errs;
492 +}
493 +
494 +static int match_explicit_refs(struct ref *src, struct ref *dst,
495 + struct ref ***dst_tail, struct refspec *rs,
496 + int rs_nr)
497 +{
498 + int i, errs;
499 + for (i = errs = 0; i < rs_nr; i++)
500 + errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
501 return -errs;
502 }
503
@@ -513,6 +521,11 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,
521 return NULL;
522 }
523
524 +/*
525 + * Note. This is used only by "push"; refspec matching rules for
526 + * push and fetch are subtly different, so do not try to reuse it
527 + * without thinking.
528 + */
529 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
530 int nr_refspec, char **refspec, int all)
531 {