Skip to contentSkip to conversation

git for-each-ref - sorting by multiple keys

PATCH Applied clime 6 participants 21 messages May 2, 2020 – May 5, 2020 Documentation

lore
Share & tools
lore

clime

author
#0 lore raw

Hello,

I have the following command:

/usr/bin/git for-each-ref --merged="${GIT_HEAD-HEAD}" --sort='-taggerdate' --sort='-*committerdate' --format="%(*committerdate)|%(taggerdate)|%(tag)" refs/tags

I thought this will use: -*commiterdate as a primary key and -taggerdate as a secondary. According to man page for --sort: "You may use the --sort=<key> option multiple times, in which case the last key becomes the primary key."

But that doesn't seem to be the case. I created a repo with a single commit and created annotated tags on the commit in the following order:

$ git tag -a -m "foo" B
$ git tag -a -m "foo" C
$ git tag -a -m "foo" A

Yet the order I am getting after running the command is:

Sat May 2 22:10:30 2020 +0200|Sat May 2 22:14:49 2020 +0200|C
Sat May 2 22:10:30 2020 +0200|Sat May 2 22:14:45 2020 +0200|B
Sat May 2 22:10:30 2020 +0200|Sat May 2 22:14:51 2020 +0200|A

Is it a mistake in man pages? Is there any way to sort by multiple keys?

Thank you
clime

#1 lore raw
Quoted 6 lines · clime

I have the following command:

/usr/bin/git for-each-ref --merged="${GIT_HEAD-HEAD}" --sort='-taggerdate' --sort='-*committerdate' --format="%(*committerdate)|%(taggerdate)|%(tag)" refs/tags

Quoted 8 lines

I thought this will use: -*commiterdate as a primary key and -taggerdate as a secondary. According to man page for --sort: "You may use the --sort=<key> option multiple times, in which case the last key becomes the primary key."

But that doesn't seem to be the case. I created a repo with a single commit and created annotated tags on the commit in the following order:

It looks like this has been quite broken since 2015 and nobody noticed. :(

Thanks for your reproduction recipe; using times is a critical part of the bug. Here's a fix, plus a fix for a related bug I noticed while working on it (the second one fixes your bug).

[1/2]: ref-filter: apply --ignore-case to all sorting keys [2/2]: ref-filter: apply fallback refname sort only after all user sorts

 builtin/branch.c        |  2 +-
 builtin/for-each-ref.c  |  2 +-
 builtin/tag.c           |  2 +-
 ref-filter.c            | 13 +++++-
 ref-filter.h            |  2 +
 t/t6300-for-each-ref.sh | 94 ++++++++++++++++++++++++++++++++++++++---
 6 files changed, 104 insertions(+), 11 deletions(-)

-Peff

#2 lore raw

PATCH 1/2 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).

---
 builtin/branch.c        |  2 +-
 builtin/for-each-ref.c  |  2 +-
 builtin/tag.c           |  2 +-
 ref-filter.c            |  6 ++++++
 ref-filter.h            |  2 ++
 t/t6300-for-each-ref.sh | 40 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 51 insertions(+), 3 deletions(-)
builtin/branch.c +1 -1
index d8297f80ff..86341cc835 100644
@@ -739,7 +739,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		 */
 		if (!sorting)
 			sorting = ref_default_sorting();
-		sorting->ignore_case = icase;
+		ref_sorting_icase_all(sorting, icase);
 		print_ref_list(&filter, sorting, &format);
 		print_columns(&output, colopts, NULL);
 		string_list_clear(&output, 0);
builtin/for-each-ref.c +1 -1
index 465153e853..57489e4eab 100644
@@ -70,7 +70,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 
 	if (!sorting)
 		sorting = ref_default_sorting();
-	sorting->ignore_case = icase;
+	ref_sorting_icase_all(sorting, icase);
 	filter.ignore_case = icase;
 
 	filter.name_patterns = argv;
