fix(packages): list highest CRAN version in PACKAGES index (#39793)

Fixes https://github.com/go-gitea/gitea/issues/35728

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Mitrahsoftandwxiaoguang authored and GitHub committed 2026-10-17 06:48:48 +00:00
1 parent 42b1f95261
commit efb0b34395
6 files changed
+74 -41

No files matched your search

+7
View File
@@ -7,12 +7,14 @@ import (
"archive/tar"
"archive/zip"
"bufio"
"cmp"
"compress/gzip"
"io"
"path"
"regexp"
"strings"
"gitea.dev/modules/base"
"gitea.dev/modules/packages"
"gitea.dev/modules/util"
)
@@ -234,6 +236,11 @@ func setField(p *Package, data string) error {
return nil
}
// CompareVersions returns -1, 0 or 1, R treats "." and "-" as equivalent separators
func CompareVersions(a, b string) int {
return cmp.Compare(base.NaturalSortCompare(strings.ReplaceAll(a, "-", "."), strings.ReplaceAll(b, "-", ".")), 0)
}
func splitAndTrim(s string) []string {
items := strings.Split(s, ", ")
for i := range items {
+17
View File
@@ -159,3 +159,20 @@ func TestParseDescription(t *testing.T) {
assert.True(t, p.Metadata.NeedsCompilation)
})
}
func TestCompareVersions(t *testing.T) {
cases := []struct {
a, b string
expected int
}{
{"1.0.3", "1.0.2", 1},
{"0.9.10", "0.9.9", 1},
{"2.5.2", "3.0.2", -1},
{"1.0-1", "1.0.0", 1},
{"1.0-2", "1.0.2", 0},
{"1.0", "1.0.0", -1},
}
for _, c := range cases {
assert.Equal(t, c.expected, CompareVersions(c.a, c.b), "%s vs %s", c.a, c.b)
}
}