sort_ref_dir(): simplify logic

Use the more usual indexing idiom for clarity. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 10, 2012 at 07:30 UTC 81a79d8e27f6dd5908ae9ba122833574b7475151
1 file changed +11 -10
refs.c
+11 -10
@@ -227,11 +227,13 @@ static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2
227 }
228
229 /*
230 - * Sort the entries in dir (if they are not already sorted).
230 + * Sort the entries in dir (if they are not already sorted)
231 + * and remove any duplicate entries.
232 */
233 static void sort_ref_dir(struct ref_dir *dir)
234 {
235 int i, j;
236 + struct ref_entry *last = NULL;
237
238 /*
239 * This check also prevents passing a zero-length array to qsort(),
@@ -242,16 +244,15 @@ static void sort_ref_dir(struct ref_dir *dir)
244
245 qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
246
245 - /* Remove any duplicates from the ref_dir */
246 - i = 0;
247 - for (j = 1; j < dir->nr; j++) {
248 - if (is_dup_ref(dir->entries[i], dir->entries[j])) {
249 - free_ref_entry(dir->entries[j]);
250 - continue;
251 - }
252 - dir->entries[++i] = dir->entries[j];
247 + /* Remove any duplicates: */
248 + for (i = 0, j = 0; j < dir->nr; j++) {
249 + struct ref_entry *entry = dir->entries[j];
250 + if (last && is_dup_ref(last, entry))
251 + free_ref_entry(entry);
252 + else
253 + last = dir->entries[i++] = entry;
254 }
254 - dir->sorted = dir->nr = i + 1;
255 + dir->sorted = dir->nr = i;
256 }
257
258 #define DO_FOR_EACH_INCLUDE_BROKEN 01