Skip to content

Commit

Permalink
fix(upgrade): export upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
Jguer committed Oct 1, 2020
1 parent e1b632c commit 678d10e
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 120 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
108 changes: 108 additions & 0 deletions pkg/upgrade/sources.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 15 additions & 16 deletions upgrade_test.go → pkg/upgrade/sources_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package upgrade

import (
"fmt"
Expand All @@ -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"
)

Expand All @@ -30,7 +29,7 @@ func Test_upAUR(t *testing.T) {
tests := []struct {
name string
args args
want upgrade.UpSlice
want UpSlice
}{
{
name: "No Updates",
Expand All @@ -46,7 +45,7 @@ func Test_upAUR(t *testing.T) {
},
timeUpdate: false,
},
want: upgrade.UpSlice{},
want: UpSlice{},
},
{
name: "Simple Update",
Expand All @@ -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",
Expand All @@ -64,15 +63,15 @@ 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 {
t.Run(tt.name, func(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()
Expand Down Expand Up @@ -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{
Expand All @@ -128,7 +127,7 @@ func Test_upDevel(t *testing.T) {
tests := []struct {
name string
args args
want upgrade.UpSlice
want UpSlice
finalLen int
}{
{
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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))
})
Expand Down
109 changes: 5 additions & 104 deletions upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -62,15 +59,15 @@ 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()
}()

if config.Devel {
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()
}()
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 678d10e

Please sign in to comment.