Skip to content

Commit 202a56a

Browse files
mhaggergitster
authored andcommitted
is_dup_ref(): extract function from sort_ref_array()
Giving the function a name makes the code easier to understand. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6af1038 commit 202a56a

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

refs.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,28 @@ static int ref_entry_cmp(const void *a, const void *b)
8484
return strcmp(one->name, two->name);
8585
}
8686

87+
/*
88+
* Emit a warning and return true iff ref1 and ref2 have the same name
89+
* and the same sha1. Die if they have the same name but different
90+
* sha1s.
91+
*/
92+
static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2)
93+
{
94+
if (!strcmp(ref1->name, ref2->name)) {
95+
/* Duplicate name; make sure that the SHA1s match: */
96+
if (hashcmp(ref1->sha1, ref2->sha1))
97+
die("Duplicated ref, and SHA1s don't match: %s",
98+
ref1->name);
99+
warning("Duplicated ref: %s", ref1->name);
100+
return 1;
101+
} else {
102+
return 0;
103+
}
104+
}
105+
87106
static void sort_ref_array(struct ref_array *array)
88107
{
89-
int i = 0, j = 1;
108+
int i, j;
90109

91110
/* Nothing to sort unless there are at least two entries */
92111
if (array->nr < 2)
@@ -95,19 +114,13 @@ static void sort_ref_array(struct ref_array *array)
95114
qsort(array->refs, array->nr, sizeof(*array->refs), ref_entry_cmp);
96115

97116
/* Remove any duplicates from the ref_array */
98-
for (; j < array->nr; j++) {
99-
struct ref_entry *a = array->refs[i];
100-
struct ref_entry *b = array->refs[j];
101-
if (!strcmp(a->name, b->name)) {
102-
if (hashcmp(a->sha1, b->sha1))
103-
die("Duplicated ref, and SHA1s don't match: %s",
104-
a->name);
105-
warning("Duplicated ref: %s", a->name);
106-
free(b);
117+
i = 0;
118+
for (j = 1; j < array->nr; j++) {
119+
if (is_dup_ref(array->refs[i], array->refs[j])) {
120+
free(array->refs[j]);
107121
continue;
108122
}
109-
i++;
110-
array->refs[i] = array->refs[j];
123+
array->refs[++i] = array->refs[j];
111124
}
112125
array->nr = i + 1;
113126
}

0 commit comments

Comments
 (0)