builtin/tag.c +1 -1
index dd160b49c7..ff7610b5c8 100644
@@ -485,7 +485,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	}
 	if (!sorting)
 		sorting = ref_default_sorting();
-	sorting->ignore_case = icase;
+	ref_sorting_icase_all(sorting, icase);
 	filter.ignore_case = icase;
 	if (cmdmode == 'l') {
 		int ret;
ref-filter.c +6 -0
index 35776838f4..bdb3535ce5 100644
@@ -2317,6 +2317,12 @@ static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
 	return 0;
 }
 
+void ref_sorting_icase_all(struct ref_sorting *sorting, int flag)
+{
+	for (; sorting; sorting = sorting->next)
+		sorting->ignore_case = !!flag;
+}
+
 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
 {
 	QSORT_S(array->items, array->nr, compare_refs, sorting);
ref-filter.h +2 -0
index 64330e9601..8ecc33cdfa 100644
@@ -114,6 +114,8 @@ void ref_array_clear(struct ref_array *array);
 int verify_ref_format(struct ref_format *format);
 /*  Sort the given ref_array as per the ref_sorting provided */
 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
+/*  Set the ignore_case flag for all elements of a sorting list */
+void ref_sorting_icase_all(struct ref_sorting *sorting, int flag);
 /*  Based on the given format and quote_style, fill the strbuf */
 int format_ref_array_item(struct ref_array_item *info,
 			  const struct ref_format *format,
t/t6300-for-each-ref.sh +40 -0
index b3c1092338..c9caf26327 100755
@@ -895,4 +895,44 @@ test_expect_success 'for-each-ref --ignore-case ignores case' '
 	test_cmp expect actual
 '
 
+test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
+	# name refs numerically to avoid case-insensitive filesystem conflicts
+	nr=0 &&
+	for email in a A b B
+	do
+		for subject in a A b B
+		do
+			GIT_COMMITTER_EMAIL="[email protected]" \
+			git tag -m "tag $subject" icase-$(printf %02d $nr) &&
+			nr=$((nr+1))||
+			return 1
+		done
+	done &&
+	git for-each-ref --ignore-case \
+		--format="%(taggeremail) %(subject) %(refname)" \
+		--sort=refname \
+		--sort=subject \
+		--sort=taggeremail \
+		refs/tags/icase-* >actual &&
+	cat >expect <<-\EOF &&
+	<[email protected]> tag a refs/tags/icase-00
+	<[email protected]> tag A refs/tags/icase-01
+	<[email protected]> tag a refs/tags/icase-04
+	<[email protected]> tag A refs/tags/icase-05
+	<[email protected]> tag b refs/tags/icase-02
+	<[email protected]> tag B refs/tags/icase-03
+	<[email protected]> tag b refs/tags/icase-06
+	<[email protected]> tag B refs/tags/icase-07
+	<[email protected]> tag a refs/tags/icase-08
+	<[email protected]> tag A refs/tags/icase-09
+	<[email protected]> tag a refs/tags/icase-12
+	<[email protected]> tag A refs/tags/icase-13
+	<[email protected]> tag b refs/tags/icase-10
+	<[email protected]> tag B refs/tags/icase-11
+	<[email protected]> tag b refs/tags/icase-14
+	<[email protected]> tag B refs/tags/icase-15
+	EOF
+	test_cmp expect actual
+'
+
 test_done
Signature · 1 line
2.26.2.957.g6dc93e954a
#3 lore raw

Re: [PATCH 1/2] ref-filter: apply --ignore-case to all sorting keys

t/t6300-for-each-ref.sh test_expect_success 'for-each-ref --ignore-case ignores case' ' · line 907

Quoted 10 lines · Jeff King
t/t6300-for-each-ref.sh +10 -0
+test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
+	# name refs numerically to avoid case-insensitive filesystem conflicts
+	nr=0 &&
+	for email in a A b B
+	do
+		for subject in a A b B
+		do
+			GIT_COMMITTER_EMAIL="[email protected]" \
+			git tag -m "tag $subject" icase-$(printf %02d $nr) &&
+			nr=$((nr+1))||

The CodingGuidelines said we want to spell $nr instead of nr inside arithmetic expansion for dash older than 0.5.4

I'm not sure if we should go with just $((nr+1)) or it's better to loosen our Guidelines. Since Debian Jessie (oldest supported Debian) ships 0.5.7. I don't know about other systems.

Signature · 1 line
Danh
#4 lore raw

Re: [PATCH 1/2] ref-filter: apply --ignore-case to all sorting keys

Quoted 15 lines · Danh Doan

t/t6300-for-each-ref.sh test_expect_success 'for-each-ref --ignore-case ignores case' ' · line 907

Quoted 10 lines · Jeff King
t/t6300-for-each-ref.sh +10 -0
+test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
+	# name refs numerically to avoid case-insensitive filesystem conflicts
+	nr=0 &&
+	for email in a A b B
+	do
+		for subject in a A b B
+		do
+			GIT_COMMITTER_EMAIL="[email protected]" \
+			git tag -m "tag $subject" icase-$(printf %02d $nr) &&
+			nr=$((nr+1))||

The CodingGuidelines said we want to spell $nr instead of nr inside arithmetic expansion for dash older than 0.5.4

Quoted 3 lines

I'm not sure if we should go with just $((nr+1)) or it's better to loosen our Guidelines. Since Debian Jessie (oldest supported Debian) ships 0.5.7. I don't know about other systems.

Hmm, somehow I didn't know about that rule. We have many cases already in the test suite and elsewhere (try grepping for '$(([a-z]', which isn't exhaustive but turns up many examples).

Maybe it's time to loosen the rule?

I've actually seen style guides suggesting to never use "$" there for a few reasons:

- it's slightly cleaner to read (this is the recommendation and rationale in Google's shell style guide)

- it's less surprising if you somehow end up with a non-number in your variable:

$ foo=bar
$ bar=41
$ echo $((foo + 1))
dash: 8: Illegal number: bar
$ echo $(($foo + 1))
42

That's using dash. With bash, both produce the answer 42! Clearly this isn't something we should be doing either way, but I'd much rather see "illegal number" in some cases which would alert us that something confusing is going on.

-Peff

#5 lore raw

Re: [PATCH 1/2] ref-filter: apply --ignore-case to all sorting keys

Quoted 5 lines · Jeff King

Hmm, somehow I didn't know about that rule. We have many cases already in the test suite and elsewhere (try grepping for '$(([a-z]', which isn't exhaustive but turns up many examples).

Maybe it's time to loosen the rule?

Let's do that. It's time.

#6 lore raw

PATCH 1/1 CodingGuidelines: drop arithmetic expansion advice to use "$x"

Quoted 9 lines · Junio C Hamano
Quoted 5 lines · Jeff King

Hmm, somehow I didn't know about that rule. We have many cases already in the test suite and elsewhere (try grepping for '$(([a-z]', which isn't exhaustive but turns up many examples).

Maybe it's time to loosen the rule?

Let's do that. It's time.

Here it is in patch form for your convenience.

-- >8 --
Subject: CodingGuidelines: drop arithmetic expansion advice to use "$x"

The advice to use "$x" rather than "x" in arithmetric expansion was working around a dash bug fixed in 0.5.4. Even Debian oldstable has 0.5.7 these days. And in the meantime, we've added almost two dozen instances of the "x" form which you can find with:

  git grep '$(([a-z]'

and nobody seems to have complained. Let's declare this workaround obsolete and simplify our style guide.

---
 Documentation/CodingGuidelines | 4 ----
 1 file changed, 4 deletions(-)
Documentation/CodingGuidelines +0 -4
index 390ceece52..a89e8dcfbc 100644
@@ -95,10 +95,6 @@ For shell scripts specifically (not exhaustive):
 
  - We use Arithmetic Expansion $(( ... )).
 
- - Inside Arithmetic Expansion, spell shell variables with $ in front
-   of them, as some shells do not grok $((x)) while accepting $(($x))
-   just fine (e.g. dash older than 0.5.4).
-
  - We do not use Process Substitution <(list) or >(list).
 
  - Do not write control structures on a single line with semicolon.
Signature · 1 line
2.26.2.957.g6dc93e954a
Reviewed-by on v1 1/1
#7 lore raw

Re: [PATCH] CodingGuidelines: drop arithmetic expansion advice to use "$x"

Quoted 6 lines · Jeff King

-- >8 --
Subject: CodingGuidelines: drop arithmetic expansion advice to use "$x"

The advice to use "$x" rather than "x" in arithmetric expansion was working around a dash bug fixed in 0.5.4. Even Debian oldstable has 0.5.7 these days.

that would be oldoldstable, oldstable is actually in 0.5.8 ;)

Quoted 2 lines

Carlo

#8 lore raw

Re: [PATCH] CodingGuidelines: drop arithmetic expansion advice to use "$x"

Quoted 9 lines · Carlo Marcelo Arenas Belón
Quoted 6 lines · Jeff King

-- >8 --
Subject: CodingGuidelines: drop arithmetic expansion advice to use "$x"

The advice to use "$x" rather than "x" in arithmetric expansion was working around a dash bug fixed in 0.5.4. Even Debian oldstable has 0.5.7 these days.

that would be oldoldstable, oldstable is actually in 0.5.8 ;)

Oh, you're right. I forgot they're doing an extra layer these days. It will officially become obsolete in less than 2 months. :)

-Peff

#9 lore raw

Re: [PATCH] CodingGuidelines: drop arithmetic expansion advice to use "$x"

Quoted 6 lines · Jeff King
Quoted 4 lines · Carlo Marcelo Arenas Belón
Quoted 3 lines · Jeff King

-- >8 --
Subject: CodingGuidelines: drop arithmetic expansion advice to use "$x"

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The advice to use "$x" rather than "x" in arithmetric expansion was working around a dash bug fixed in 0.5.4. Even Debian oldstable has 0.5.7 these days.

that would be oldoldstable, oldstable is actually in 0.5.8 ;)

