mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
Pure refactors with no behavior change, mostly reusing existing helpers and dropping duplicated or dead code. Net −222 lines. - Reuse existing helpers instead of inlined copies: `getIssueIndex`, `NewLabel`, `ToCommitUser`, `GetPackageFileDescriptors`, `util.SplitTrimSpace` - Merge copy-pasted blocks in review comment deletion, project deletion and the pre/post-receive hook templates - Drop dead code: an error `toStringKeys` never returned, impossible `bytes.Buffer` write errors, redundant nil-map branches, a `base64` wrapper - Use stdlib where it fits: `strings.Cut`, `bytes.IndexAny`, `cmp.Compare`, `min`/`max`, `slices.Concat`, map lookups instead of key scans
93 lines
1.6 KiB
Go
93 lines
1.6 KiB
Go
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package arch
|
|
|
|
import (
|
|
"cmp"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
// https://gitlab.archlinux.org/pacman/pacman/-/blob/d55b47e5512808b67bc944feb20c2bcc6c1a4c45/lib/libalpm/version.c
|
|
|
|
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 {
|
|
for i := range min(len(a), len(b)) {
|
|
if r := compare(a[i], b[i]); r != 0 {
|
|
return r
|
|
}
|
|
}
|
|
return cmp.Compare(len(a), len(b))
|
|
}
|
|
|
|
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)
|
|
return cmp.Compare(aInt, bInt)
|
|
}
|
|
|
|
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})
|
|
}
|