filter_refs(): simplify logic

Simplify flow within loop: first decide whether to keep the reference, then keep/free it. This makes it clearer that each ref has exactly two possible destinies, and removes duplication of the code for appending the reference to the linked list. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 9, 2012 at 08:19 UTC f537cfa7501de5e4dcdac55a95515e9a2ff880e3
1 file changed +18 -19
builtin/fetch-pack.c
+18 -19
@@ -544,37 +544,36 @@ static void filter_refs(struct ref **refs, struct string_list *sought)
544
545 sought_pos = 0;
546 for (ref = *refs; ref; ref = next) {
547 + int keep = 0;
548 next = ref->next;
549 if (!memcmp(ref->name, "refs/", 5) &&
550 check_refname_format(ref->name + 5, 0))
551 ; /* trash */
552 else if (args.fetch_all &&
552 - (!args.depth || prefixcmp(ref->name, "refs/tags/") )) {
553 - *newtail = ref;
554 - ref->next = NULL;
555 - newtail = &ref->next;
556 - continue;
557 - }
553 + (!args.depth || prefixcmp(ref->name, "refs/tags/")))
554 + keep = 1;
555 else {
559 - int cmp = -1;
556 while (sought_pos < sought->nr) {
561 - cmp = strcmp(ref->name, sought->items[sought_pos].string);
562 - if (cmp < 0) /* definitely do not have it */
563 - break;
564 - else if (cmp == 0) { /* definitely have it */
557 + int cmp = strcmp(ref->name, sought->items[sought_pos].string);
558 + if (cmp < 0)
559 + break; /* definitely do not have it */
560 + else if (cmp == 0) {
561 + keep = 1; /* definitely have it */
562 sought->items[sought_pos++].util = "matched";
566 - *newtail = ref;
567 - ref->next = NULL;
568 - newtail = &ref->next;
563 break;
564 }
571 - else /* might have it; keep looking */
572 - sought_pos++;
565 + else
566 + sought_pos++; /* might have it; keep looking */
567 }
574 - if (!cmp)
575 - continue; /* we will link it later */
568 }
577 - free(ref);
569 +
570 + if (keep) {
571 + *newtail = ref;
572 + ref->next = NULL;
573 + newtail = &ref->next;
574 + } else {
575 + free(ref);
576 + }
577 }
578
579 if (!args.fetch_all)