Skip to content

Commit 81a79d8

Browse files
mhaggergitster
authored andcommitted
sort_ref_dir(): simplify logic
Use the more usual indexing idiom for clarity. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d317727 commit 81a79d8

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

refs.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,13 @@ static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2
227227
}
228228

229229
/*
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.
231232
*/
232233
static void sort_ref_dir(struct ref_dir *dir)
233234
{
234235
int i, j;
236+
struct ref_entry *last = NULL;
235237

236238
/*
237239
* This check also prevents passing a zero-length array to qsort(),
@@ -242,16 +244,15 @@ static void sort_ref_dir(struct ref_dir *dir)
242244

243245
qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
244246

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;
253254
}
254-
dir->sorted = dir->nr = i + 1;
255+
dir->sorted = dir->nr = i;
255256
}
256257

257258
#define DO_FOR_EACH_INCLUDE_BROKEN 01

0 commit comments

Comments
 (0)