Skip to content

Commit 9e46833

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Reported-by: Johannes Sixt <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aa3bc55 commit 9e46833

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

ref-filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
14831483
if (va->ul < vb->ul)
14841484
cmp = -1;
14851485
else if (va->ul == vb->ul)
1486-
cmp = 0;
1486+
cmp = strcmp(a->refname, b->refname);
14871487
else
14881488
cmp = 1;
14891489
}

t/t3203-branch-output.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ EOF
145145

146146
test_expect_success 'git branch `--sort` option' '
147147
cat >expect <<-\EOF &&
148-
branch-two
149148
* (HEAD detached from fromtag)
149+
branch-two
150150
branch-one
151151
master
152152
EOF

0 commit comments

Comments
 (0)