# [PATCH v2] ref-filter: fallback on alphabetical comparison

3 messages from 2015-10-25 to 2015-10-26. Participants: Karthik Nayak, Eric Sunshine.
Thread: https://gitlist.dev/t/40639

## Karthik Nayak, 2015-10-25 16:29

Subject: [PATCH v2] ref-filter: fallback on alphabetical comparison
Message-ID: <1445790540-22764-1-git-send-email-Karthik.188@gmail.com>
URL: https://gitlist.dev/e/1445790540-22764-1-git-send-email-Karthik.188%40gmail.com

```
In ref-filter.c the comparison of refs while sorting is handled by
cmp_ref_sorting() function. When sorting as per numerical values
(e.g. --sort=objectsize) there is no fallback comparison when both
refs hold the same value. This can cause unexpected results (i.e. the
order of listting refs with equal values cannot be pre-determined) as
pointed out by Johannes Sixt ($gmane/280117).

Hence, fallback to alphabetical comparison based on the refname
whenever the other criterion is equal. Fix the test in t3203 in this
regard.

Reported-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Karthik Nayak <Karthik.188@gmail.com>
---
 ref-filter.c             | 2 +-
 t/t3203-branch-output.sh | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/ref-filter.c b/ref-filter.c
index 046e73b..7b33cb8 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1698,7 +1698,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
 		if (va->ul < vb->ul)
 			cmp = -1;
 		else if (va->ul == vb->ul)
-			cmp = 0;
+			cmp = strcmp(a->refname, b->refname);
 		else
 			cmp = 1;
 	}
diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
index f77971c..9f2d482 100755
--- a/t/t3203-branch-output.sh
+++ b/t/t3203-branch-output.sh
@@ -158,8 +158,8 @@ EOF
 
 test_expect_success 'git branch `--sort` option' '
 	cat >expect <<-\EOF &&
-	  branch-two
 	* (HEAD detached from fromtag)
+	  branch-two
 	  branch-one
 	  master
 	EOF
-- 
2.6.2

```

## Eric Sunshine, 2015-10-26 01:13

Subject: Re: [PATCH v2] ref-filter: fallback on alphabetical comparison
Message-ID: <CAPig+cRRB5MNxuBc-5rM_8cJh69PjH5-OSmkRNw_6OFQdQOoTw@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcRRB5MNxuBc-5rM_8cJh69PjH5-OSmkRNw_6OFQdQOoTw%40mail.gmail.com
In-Reply-To: <1445790540-22764-1-git-send-email-Karthik.188@gmail.com>

```
On Sun, Oct 25, 2015 at 12:29 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
> In ref-filter.c the comparison of refs while sorting is handled by
> cmp_ref_sorting() function. When sorting as per numerical values
> (e.g. --sort=objectsize) there is no fallback comparison when both
> refs hold the same value. This can cause unexpected results (i.e. the
> order of listting refs with equal values cannot be pre-determined) as

s/listting/listing/

> pointed out by Johannes Sixt ($gmane/280117).
>
> Hence, fallback to alphabetical comparison based on the refname
> whenever the other criterion is equal. Fix the test in t3203 in this
> regard.
>
> Reported-by: Johannes Sixt <j6t@kdbg.org>
> Signed-off-by: Karthik Nayak <Karthik.188@gmail.com>
> ---
>  ref-filter.c             | 2 +-
>  t/t3203-branch-output.sh | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/ref-filter.c b/ref-filter.c
> index 046e73b..7b33cb8 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -1698,7 +1698,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
>                 if (va->ul < vb->ul)
>                         cmp = -1;
>                 else if (va->ul == vb->ul)
> -                       cmp = 0;
> +                       cmp = strcmp(a->refname, b->refname);
>                 else
>                         cmp = 1;
>         }
> diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
> index f77971c..9f2d482 100755
> --- a/t/t3203-branch-output.sh
> +++ b/t/t3203-branch-output.sh
> @@ -158,8 +158,8 @@ EOF
>
>  test_expect_success 'git branch `--sort` option' '
>         cat >expect <<-\EOF &&
> -         branch-two
>         * (HEAD detached from fromtag)
> +         branch-two
>           branch-one
>           master
>         EOF
> --
> 2.6.2

```

## Karthik Nayak, 2015-10-26 17:13

Subject: Re: [PATCH v2] ref-filter: fallback on alphabetical comparison
Message-ID: <CAOLa=ZQGwrQrie+Bp0B61LdG37-UeEdyHqvQEtq26nX9VjYu7Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAOLa%3DZQGwrQrie%2BBp0B61LdG37-UeEdyHqvQEtq26nX9VjYu7Q%40mail.gmail.com
In-Reply-To: <CAPig+cRRB5MNxuBc-5rM_8cJh69PjH5-OSmkRNw_6OFQdQOoTw@mail.gmail.com>

```
On Mon, Oct 26, 2015 at 6:43 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, Oct 25, 2015 at 12:29 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> In ref-filter.c the comparison of refs while sorting is handled by
>> cmp_ref_sorting() function. When sorting as per numerical values
>> (e.g. --sort=objectsize) there is no fallback comparison when both
>> refs hold the same value. This can cause unexpected results (i.e. the
>> order of listting refs with equal values cannot be pre-determined) as
>
> s/listting/listing/

Thanks for pointing out. Should I re-roll?

-- 
Regards,
Karthik Nayak

```