Oh, you're right. I forgot they're doing an extra layer these days. It will officially become obsolete in less than 2 months. :)

Thanks, both. I'll just do s/0.5.7/0.5.8/ when I add Carlo's reviewed-by to queue the patch.

#10 lore raw

Re: [PATCH] CodingGuidelines: drop arithmetic expansion advice to use "$x"

In reply to Jeff King
Quoted 9 lines · Jeff King

On Mon, May 04, 2020 at 08:37:38AM -0700, Junio C Hamano wrote:

Subject: CodingGuidelines: drop arithmetic expansion advice to use "$x"

The advice to use "$x" rather than "x" in arithmetric expansion was working around a dash bug fixed in 0.5.4. Even Debian oldstable has 0.5.7 these days. And in the meantime, we've added almost two dozen instances of the "x" form which you can find with:

Quoted 7 lines
  git grep '$(([a-z]'

and nobody seems to have complained. Let's declare this workaround obsolete and simplify our style guide.

I see this patch hasn't been merged to pu yet.

Please have my name as (if it's not too much trouble for you):

Đoàn Trần Công Danh <[email protected]>

(I'm going to change my name in email setting)

Signature · 1 line
Danh
#11 lore raw

Re: [PATCH] CodingGuidelines: drop arithmetic expansion advice to use "$x"

In reply to Jeff King
Quoted 5 lines · Jeff King
Documentation/CodingGuidelines +0 -0
index 390ceece52..a89e8dcfbc 100644

Documentation/CodingGuidelines For shell scripts specifically (not exhaustive): · line 100

Quoted 10 lines
Documentation/CodingGuidelines +0 -4
@@ -95,10 +95,6 @@ For shell scripts specifically (not exhaustive):
 
  - We use Arithmetic Expansion $(( ... )).
 
- - Inside Arithmetic Expansion, spell shell variables with $ in front
-   of them, as some shells do not grok $((x)) while accepting $(($x))
-   just fine (e.g. dash older than 0.5.4).
-
  - We do not use Process Substitution <(list) or >(list).
 
  - Do not write control structures on a single line with semicolon.

A new entry in the "What's cooking" report has this:

* jk/arith-expansion-coding-guidelines (2020-05-04) 1 commit
- CodingGuidelines: drop arithmetic expansion advice to use "$x"

The coding guideline for shell scripts instructed to refer to a variable with dollar-sign inside airthmetic expansion to work around a bug in old versions of bash, which is a thing of the past. Now we are not forbidden from writing $((var+1)).

Writing the last sentence made me wonder if we should go one step further and actually encourage actively omitting the dollar-sign from variable reference instead.

#12 lore raw

Re: [PATCH] CodingGuidelines: drop arithmetic expansion advice to use "$x"

Quoted 9 lines · Junio C Hamano

A new entry in the "What's cooking" report has this:

* jk/arith-expansion-coding-guidelines (2020-05-04) 1 commit
- CodingGuidelines: drop arithmetic expansion advice to use "$x"

The coding guideline for shell scripts instructed to refer to a variable with dollar-sign inside airthmetic expansion to work around a bug in old versions of bash, which is a thing of the past. Now we are not forbidden from writing $((var+1)).

s/bash/dash/, I think

Quoted 3 lines

Writing the last sentence made me wonder if we should go one step further and actually encourage actively omitting the dollar-sign from variable reference instead.

I don't have a strong preference either way. I gave a few reasons to prefer the dollar-less version in:

https://lore.kernel.org/git/[email protected]/

but I couldn't find a case where the difference really matters in practice for otherwise-correct code. If we don't care much either way, I'd just as soon not have a rule. We have enough rules as it is, and I don't think either is obvious enough that somebody who is used to one style will get confused by the other.

-Peff

#13 lore raw

Re: [PATCH] CodingGuidelines: drop arithmetic expansion advice to use "$x"

Quoted 3 lines · Jeff King

I'd just as soon not have a rule. We have enough rules as it is, and I don't think either is obvious enough that somebody who is used to one style will get confused by the other.

OK. I very much welcome one fewer paragraphs in the file ;-)

#14 lore raw

Re: [PATCH 1/2] ref-filter: apply --ignore-case to all sorting keys

In reply to Jeff King
Quoted 5 lines · Jeff King

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

Quoted 8 lines

- 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).

