Skip to content

Commit fa6ca11

Browse files
pcloudsgitster
authored andcommitted
merge-recursive.c: use string_list_sort instead of qsort
Merge-recursive sorts a string list using a raw qsort(), where it feeds the "items" from one struct but the "nr" and size fields from another struct. This isn't a bug because one list is a copy of the other, but it's unnecessarily confusing (and also caused our recent QSORT() cleanups via coccinelle to miss this call site). Let's use string_list_sort() instead, which is more concise and harder to get wrong. Note that we need to adjust our comparison function, which gets fed only the strings now, not the string_list_items. That's OK because we don't use the "util" field as part of our sort. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac84098 commit fa6ca11

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

merge-recursive.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,10 @@ static struct string_list *get_unmerged(void)
388388
return unmerged;
389389
}
390390

391-
static int string_list_df_name_compare(const void *a, const void *b)
391+
static int string_list_df_name_compare(const char *one, const char *two)
392392
{
393-
const struct string_list_item *one = a;
394-
const struct string_list_item *two = b;
395-
int onelen = strlen(one->string);
396-
int twolen = strlen(two->string);
393+
int onelen = strlen(one);
394+
int twolen = strlen(two);
397395
/*
398396
* Here we only care that entries for D/F conflicts are
399397
* adjacent, in particular with the file of the D/F conflict
@@ -406,8 +404,8 @@ static int string_list_df_name_compare(const void *a, const void *b)
406404
* since in other cases any changes in their order due to
407405
* sorting cause no problems for us.
408406
*/
409-
int cmp = df_name_compare(one->string, onelen, S_IFDIR,
410-
two->string, twolen, S_IFDIR);
407+
int cmp = df_name_compare(one, onelen, S_IFDIR,
408+
two, twolen, S_IFDIR);
411409
/*
412410
* Now that 'foo' and 'foo/bar' compare equal, we have to make sure
413411
* that 'foo' comes before 'foo/bar'.
@@ -451,8 +449,8 @@ static void record_df_conflict_files(struct merge_options *o,
451449
string_list_append(&df_sorted_entries, next->string)->util =
452450
next->util;
453451
}
454-
qsort(df_sorted_entries.items, entries->nr, sizeof(*entries->items),
455-
string_list_df_name_compare);
452+
df_sorted_entries.cmp = string_list_df_name_compare;
453+
string_list_sort(&df_sorted_entries);
456454

457455
string_list_clear(&o->df_conflict_file_set, 1);
458456
for (i = 0; i < df_sorted_entries.nr; i++) {

0 commit comments

Comments
 (0)