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

5 messages from 2015-10-27 to 2015-10-29. Participants: Karthik Nayak, Junio C Hamano.
Thread: https://gitlist.dev/t/40657

## Karthik Nayak, 2015-10-27 19:00

Subject: [PATCH v3] ref-filter: fallback on alphabetical comparison
Message-ID: <1445972456-5621-1-git-send-email-Karthik.188@gmail.com>
URL: https://gitlist.dev/e/1445972456-5621-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 listing 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

```

## Junio C Hamano, 2015-10-27 19:50

Subject: Re: [PATCH v3] ref-filter: fallback on alphabetical comparison
Message-ID: <xmqq7fm8ozu9.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq7fm8ozu9.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <1445972456-5621-1-git-send-email-Karthik.188@gmail.com>

```
Karthik Nayak <karthik.188@gmail.com> writes:

> 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 listing refs with equal values cannot be pre-determined) as
> pointed out by Johannes Sixt ($gmane/280117).

As we use qsort(), if two members compare as equal, their order in
the sorted array is undefined.  I think this is a good change.

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

It is unclear what "in this regard" is.  Do you mean this (I am not
suggesting you to spell these out in a very detailed way in the
final log message; I am deliberately being detailed here to help me
understand what you really mean)?

    A test in t3203 was expecting that branch-two sorts before HEAD,
    which happened to be how qsort(3) on Linux sorted the array, but
    (1) that outcome was not even guaranteed, and (2) once we start
    breaking ties with the refname, "HEAD" should sort before
    "branch-two" so the original expectation was inconsistent with
    the criterion we now use.

    Update it to match the new world order, which we can now depend
    on being stable.

I am not sure about "HEAD" and "branch-two" in the above (it may be
comparison between "HEAD" and "refs/heads/branch-two", for example).

> 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

```

## Karthik Nayak, 2015-10-28 18:55

Subject: Re: [PATCH v3] ref-filter: fallback on alphabetical comparison
Message-ID: <CAOLa=ZSF5bJocJkMG70x0kTaU139pj0-ONypC=V2Tdnu3yQtow@mail.gmail.com>
URL: https://gitlist.dev/e/CAOLa%3DZSF5bJocJkMG70x0kTaU139pj0-ONypC%3DV2Tdnu3yQtow%40mail.gmail.com
In-Reply-To: <xmqq7fm8ozu9.fsf@gitster.mtv.corp.google.com>

```
On Wed, Oct 28, 2015 at 1:20 AM, Junio C Hamano <gitster@pobox.com> wrote:
>> Hence, fallback to alphabetical comparison based on the refname
>> whenever the other criterion is equal. Fix the test in t3203 in this
>> regard.
>
> It is unclear what "in this regard" is.  Do you mean this (I am not
> suggesting you to spell these out in a very detailed way in the
> final log message; I am deliberately being detailed here to help me
> understand what you really mean)?
>
>     A test in t3203 was expecting that branch-two sorts before HEAD,
>     which happened to be how qsort(3) on Linux sorted the array, but
>     (1) that outcome was not even guaranteed, and (2) once we start
>     breaking ties with the refname, "HEAD" should sort before
>     "branch-two" so the original expectation was inconsistent with
>     the criterion we now use.
>

Exactly what you're saying, they happened to have the same objectsize.
Hence sorting them would put them together, but since we compare the
refname's the "HEAD" ref would come before "branch-two".

>     Update it to match the new world order, which we can now depend
>     on being stable.
>
> I am not sure about "HEAD" and "branch-two" in the above (it may be
> comparison between "HEAD" and "refs/heads/branch-two", for example).
>

It actually is, we consider "refs/heads/branch-two rather then the shortened
version of this. It makes sense to classify refs this way, even though this
was a side effect of this commit.

-- 
Regards,
Karthik Nayak

```

## Junio C Hamano, 2015-10-28 22:16

Subject: Re: [PATCH v3] ref-filter: fallback on alphabetical comparison
Message-ID: <xmqqy4emskod.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqy4emskod.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CAOLa=ZSF5bJocJkMG70x0kTaU139pj0-ONypC=V2Tdnu3yQtow@mail.gmail.com>

```
Karthik Nayak <karthik.188@gmail.com> writes:

