Line | Count | Source (jump to first uncovered line) |
1 | | #define USE_THE_REPOSITORY_VARIABLE |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "config.h" |
5 | | #include "strbuf.h" |
6 | | #include "string-list.h" |
7 | | #include "versioncmp.h" |
8 | | |
9 | | /* |
10 | | * versioncmp(): copied from string/strverscmp.c in glibc commit |
11 | | * ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding |
12 | | * style. The implementation is under LGPL-2.1 and Git relicenses it |
13 | | * to GPLv2. |
14 | | */ |
15 | | |
16 | | /* |
17 | | * states: S_N: normal, S_I: comparing integral part, S_F: comparing |
18 | | * fractionnal parts, S_Z: idem but with leading Zeroes only |
19 | | */ |
20 | 0 | #define S_N 0x0 |
21 | 0 | #define S_I 0x3 |
22 | 0 | #define S_F 0x6 |
23 | 0 | #define S_Z 0x9 |
24 | | |
25 | | /* result_type: CMP: return diff; LEN: compare using len_diff/diff */ |
26 | 0 | #define CMP 2 |
27 | 0 | #define LEN 3 |
28 | | |
29 | | static const struct string_list *prereleases; |
30 | | static int initialized; |
31 | | |
32 | | struct suffix_match { |
33 | | int conf_pos; |
34 | | int start; |
35 | | int len; |
36 | | }; |
37 | | |
38 | | static void find_better_matching_suffix(const char *tagname, const char *suffix, |
39 | | int suffix_len, int start, int conf_pos, |
40 | | struct suffix_match *match) |
41 | 0 | { |
42 | | /* |
43 | | * A better match either starts earlier or starts at the same offset |
44 | | * but is longer. |
45 | | */ |
46 | 0 | int end = match->len < suffix_len ? match->start : match->start-1; |
47 | 0 | int i; |
48 | 0 | for (i = start; i <= end; i++) |
49 | 0 | if (starts_with(tagname + i, suffix)) { |
50 | 0 | match->conf_pos = conf_pos; |
51 | 0 | match->start = i; |
52 | 0 | match->len = suffix_len; |
53 | 0 | break; |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | | /* |
58 | | * off is the offset of the first different character in the two strings |
59 | | * s1 and s2. If either s1 or s2 contains a prerelease suffix containing |
60 | | * that offset or a suffix ends right before that offset, then that |
61 | | * string will be forced to be on top. |
62 | | * |
63 | | * If both s1 and s2 contain a (different) suffix around that position, |
64 | | * their order is determined by the order of those two suffixes in the |
65 | | * configuration. |
66 | | * If any of the strings contains more than one different suffixes around |
67 | | * that position, then that string is sorted according to the contained |
68 | | * suffix which starts at the earliest offset in that string. |
69 | | * If more than one different contained suffixes start at that earliest |
70 | | * offset, then that string is sorted according to the longest of those |
71 | | * suffixes. |
72 | | * |
73 | | * Return non-zero if *diff contains the return value for versioncmp() |
74 | | */ |
75 | | static int swap_prereleases(const char *s1, |
76 | | const char *s2, |
77 | | int off, |
78 | | int *diff) |
79 | 0 | { |
80 | 0 | int i; |
81 | 0 | struct suffix_match match1 = { -1, off, -1 }; |
82 | 0 | struct suffix_match match2 = { -1, off, -1 }; |
83 | |
|
84 | 0 | for (i = 0; i < prereleases->nr; i++) { |
85 | 0 | const char *suffix = prereleases->items[i].string; |
86 | 0 | int start, suffix_len = strlen(suffix); |
87 | 0 | if (suffix_len < off) |
88 | 0 | start = off - suffix_len; |
89 | 0 | else |
90 | 0 | start = 0; |
91 | 0 | find_better_matching_suffix(s1, suffix, suffix_len, start, |
92 | 0 | i, &match1); |
93 | 0 | find_better_matching_suffix(s2, suffix, suffix_len, start, |
94 | 0 | i, &match2); |
95 | 0 | } |
96 | 0 | if (match1.conf_pos == -1 && match2.conf_pos == -1) |
97 | 0 | return 0; |
98 | 0 | if (match1.conf_pos == match2.conf_pos) |
99 | | /* Found the same suffix in both, e.g. "-rc" in "v1.0-rcX" |
100 | | * and "v1.0-rcY": the caller should decide based on "X" |
101 | | * and "Y". */ |
102 | 0 | return 0; |
103 | | |
104 | 0 | if (match1.conf_pos >= 0 && match2.conf_pos >= 0) |
105 | 0 | *diff = match1.conf_pos - match2.conf_pos; |
106 | 0 | else if (match1.conf_pos >= 0) |
107 | 0 | *diff = -1; |
108 | 0 | else /* if (match2.conf_pos >= 0) */ |
109 | 0 | *diff = 1; |
110 | 0 | return 1; |
111 | 0 | } |
112 | | |
113 | | /* |
114 | | * Compare S1 and S2 as strings holding indices/version numbers, |
115 | | * returning less than, equal to or greater than zero if S1 is less |
116 | | * than, equal to or greater than S2 (for more info, see the texinfo |
117 | | * doc). |
118 | | */ |
119 | | |
120 | | int versioncmp(const char *s1, const char *s2) |
121 | 0 | { |
122 | 0 | const unsigned char *p1 = (const unsigned char *) s1; |
123 | 0 | const unsigned char *p2 = (const unsigned char *) s2; |
124 | 0 | unsigned char c1, c2; |
125 | 0 | int state, diff; |
126 | | |
127 | | /* |
128 | | * Symbol(s) 0 [1-9] others |
129 | | * Transition (10) 0 (01) d (00) x |
130 | | */ |
131 | 0 | static const uint8_t next_state[] = { |
132 | | /* state x d 0 */ |
133 | 0 | /* S_N */ S_N, S_I, S_Z, |
134 | | /* S_I */ S_N, S_I, S_I, |
135 | | /* S_F */ S_N, S_F, S_F, |
136 | | /* S_Z */ S_N, S_F, S_Z |
137 | 0 | }; |
138 | |
|
139 | 0 | static const int8_t result_type[] = { |
140 | | /* state x/x x/d x/0 d/x d/d d/0 0/x 0/d 0/0 */ |
141 | | |
142 | 0 | /* S_N */ CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP, |
143 | | /* S_I */ CMP, -1, -1, +1, LEN, LEN, +1, LEN, LEN, |
144 | | /* S_F */ CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, |
145 | | /* S_Z */ CMP, +1, +1, -1, CMP, CMP, -1, CMP, CMP |
146 | 0 | }; |
147 | |
|
148 | 0 | if (p1 == p2) |
149 | 0 | return 0; |
150 | | |
151 | 0 | c1 = *p1++; |
152 | 0 | c2 = *p2++; |
153 | | /* Hint: '0' is a digit too. */ |
154 | 0 | state = S_N + ((c1 == '0') + (isdigit (c1) != 0)); |
155 | |
|
156 | 0 | while ((diff = c1 - c2) == 0) { |
157 | 0 | if (c1 == '\0') |
158 | 0 | return diff; |
159 | | |
160 | 0 | state = next_state[state]; |
161 | 0 | c1 = *p1++; |
162 | 0 | c2 = *p2++; |
163 | 0 | state += (c1 == '0') + (isdigit (c1) != 0); |
164 | 0 | } |
165 | | |
166 | 0 | if (!initialized) { |
167 | 0 | const char *const newk = "versionsort.suffix"; |
168 | 0 | const char *const oldk = "versionsort.prereleasesuffix"; |
169 | 0 | const struct string_list *newl; |
170 | 0 | const struct string_list *oldl; |
171 | 0 | int new = git_config_get_string_multi(newk, &newl); |
172 | 0 | int old = git_config_get_string_multi(oldk, &oldl); |
173 | |
|
174 | 0 | if (!new && !old) |
175 | 0 | warning("ignoring %s because %s is set", oldk, newk); |
176 | 0 | if (!new) |
177 | 0 | prereleases = newl; |
178 | 0 | else if (!old) |
179 | 0 | prereleases = oldl; |
180 | |
|
181 | 0 | initialized = 1; |
182 | 0 | } |
183 | 0 | if (prereleases && swap_prereleases(s1, s2, (const char *) p1 - s1 - 1, |
184 | 0 | &diff)) |
185 | 0 | return diff; |
186 | | |
187 | 0 | state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))]; |
188 | |
|
189 | 0 | switch (state) { |
190 | 0 | case CMP: |
191 | 0 | return diff; |
192 | | |
193 | 0 | case LEN: |
194 | 0 | while (isdigit (*p1++)) |
195 | 0 | if (!isdigit (*p2++)) |
196 | 0 | return 1; |
197 | | |
198 | 0 | return isdigit (*p2) ? -1 : diff; |
199 | | |
200 | 0 | default: |
201 | 0 | return state; |
202 | 0 | } |
203 | 0 | } |