Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only show the latest version in the Arch index #33262

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2624,6 +2624,9 @@ LEVEL = Info
;LIMIT_SIZE_VAGRANT = -1
;; Enable RPM re-signing by default. (It will overwrite the old signature ,using v4 format, not compatible with CentOS 6 or older)
;DEFAULT_RPM_SIGN_ENABLED = false
;; Keep only the latest version in Arch DB.
;METADATA_ARCH_LATEST_VERSION = false

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; default storage for attachments, lfs and avatars
Expand Down
3 changes: 3 additions & 0 deletions modules/setting/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var (
LimitSizeVagrant int64

DefaultRPMSignEnabled bool

DefaultMetaArchLatestVersion bool
}{
Enabled: true,
LimitTotalOwnerCount: -1,
Expand Down Expand Up @@ -102,6 +104,7 @@ func loadPackagesFrom(rootCfg ConfigProvider) (err error) {
Packages.LimitSizeSwift = mustBytes(sec, "LIMIT_SIZE_SWIFT")
Packages.LimitSizeVagrant = mustBytes(sec, "LIMIT_SIZE_VAGRANT")
Packages.DefaultRPMSignEnabled = sec.Key("DEFAULT_RPM_SIGN_ENABLED").MustBool(false)
Packages.DefaultMetaArchLatestVersion = sec.Key("METADATA_ARCH_LATEST_VERSION").MustBool(false)
return nil
}

Expand Down
41 changes: 29 additions & 12 deletions services/packages/arch/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"code.gitea.io/gitea/modules/json"
packages_module "code.gitea.io/gitea/modules/packages"
arch_module "code.gitea.io/gitea/modules/packages/arch"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
packages_service "code.gitea.io/gitea/services/packages"

Expand Down Expand Up @@ -235,6 +236,30 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package
return packages_service.DeletePackageFile(ctx, pf)
}

vpfs := make(map[string]*entryOptions)
for _, pf := range pfs {
current := &entryOptions{
File: pf,
}
current.Version, err = packages_model.GetVersionByID(ctx, pf.VersionID)
if err != nil {
return err
}

if setting.Packages.DefaultMetaArchLatestVersion {
old := vpfs[pf.Name]
if old != nil {
if compareVersions(old.Version.Version, current.Version.Version) == -1 {
vpfs[pf.Name] = current
}
} else {
vpfs[pf.Name] = current
}
} else {
vpfs[pf.Name] = current
}
}

indexContent, _ := packages_module.NewHashedBuffer()
defer indexContent.Close()

Expand All @@ -243,15 +268,7 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package

cache := make(map[int64]*packages_model.Package)

for _, pf := range pfs {
opts := &entryOptions{
File: pf,
}

opts.Version, err = packages_model.GetVersionByID(ctx, pf.VersionID)
if err != nil {
return err
}
for _, opts := range vpfs {
if err := json.Unmarshal([]byte(opts.Version.MetadataJSON), &opts.VersionMetadata); err != nil {
return err
}
Expand All @@ -263,12 +280,12 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package
}
cache[opts.Package.ID] = opts.Package
}
opts.Blob, err = packages_model.GetBlobByID(ctx, pf.BlobID)
opts.Blob, err = packages_model.GetBlobByID(ctx, opts.File.BlobID)
if err != nil {
return err
}

sig, err := packages_model.GetPropertiesByName(ctx, packages_model.PropertyTypeFile, pf.ID, arch_module.PropertySignature)
sig, err := packages_model.GetPropertiesByName(ctx, packages_model.PropertyTypeFile, opts.File.ID, arch_module.PropertySignature)
if err != nil {
return err
}
Expand All @@ -277,7 +294,7 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package
}
opts.Signature = sig[0].Value

meta, err := packages_model.GetPropertiesByName(ctx, packages_model.PropertyTypeFile, pf.ID, arch_module.PropertyMetadata)
meta, err := packages_model.GetPropertiesByName(ctx, packages_model.PropertyTypeFile, opts.File.ID, arch_module.PropertyMetadata)
if err != nil {
return err
}
Expand Down
111 changes: 111 additions & 0 deletions services/packages/arch/vercmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package arch

