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 <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Dec 12, 2011 at 06:38 UTC
202a56a92414a2142cd92f0c3fa89bfb29149388
1 file changed
+25
-12
refs.c
+25
-12
@@ -84,9 +84,28 @@ static int ref_entry_cmp(const void *a, const void *b)
84
return strcmp(one->name, two->name);
85
}
86
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
+
106
static void sort_ref_array(struct ref_array *array)
107
{
89
- int i = 0, j = 1;
108
+ int i, j;
109
110
/* Nothing to sort unless there are at least two entries */
111
if (array->nr < 2)
@@ -95,19 +114,13 @@ static void sort_ref_array(struct ref_array *array)
114
qsort(array->refs, array->nr, sizeof(*array->refs), ref_entry_cmp);
115
116
/* 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]);
121
continue;
122
}
109
- i++;
110
- array->refs[i] = array->refs[j];
123
+ array->refs[++i] = array->refs[j];
124
}
125
array->nr = i + 1;
126
}