> On Wed, Oct 28, 2015 at 1:20 AM, Junio C Hamano <gitster@pobox.com> wrote:
>>> Hence, fallback to alphabetical comparison based on the refname
>>> whenever the other criterion is equal. Fix the test in t3203 in this
>>> regard.
>>
>> It is unclear what "in this regard" is.  Do you mean this (I am not
>> suggesting you to spell these out in a very detailed way in the
>> final log message; I am deliberately being detailed here to help me
>> understand what you really mean)?
>>
>>     A test in t3203 was expecting that branch-two sorts before HEAD,
>>     which happened to be how qsort(3) on Linux sorted the array, but
>>     (1) that outcome was not even guaranteed, and (2) once we start
>>     breaking ties with the refname, "HEAD" should sort before
>>     "branch-two" so the original expectation was inconsistent with
>>     the criterion we now use.
>>
>
> Exactly what you're saying, they happened to have the same objectsize.
> Hence sorting them would put them together, but since we compare the
> refname's the "HEAD" ref would come before "branch-two".
>
>>     Update it to match the new world order, which we can now depend
>>     on being stable.
>>
>> I am not sure about "HEAD" and "branch-two" in the above (it may be
>> comparison between "HEAD" and "refs/heads/branch-two", for example).
>
> It actually is, we consider "refs/heads/branch-two rather then the shortened
> version of this. It makes sense to classify refs this way, even though this
> was a side effect of this commit.

Now these are enough bits of info, that can and needs to be
condenced into an updated log message to help future readers.

Thanks.

```

## Karthik Nayak, 2015-10-29 14:17

Subject: Re: [PATCH v3] ref-filter: fallback on alphabetical comparison
Message-ID: <CAOLa=ZTKQb2RyBKN8+xmqvOhDwbfRjDKpNthdLT-wgCtGcB8-g@mail.gmail.com>
URL: https://gitlist.dev/e/CAOLa%3DZTKQb2RyBKN8%2BxmqvOhDwbfRjDKpNthdLT-wgCtGcB8-g%40mail.gmail.com
In-Reply-To: <xmqqy4emskod.fsf@gitster.mtv.corp.google.com>

```
On Thu, Oct 29, 2015 at 3:46 AM, Junio C Hamano <gitster@pobox.com> wrote:
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> On Wed, Oct 28, 2015 at 1:20 AM, Junio C Hamano <gitster@pobox.com> wrote:
>>>> Hence, fallback to alphabetical comparison based on the refname
>>>> whenever the other criterion is equal. Fix the test in t3203 in this
>>>> regard.
>>>
>>> It is unclear what "in this regard" is.  Do you mean this (I am not
>>> suggesting you to spell these out in a very detailed way in the
>>> final log message; I am deliberately being detailed here to help me
>>> understand what you really mean)?
>>>
>>>     A test in t3203 was expecting that branch-two sorts before HEAD,
>>>     which happened to be how qsort(3) on Linux sorted the array, but
>>>     (1) that outcome was not even guaranteed, and (2) once we start
>>>     breaking ties with the refname, "HEAD" should sort before
>>>     "branch-two" so the original expectation was inconsistent with
>>>     the criterion we now use.
>>>
>>
>> Exactly what you're saying, they happened to have the same objectsize.
>> Hence sorting them would put them together, but since we compare the
>> refname's the "HEAD" ref would come before "branch-two".
>>
>>>     Update it to match the new world order, which we can now depend
>>>     on being stable.
>>>
>>> I am not sure about "HEAD" and "branch-two" in the above (it may be
>>> comparison between "HEAD" and "refs/heads/branch-two", for example).
>>
>> It actually is, we consider "refs/heads/branch-two rather then the shortened
>> version of this. It makes sense to classify refs this way, even though this
>> was a side effect of this commit.
>
> Now these are enough bits of info, that can and needs to be
> condenced into an updated log message to help future readers.
>
> Thanks.

Will update and send, thanks :)

-- 
Regards,
Karthik Nayak

```
