Skip to content

Commit 2e711ac

Browse files
amishhaagitster
authored andcommitted
string-list: add string_list_sort_u() that mimics "sort -u"
Many callsites of string_list_remove_duplicates() call it immdediately after calling string_list_sort(), understandably as the former requires string-list to be sorted, it is clear that these places are sorting only to remove duplicates and for no other reason. Introduce a helper function string_list_sort_u that combines these two calls that often appear together, to simplify these callsites. Replace the current calls of those methods with string_list_sort_u(). Signed-off-by: Amisha Chhajed <amishhhaaaa@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 208642c commit 2e711ac

9 files changed

Lines changed: 54 additions & 16 deletions

File tree

builtin/clone.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,7 @@ int cmd_clone(int argc,
11361136
int val;
11371137

11381138
/* remove duplicates */
1139-
string_list_sort(&option_recurse_submodules);
1140-
string_list_remove_duplicates(&option_recurse_submodules, 0);
1139+
string_list_sort_u(&option_recurse_submodules, 0);
11411140

11421141
/*
11431142
* NEEDSWORK: In a multi-working-tree world, this needs to be

builtin/fast-export.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,7 @@ static void get_tags_and_duplicates(struct rev_cmdline_info *info)
11181118
free(full_name);
11191119
}
11201120

1121-
string_list_sort(&extra_refs);
1122-
string_list_remove_duplicates(&extra_refs, 0);
1121+
string_list_sort_u(&extra_refs, 0);
11231122
}
11241123

11251124
static void handle_tags_and_duplicates(struct string_list *extras)

builtin/pack-objects.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,10 +3855,8 @@ static void read_packs_list_from_stdin(struct rev_info *revs)
38553855
strbuf_reset(&buf);
38563856
}
38573857

3858-
string_list_sort(&include_packs);
3859-
string_list_remove_duplicates(&include_packs, 0);
3860-
string_list_sort(&exclude_packs);
3861-
string_list_remove_duplicates(&exclude_packs, 0);
3858+
string_list_sort_u(&include_packs, 0);
3859+
string_list_sort_u(&exclude_packs, 0);
38623860

38633861
repo_for_each_pack(the_repository, p) {
38643862
const char *pack_name = pack_basename(p);

builtin/sparse-checkout.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
292292
string_list_insert(&sl, pe->pattern);
293293
}
294294

295-
string_list_sort(&sl);
296-
string_list_remove_duplicates(&sl, 0);
295+
string_list_sort_u(&sl, 0);
297296

298297
fprintf(fp, "/*\n!/*/\n");
299298

@@ -316,8 +315,7 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
316315

317316
strbuf_release(&parent_pattern);
318317

319-
string_list_sort(&sl);
320-
string_list_remove_duplicates(&sl, 0);
318+
string_list_sort_u(&sl, 0);
321319

322320
for (i = 0; i < sl.nr; i++) {
323321
char *pattern = escaped_pattern(sl.items[i].string);

help.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,7 @@ void list_cmds_by_config(struct string_list *list)
420420
if (repo_config_get_string_tmp(the_repository, "completion.commands", &cmd_list))
421421
return;
422422

423-
string_list_sort(list);
424-
string_list_remove_duplicates(list, 0);
423+
string_list_sort_u(list, 0);
425424

426425
while (*cmd_list) {
427426
struct strbuf sb = STRBUF_INIT;

notes.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,7 @@ int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
921921
if (string_list_add_note_lines(&sort_uniq_list, new_oid))
922922
goto out;
923923
string_list_remove_empty_items(&sort_uniq_list, 0);
924-
string_list_sort(&sort_uniq_list);
925-
string_list_remove_duplicates(&sort_uniq_list, 0);
924+
string_list_sort_u(&sort_uniq_list, 0);
926925

927926
/* create a new blob object from sort_uniq_list */
928927
if (for_each_string_list(&sort_uniq_list,

string-list.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ void string_list_sort(struct string_list *list)
247247
QSORT_S(list->items, list->nr, cmp_items, &sort_ctx);
248248
}
249249

250+
void string_list_sort_u(struct string_list *list, int free_util)
251+
{
252+
string_list_sort(list);
253+
string_list_remove_duplicates(list, free_util);
254+
}
255+
250256
struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
251257
const char *string)
252258
{

string-list.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ struct string_list_item *string_list_append_nodup(struct string_list *list, char
239239
*/
240240
void string_list_sort(struct string_list *list);
241241

242+
/**
243+
* Sort the list and then remove duplicate entries. If free_util is true,
244+
* call free() on the util members of any items that have to be deleted.
245+
*/
246+
void string_list_sort_u(struct string_list *list, int free_util);
247+
242248
/**
243249
* Like `string_list_has_string()` but for unsorted lists. Linear in
244250
* size of the list.

t/unit-tests/u-string-list.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,40 @@ void test_string_list__remove_duplicates(void)
437437
t_string_list_clear(&list, 0);
438438
}
439439

440+
static void t_string_list_sort_u(struct string_list *list, ...)
441+
{
442+
struct string_list expected_strings = STRING_LIST_INIT_DUP;
443+
va_list ap;
444+
445+
va_start(ap, list);
446+
t_vcreate_string_list_dup(&expected_strings, 0, ap);
447+
va_end(ap);
448+
449+
string_list_sort_u(list, 0);
450+
t_string_list_equal(list, &expected_strings);
451+
452+
string_list_clear(&expected_strings, 0);
453+
}
454+
455+
void test_string_list__sort_u(void)
456+
{
457+
struct string_list list = STRING_LIST_INIT_DUP;
458+
459+
t_create_string_list_dup(&list, 0, NULL);
460+
t_string_list_sort_u(&list, NULL);
461+
462+
t_create_string_list_dup(&list, 0, "", "", "", "", NULL);
463+
t_string_list_sort_u(&list, "", NULL);
464+
465+
t_create_string_list_dup(&list, 0, "b", "a", "a", "", NULL);
466+
t_string_list_sort_u(&list, "", "a", "b", NULL);
467+
468+
t_create_string_list_dup(&list, 0, "b", "a", "a", "d", "c", "c", NULL);
469+
t_string_list_sort_u(&list, "a", "b", "c", "d", NULL);
470+
471+
t_string_list_clear(&list, 0);
472+
}
473+
440474
static void t_string_list_remove_empty_items(
441475
struct string_list *expected_strings,
442476
struct string_list *list)

0 commit comments

Comments
 (0)