1
0
mirror of https://github.com/Jguer/yay.git synced 2026-02-24 01:01:18 +01:00
yay/pkg/query/version_diff.go
Jo 0a928ba9f4
feat(yay): use dyalpm for alpm usage (#2769)
* squash: dyalpm integration

* fix vercmp

* fix linting
2026-01-22 16:46:55 +01:00

80 lines
1.8 KiB
Go

package query
import (
"strings"
"unicode"
"github.com/Jguer/yay/v12/pkg/text"
alpm "github.com/Jguer/dyalpm"
)
func GetVersionDiff(oldVersion, newVersion string) (left, right string) {
if oldVersion == newVersion {
return oldVersion + text.Red(""), newVersion + text.Green("")
}
diffPosition := 0
checkWords := func(str string, index int, words ...string) bool {
// Make sure the word is not part of a longer word
ongoingWord := unicode.IsLetter(rune(str[index]))
if ongoingWord {
return false
}
for _, word := range words {
wordLength := len(word)
nextIndex := index + 1
if (index < len(str)-wordLength) &&
(str[nextIndex:(nextIndex+wordLength)] == word) {
return true
}
}
return false
}
for index, char := range oldVersion {
charIsSpecial := !unicode.IsLetter(char) && !unicode.IsNumber(char)
if (index >= len(newVersion)) || (char != rune(newVersion[index])) {
if charIsSpecial {
diffPosition = index
}
break
}
if charIsSpecial ||
(((index == len(oldVersion)-1) || (index == len(newVersion)-1)) &&
((len(oldVersion) != len(newVersion)) ||
(oldVersion[index] == newVersion[index]))) ||
checkWords(oldVersion, index, "rc", "pre", "alpha", "beta") {
diffPosition = index + 1
}
}
samePart := oldVersion[0:diffPosition]
left = samePart + text.Red(oldVersion[diffPosition:])
right = samePart + text.Green(newVersion[diffPosition:])
return left, right
}
func isDevelName(name string) bool {
for _, suffix := range []string{"git", "svn", "hg", "bzr", "nightly", "insiders-bin"} {
if strings.HasSuffix(name, "-"+suffix) {
return true
}
}
return strings.Contains(name, "-always-")
}
func isDevelPackage(pkg alpm.Package) bool {
return isDevelName(pkg.Name()) || isDevelName(pkg.Base())
}