Skip to content

Commit

Permalink
ref-filter: fallback on alphabetical comparison
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Karthik Nayak authored and Junio C Hamano committed Oct 30, 2015
1 parent aa3bc55 commit 9e46833
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ref-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,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;
}
Expand Down
2 changes: 1 addition & 1 deletion t/t3203-branch-output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ EOF

test_expect_success 'git branch `--sort` option' '
cat >expect <<-\EOF &&
branch-two
* (HEAD detached from fromtag)
branch-two
branch-one
master
EOF
Expand Down

0 comments on commit 9e46833

Please sign in to comment.