@matheusbsbs / matheus-bagliano / commits / 76f9e569ad
Fork 0

ref-filter: apply --ignore-case to all sorting keys

All of the ref-filter users (for-each-ref, branch, and tag) take an --ignore-case option which makes filtering and sorting case-insensitive. However, this option was applied only to the first element of the ref_sorting list. So: git for-each-ref --ignore-case --sort=refname would do what you expect, but: git for-each-ref --ignore-case --sort=refname --sort=taggername would sort the primary key (taggername) case-insensitively, but sort the refname case-sensitively. We have two options here: - teach callers to set ignore_case on the whole list - replace the ref_sorting list with a struct that contains both the list of sorting keys, as well as options that apply to _all_ keys I went with the first one here, as it gives more flexibility if we later want to let the users set the flag per-key (presumably through some special syntax when defining the key; for now it's all or nothing through --ignore-case). The new test covers this by sorting on both tagger and subject case-insensitively, which should compare "a" and "A" identically, but still sort them before "b" and "B". We'll break ties by sorting on the refname to give ourselves a stable output (this is actually supposed to be done automatically, but there's another bug which will be fixed in the next commit). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 3, 2020 at 05:11 UTC 76f9e569adb540a1ca1a590c512d57fce4eea878
6 files changed +51 -3
builtin/branch.c
+1 -1
@@ -711,7 +711,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
711 */
712 if (!sorting)
713 sorting = ref_default_sorting();
714 - sorting->ignore_case = icase;
714 + ref_sorting_icase_all(sorting, icase);
715 print_ref_list(&filter, sorting, &format);
716 print_columns(&output, colopts, NULL);
717 string_list_clear(&output, 0);
builtin/for-each-ref.c
+1 -1
@@ -71,7 +71,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
71
72 if (!sorting)
73 sorting = ref_default_sorting();
74 - sorting->ignore_case = icase;
74 + ref_sorting_icase_all(sorting, icase);
75 filter.ignore_case = icase;
76
77 filter.name_patterns = argv;
builtin/tag.c
+1 -1
@@ -465,7 +465,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
465 }
466 if (!sorting)
467 sorting = ref_default_sorting();
468 - sorting->ignore_case = icase;
468 + ref_sorting_icase_all(sorting, icase);
469 filter.ignore_case = icase;
470 if (cmdmode == 'l') {
471 int ret;
ref-filter.c
+6
@@ -2163,6 +2163,12 @@ static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
2163 return 0;
2164 }
2165
2166 +void ref_sorting_icase_all(struct ref_sorting *sorting, int flag)
2167 +{
2168 + for (; sorting; sorting = sorting->next)
2169 + sorting->ignore_case = !!flag;
2170 +}
2171 +
2172 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
2173 {
2174 QSORT_S(array->items, array->nr, compare_refs, sorting);
ref-filter.h
+2
@@ -109,6 +109,8 @@ void ref_array_clear(struct ref_array *array);
109 int verify_ref_format(struct ref_format *format);
110 /* Sort the given ref_array as per the ref_sorting provided */
111 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
112 +/* Set the ignore_case flag for all elements of a sorting list */
113 +void ref_sorting_icase_all(struct ref_sorting *sorting, int flag);
114 /* Based on the given format and quote_style, fill the strbuf */
115 int format_ref_array_item(struct ref_array_item *info,
116 const struct ref_format *format,
t/t6300-for-each-ref.sh
+40
@@ -828,4 +828,44 @@ test_expect_success 'for-each-ref --ignore-case ignores case' '
828 test_cmp expect actual
829 '
830
831 +test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
832 + # name refs numerically to avoid case-insensitive filesystem conflicts
833 + nr=0 &&
834 + for email in a A b B
835 + do
836 + for subject in a A b B
837 + do
838 + GIT_COMMITTER_EMAIL="$email@example.com" \
839 + git tag -m "tag $subject" icase-$(printf %02d $nr) &&
840 + nr=$((nr+1))||
841 + return 1
842 + done
843 + done &&
844 + git for-each-ref --ignore-case \
845 + --format="%(taggeremail) %(subject) %(refname)" \
846 + --sort=refname \
847 + --sort=subject \
848 + --sort=taggeremail \
849 + refs/tags/icase-* >actual &&
850 + cat >expect <<-\EOF &&
851 + <a@example.com> tag a refs/tags/icase-00
852 + <a@example.com> tag A refs/tags/icase-01
853 + <A@example.com> tag a refs/tags/icase-04
854 + <A@example.com> tag A refs/tags/icase-05
855 + <a@example.com> tag b refs/tags/icase-02
856 + <a@example.com> tag B refs/tags/icase-03
857 + <A@example.com> tag b refs/tags/icase-06
858 + <A@example.com> tag B refs/tags/icase-07
859 + <b@example.com> tag a refs/tags/icase-08
860 + <b@example.com> tag A refs/tags/icase-09
861 + <B@example.com> tag a refs/tags/icase-12
862 + <B@example.com> tag A refs/tags/icase-13
863 + <b@example.com> tag b refs/tags/icase-10
864 + <b@example.com> tag B refs/tags/icase-11
865 + <B@example.com> tag b refs/tags/icase-14
866 + <B@example.com> tag B refs/tags/icase-15
867 + EOF
868 + test_cmp expect actual
869 +'
870 +
871 test_done