A good design decision I would fully support.

t/t6300-for-each-ref.sh test_expect_success 'for-each-ref --ignore-case ignores case' ' · line 899

Quoted 2 lines
t/t6300-for-each-ref.sh +2 -0
+test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
+	# name refs numerically to avoid case-insensitive filesystem conflicts

Very considerate. If I were writing these nested loops, I am sure I would have used "tag-$email-$subject" to be cute.

Queued. Thanks.

Quoted 39 lines
t/t6300-for-each-ref.sh +38 -0
+	nr=0 &&
+	for email in a A b B
+	do
+		for subject in a A b B
+		do
+			GIT_COMMITTER_EMAIL="[email protected]" \
+			git tag -m "tag $subject" icase-$(printf %02d $nr) &&
+			nr=$((nr+1))||
+			return 1
+		done
+	done &&
+	git for-each-ref --ignore-case \
+		--format="%(taggeremail) %(subject) %(refname)" \
+		--sort=refname \
+		--sort=subject \
+		--sort=taggeremail \
+		refs/tags/icase-* >actual &&
+	cat >expect <<-\EOF &&
+	<[email protected]> tag a refs/tags/icase-00
+	<[email protected]> tag A refs/tags/icase-01
+	<[email protected]> tag a refs/tags/icase-04
+	<[email protected]> tag A refs/tags/icase-05
+	<[email protected]> tag b refs/tags/icase-02
+	<[email protected]> tag B refs/tags/icase-03
+	<[email protected]> tag b refs/tags/icase-06
+	<[email protected]> tag B refs/tags/icase-07
+	<[email protected]> tag a refs/tags/icase-08
+	<[email protected]> tag A refs/tags/icase-09
+	<[email protected]> tag a refs/tags/icase-12
+	<[email protected]> tag A refs/tags/icase-13
+	<[email protected]> tag b refs/tags/icase-10
+	<[email protected]> tag B refs/tags/icase-11
+	<[email protected]> tag b refs/tags/icase-14
+	<[email protected]> tag B refs/tags/icase-15
+	EOF
+	test_cmp expect actual
+'
+
 test_done
