Skip to content

Commit 1202809

Browse files
committed
Move git_sort(), a stable sort, into into libgit.a
The `qsort()` function is not guaranteed to be stable, i.e. it does not promise to maintain the order of items it is told to consider equal. In contrast, the `git_sort()` function we carry in `compat/qsort.c` _is_ stable, by virtue of implementing a merge sort algorithm. In preparation for using a stable sort in Git's rename detection, move the stable sort into `libgit.a` so that it is compiled in unconditionally. Note: this also makes the hack obsolete that was introduced in fe21c6b (mingw: reencode environment variables on the fly (UTF-16 <-> UTF-8), 2018-10-30), where we included `compat/qsort.c` directly in `compat/mingw.c` to use the stable sort. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4615a8c commit 1202809

File tree

4 files changed

+5
-8
lines changed

4 files changed

+5
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ LIB_OBJS += prio-queue.o
950950
LIB_OBJS += progress.o
951951
LIB_OBJS += prompt.o
952952
LIB_OBJS += protocol.o
953+
LIB_OBJS += qsort.o
953954
LIB_OBJS += quote.o
954955
LIB_OBJS += range-diff.o
955956
LIB_OBJS += reachable.o
@@ -1714,7 +1715,6 @@ ifdef NO_GETPAGESIZE
17141715
endif
17151716
ifdef INTERNAL_QSORT
17161717
COMPAT_CFLAGS += -DINTERNAL_QSORT
1717-
COMPAT_OBJS += compat/qsort.o
17181718
endif
17191719
ifdef HAVE_ISO_QSORT_S
17201720
COMPAT_CFLAGS += -DHAVE_ISO_QSORT_S

compat/mingw.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,11 +1229,6 @@ static int wenvcmp(const void *a, const void *b)
12291229
return _wcsnicmp(p, q, p_len);
12301230
}
12311231

1232-
/* We need a stable sort to convert the environment between UTF-16 <-> UTF-8 */
1233-
#ifndef INTERNAL_QSORT
1234-
#include "qsort.c"
1235-
#endif
1236-
12371232
/*
12381233
* Build an environment block combining the inherited environment
12391234
* merged with the given list of settings.

git-compat-util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,9 +1094,9 @@ static inline int strtol_i(char const *s, int base, int *result)
10941094
return 0;
10951095
}
10961096

1097-
#ifdef INTERNAL_QSORT
10981097
void git_qsort(void *base, size_t nmemb, size_t size,
10991098
int(*compar)(const void *, const void *));
1099+
#ifdef INTERNAL_QSORT
11001100
#define qsort git_qsort
11011101
#endif
11021102

@@ -1108,6 +1108,8 @@ static inline void sane_qsort(void *base, size_t nmemb, size_t size,
11081108
qsort(base, nmemb, size, compar);
11091109
}
11101110

1111+
#define QSORT_STABLE(base, n, compar) git_qsort((base), (n), sizeof(*(base)), compar)
1112+
11111113
#ifndef HAVE_ISO_QSORT_S
11121114
int git_qsort_s(void *base, size_t nmemb, size_t size,
11131115
int (*compar)(const void *, const void *, void *), void *ctx);

compat/qsort.c renamed to qsort.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../git-compat-util.h"
1+
#include "git-compat-util.h"
22

33
/*
44
* A merge sort implementation, simplified from the qsort implementation

0 commit comments

Comments
 (0)