|
1 | 1 | #include "cache.h"
|
| 2 | +#include "string-list.h" |
2 | 3 |
|
3 | 4 | /*
|
4 | 5 | * versioncmp(): copied from string/strverscmp.c in glibc commit
|
|
20 | 21 | #define CMP 2
|
21 | 22 | #define LEN 3
|
22 | 23 |
|
| 24 | +static const struct string_list *prereleases; |
| 25 | +static int initialized; |
| 26 | + |
| 27 | +/* |
| 28 | + * p1 and p2 point to the first different character in two strings. If |
| 29 | + * either p1 or p2 starts with a prerelease suffix, it will be forced |
| 30 | + * to be on top. |
| 31 | + * |
| 32 | + * If both p1 and p2 start with (different) suffix, the order is |
| 33 | + * determined by config file. |
| 34 | + * |
| 35 | + * Note that we don't have to deal with the situation when both p1 and |
| 36 | + * p2 start with the same suffix because the common part is already |
| 37 | + * consumed by the caller. |
| 38 | + * |
| 39 | + * Return non-zero if *diff contains the return value for versioncmp() |
| 40 | + */ |
| 41 | +static int swap_prereleases(const void *p1_, |
| 42 | + const void *p2_, |
| 43 | + int *diff) |
| 44 | +{ |
| 45 | + const char *p1 = p1_; |
| 46 | + const char *p2 = p2_; |
| 47 | + int i, i1 = -1, i2 = -1; |
| 48 | + |
| 49 | + for (i = 0; i < prereleases->nr; i++) { |
| 50 | + const char *suffix = prereleases->items[i].string; |
| 51 | + if (i1 == -1 && starts_with(p1, suffix)) |
| 52 | + i1 = i; |
| 53 | + if (i2 == -1 && starts_with(p2, suffix)) |
| 54 | + i2 = i; |
| 55 | + } |
| 56 | + if (i1 == -1 && i2 == -1) |
| 57 | + return 0; |
| 58 | + if (i1 >= 0 && i2 >= 0) |
| 59 | + *diff = i1 - i2; |
| 60 | + else if (i1 >= 0) |
| 61 | + *diff = -1; |
| 62 | + else /* if (i2 >= 0) */ |
| 63 | + *diff = 1; |
| 64 | + return 1; |
| 65 | +} |
23 | 66 |
|
24 | 67 | /*
|
25 | 68 | * Compare S1 and S2 as strings holding indices/version numbers,
|
@@ -74,6 +117,13 @@ int versioncmp(const char *s1, const char *s2)
|
74 | 117 | state += (c1 == '0') + (isdigit (c1) != 0);
|
75 | 118 | }
|
76 | 119 |
|
| 120 | + if (!initialized) { |
| 121 | + initialized = 1; |
| 122 | + prereleases = git_config_get_value_multi("versionsort.prereleasesuffix"); |
| 123 | + } |
| 124 | + if (prereleases && swap_prereleases(p1 - 1, p2 - 1, &diff)) |
| 125 | + return diff; |
| 126 | + |
77 | 127 | state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];
|
78 | 128 |
|
79 | 129 | switch (state) {
|
|
0 commit comments