Skip to content

Commit

Permalink
add basic sync upgrade capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Jguer committed Nov 1, 2022
1 parent 776fc96 commit b5bdcfb
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 31 deletions.
3 changes: 2 additions & 1 deletion aur_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (installer *Installer) getNewTargets(pkgdests map[string]string, name strin
pkgArchives := make([]string, 0, 2)

if _, errStat := os.Stat(pkgdest); os.IsNotExist(errStat) {
return nil, false, &UnableToFindPkgDestError{name: name, pkgDest: pkgdest}
return nil, false, &FindPkgDestError{name: name, pkgDest: pkgdest}
}

pkgArchives = append(pkgArchives, pkgdest)
Expand Down Expand Up @@ -229,6 +229,7 @@ func (*Installer) installSyncPackages(ctx context.Context, cmdArgs *parser.Argum
arguments.DelArg("asdeps", "asdep")
arguments.DelArg("asexplicit", "asexp")
arguments.DelArg("i", "install")
arguments.DelArg("u", "upgrade")
arguments.Op = "S"
arguments.ClearTargets()
arguments.AddTarget(repoTargets...)
Expand Down
17 changes: 15 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@ package main

import "github.com/leonelquinteros/gotext"

type UnableToFindPkgDestError struct {
type SetPkgReasonError struct {
exp bool // explicit
}

func (e *SetPkgReasonError) Error() string {
reason := gotext.Get("explicit")
if !e.exp {
reason = gotext.Get("dependency")
}

return gotext.Get("error updating package install reason to %s", reason)
}

type FindPkgDestError struct {
name, pkgDest string
}

func (e *UnableToFindPkgDestError) Error() string {
func (e *FindPkgDestError) Error() string {
return gotext.Get(
"the PKGDEST for %s is listed by makepkg but does not exist: %s",
e.name, e.pkgDest)
Expand Down
43 changes: 25 additions & 18 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,49 @@ import (
"github.com/Jguer/yay/v11/pkg/text"
)

func asdeps(ctx context.Context, cmdArgs *parser.Arguments, pkgs []string) error {
func setPkgReason(ctx context.Context, cmdArgs *parser.Arguments, pkgs []string, exp bool) error {
if len(pkgs) == 0 {
return nil
}

cmdArgs = cmdArgs.CopyGlobal()
_ = cmdArgs.AddArg("q", "D", "asdeps")
cmdArgs.AddTarget(pkgs...)

err := config.Runtime.CmdBuilder.Show(config.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
cmdArgs, config.Runtime.Mode, settings.NoConfirm))
if err != nil {
return errors.New(gotext.Get("error updating package install reason to dependency"))
if exp {
if err := cmdArgs.AddArg("q", "D", "asexplicit"); err != nil {
return err
}
} else {
if err := cmdArgs.AddArg("q", "D", "asdeps"); err != nil {
return err
}
}

return nil
}
for _, compositePkgName := range pkgs {
pkgSplit := strings.Split(compositePkgName, "/")
pkgName := pkgSplit[0]
if len(pkgSplit) > 1 {
pkgName = pkgSplit[1]
}

func asexp(ctx context.Context, cmdArgs *parser.Arguments, pkgs []string) error {
if len(pkgs) == 0 {
return nil
cmdArgs.AddTarget(pkgName)
}

cmdArgs = cmdArgs.CopyGlobal()
_ = cmdArgs.AddArg("q", "D", "asexplicit")
cmdArgs.AddTarget(pkgs...)

err := config.Runtime.CmdBuilder.Show(config.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
cmdArgs, config.Runtime.Mode, settings.NoConfirm))
if err != nil {
return fmt.Errorf("%s - %w", gotext.Get("error updating package install reason to explicit"), err)
return &SetPkgReasonError{exp: exp}
}

return nil
}

func asdeps(ctx context.Context, cmdArgs *parser.Arguments, pkgs []string) error {
return setPkgReason(ctx, cmdArgs, pkgs, false)
}

func asexp(ctx context.Context, cmdArgs *parser.Arguments, pkgs []string) error {
return setPkgReason(ctx, cmdArgs, pkgs, true)
}

// Install handles package installs.
func install(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Executor, ignoreProviders bool) error {
var (
Expand Down
1 change: 1 addition & 0 deletions pkg/db/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func VerCmp(v1, v2 string) int {

type Upgrade struct {
Name string
Base string
Repository string
LocalVersion string
RemoteVersion string
Expand Down
1 change: 1 addition & 0 deletions pkg/db/ialpm/alpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ func (ae *AlpmExecutor) RepoUpgrades(enableDowngrade bool) ([]db.Upgrade, error)

slice = append(slice, upgrade.Upgrade{
Name: pkg.Name(),
Base: pkg.Base(),
Repository: pkg.DB().Name(),
LocalVersion: localVer,
RemoteVersion: pkg.Version(),
Expand Down
6 changes: 5 additions & 1 deletion pkg/upgrade/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func UpDevel(
ctx context.Context,
remote []db.IPackage,
aurdata map[string]*query.Pkg,
localCache *vcs.InfoStore) UpSlice {
localCache *vcs.InfoStore,
) UpSlice {
toUpdate := make([]db.IPackage, 0, len(aurdata))
toRemove := make([]string, 0)

Expand Down Expand Up @@ -64,9 +65,11 @@ func UpDevel(
toUpgrade.Up = append(toUpgrade.Up,
Upgrade{
Name: pkg.Name(),
Base: pkg.Base(),
Repository: "devel",
LocalVersion: pkg.Version(),
RemoteVersion: "latest-commit",
Reason: pkg.Reason(),
})
}
}
Expand Down Expand Up @@ -104,6 +107,7 @@ func UpAUR(remote []db.IPackage, aurdata map[string]*query.Pkg, timeUpdate bool)
toUpgrade.Up = append(toUpgrade.Up,
Upgrade{
Name: aurPkg.Name,
Base: aurPkg.PackageBase,
Repository: "aur",
LocalVersion: pkg.Version(),
RemoteVersion: aurPkg.Version,
Expand Down
17 changes: 8 additions & 9 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@ func syncInstall(ctx context.Context,
) error {
aurCache := config.Runtime.AURCache

if cmdArgs.ExistsArg("u", "sysupgrade") {
var errSysUp error
// All of the installs are done as explicit installs, this should be move to a grapher method
_, errSysUp = addUpgradeTargetsToArgs(ctx, dbExecutor, cmdArgs, []string{}, cmdArgs)
if errSysUp != nil {
return errSysUp
}
}

grapher := dep.NewGrapher(dbExecutor, aurCache, false, settings.NoConfirm, os.Stdout)

graph, err := grapher.GraphFromTargets(cmdArgs.Targets)
if err != nil {
return err
}

if cmdArgs.ExistsArg("u", "sysupgrade") {
var errSysUp error
graph, _, errSysUp = sysupgradeTargetsV2(ctx, dbExecutor, graph, cmdArgs.ExistsDouble("u", "sysupgrade"))
if errSysUp != nil {
return errSysUp
}
}

topoSorted := graph.TopoSortedLayerMap()

preparer := &Preparer{
Expand Down
105 changes: 105 additions & 0 deletions upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
"github.com/leonelquinteros/gotext"

"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/dep"
"github.com/Jguer/yay/v11/pkg/intrange"
"github.com/Jguer/yay/v11/pkg/multierror"
"github.com/Jguer/yay/v11/pkg/query"
"github.com/Jguer/yay/v11/pkg/settings"
"github.com/Jguer/yay/v11/pkg/stringset"
"github.com/Jguer/yay/v11/pkg/text"
"github.com/Jguer/yay/v11/pkg/topo"
"github.com/Jguer/yay/v11/pkg/upgrade"
)

Expand Down Expand Up @@ -249,3 +251,106 @@ func sysupgradeTargets(ctx context.Context, dbExecutor db.Executor,

return upgradePkgsMenu(aurUp, repoUp)
}

// Targets for sys upgrade.
func sysupgradeTargetsV2(ctx context.Context,
dbExecutor db.Executor,
graph *topo.Graph[string, *dep.InstallInfo],
enableDowngrade bool,
) (*topo.Graph[string, *dep.InstallInfo], stringset.StringSet, error) {
warnings := query.NewWarnings()

aurUp, repoUp, err := upList(ctx, warnings, dbExecutor, enableDowngrade,
func(upgrade.Upgrade) bool { return true })
if err != nil {
return graph, nil, err
}

warnings.Print()
ignore := make(stringset.StringSet)

allUpLen := len(repoUp.Up) + len(aurUp.Up)
if allUpLen == 0 {
return graph, ignore, nil
}

sort.Sort(repoUp)
sort.Sort(aurUp)

allUp := upgrade.UpSlice{Up: append(repoUp.Up, aurUp.Up...), Repos: append(repoUp.Repos, aurUp.Repos...)}

fmt.Printf("%s"+text.Bold(" %d ")+"%s\n", text.Bold(text.Cyan("::")), allUpLen, text.Bold(gotext.Get("Packages to upgrade.")))
allUp.Print()

text.Infoln(gotext.Get("Packages to exclude: (eg: \"1 2 3\", \"1-3\", \"^4\" or repo name)"))

numbers, err := text.GetInput(config.AnswerUpgrade, settings.NoConfirm)
if err != nil {
return nil, nil, err
}

// upgrade menu asks you which packages to NOT upgrade so in this case
// include and exclude are kind of swapped
include, exclude, otherInclude, otherExclude := intrange.ParseNumberMenu(numbers)

isInclude := len(exclude) == 0 && len(otherExclude) == 0

for i, pkg := range repoUp.Up {
if isInclude && otherInclude.Get(pkg.Repository) {
ignore.Set(pkg.Name)
}

if isInclude && !include.Get(len(repoUp.Up)-i+len(aurUp.Up)) {
addUpgradeToGraph(pkg, graph)
continue
}

if !isInclude && (exclude.Get(len(repoUp.Up)-i+len(aurUp.Up)) || otherExclude.Get(pkg.Repository)) {
addUpgradeToGraph(pkg, graph)
continue
}

ignore.Set(pkg.Name)
}

for i, pkg := range aurUp.Up {
if isInclude && otherInclude.Get(pkg.Repository) {
continue
}

if isInclude && !include.Get(len(aurUp.Up)-i) {
addUpgradeToGraph(pkg, graph)
}

if !isInclude && (exclude.Get(len(aurUp.Up)-i) || otherExclude.Get(pkg.Repository)) {
addUpgradeToGraph(pkg, graph)
}
}

return graph, ignore, err
}

func addUpgradeToGraph(pkg db.Upgrade, graph *topo.Graph[string, *dep.InstallInfo]) {
source := dep.Sync
if pkg.Repository == "aur" {
source = dep.AUR
}

reason := dep.Explicit
if pkg.Reason == alpm.PkgReasonDepend {
reason = dep.Dep
}

graph.AddNode(pkg.Name)
graph.SetNodeInfo(pkg.Name, &topo.NodeInfo[*dep.InstallInfo]{
Color: "",
Background: "",
Value: &dep.InstallInfo{
Source: source,
Reason: reason,
Version: pkg.RemoteVersion,
AURBase: &pkg.Base,
SyncDBName: &pkg.Repository,
},
})
}

0 comments on commit b5bdcfb

Please sign in to comment.