chore(yay): add local newer to AUR warnings (#2113)

This commit is contained in:
Jo 2023-04-11 23:15:47 +02:00 committed by GitHub
parent 161fede450
commit 83214fbc1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 103 additions and 59 deletions

View File

@ -8,15 +8,17 @@ import (
"github.com/Jguer/aur" "github.com/Jguer/aur"
"github.com/Jguer/go-alpm/v2" "github.com/Jguer/go-alpm/v2"
"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/stringset" "github.com/Jguer/yay/v12/pkg/stringset"
"github.com/Jguer/yay/v12/pkg/text" "github.com/Jguer/yay/v12/pkg/text"
) )
type AURWarnings struct { type AURWarnings struct {
Orphans []string Orphans []string
OutOfDate []string OutOfDate []string
Missing []string Missing []string
Ignore stringset.StringSet LocalNewer []string
Ignore stringset.StringSet
log *text.Logger log *text.Logger
} }
@ -42,6 +44,17 @@ func (warnings *AURWarnings) AddToWarnings(remote map[string]alpm.IPackage, aurP
if aurPkg.OutOfDate != 0 && !pkg.ShouldIgnore() { if aurPkg.OutOfDate != 0 && !pkg.ShouldIgnore() {
warnings.OutOfDate = append(warnings.OutOfDate, name) warnings.OutOfDate = append(warnings.OutOfDate, name)
} }
if !pkg.ShouldIgnore() && !isDevelPackage(pkg) && db.VerCmp(pkg.Version(), aurPkg.Version) > 0 {
left, right := GetVersionDiff(pkg.Version(), aurPkg.Version)
newerMsg := gotext.Get("%s: local (%s) is newer than AUR (%s)",
text.Cyan(name),
left, right,
)
warnings.LocalNewer = append(warnings.LocalNewer, newerMsg)
}
} }
func (warnings *AURWarnings) CalculateMissing(remoteNames []string, remote map[string]alpm.IPackage, aurData map[string]*aur.Pkg) { func (warnings *AURWarnings) CalculateMissing(remoteNames []string, remote map[string]alpm.IPackage, aurData map[string]*aur.Pkg) {
@ -70,6 +83,12 @@ func (warnings *AURWarnings) Print() {
if len(warnings.OutOfDate) > 0 { if len(warnings.OutOfDate) > 0 {
warnings.log.Warnln(gotext.Get("Flagged Out Of Date AUR Packages:"), formatNames(warnings.OutOfDate)) warnings.log.Warnln(gotext.Get("Flagged Out Of Date AUR Packages:"), formatNames(warnings.OutOfDate))
} }
if len(warnings.LocalNewer) > 0 {
for _, newer := range warnings.LocalNewer {
warnings.log.Warnln(newer)
}
}
} }
func filterDebugPkgs(names []string) (normal, debug []string) { func filterDebugPkgs(names []string) (normal, debug []string) {

73
pkg/query/version_diff.go Normal file
View File

@ -0,0 +1,73 @@
package query
import (
"strings"
"unicode"
"github.com/Jguer/yay/v12/pkg/text"
"github.com/Jguer/go-alpm/v2"
)
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 {
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.IPackage) bool {
return isDevelName(pkg.Name()) || isDevelName(pkg.Base())
}

View File

@ -54,7 +54,7 @@ func UpDevel(
} }
func printIgnoringPackage(log *text.Logger, pkg db.IPackage, newPkgVersion string) { func printIgnoringPackage(log *text.Logger, pkg db.IPackage, newPkgVersion string) {
left, right := GetVersionDiff(pkg.Version(), newPkgVersion) left, right := query.GetVersionDiff(pkg.Version(), newPkgVersion)
pkgName := pkg.Name() pkgName := pkg.Name()
log.Warnln(gotext.Get("%s: ignoring package upgrade (%s => %s)", log.Warnln(gotext.Get("%s: ignoring package upgrade (%s => %s)",

View File

@ -3,10 +3,10 @@ package upgrade
import ( import (
"fmt" "fmt"
"strings" "strings"
"unicode"
"github.com/Jguer/yay/v12/pkg/db" "github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/intrange" "github.com/Jguer/yay/v12/pkg/intrange"
"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/text" "github.com/Jguer/yay/v12/pkg/text"
) )
@ -51,55 +51,6 @@ func (u UpSlice) Less(i, j int) bool {
return text.LessRunes(iRunes, jRunes) return text.LessRunes(iRunes, jRunes)
} }
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 {
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
}
// Print prints the details of the packages to upgrade. // Print prints the details of the packages to upgrade.
func (u UpSlice) Print(logger *text.Logger) { func (u UpSlice) Print(logger *text.Logger) {
longestName, longestVersion := 0, 0 longestName, longestVersion := 0, 0
@ -107,7 +58,7 @@ func (u UpSlice) Print(logger *text.Logger) {
for k := range u.Up { for k := range u.Up {
upgrade := &u.Up[k] upgrade := &u.Up[k]
packNameLen := len(StylizedNameWithRepository(upgrade)) packNameLen := len(StylizedNameWithRepository(upgrade))
packVersion, _ := GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion) packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
packVersionLen := len(packVersion) packVersionLen := len(packVersion)
longestName = intrange.Max(packNameLen, longestName) longestName = intrange.Max(packNameLen, longestName)
longestVersion = intrange.Max(packVersionLen, longestVersion) longestVersion = intrange.Max(packVersionLen, longestVersion)
@ -121,7 +72,7 @@ func (u UpSlice) Print(logger *text.Logger) {
for k := range u.Up { for k := range u.Up {
upgrade := &u.Up[k] upgrade := &u.Up[k]
left, right := GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion) left, right := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
logger.Printf(text.Magenta(fmt.Sprintf(numberPadding, lenUp-k))) logger.Printf(text.Magenta(fmt.Sprintf(numberPadding, lenUp-k)))

View File

@ -3,6 +3,7 @@ package upgrade
import ( import (
"testing" "testing"
"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/text" "github.com/Jguer/yay/v12/pkg/text"
) )
@ -57,7 +58,7 @@ func TestGetVersionDiff(t *testing.T) {
} }
for i, pair := range in { for i, pair := range in {
o, n := GetVersionDiff(pair.Old, pair.New) o, n := query.GetVersionDiff(pair.Old, pair.New)
if o != out[i].Old || n != out[i].New { if o != out[i].Old || n != out[i].New {
t.Errorf("Test %-2d failed for update: expected (%s => %s) got (%s => %s) %d %d %d %d", t.Errorf("Test %-2d failed for update: expected (%s => %s) got (%s => %s) %d %d %d %d",

View File

@ -159,7 +159,7 @@ func printLocalNewerThanAUR(
continue continue
} }
left, right := upgrade.GetVersionDiff(pkg.Version(), aurPkg.Version) left, right := query.GetVersionDiff(pkg.Version(), aurPkg.Version)
if !isDevelPackage(pkg) && db.VerCmp(pkg.Version(), aurPkg.Version) > 0 { if !isDevelPackage(pkg) && db.VerCmp(pkg.Version(), aurPkg.Version) > 0 {
text.Warnln(gotext.Get("%s: local (%s) is newer than AUR (%s)", text.Warnln(gotext.Get("%s: local (%s) is newer than AUR (%s)",