Skip to content

Commit 76f9e56

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 041bc65 commit 76f9e56

File tree

6 files changed

+51
-3
lines changed

6 files changed

+51
-3
lines changed

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
711711
*/
712712
if (!sorting)
713713
sorting = ref_default_sorting();
714-
sorting->ignore_case = icase;
714+
ref_sorting_icase_all(sorting, icase);
715715
print_ref_list(&filter, sorting, &format);
716716
print_columns(&output, colopts, NULL);
717717
string_list_clear(&output, 0);

builtin/for-each-ref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
7171

7272
if (!sorting)
7373
sorting = ref_default_sorting();
74-
sorting->ignore_case = icase;
74+
ref_sorting_icase_all(sorting, icase);
7575
filter.ignore_case = icase;
7676

7777
filter.name_patterns = argv;

builtin/tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
465465
}
466466
if (!sorting)
467467
sorting = ref_default_sorting();
468-
sorting->ignore_case = icase;
468+
ref_sorting_icase_all(sorting, icase);
469469
filter.ignore_case = icase;
470470
if (cmdmode == 'l') {
471471
int ret;

ref-filter.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,12 @@ static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
21632163
return 0;
21642164
}
21652165

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+
21662172
void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
21672173
{
21682174
QSORT_S(array->items, array->nr, compare_refs, sorting);

ref-filter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ void ref_array_clear(struct ref_array *array);
109109
int verify_ref_format(struct ref_format *format);
110110
/* Sort the given ref_array as per the ref_sorting provided */
111111
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);
112114
/* Based on the given format and quote_style, fill the strbuf */
113115
int format_ref_array_item(struct ref_array_item *info,
114116
const struct ref_format *format,

t/t6300-for-each-ref.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,4 +828,44 @@ test_expect_success 'for-each-ref --ignore-case ignores case' '
828828
test_cmp expect actual
829829
'
830830

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 protected]" \
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+
<[email protected]> tag a refs/tags/icase-00
852+
<[email protected]> tag A refs/tags/icase-01
853+
<[email protected]> tag a refs/tags/icase-04
854+
<[email protected]> tag A refs/tags/icase-05
855+
<[email protected]> tag b refs/tags/icase-02
856+
<[email protected]> tag B refs/tags/icase-03
857+
<[email protected]> tag b refs/tags/icase-06
858+
<[email protected]> tag B refs/tags/icase-07
859+
<[email protected]> tag a refs/tags/icase-08
860+
<[email protected]> tag A refs/tags/icase-09
861+
<[email protected]> tag a refs/tags/icase-12
862+
<[email protected]> tag A refs/tags/icase-13
863+
<[email protected]> tag b refs/tags/icase-10
864+
<[email protected]> tag B refs/tags/icase-11
865+
<[email protected]> tag b refs/tags/icase-14
866+
<[email protected]> tag B refs/tags/icase-15
867+
EOF
868+
test_cmp expect actual
869+
'
870+
831871
test_done

0 commit comments

Comments
 (0)