ref-filter: fallback on alphabetical comparison

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. 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. Helped-by: Junio C Hamano <gitster@pobox.com> Reported-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Karthik Nayak <Karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Oct 30, 2015 at 14:15 UTC 9e468334b41c1d1fc715de177ef1f61a36c1cf01
2 files changed +2 -2
ref-filter.c
+1 -1
@@ -1483,7 +1483,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
1483 if (va->ul < vb->ul)
1484 cmp = -1;
1485 else if (va->ul == vb->ul)
1486 - cmp = 0;
1486 + cmp = strcmp(a->refname, b->refname);
1487 else
1488 cmp = 1;
1489 }
t/t3203-branch-output.sh
+1 -1
@@ -145,8 +145,8 @@ EOF
145
146 test_expect_success 'git branch `--sort` option' '
147 cat >expect <<-\EOF &&
148 - branch-two
148 * (HEAD detached from fromtag)
149 + branch-two
150 branch-one
151 master
152 EOF