fix(upgrade): export upgrades

This commit is contained in:
jguer 2020-10-01 14:06:21 +02:00
parent e1b632cf3d
commit 678d10e04e
No known key found for this signature in database
GPG key ID: 6D6CC9BEA8556B35
6 changed files with 128 additions and 120 deletions

108
pkg/upgrade/sources.go Normal file
View file

@ -0,0 +1,108 @@
package upgrade
import (
"sync"
alpm "github.com/Jguer/go-alpm/v2"
"github.com/leonelquinteros/gotext"
rpc "github.com/mikkeloscar/aur"
"github.com/Jguer/yay/v10/pkg/text"
"github.com/Jguer/yay/v10/pkg/vcs"
)
func UpDevel(
remote []alpm.IPackage,
aurdata map[string]*rpc.Pkg,
localCache *vcs.InfoStore) UpSlice {
toUpdate := make([]alpm.IPackage, 0, len(aurdata))
toRemove := make([]string, 0)
var mux1, mux2 sync.Mutex
var wg sync.WaitGroup
checkUpdate := func(pkgName string, e vcs.OriginInfoByURL) {
defer wg.Done()
if localCache.NeedsUpdate(e) {
if _, ok := aurdata[pkgName]; ok {
for _, pkg := range remote {
if pkg.Name() == pkgName {
mux1.Lock()
toUpdate = append(toUpdate, pkg)
mux1.Unlock()
return
}
}
}
mux2.Lock()
toRemove = append(toRemove, pkgName)
mux2.Unlock()
}
}
for pkgName, e := range localCache.OriginsByPackage {
wg.Add(1)
go checkUpdate(pkgName, e)
}
wg.Wait()
toUpgrade := make(UpSlice, 0, len(toUpdate))
for _, pkg := range toUpdate {
if pkg.ShouldIgnore() {
printIgnoringPackage(pkg, "latest-commit")
} else {
toUpgrade = append(toUpgrade,
Upgrade{
Name: pkg.Name(),
Repository: "devel",
LocalVersion: pkg.Version(),
RemoteVersion: "latest-commit",
})
}
}
localCache.RemovePackage(toRemove)
return toUpgrade
}
func printIgnoringPackage(pkg alpm.IPackage, newPkgVersion string) {
left, right := GetVersionDiff(pkg.Version(), newPkgVersion)
text.Warnln(gotext.Get("%s: ignoring package upgrade (%s => %s)",
text.Cyan(pkg.Name()),
left, right,
))
}
// UpAUR gathers foreign packages and checks if they have new versions.
// Output: Upgrade type package list.
func UpAUR(remote []alpm.IPackage, aurdata map[string]*rpc.Pkg, timeUpdate bool) UpSlice {
toUpgrade := make(UpSlice, 0)
for _, pkg := range remote {
aurPkg, ok := aurdata[pkg.Name()]
if !ok {
continue
}
if (timeUpdate && (int64(aurPkg.LastModified) > pkg.BuildDate().Unix())) ||
(alpm.VerCmp(pkg.Version(), aurPkg.Version) < 0) {
if pkg.ShouldIgnore() {
printIgnoringPackage(pkg, aurPkg.Version)
} else {
toUpgrade = append(toUpgrade,
Upgrade{
Name: aurPkg.Name,
Repository: "aur",
LocalVersion: pkg.Version(),
RemoteVersion: aurPkg.Version,
})
}
}
}
return toUpgrade
}

View file

