Skip to content

Git's rename detection requires a stable sort #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ LIB_OBJS += shallow.o
LIB_OBJS += sideband.o
LIB_OBJS += sigchain.o
LIB_OBJS += split-index.o
LIB_OBJS += stable-qsort.o
LIB_OBJS += strbuf.o
LIB_OBJS += streaming.o
LIB_OBJS += string-list.o
Expand Down Expand Up @@ -1714,7 +1715,6 @@ ifdef NO_GETPAGESIZE
endif
ifdef INTERNAL_QSORT
COMPAT_CFLAGS += -DINTERNAL_QSORT
COMPAT_OBJS += compat/qsort.o
endif
ifdef HAVE_ISO_QSORT_S
COMPAT_CFLAGS += -DHAVE_ISO_QSORT_S
Expand Down
11 changes: 3 additions & 8 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1229,11 +1229,6 @@ static int wenvcmp(const void *a, const void *b)
return _wcsnicmp(p, q, p_len);
}

/* We need a stable sort to convert the environment between UTF-16 <-> UTF-8 */
#ifndef INTERNAL_QSORT
#include "qsort.c"
#endif

/*
* Build an environment block combining the inherited environment
* merged with the given list of settings.
Expand Down Expand Up @@ -1272,8 +1267,8 @@ static wchar_t *make_environment_block(char **deltaenv)

/*
* If there is a deltaenv, let's accumulate all keys into `array`,
* sort them using the stable git_qsort() and then copy, skipping
* duplicate keys
* sort them using the stable git_stable_qsort() and then copy,
* skipping duplicate keys
*/
for (p = wenv; p && *p; ) {
ALLOC_GROW(array, nr + 1, alloc);
Expand All @@ -1296,7 +1291,7 @@ static wchar_t *make_environment_block(char **deltaenv)
p += wlen + 1;
}

git_qsort(array, nr, sizeof(*array), wenvcmp);
git_stable_qsort(array, nr, sizeof(*array), wenvcmp);
ALLOC_ARRAY(result, size + delta_size);

for (p = result, i = 0; i < nr; i++) {
Expand Down
2 changes: 1 addition & 1 deletion diffcore-rename.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ void diffcore_rename(struct diff_options *options)
stop_progress(&progress);

/* cost matrix sorted by most to least similar pair */
QSORT(mx, dst_cnt * NUM_CANDIDATE_PER_DST, score_compare);
STABLE_QSORT(mx, dst_cnt * NUM_CANDIDATE_PER_DST, score_compare);

rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
if (detect_rename == DIFF_DETECT_COPY)
Expand Down
9 changes: 6 additions & 3 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1094,10 +1094,10 @@ static inline int strtol_i(char const *s, int base, int *result)
return 0;
}

void git_stable_qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
#ifdef INTERNAL_QSORT
void git_qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
#define qsort git_qsort
#define qsort git_stable_qsort
#endif

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

#define STABLE_QSORT(base, n, compar) \
git_stable_qsort((base), (n), sizeof(*(base)), compar)

#ifndef HAVE_ISO_QSORT_S
int git_qsort_s(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *, void *), void *ctx);
Expand Down
6 changes: 3 additions & 3 deletions compat/qsort.c → stable-qsort.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../git-compat-util.h"
#include "git-compat-util.h"

/*
* A merge sort implementation, simplified from the qsort implementation
Expand Down Expand Up @@ -44,8 +44,8 @@ static void msort_with_tmp(void *b, size_t n, size_t s,
memcpy(b, t, (n - n2) * s);
}

void git_qsort(void *b, size_t n, size_t s,
int (*cmp)(const void *, const void *))
void git_stable_qsort(void *b, size_t n, size_t s,
int (*cmp)(const void *, const void *))
{
const size_t size = st_mult(n, s);
char buf[1024];
Expand Down