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

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

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
chore(yay): add local newer to AUR warnings
  • Loading branch information
Jguer committed Apr 11, 2023
commit c4719a70003282912886623910810a48c2f16b02
27 changes: 23 additions & 4 deletions pkg/query/aur_warnings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (
"github.com/Jguer/aur"
"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/text"
)

type AURWarnings struct {
Orphans []string
OutOfDate []string
Missing []string
Ignore stringset.StringSet
Orphans []string
OutOfDate []string
Missing []string
LocalNewer []string
Ignore stringset.StringSet

log *text.Logger
}
Expand All @@ -42,6 +44,17 @@ func (warnings *AURWarnings) AddToWarnings(remote map[string]alpm.IPackage, aurP
if aurPkg.OutOfDate != 0 && !pkg.ShouldIgnore() {
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) {
Expand Down Expand Up @@ -70,6 +83,12 @@ func (warnings *AURWarnings) Print() {
if len(warnings.OutOfDate) > 0 {
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) {
Expand Down
73 changes: 73 additions & 0 deletions pkg/query/version_diff.go
Original file line number Diff line number Diff line change
@@ -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())
}
2 changes: 1 addition & 1 deletion pkg/upgrade/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func UpDevel(
}

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()
log.Warnln(gotext.Get("%s: ignoring package upgrade (%s => %s)",
Expand Down
55 changes: 3 additions & 52 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package upgrade
import (
"fmt"
"strings"
"unicode"

"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/intrange"
"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/text"
)

Expand Down Expand Up @@ -51,63 +51,14 @@ func (u UpSlice) Less(i, j int) bool {
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.
func (u UpSlice) Print(logger *text.Logger) {
longestName, longestVersion := 0, 0

for k := range u.Up {
upgrade := &u.Up[k]
packNameLen := len(StylizedNameWithRepository(upgrade))
packVersion, _ := GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
packVersionLen := len(packVersion)
longestName = intrange.Max(packNameLen, longestName)
longestVersion = intrange.Max(packVersionLen, longestVersion)
Expand All @@ -121,7 +72,7 @@ func (u UpSlice) Print(logger *text.Logger) {

for k := range u.Up {
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)))

Expand Down
3 changes: 2 additions & 1 deletion pkg/upgrade/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package upgrade
import (
"testing"

"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/text"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func TestGetVersionDiff(t *testing.T) {
}

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 {
t.Errorf("Test %-2d failed for update: expected (%s => %s) got (%s => %s) %d %d %d %d",
Expand Down
2 changes: 1 addition & 1 deletion upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func printLocalNewerThanAUR(
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 {
text.Warnln(gotext.Get("%s: local (%s) is newer than AUR (%s)",
Expand Down