#15 lore raw

Re: [PATCH 1/2] ref-filter: apply --ignore-case to all sorting keys

Quoted 11 lines · Junio C Hamano
Quoted 9 lines · Jeff King

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

Quoted 6 lines
Quoted 4 lines

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).

A good design decision I would fully support.

I admit I had second thoughts when dealing with the "oops, we have to choose ignore_case from the first one" part of the second patch. But I think it works OK in practice, and I did like having a less invasive diff. :)

-Peff

Reviewed-by on v1 1/2
#16 lore raw

Re: [PATCH 1/2] ref-filter: apply --ignore-case to all sorting keys

In reply to Jeff King
Quoted 16 lines · Jeff King

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

Quoted 8 lines

- 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).

Makes sense, I think that this will provide us more flexibility in the future in case we want to have per-flag keys or some such.

Quoted 6 lines

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).

Thanks for adding a test.

Quoted 129 lines
---
 builtin/branch.c        |  2 +-
 builtin/for-each-ref.c  |  2 +-
 builtin/tag.c           |  2 +-
 ref-filter.c            |  6 ++++++
 ref-filter.h            |  2 ++
 t/t6300-for-each-ref.sh | 40 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 51 insertions(+), 3 deletions(-)
builtin/branch.c +1 -1
index d8297f80ff..86341cc835 100644
@@ -739,7 +739,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		 */
 		if (!sorting)
 			sorting = ref_default_sorting();