@ -1,4 +1,4 @@
package main
package upgrade
import (
"fmt"
@ -17,7 +17,6 @@ import (
"github.com/Jguer/yay/v10/pkg/db/mock"
"github.com/Jguer/yay/v10/pkg/settings"
"github.com/Jguer/yay/v10/pkg/upgrade"
"github.com/Jguer/yay/v10/pkg/vcs"
)
@ -30,7 +29,7 @@ func Test_upAUR(t *testing.T) {
tests := []struct {
name string
args args
want upgrade.UpSlice
want UpSlice
}{
{
name: "No Updates",
@ -46,7 +45,7 @@ func Test_upAUR(t *testing.T) {
},
timeUpdate: false,
},
want: upgrade.UpSlice{},
want: UpSlice{},
},
{
name: "Simple Update",
@ -55,7 +54,7 @@ func Test_upAUR(t *testing.T) {
aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
timeUpdate: false,
},
want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}},
want: UpSlice{Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}},
},
{
name: "Time Update",
@ -64,7 +63,7 @@ func Test_upAUR(t *testing.T) {
aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello", LastModified: int(time.Now().AddDate(0, 0, 2).Unix())}},
timeUpdate: true,
},
want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.0.0"}},
want: UpSlice{Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.0.0"}},
},
}
for _, tt := range tests {
@ -72,7 +71,7 @@ func Test_upAUR(t *testing.T) {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
got := upAUR(tt.args.remote, tt.args.aurdata, tt.args.timeUpdate)
got := UpAUR(tt.args.remote, tt.args.aurdata, tt.args.timeUpdate)
assert.EqualValues(t, tt.want, got)
w.Close()
@ -107,7 +106,7 @@ func (r *MockRunner) Capture(cmd *exec.Cmd, timeout int64) (stdout, stderr strin
func Test_upDevel(t *testing.T) {
var err error
config, err = settings.NewConfig()
config, err := settings.NewConfig()
assert.NoError(t, err)
config.Runtime.CmdRunner = &MockRunner{
@ -128,7 +127,7 @@ func Test_upDevel(t *testing.T) {
tests := []struct {
name string
args args
want upgrade.UpSlice
want UpSlice
finalLen int
}{
{
@ -148,7 +147,7 @@ func Test_upDevel(t *testing.T) {
"ignored": {Version: "2.0.0", Name: "ignored"},
},
},
want: upgrade.UpSlice{},
want: UpSlice{},
},
{
name: "Simple Update",
@ -204,14 +203,14 @@ func Test_upDevel(t *testing.T) {
"hello4": {Version: "2.0.0", Name: "hello4"},
},
},
want: upgrade.UpSlice{
upgrade.Upgrade{
want: UpSlice{
Upgrade{
Name: "hello",
Repository: "devel",
LocalVersion: "2.0.0",
RemoteVersion: "latest-commit",
},
upgrade.Upgrade{
Upgrade{
Name: "hello4",
Repository: "devel",
LocalVersion: "4.0.0",
@ -239,7 +238,7 @@ func Test_upDevel(t *testing.T) {
remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
},
want: upgrade.UpSlice{},
want: UpSlice{},
},
{
name: "No update returned - ignored",
@ -261,13 +260,13 @@ func Test_upDevel(t *testing.T) {
remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PShouldIgnore: true}},
aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
},
want: upgrade.UpSlice{},
want: UpSlice{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Runtime.CmdRunner.(*MockRunner).t = t
got := upDevel(tt.args.remote, tt.args.aurdata, &tt.args.cached)
got := UpDevel(tt.args.remote, tt.args.aurdata, &tt.args.cached)
assert.ElementsMatch(t, tt.want, got)
assert.Equal(t, tt.finalLen, len(tt.args.cached.OriginsByPackage))
})

View file

@ -8,19 +8,16 @@ import (
alpm "github.com/Jguer/go-alpm/v2"
"github.com/leonelquinteros/gotext"
rpc "github.com/mikkeloscar/aur"
"github.com/Jguer/yay/v10/pkg/db"
"github.com/Jguer/yay/v10/pkg/intrange"
"github.com/Jguer/yay/v10/pkg/multierror"
"github.com/Jguer/yay/v10/pkg/query"
"github.com/Jguer/yay/v10/pkg/settings"
"github.com/Jguer/yay/v10/pkg/stringset"
"github.com/Jguer/yay/v10/pkg/text"
"github.com/Jguer/yay/v10/pkg/upgrade"
"github.com/Jguer/yay/v10/pkg/vcs"
rpc "github.com/mikkeloscar/aur"
"github.com/Jguer/yay/v10/pkg/multierror"
"github.com/Jguer/yay/v10/pkg/stringset"
)
// upList returns lists of packages to upgrade from each source.
@ -62,7 +59,7 @@ func upList(warnings *query.AURWarnings, dbExecutor db.Executor, enableDowngrade
wg.Add(1)
go func() {
aurUp = upAUR(remote, aurdata, config.TimeUpdate)
aurUp = upgrade.UpAUR(remote, aurdata, config.TimeUpdate)
wg.Done()
}()
@ -70,7 +67,7 @@ func upList(warnings *query.AURWarnings, dbExecutor db.Executor, enableDowngrade
text.OperationInfoln(gotext.Get("Checking development packages..."))
wg.Add(1)
go func() {
develUp = upDevel(remote, aurdata, config.Runtime.VCSStore)
develUp = upgrade.UpDevel(remote, aurdata, config.Runtime.VCSStore)
wg.Done()
}()
}
@ -98,102 +95,6 @@ func upList(warnings *query.AURWarnings, dbExecutor db.Executor, enableDowngrade
return aurUp, repoUp, errs.Return()
}
func upDevel(
remote []alpm.IPackage,
aurdata map[string]*rpc.Pkg,
localCache *vcs.InfoStore) upgrade.UpSlice {
toUpdate := make([]alpm.IPackage, 0, len(aurdata))
toRemove := make([]string, 0)
var mux1, mux2 sync.Mutex
var wg sync.WaitGroup
checkUpdate := func(pkgName string, e vcs.OriginInfoByURL) {
defer wg.Done()
if localCache.NeedsUpdate(e) {
if _, ok := aurdata[pkgName]; ok {
for _, pkg := range remote {
if pkg.Name() == pkgName {
mux1.Lock()
toUpdate = append(toUpdate, pkg)
mux1.Unlock()
return
}
}
}
mux2.Lock()
toRemove = append(toRemove, pkgName)
mux2.Unlock()
}
}
for pkgName, e := range localCache.OriginsByPackage {
wg.Add(1)
go checkUpdate(pkgName, e)
}
wg.Wait()
toUpgrade := make(upgrade.UpSlice, 0, len(toUpdate))
for _, pkg := range toUpdate {
if pkg.ShouldIgnore() {
printIgnoringPackage(pkg, "latest-commit")
} else {
toUpgrade = append(toUpgrade,
upgrade.Upgrade{
Name: pkg.Name(),
Repository: "devel",
LocalVersion: pkg.Version(),
RemoteVersion: "latest-commit",
})
}
}
localCache.RemovePackage(toRemove)
return toUpgrade
}
func printIgnoringPackage(pkg alpm.IPackage, newPkgVersion string) {
left, right := upgrade.GetVersionDiff(pkg.Version(), newPkgVersion)
text.Warnln(gotext.Get("%s: ignoring package upgrade (%s => %s)",
text.Cyan(pkg.Name()),
left, right,
))
}
// upAUR gathers foreign packages and checks if they have new versions.
// Output: Upgrade type package list.
func upAUR(remote []alpm.IPackage, aurdata map[string]*rpc.Pkg, timeUpdate bool) upgrade.UpSlice {
toUpgrade := make(upgrade.UpSlice, 0)
for _, pkg := range remote {
aurPkg, ok := aurdata[pkg.Name()]
if !ok {
continue
}
if (timeUpdate && (int64(aurPkg.LastModified) > pkg.BuildDate().Unix())) ||
(alpm.VerCmp(pkg.Version(), aurPkg.Version) < 0) {
if pkg.ShouldIgnore() {
printIgnoringPackage(pkg, aurPkg.Version)
} else {
toUpgrade = append(toUpgrade,
upgrade.Upgrade{
Name: aurPkg.Name,
Repository: "aur",
LocalVersion: pkg.Version(),
RemoteVersion: aurPkg.Version,
})
}
}
}
return toUpgrade
}
func printLocalNewerThanAUR(
remote []alpm.IPackage, aurdata map[string]*rpc.Pkg) {
for _, pkg := range remote {