Skip to content

Commit 109064a

Browse files
szedergitster
authored andcommitted
versioncmp: pass full tagnames to swap_prereleases()
The swap_prereleases() helper function is responsible for finding configured prerelease suffixes in a pair of tagnames to be compared, but this function currently gets to see only the parts of those two tagnames starting at the first different character. To fix some issues related to the common part of two tagnames overlapping with leading part of a prerelease suffix, this helper function must see both full tagnames. In preparation for the fix in the following patch, refactor swap_prereleases() and its caller to pass two full tagnames and an additional offset indicating the position of the first different character. While updating the comment describing that function, remove the sentence about not dealing with both tagnames having the same suffix. Currently it doesn't add much value (we know that there is a different character, so it's obvious that it can't possibly be the same suffix in both), and at the end of this patch series it won't even be true anymore. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 0c1b487 commit 109064a

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

versioncmp.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,28 @@ static const struct string_list *prereleases;
2525
static int initialized;
2626

2727
/*
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.
28+
* off is the offset of the first different character in the two strings
29+
* s1 and s2. If either s1 or s2 contains a prerelease suffix starting
30+
* at that offset, it will be forced to be on top.
3131
*
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.
32+
* If both s1 and s2 contain a (different) suffix at that position,
33+
* their order is determined by the order of those two suffixes in the
34+
* configuration.
3835
*
3936
* Return non-zero if *diff contains the return value for versioncmp()
4037
*/
41-
static int swap_prereleases(const void *p1_,
42-
const void *p2_,
38+
static int swap_prereleases(const char *s1,
39+
const char *s2,
40+
int off,
4341
int *diff)
4442
{
45-
const char *p1 = p1_;
46-
const char *p2 = p2_;
4743
int i, i1 = -1, i2 = -1;
4844

4945
for (i = 0; i < prereleases->nr; i++) {
5046
const char *suffix = prereleases->items[i].string;
51-
if (i1 == -1 && starts_with(p1, suffix))
47+
if (i1 == -1 && starts_with(s1 + off, suffix))
5248
i1 = i;
53-
if (i2 == -1 && starts_with(p2, suffix))
49+
if (i2 == -1 && starts_with(s2 + off, suffix))
5450
i2 = i;
5551
}
5652
if (i1 == -1 && i2 == -1)
@@ -121,7 +117,8 @@ int versioncmp(const char *s1, const char *s2)
121117
initialized = 1;
122118
prereleases = git_config_get_value_multi("versionsort.prereleasesuffix");
123119
}
124-
if (prereleases && swap_prereleases(p1 - 1, p2 - 1, &diff))
120+
if (prereleases && swap_prereleases(s1, s2, (const char *) p1 - s1 - 1,
121+
&diff))
125122
return diff;
126123

127124
state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];

0 commit comments

Comments
 (0)