-		sorting->ignore_case = icase;
+		ref_sorting_icase_all(sorting, icase);
 		print_ref_list(&filter, sorting, &format);
 		print_columns(&output, colopts, NULL);
 		string_list_clear(&output, 0);
builtin/for-each-ref.c +1 -1
index 465153e853..57489e4eab 100644
@@ -70,7 +70,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 
 	if (!sorting)
 		sorting = ref_default_sorting();
-	sorting->ignore_case = icase;
+	ref_sorting_icase_all(sorting, icase);
 	filter.ignore_case = icase;
 
 	filter.name_patterns = argv;
builtin/tag.c +1 -1
index dd160b49c7..ff7610b5c8 100644
@@ -485,7 +485,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	}
 	if (!sorting)
 		sorting = ref_default_sorting();
-	sorting->ignore_case = icase;
+	ref_sorting_icase_all(sorting, icase);
 	filter.ignore_case = icase;
 	if (cmdmode == 'l') {
 		int ret;
ref-filter.c +6 -0
index 35776838f4..bdb3535ce5 100644
@@ -2317,6 +2317,12 @@ static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
 	return 0;
 }
 
+void ref_sorting_icase_all(struct ref_sorting *sorting, int flag)
+{
+	for (; sorting; sorting = sorting->next)
+		sorting->ignore_case = !!flag;
+}
+
 void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
 {
 	QSORT_S(array->items, array->nr, compare_refs, sorting);
ref-filter.h +2 -0
index 64330e9601..8ecc33cdfa 100644
@@ -114,6 +114,8 @@ void ref_array_clear(struct ref_array *array);
 int verify_ref_format(struct ref_format *format);
 /*  Sort the given ref_array as per the ref_sorting provided */
 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
+/*  Set the ignore_case flag for all elements of a sorting list */
+void ref_sorting_icase_all(struct ref_sorting *sorting, int flag);
 /*  Based on the given format and quote_style, fill the strbuf */
 int format_ref_array_item(struct ref_array_item *info,
 			  const struct ref_format *format,
t/t6300-for-each-ref.sh +40 -1
index b3c1092338..c9caf26327 100755
@@ -895,4 +895,44 @@ test_expect_success 'for-each-ref --ignore-case ignores case' '
 	test_cmp expect actual
 '
 
+test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
+	# name refs numerically to avoid case-insensitive filesystem conflicts
+	nr=0 &&
+	for email in a A b B
+	do
+		for subject in a A b B
+		do
+			GIT_COMMITTER_EMAIL="[email protected]" \
+			git tag -m "tag $subject" icase-$(printf %02d $nr) &&
+			nr=$((nr+1))||
+			return 1
+		done
+	done &&
+	git for-each-ref --ignore-case \
+		--format="%(taggeremail) %(subject) %(refname)" \
+		--sort=refname \
+		--sort=subject \
+		--sort=taggeremail \
+		refs/tags/icase-* >actual &&
+	cat >expect <<-\EOF &&
+	<[email protected]> tag a refs/tags/icase-00
+	<[email protected]> tag A refs/tags/icase-01
+	<[email protected]> tag a refs/tags/icase-04
+	<[email protected]> tag A refs/tags/icase-05
+	<[email protected]> tag b refs/tags/icase-02
+	<[email protected]> tag B refs/tags/icase-03
+	<[email protected]> tag b refs/tags/icase-06
+	<[email protected]> tag B refs/tags/icase-07
+	<[email protected]> tag a refs/tags/icase-08
+	<[email protected]> tag A refs/tags/icase-09
+	<[email protected]> tag a refs/tags/icase-12
+	<[email protected]> tag A refs/tags/icase-13
+	<[email protected]> tag b refs/tags/icase-10
+	<[email protected]> tag B refs/tags/icase-11
+	<[email protected]> tag b refs/tags/icase-14
+	<[email protected]> tag B refs/tags/icase-15
+	EOF
+	test_cmp expect actual
+'
+
 test_done
--
Quoted 1 line

2.26.2.957.g6dc93e954a

All looks very reasonable, so:

Thanks,
Taylor

#17 lore raw

PATCH 2/2 ref-filter: apply fallback refname sort only after all user sorts

In reply to Jeff King

Commit 9e468334b4 (ref-filter: fallback on alphabetical comparison, 2015-10-30) taught ref-filter's sort to fallback to comparing refnames. But it did it at the wrong level, overriding the comparison result for a single "--sort" key from the user, rather than after all sort keys have been exhausted.

This worked correctly for a single "--sort" option, but not for multiple ones. We'd break any ties in the first key with the refname and never evaluate the second key at all.

To make matters even more interesting, we only applied this fallback sometimes! For a field like "taggeremail" which requires a string comparison, we'd truly return the result of strcmp(), even if it was 0. But for numerical "value" fields like "taggerdate", we did apply the fallback. And that's why our multiple-sort test missed this: it uses taggeremail as the main comparison.

So let's start by adding a much more rigorous test. We'll have a set of commits expressing every combination of two tagger emails, dates, and refnames. Then we can confirm that our sort is applied with the correct precedence, and we'll be hitting both the string and value comparators.

That does show the bug, and the fix is simple: moving the fallback to the outer compare_refs() function, after all ref_sorting keys have been exhausted.

Note that in the outer function we don't have an "ignore_case" flag, as it's part of each individual ref_sorting element. It's debatable what such a fallback should do, since we didn't use the user's keys to match. But until now we have been trying to respect that flag, so the least-invasive thing is to try to continue to do so. Since all callers in the current code either set the flag for all keys or for none, we can just pull the flag from the first key. In a hypothetical world where the user really can flip the case-insensitivity of keys separately, we may want to extend the code to distinguish that case from a blanket "--ignore-case".

---
 ref-filter.c            |  7 ++++--
 t/t6300-for-each-ref.sh | 54 ++++++++++++++++++++++++++++++++++++-----
 2 files changed, 53 insertions(+), 8 deletions(-)
ref-filter.c +5 -2
index bdb3535ce5..bf7b70299b 100644
@@ -2295,7 +2295,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
 		if (va->value < vb->value)
 			cmp = -1;
 		else if (va->value == vb->value)
-			cmp = cmp_fn(a->refname, b->refname);
+			cmp = 0;
 		else
 			cmp = 1;
 	}
@@ -2314,7 +2314,10 @@ static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
 		if (cmp)
 			return cmp;
 	}