import (
"strings"
"unicode"
)

// https://gitlab.archlinux.org/pacman/pacman/-/blob/d55b47e5512808b67bc944feb20c2bcc6c1a4c45/lib/libalpm/version.c

import (
"strconv"
)

func parseEVR(evr string) (epoch, version, release string) {
if before, after, f := strings.Cut(evr, ":"); f {
epoch = before
evr = after
} else {
epoch = "0"
}

if before, after, f := strings.Cut(evr, "-"); f {
version = before
release = after
} else {
version = evr
release = "1"
}
return epoch, version, release
}

func compareSegments(a, b []string) int {
lenA, lenB := len(a), len(b)
var l int
if lenA > lenB {
l = lenB
} else {
l = lenA
}
for i := 0; i < l; i++ {
if r := compare(a[i], b[i]); r != 0 {
return r
}
}
if lenA == lenB {
return 0
} else if l == lenA {
return -1
} else {

Check failure on line 49 in services/packages/arch/vercmp.go

View workflow job for this annotation

GitHub Actions / lint-backend

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

Check failure on line 49 in services/packages/arch/vercmp.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

Check failure on line 49 in services/packages/arch/vercmp.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return 1
}
}

func compare(a, b string) int {
if a == b {
return 0
}

aNumeric := isNumeric(a)
bNumeric := isNumeric(b)

if aNumeric && bNumeric {
aInt, _ := strconv.Atoi(a)
bInt, _ := strconv.Atoi(b)
switch {
case aInt < bInt:
return -1
case aInt > bInt:
return 1
default:
return 0
}
}

if aNumeric {
return 1
}
if bNumeric {
return -1
}

return strings.Compare(a, b)
}

func isNumeric(s string) bool {
for _, c := range s {
if !unicode.IsDigit(c) {
return false
}
}
return true
}

func compareVersions(a, b string) int {
if a == b {
return 0
}

epochA, versionA, releaseA := parseEVR(a)
epochB, versionB, releaseB := parseEVR(b)

if res := compareSegments([]string{epochA}, []string{epochB}); res != 0 {
return res
}

if res := compareSegments(strings.Split(versionA, "."), strings.Split(versionB, ".")); res != 0 {
return res
}

return compareSegments([]string{releaseA}, []string{releaseB})
}
ExplodingDragon marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions services/packages/arch/vercmp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package arch

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestCompareVersions(t *testing.T) {
// https://man.archlinux.org/man/vercmp.8.en
checks := [][]string{
{"1.0a", "1.0b", "1.0beta", "1.0p", "1.0pre", "1.0rc", "1.0", "1.0.a", "1.0.1"},
{"1", "1.0", "1.1", "1.1.1", "1.2", "2.0", "3.0.0"},
}
for _, check := range checks {
for i := 0; i < len(check)-1; i++ {
require.Equal(t, -1, compareVersions(check[i], check[i+1]), fmt.Sprintf("%s > %s", check[i], check[i+1]))

Check failure on line 18 in services/packages/arch/vercmp_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

formatter: remove unnecessary fmt.Sprintf (testifylint)

Check failure on line 18 in services/packages/arch/vercmp_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

formatter: remove unnecessary fmt.Sprintf (testifylint)

Check failure on line 18 in services/packages/arch/vercmp_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

formatter: remove unnecessary fmt.Sprintf (testifylint)
require.Equal(t, 1, compareVersions(check[i+1], check[i]), fmt.Sprintf("%s > %s", check[i], check[i+1]))

Check failure on line 19 in services/packages/arch/vercmp_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

formatter: remove unnecessary fmt.Sprintf (testifylint)

Check failure on line 19 in services/packages/arch/vercmp_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

formatter: remove unnecessary fmt.Sprintf (testifylint)

Check failure on line 19 in services/packages/arch/vercmp_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

formatter: remove unnecessary fmt.Sprintf (testifylint)
}
}
require.Equal(t, 1, compareVersions("1.0-2", "1.0"))
require.Equal(t, 0, compareVersions("0:1.0-1", "1.0"))
require.Equal(t, 1, compareVersions("1:1.0-1", "2.0"))
}
Loading