upgrade.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "sync"
  8. "github.com/Jguer/yay/v11/pkg/db"
  9. "github.com/Jguer/yay/v11/pkg/dep"
  10. "github.com/Jguer/yay/v11/pkg/intrange"
  11. "github.com/Jguer/yay/v11/pkg/multierror"
  12. "github.com/Jguer/yay/v11/pkg/query"
  13. "github.com/Jguer/yay/v11/pkg/settings"
  14. "github.com/Jguer/yay/v11/pkg/stringset"
  15. "github.com/Jguer/yay/v11/pkg/text"
  16. "github.com/Jguer/yay/v11/pkg/topo"
  17. "github.com/Jguer/yay/v11/pkg/upgrade"
  18. aur "github.com/Jguer/aur"
  19. "github.com/Jguer/aur/metadata"
  20. alpm "github.com/Jguer/go-alpm/v2"
  21. "github.com/leonelquinteros/gotext"
  22. )
  23. func filterUpdateList(list []db.Upgrade, filter upgrade.Filter) []db.Upgrade {
  24. tmp := list[:0]
  25. for _, pkg := range list {
  26. if filter(pkg) {
  27. tmp = append(tmp, pkg)
  28. }
  29. }
  30. return tmp
  31. }
  32. // upList returns lists of packages to upgrade from each source.
  33. func upList(ctx context.Context, aurCache settings.AURCache,
  34. warnings *query.AURWarnings, dbExecutor db.Executor, enableDowngrade bool,
  35. filter upgrade.Filter,
  36. ) (aurUp, repoUp upgrade.UpSlice, err error) {
  37. remote := dbExecutor.InstalledRemotePackages()
  38. remoteNames := dbExecutor.InstalledRemotePackageNames()
  39. var (
  40. wg sync.WaitGroup
  41. develUp upgrade.UpSlice
  42. repoSlice []db.Upgrade
  43. errs multierror.MultiError
  44. )
  45. aurdata := make(map[string]*aur.Pkg)
  46. for _, pkg := range remote {
  47. if pkg.ShouldIgnore() {
  48. warnings.Ignore.Set(pkg.Name())
  49. }
  50. }
  51. if config.Runtime.Mode.AtLeastRepo() {
  52. text.OperationInfoln(gotext.Get("Searching databases for updates..."))
  53. wg.Add(1)
  54. go func() {
  55. repoSlice, err = dbExecutor.RepoUpgrades(enableDowngrade)
  56. errs.Add(err)
  57. wg.Done()
  58. }()
  59. }
  60. if config.Runtime.Mode.AtLeastAUR() {
  61. text.OperationInfoln(gotext.Get("Searching AUR for updates..."))
  62. var _aurdata []aur.Pkg
  63. if aurCache != nil {
  64. _aurdata, err = aurCache.Get(ctx, &metadata.AURQuery{Needles: remoteNames, By: aur.Name})
  65. } else {
  66. _aurdata, err = query.AURInfo(ctx, config.Runtime.AURClient, remoteNames, warnings, config.RequestSplitN)
  67. }
  68. errs.Add(err)
  69. if err == nil {
  70. for i := range _aurdata {
  71. pkg := &_aurdata[i]
  72. aurdata[pkg.Name] = pkg
  73. }
  74. wg.Add(1)
  75. go func() {
  76. aurUp = upgrade.UpAUR(remote, aurdata, config.TimeUpdate)
  77. wg.Done()
  78. }()
  79. if config.Devel {
  80. text.OperationInfoln(gotext.Get("Checking development packages..."))
  81. wg.Add(1)
  82. go func() {
  83. develUp = upgrade.UpDevel(ctx, remote, aurdata, config.Runtime.VCSStore)
  84. wg.Done()
  85. }()
  86. }
  87. }
  88. }
  89. wg.Wait()
  90. printLocalNewerThanAUR(remote, aurdata)
  91. names := make(stringset.StringSet)
  92. for _, up := range develUp.Up {
  93. names.Set(up.Name)
  94. }
  95. for _, up := range aurUp.Up {
  96. if !names.Get(up.Name) {
  97. develUp.Up = append(develUp.Up, up)
  98. }
  99. }
  100. aurUp = develUp
  101. aurUp.Repos = []string{"aur", "devel"}
  102. repoUp = upgrade.UpSlice{Up: repoSlice, Repos: dbExecutor.Repos()}
  103. aurUp.Up = filterUpdateList(aurUp.Up, filter)
  104. repoUp.Up = filterUpdateList(repoUp.Up, filter)
  105. return aurUp, repoUp, errs.Return()
  106. }
  107. func printLocalNewerThanAUR(
  108. remote []alpm.IPackage, aurdata map[string]*aur.Pkg,
  109. ) {
  110. for _, pkg := range remote {
  111. aurPkg, ok := aurdata[pkg.Name()]
  112. if !ok {
  113. continue
  114. }
  115. left, right := upgrade.GetVersionDiff(pkg.Version(), aurPkg.Version)
  116. if !isDevelPackage(pkg) && db.VerCmp(pkg.Version(), aurPkg.Version) > 0 {
  117. text.Warnln(gotext.Get("%s: local (%s) is newer than AUR (%s)",
  118. text.Cyan(pkg.Name()),
  119. left, right,
  120. ))
  121. }
  122. }
  123. }
  124. func isDevelName(name string) bool {
  125. for _, suffix := range []string{"git", "svn", "hg", "bzr", "nightly", "insiders-bin"} {
  126. if strings.HasSuffix(name, "-"+suffix) {
  127. return true
  128. }
  129. }
  130. return strings.Contains(name, "-always-")
  131. }
  132. func isDevelPackage(pkg alpm.IPackage) bool {
  133. return isDevelName(pkg.Name()) || isDevelName(pkg.Base())
  134. }
  135. // upgradePkgsMenu handles updating the cache and installing updates.
  136. func upgradePkgsMenu(aurUp, repoUp upgrade.UpSlice) (stringset.StringSet, []string, error) {
  137. ignore := make(stringset.StringSet)
  138. targets := []string{}
  139. allUpLen := len(repoUp.Up) + len(aurUp.Up)
  140. if allUpLen == 0 {
  141. return ignore, nil, nil
  142. }
  143. if !config.UpgradeMenu {
  144. for _, pkg := range aurUp.Up {
  145. targets = append(targets, pkg.Repository+"/"+pkg.Name)
  146. }
  147. return ignore, targets, nil
  148. }
  149. sort.Sort(repoUp)
  150. sort.Sort(aurUp)
  151. allUp := upgrade.UpSlice{Up: append(repoUp.Up, aurUp.Up...), Repos: append(repoUp.Repos, aurUp.Repos...)}
  152. fmt.Printf("%s"+text.Bold(" %d ")+"%s\n", text.Bold(text.Cyan("::")), allUpLen, text.Bold(gotext.Get("Packages to upgrade.")))
  153. allUp.Print()
  154. text.Infoln(gotext.Get("Packages to exclude: (eg: \"1 2 3\", \"1-3\", \"^4\" or repo name)"))
  155. numbers, err := text.GetInput(config.AnswerUpgrade, settings.NoConfirm)
  156. if err != nil {
  157. return nil, nil, err
  158. }
  159. // upgrade menu asks you which packages to NOT upgrade so in this case
  160. // include and exclude are kind of swapped
  161. include, exclude, otherInclude, otherExclude := intrange.ParseNumberMenu(numbers)
  162. isInclude := len(exclude) == 0 && len(otherExclude) == 0
  163. for i, pkg := range repoUp.Up {
  164. if isInclude && otherInclude.Get(pkg.Repository) {
  165. ignore.Set(pkg.Name)
  166. }
  167. if isInclude && !include.Get(len(repoUp.Up)-i+len(aurUp.Up)) {
  168. targets = append(targets, pkg.Repository+"/"+pkg.Name)
  169. continue
  170. }
  171. if !isInclude && (exclude.Get(len(repoUp.Up)-i+len(aurUp.Up)) || otherExclude.Get(pkg.Repository)) {
  172. targets = append(targets, pkg.Repository+"/"+pkg.Name)
  173. continue
  174. }
  175. ignore.Set(pkg.Name)
  176. }
  177. for i, pkg := range aurUp.Up {
  178. if isInclude && otherInclude.Get(pkg.Repository) {
  179. continue
  180. }
  181. if isInclude && !include.Get(len(aurUp.Up)-i) {
  182. targets = append(targets, "aur/"+pkg.Name)
  183. }
  184. if !isInclude && (exclude.Get(len(aurUp.Up)-i) || otherExclude.Get(pkg.Repository)) {
  185. targets = append(targets, "aur/"+pkg.Name)
  186. }
  187. }
  188. return ignore, targets, err
  189. }
  190. // Targets for sys upgrade.
  191. func sysupgradeTargets(ctx context.Context, dbExecutor db.Executor,
  192. enableDowngrade bool,
  193. ) (stringset.StringSet, []string, error) {
  194. warnings := query.NewWarnings()
  195. aurUp, repoUp, err := upList(ctx, nil, warnings, dbExecutor, enableDowngrade,
  196. func(upgrade.Upgrade) bool { return true })
  197. if err != nil {
  198. return nil, nil, err
  199. }
  200. warnings.Print()
  201. return upgradePkgsMenu(aurUp, repoUp)
  202. }
  203. // Targets for sys upgrade.
  204. func sysupgradeTargetsV2(ctx context.Context,
  205. aurCache settings.AURCache,
  206. dbExecutor db.Executor,
  207. graph *topo.Graph[string, *dep.InstallInfo],
  208. enableDowngrade bool,
  209. ) (*topo.Graph[string, *dep.InstallInfo], stringset.StringSet, error) {
  210. warnings := query.NewWarnings()
  211. aurUp, repoUp, err := upList(ctx, aurCache, warnings, dbExecutor, enableDowngrade,
  212. func(upgrade.Upgrade) bool { return true })
  213. if err != nil {
  214. return graph, nil, err
  215. }
  216. warnings.Print()
  217. ignore := make(stringset.StringSet)
  218. allUpLen := len(repoUp.Up) + len(aurUp.Up)
  219. if allUpLen == 0 {
  220. return graph, ignore, nil
  221. }
  222. sort.Sort(repoUp)
  223. sort.Sort(aurUp)
  224. allUp := upgrade.UpSlice{Up: append(repoUp.Up, aurUp.Up...), Repos: append(repoUp.Repos, aurUp.Repos...)}
  225. fmt.Printf("%s"+text.Bold(" %d ")+"%s\n", text.Bold(text.Cyan("::")), allUpLen, text.Bold(gotext.Get("Packages to upgrade.")))
  226. allUp.Print()
  227. text.Infoln(gotext.Get("Packages to exclude: (eg: \"1 2 3\", \"1-3\", \"^4\" or repo name)"))
  228. numbers, err := text.GetInput(config.AnswerUpgrade, settings.NoConfirm)
  229. if err != nil {
  230. return nil, nil, err
  231. }
  232. // upgrade menu asks you which packages to NOT upgrade so in this case
  233. // include and exclude are kind of swapped
  234. include, exclude, otherInclude, otherExclude := intrange.ParseNumberMenu(numbers)
  235. isInclude := len(exclude) == 0 && len(otherExclude) == 0
  236. for i := range repoUp.Up {
  237. pkg := &repoUp.Up[i]
  238. if isInclude && otherInclude.Get(pkg.Repository) {
  239. ignore.Set(pkg.Name)
  240. }
  241. if isInclude && !include.Get(len(repoUp.Up)-i+len(aurUp.Up)) {
  242. dep.AddUpgradeToGraph(pkg, graph)
  243. continue
  244. }
  245. if !isInclude && (exclude.Get(len(repoUp.Up)-i+len(aurUp.Up)) || otherExclude.Get(pkg.Repository)) {
  246. dep.AddUpgradeToGraph(pkg, graph)
  247. continue
  248. }
  249. ignore.Set(pkg.Name)
  250. }
  251. for i := range aurUp.Up {
  252. pkg := &aurUp.Up[i]
  253. if isInclude && otherInclude.Get(pkg.Repository) {
  254. continue
  255. }
  256. if isInclude && !include.Get(len(aurUp.Up)-i) {
  257. dep.AddUpgradeToGraph(pkg, graph)
  258. }
  259. if !isInclude && (exclude.Get(len(aurUp.Up)-i) || otherExclude.Get(pkg.Repository)) {
  260. dep.AddUpgradeToGraph(pkg, graph)
  261. }
  262. }
  263. return graph, ignore, err
  264. }