-	return 0;
+	s = ref_sorting;
+	return s && s->ignore_case ?
+		strcasecmp(a->refname, b->refname) :
+		strcmp(a->refname, b->refname);
 }
 
 void ref_sorting_icase_all(struct ref_sorting *sorting, int flag)
t/t6300-for-each-ref.sh +48 -6

65 lines not on this page · raw message

Signature · 1 line
2.26.2.957.g6dc93e954a
#18 lore raw

Re: [PATCH 2/2] ref-filter: apply fallback refname sort only after all user sorts

Quoted 5 lines · Jeff King

Commit 9e468334b4 (ref-filter: fallback on alphabetical comparison, 2015-10-30) taught ref-filter's sort to fallback to comparing refnames. But it did it at the wrong level, overriding the comparison result for a single "--sort" key from the user, rather than after all sort keys have been exhausted.

Good find. It does look like that the fallback was broken from the very first version when it was introduced, as we did have multiple keys support back in that version already.

The fix looks good to me.

Thanks.

Reviewed-by
#19 lore raw

Re: [PATCH 2/2] ref-filter: apply fallback refname sort only after all user sorts

Quoted 8 lines · Junio C Hamano
Quoted 5 lines · Jeff King

Commit 9e468334b4 (ref-filter: fallback on alphabetical comparison, 2015-10-30) taught ref-filter's sort to fallback to comparing refnames. But it did it at the wrong level, overriding the comparison result for a single "--sort" key from the user, rather than after all sort keys have been exhausted.

