From 2e711acfbdfc0fedf631688d78cde153ba835c93 Mon Sep 17 00:00:00 2001 From: Amisha Chhajed Date: Thu, 29 Jan 2026 17:42:20 +0530 Subject: 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 Signed-off-by: Junio C Hamano --- t/unit-tests/u-string-list.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 't') diff --git a/t/unit-tests/u-string-list.c b/t/unit-tests/u-string-list.c index d469a06eca..7ad84cc1cd 100644 --- a/t/unit-tests/u-string-list.c +++ b/t/unit-tests/u-string-list.c @@ -437,6 +437,40 @@ void test_string_list__remove_duplicates(void) t_string_list_clear(&list, 0); } +static void t_string_list_sort_u(struct string_list *list, ...) +{ + struct string_list expected_strings = STRING_LIST_INIT_DUP; + va_list ap; + + va_start(ap, list); + t_vcreate_string_list_dup(&expected_strings, 0, ap); + va_end(ap); + + string_list_sort_u(list, 0); + t_string_list_equal(list, &expected_strings); + + string_list_clear(&expected_strings, 0); +} + +void test_string_list__sort_u(void) +{ + struct string_list list = STRING_LIST_INIT_DUP; + + t_create_string_list_dup(&list, 0, NULL); + t_string_list_sort_u(&list, NULL); + + t_create_string_list_dup(&list, 0, "", "", "", "", NULL); + t_string_list_sort_u(&list, "", NULL); + + t_create_string_list_dup(&list, 0, "b", "a", "a", "", NULL); + t_string_list_sort_u(&list, "", "a", "b", NULL); + + t_create_string_list_dup(&list, 0, "b", "a", "a", "d", "c", "c", NULL); + t_string_list_sort_u(&list, "a", "b", "c", "d", NULL); + + t_string_list_clear(&list, 0); +} + static void t_string_list_remove_empty_items( struct string_list *expected_strings, struct string_list *list) -- cgit v1.3-6-g1900