Skip to content

Commit 8a99008

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, and rename it to `git_stable_qsort()`. 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 8a99008

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ LIB_OBJS += shallow.o
983983
LIB_OBJS += sideband.o
984984
LIB_OBJS += sigchain.o
985985
LIB_OBJS += split-index.o
986+
LIB_OBJS += stable-qsort.o
986987
LIB_OBJS += strbuf.o
987988
LIB_OBJS += streaming.o
988989
LIB_OBJS += string-list.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: 3 additions & 8 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.
@@ -1272,8 +1267,8 @@ static wchar_t *make_environment_block(char **deltaenv)
12721267

12731268
/*
12741269
* If there is a deltaenv, let's accumulate all keys into `array`,
1275-
* sort them using the stable git_qsort() and then copy, skipping
1276-
* duplicate keys
1270+
* sort them using the stable git_stable_qsort() and then copy,
1271+
* skipping duplicate keys
12771272
*/
12781273
for (p = wenv; p && *p; ) {
12791274
ALLOC_GROW(array, nr + 1, alloc);
@@ -1296,7 +1291,7 @@ static wchar_t *make_environment_block(char **deltaenv)
12961291
p += wlen + 1;
12971292
}
12981293

1299-
git_qsort(array, nr, sizeof(*array), wenvcmp);
1294+
git_stable_qsort(array, nr, sizeof(*array), wenvcmp);
13001295
ALLOC_ARRAY(result, size + delta_size);
13011296

13021297
for (p = result, i = 0; i < nr; i++) {

git-compat-util.h

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

1097+
void git_stable_qsort(void *base, size_t nmemb, size_t size,
1098+
int(*compar)(const void *, const void *));
10971099
#ifdef INTERNAL_QSORT
1098-
void git_qsort(void *base, size_t nmemb, size_t size,
1099-
int(*compar)(const void *, const void *));
1100-
#define qsort git_qsort
1100+
#define qsort git_stable_qsort
11011101
#endif
11021102

11031103
#define QSORT(base, n, compar) sane_qsort((base), (n), sizeof(*(base)), compar)
@@ -1108,6 +1108,9 @@ static inline void sane_qsort(void *base, size_t nmemb, size_t size,
11081108
qsort(base, nmemb, size, compar);
11091109
}
11101110

1111+
#define STABLE_QSORT(base, n, compar) \
1112+
git_stable_qsort((base), (n), sizeof(*(base)), compar)
1113+
11111114
#ifndef HAVE_ISO_QSORT_S
11121115
int git_qsort_s(void *base, size_t nmemb, size_t size,
11131116
int (*compar)(const void *, const void *, void *), void *ctx);

compat/qsort.c renamed to stable-qsort.c

Lines changed: 3 additions & 3 deletions
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
@@ -44,8 +44,8 @@ static void msort_with_tmp(void *b, size_t n, size_t s,
4444
memcpy(b, t, (n - n2) * s);
4545
}
4646

47-
void git_qsort(void *b, size_t n, size_t s,
48-
int (*cmp)(const void *, const void *))
47+
void git_stable_qsort(void *b, size_t n, size_t s,
48+
int (*cmp)(const void *, const void *))
4949
{
5050
const size_t size = st_mult(n, s);
5151
char buf[1024];

0 commit comments

Comments
 (0)