Quoted 5 lines

Good find. It does look like that the fallback was broken from the very first version when it was introduced, as we did have multiple keys support back in that version already.

The fix looks good to me.

Me too.

Quoted 1 line

Thanks.

Thanks,
Taylor

author
#20 lore raw
In reply to Jeff King

Awesome, thanks!

Quoted 36 lines · Jeff King
Quoted 6 lines · clime

I have the following command:

/usr/bin/git for-each-ref --merged="${GIT_HEAD-HEAD}" --sort='-taggerdate' --sort='-*committerdate' --format="%(*committerdate)|%(taggerdate)|%(tag)" refs/tags

Quoted 8 lines

I thought this will use: -*commiterdate as a primary key and -taggerdate as a secondary. According to man page for --sort: "You may use the --sort=<key> option multiple times, in which case the last key becomes the primary key."

But that doesn't seem to be the case. I created a repo with a single commit and created annotated tags on the commit in the following order:

It looks like this has been quite broken since 2015 and nobody noticed. :(

Thanks for your reproduction recipe; using times is a critical part of the bug. Here's a fix, plus a fix for a related bug I noticed while working on it (the second one fixes your bug).

[1/2]: ref-filter: apply --ignore-case to all sorting keys [2/2]: ref-filter: apply fallback refname sort only after all user sorts

 builtin/branch.c        |  2 +-
 builtin/for-each-ref.c  |  2 +-
 builtin/tag.c           |  2 +-
 ref-filter.c            | 13 +++++-
 ref-filter.h            |  2 +
 t/t6300-for-each-ref.sh | 94 ++++++++++++++++++++++++++++++++++++++---
 6 files changed, 104 insertions(+), 11 deletions(-)

-Peff