Skip to content

Commit df534dc

Browse files
dschogitster
authored andcommitted
shortlog: use a stable sort
When sorting the output of `git shortlog` by count, a list of authors in alphabetical order is then sorted by contribution count. Obviously, the idea is to maintain the alphabetical order for items with identical contribution count. At the moment, this job is performed by `qsort()`. As that function is not guaranteed to implement a stable sort algorithm, this can lead to inconsistent and/or surprising behavior: items with identical contribution count could lose their alphabetical sub-order. The `qsort()` in MS Visual C's runtime does _not_ implement a stable sort algorithm, and under certain circumstances this even causes a test failure in t4201.21 "shortlog can match multiple groups", where two authors both are listed with 2 contributions, and are listed in inverse alphabetical order. Let's instead use the stable sort provided by `git_stable_qsort()` to avoid this inconsistency. This is a companion to 2049b8d (diffcore_rename(): use a stable sort, 2019-09-30). Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bbea4dc commit df534dc

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

builtin/shortlog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ void shortlog_output(struct shortlog *log)
443443
struct strbuf sb = STRBUF_INIT;
444444

445445
if (log->sort_by_number)
446-
QSORT(log->list.items, log->list.nr,
446+
STABLE_QSORT(log->list.items, log->list.nr,
447447
log->summary ? compare_by_counter : compare_by_list);
448448
for (i = 0; i < log->list.nr; i++) {
449449
const struct string_list_item *item = &log->list.items[i];

0 commit comments

Comments
 (0)