Skip to content

Commit

Permalink
Add support for sorting caret ('^') higher than base version
Browse files Browse the repository at this point in the history
1.1^20160101 means 1.1 version (base) and patches which were applied at
that date on top of it.

* 1.1^201601 > 1.1
* 1.1^201601 < 1.1.1

Having symmetry is also good.

Signed-off-by: Igor Gnatenko <i.gnatenko.brain@gmail.com>
  • Loading branch information
ignatenkobrain authored and pmatilai committed Nov 26, 2018
1 parent e7fa1f1 commit c7e711b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions build/pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ static void finalizeDeps(Package pkg)
if (haveCharInDep(pkg, '~'))
(void) rpmlibNeedsFeature(pkg, "TildeInVersions", "4.10.0-1");

/* check if the package has a dependency with a '^' */
if (haveCharInDep(pkg, '^'))
(void) rpmlibNeedsFeature(pkg, "CaretInVersions", "4.15.0-1");

/* check if the package has a rich dependency */
if (haveRichDep(pkg))
(void) rpmlibNeedsFeature(pkg, "RichDependencies", "4.12.0-1");
Expand Down
3 changes: 3 additions & 0 deletions lib/rpmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,9 @@ static const struct rpmlibProvides_s rpmlibProvides[] = {
{ "rpmlib(TildeInVersions)", "4.10.0-1",
( RPMSENSE_EQUAL),
N_("dependency comparison supports versions with tilde.") },
{ "rpmlib(CaretInVersions)", "4.15.0-1",
( RPMSENSE_EQUAL),
N_("dependency comparison supports versions with caret.") },
{ "rpmlib(LargeFiles)", "4.12.0-1",
( RPMSENSE_EQUAL),
N_("support files larger than 4GB") },
Expand Down
19 changes: 17 additions & 2 deletions lib/rpmvercmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ int rpmvercmp(const char * a, const char * b)

/* loop through each version segment of str1 and str2 and compare them */
while (*one || *two) {
while (*one && !risalnum(*one) && *one != '~') one++;
while (*two && !risalnum(*two) && *two != '~') two++;
while (*one && !risalnum(*one) && *one != '~' && *one != '^') one++;
while (*two && !risalnum(*two) && *two != '~' && *two != '^') two++;

/* handle the tilde separator, it sorts before everything else */
if (*one == '~' || *two == '~') {
Expand All @@ -45,6 +45,21 @@ int rpmvercmp(const char * a, const char * b)
continue;
}

/*
* Handle caret separator. Concept is the same as tilde,
* except that if one of the strings ends (base version),
* the other is considered as higher version.
*/
if (*one == '^' || *two == '^') {
if (!*one) return -1;
if (!*two) return 1;
if (*one != '^') return 1;
if (*two != '^') return -1;
one++;
two++;
continue;
}

/* If we ran to the end of either, we are finished with the loop */
if (!(*one && *two)) break;

Expand Down
26 changes: 26 additions & 0 deletions tests/rpmvercmp.at
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ RPMVERCMP(1.0~rc1~git123, 1.0~rc1~git123, 0)
RPMVERCMP(1.0~rc1~git123, 1.0~rc1, -1)
RPMVERCMP(1.0~rc1, 1.0~rc1~git123, 1)

dnl Basic testcases for caret sorting
RPMVERCMP(1.0^, 1.0^, 0)
RPMVERCMP(1.0^, 1.0, 1)
RPMVERCMP(1.0, 1.0^, -1)
RPMVERCMP(1.0^git1, 1.0^git1, 0)
RPMVERCMP(1.0^git1, 1.0, 1)
RPMVERCMP(1.0, 1.0^git1, -1)
RPMVERCMP(1.0^git1, 1.0^git2, -1)
RPMVERCMP(1.0^git2, 1.0^git1, 1)
RPMVERCMP(1.0^git1, 1.01, -1)
RPMVERCMP(1.01, 1.0^git1, 1)
RPMVERCMP(1.0^20160101, 1.0^20160101, 0)
RPMVERCMP(1.0^20160101, 1.0.1, -1)
RPMVERCMP(1.0.1, 1.0^20160101, 1)
RPMVERCMP(1.0^20160101^git1, 1.0^20160101^git1, 0)
RPMVERCMP(1.0^20160102, 1.0^20160101^git1, 1)
RPMVERCMP(1.0^20160101^git1, 1.0^20160102, -1)

dnl Basic testcases for tilde and caret sorting
RPMVERCMP(1.0~rc1^git1, 1.0~rc1^git1, 0)
RPMVERCMP(1.0~rc1^git1, 1.0~rc1, 1)
RPMVERCMP(1.0~rc1, 1.0~rc1^git1, -1)
RPMVERCMP(1.0^git1~pre, 1.0^git1~pre, 0)
RPMVERCMP(1.0^git1, 1.0^git1~pre, 1)
RPMVERCMP(1.0^git1~pre, 1.0^git1, -1)

dnl These are included here to document current, arguably buggy behaviors
dnl for reference purposes and for easy checking against unintended
dnl behavior changes.
Expand Down

0 comments on commit c7e711b

Please sign in to comment.