Skip to content

Commit f537cfa

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5096e48 commit f537cfa

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

builtin/fetch-pack.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -544,37 +544,36 @@ static void filter_refs(struct ref **refs, struct string_list *sought)
544544

545545
sought_pos = 0;
546546
for (ref = *refs; ref; ref = next) {
547+
int keep = 0;
547548
next = ref->next;
548549
if (!memcmp(ref->name, "refs/", 5) &&
549550
check_refname_format(ref->name + 5, 0))
550551
; /* trash */
551552
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;
558555
else {
559-
int cmp = -1;
560556
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 */
565562
sought->items[sought_pos++].util = "matched";
566-
*newtail = ref;
567-
ref->next = NULL;
568-
newtail = &ref->next;
569563
break;
570564
}
571-
else /* might have it; keep looking */
572-
sought_pos++;
565+
else
566+
sought_pos++; /* might have it; keep looking */
573567
}
574-
if (!cmp)
575-
continue; /* we will link it later */
576568
}
577-
free(ref);
569+
570+
if (keep) {
571+
*newtail = ref;
572+
ref->next = NULL;
573+
newtail = &ref->next;
574+
} else {
575+
free(ref);
576+
}
578577
}
579578

580579
if (!args.fetch_all)

0 commit comments

Comments
 (0)