blob: 9f81dc1062bc1f7f243afa8c0404ff5100fc0ef8 [file] [log] [blame]
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +07001#include "cache.h"
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +07002#include "string-list.h"
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +07003
4/*
5 * versioncmp(): copied from string/strverscmp.c in glibc commit
6 * ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding
7 * style. The implementation is under LGPL-2.1 and Git relicenses it
8 * to GPLv2.
9 */
10
11/*
12 * states: S_N: normal, S_I: comparing integral part, S_F: comparing
13 * fractionnal parts, S_Z: idem but with leading Zeroes only
14 */
15#define S_N 0x0
16#define S_I 0x3
17#define S_F 0x6
18#define S_Z 0x9
19
20/* result_type: CMP: return diff; LEN: compare using len_diff/diff */
21#define CMP 2
22#define LEN 3
23
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +070024static const struct string_list *prereleases;
25static int initialized;
26
SZEDER Gáborb1784642016-12-08 15:48:11 +010027struct suffix_match {
28 int conf_pos;
29 int start;
30 int len;
31};
32
33static void find_better_matching_suffix(const char *tagname, const char *suffix,
34 int suffix_len, int start, int conf_pos,
35 struct suffix_match *match)
36{
37 /*
38 * A better match either starts earlier or starts at the same offset
39 * but is longer.
40 */
41 int end = match->len < suffix_len ? match->start : match->start-1;
42 int i;
43 for (i = start; i <= end; i++)
44 if (starts_with(tagname + i, suffix)) {
45 match->conf_pos = conf_pos;
46 match->start = i;
47 match->len = suffix_len;
48 break;
49 }
50}
51
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +070052/*
SZEDER Gábor109064a2016-12-08 15:23:58 +010053 * off is the offset of the first different character in the two strings
SZEDER Gáborb8231662016-12-08 15:23:59 +010054 * s1 and s2. If either s1 or s2 contains a prerelease suffix containing
SZEDER Gábor51acfa92016-12-08 15:24:00 +010055 * that offset or a suffix ends right before that offset, then that
56 * string will be forced to be on top.
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +070057 *
SZEDER Gáborb8231662016-12-08 15:23:59 +010058 * If both s1 and s2 contain a (different) suffix around that position,
SZEDER Gábor109064a2016-12-08 15:23:58 +010059 * their order is determined by the order of those two suffixes in the
60 * configuration.
SZEDER Gáborb8231662016-12-08 15:23:59 +010061 * If any of the strings contains more than one different suffixes around
62 * that position, then that string is sorted according to the contained
SZEDER Gábor51acfa92016-12-08 15:24:00 +010063 * suffix which starts at the earliest offset in that string.
64 * If more than one different contained suffixes start at that earliest
65 * offset, then that string is sorted according to the longest of those
66 * suffixes.
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +070067 *
68 * Return non-zero if *diff contains the return value for versioncmp()
69 */
SZEDER Gábor109064a2016-12-08 15:23:58 +010070static int swap_prereleases(const char *s1,
71 const char *s2,
72 int off,
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +070073 int *diff)
74{
SZEDER Gáborb1784642016-12-08 15:48:11 +010075 int i;
76 struct suffix_match match1 = { -1, off, -1 };
77 struct suffix_match match2 = { -1, off, -1 };
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +070078
79 for (i = 0; i < prereleases->nr; i++) {
80 const char *suffix = prereleases->items[i].string;
SZEDER Gáborb1784642016-12-08 15:48:11 +010081 int start, suffix_len = strlen(suffix);
SZEDER Gáborb8231662016-12-08 15:23:59 +010082 if (suffix_len < off)
SZEDER Gábor51acfa92016-12-08 15:24:00 +010083 start = off - suffix_len;
SZEDER Gáborb8231662016-12-08 15:23:59 +010084 else
85 start = 0;
SZEDER Gáborb1784642016-12-08 15:48:11 +010086 find_better_matching_suffix(s1, suffix, suffix_len, start,
87 i, &match1);
88 find_better_matching_suffix(s2, suffix, suffix_len, start,
89 i, &match2);
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +070090 }
SZEDER Gáborb1784642016-12-08 15:48:11 +010091 if (match1.conf_pos == -1 && match2.conf_pos == -1)
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +070092 return 0;
SZEDER Gáborb1784642016-12-08 15:48:11 +010093 if (match1.conf_pos == match2.conf_pos)
SZEDER Gábor51acfa92016-12-08 15:24:00 +010094 /* Found the same suffix in both, e.g. "-rc" in "v1.0-rcX"
95 * and "v1.0-rcY": the caller should decide based on "X"
96 * and "Y". */
97 return 0;
98
SZEDER Gáborb1784642016-12-08 15:48:11 +010099 if (match1.conf_pos >= 0 && match2.conf_pos >= 0)
100 *diff = match1.conf_pos - match2.conf_pos;
101 else if (match1.conf_pos >= 0)
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +0700102 *diff = -1;
SZEDER Gáborb1784642016-12-08 15:48:11 +0100103 else /* if (match2.conf_pos >= 0) */
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +0700104 *diff = 1;
105 return 1;
106}
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700107
108/*
109 * Compare S1 and S2 as strings holding indices/version numbers,
110 * returning less than, equal to or greater than zero if S1 is less
111 * than, equal to or greater than S2 (for more info, see the texinfo
112 * doc).
113 */
114
115int versioncmp(const char *s1, const char *s2)
116{
117 const unsigned char *p1 = (const unsigned char *) s1;
118 const unsigned char *p2 = (const unsigned char *) s2;
119 unsigned char c1, c2;
120 int state, diff;
121
122 /*
123 * Symbol(s) 0 [1-9] others
124 * Transition (10) 0 (01) d (00) x
125 */
126 static const uint8_t next_state[] = {
127 /* state x d 0 */
128 /* S_N */ S_N, S_I, S_Z,
129 /* S_I */ S_N, S_I, S_I,
130 /* S_F */ S_N, S_F, S_F,
131 /* S_Z */ S_N, S_F, S_Z
132 };
133
134 static const int8_t result_type[] = {
135 /* state x/x x/d x/0 d/x d/d d/0 0/x 0/d 0/0 */
136
137 /* S_N */ CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP,
138 /* S_I */ CMP, -1, -1, +1, LEN, LEN, +1, LEN, LEN,
139 /* S_F */ CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
140 /* S_Z */ CMP, +1, +1, -1, CMP, CMP, -1, CMP, CMP
141 };
142
143 if (p1 == p2)
144 return 0;
145
146 c1 = *p1++;
147 c2 = *p2++;
148 /* Hint: '0' is a digit too. */
149 state = S_N + ((c1 == '0') + (isdigit (c1) != 0));
150
151 while ((diff = c1 - c2) == 0) {
152 if (c1 == '\0')
153 return diff;
154
155 state = next_state[state];
156 c1 = *p1++;
157 c2 = *p2++;
158 state += (c1 == '0') + (isdigit (c1) != 0);
159 }
160
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +0700161 if (!initialized) {
SZEDER Gáborc0265572016-12-08 15:24:01 +0100162 const struct string_list *deprecated_prereleases;
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +0700163 initialized = 1;
SZEDER Gáborc0265572016-12-08 15:24:01 +0100164 prereleases = git_config_get_value_multi("versionsort.suffix");
165 deprecated_prereleases = git_config_get_value_multi("versionsort.prereleasesuffix");
166 if (prereleases) {
167 if (deprecated_prereleases)
168 warning("ignoring versionsort.prereleasesuffix because versionsort.suffix is set");
169 } else
170 prereleases = deprecated_prereleases;
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +0700171 }
SZEDER Gábor109064a2016-12-08 15:23:58 +0100172 if (prereleases && swap_prereleases(s1, s2, (const char *) p1 - s1 - 1,
173 &diff))
Nguyễn Thái Ngọc Duyd811c8e2015-02-26 17:44:01 +0700174 return diff;
175
Nguyễn Thái Ngọc Duy9ef176b2014-02-27 19:56:52 +0700176 state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];
177
178 switch (state) {
179 case CMP:
180 return diff;
181
182 case LEN:
183 while (isdigit (*p1++))
184 if (!isdigit (*p2++))
185 return 1;
186
187 return isdigit (*p2) ? -1 : diff;
188
189 default:
190 return state;
191 }
192}