git
/
versioncmp.c
203 строки · 5.3 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
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#define S_N 0x021#define S_I 0x322#define S_F 0x623#define S_Z 0x924
25/* result_type: CMP: return diff; LEN: compare using len_diff/diff */
26#define CMP 227#define LEN 328
29static const struct string_list *prereleases;30static int initialized;31
32struct suffix_match {33int conf_pos;34int start;35int len;36};37
38static void find_better_matching_suffix(const char *tagname, const char *suffix,39int suffix_len, int start, int conf_pos,40struct suffix_match *match)41{
42/*43* A better match either starts earlier or starts at the same offset
44* but is longer.
45*/
46int end = match->len < suffix_len ? match->start : match->start-1;47int i;48for (i = start; i <= end; i++)49if (starts_with(tagname + i, suffix)) {50match->conf_pos = conf_pos;51match->start = i;52match->len = suffix_len;53break;54}55}
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*/
75static int swap_prereleases(const char *s1,76const char *s2,77int off,78int *diff)79{
80int i;81struct suffix_match match1 = { -1, off, -1 };82struct suffix_match match2 = { -1, off, -1 };83
84for (i = 0; i < prereleases->nr; i++) {85const char *suffix = prereleases->items[i].string;86int start, suffix_len = strlen(suffix);87if (suffix_len < off)88start = off - suffix_len;89else90start = 0;91find_better_matching_suffix(s1, suffix, suffix_len, start,92i, &match1);93find_better_matching_suffix(s2, suffix, suffix_len, start,94i, &match2);95}96if (match1.conf_pos == -1 && match2.conf_pos == -1)97return 0;98if (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". */
102return 0;103
104if (match1.conf_pos >= 0 && match2.conf_pos >= 0)105*diff = match1.conf_pos - match2.conf_pos;106else if (match1.conf_pos >= 0)107*diff = -1;108else /* if (match2.conf_pos >= 0) */109*diff = 1;110return 1;111}
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
120int versioncmp(const char *s1, const char *s2)121{
122const unsigned char *p1 = (const unsigned char *) s1;123const unsigned char *p2 = (const unsigned char *) s2;124unsigned char c1, c2;125int state, diff;126
127/*128* Symbol(s) 0 [1-9] others
129* Transition (10) 0 (01) d (00) x
130*/
131static const uint8_t next_state[] = {132/* state x d 0 */133/* 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_Z137};138
139static 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/* 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, CMP146};147
148if (p1 == p2)149return 0;150
151c1 = *p1++;152c2 = *p2++;153/* Hint: '0' is a digit too. */154state = S_N + ((c1 == '0') + (isdigit (c1) != 0));155
156while ((diff = c1 - c2) == 0) {157if (c1 == '\0')158return diff;159
160state = next_state[state];161c1 = *p1++;162c2 = *p2++;163state += (c1 == '0') + (isdigit (c1) != 0);164}165
166if (!initialized) {167const char *const newk = "versionsort.suffix";168const char *const oldk = "versionsort.prereleasesuffix";169const struct string_list *newl;170const struct string_list *oldl;171int new = git_config_get_string_multi(newk, &newl);172int old = git_config_get_string_multi(oldk, &oldl);173
174if (!new && !old)175warning("ignoring %s because %s is set", oldk, newk);176if (!new)177prereleases = newl;178else if (!old)179prereleases = oldl;180
181initialized = 1;182}183if (prereleases && swap_prereleases(s1, s2, (const char *) p1 - s1 - 1,184&diff))185return diff;186
187state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];188
189switch (state) {190case CMP:191return diff;192
193case LEN:194while (isdigit (*p1++))195if (!isdigit (*p2++))196return 1;197
198return isdigit (*p2) ? -1 : diff;199
200default:201return state;202}203}
204