upgrade.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 *metadata.Client,
  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 _, pkg := range _aurdata {
  71. aurdata[pkg.Name] = pkg
  72. }
  73. wg.Add(1)
  74. go func() {
  75. aurUp = upgrade.UpAUR(remote, aurdata, config.TimeUpdate)
  76. wg.Done()
  77. }()
  78. if config.Devel {
  79. text.OperationInfoln(gotext.Get("Checking development packages..."))
  80. wg.Add(1)
  81. go func() {
  82. develUp = upgrade.UpDevel(ctx, remote, aurdata, config.Runtime.VCSStore)
  83. wg.Done()
  84. }()
  85. }
  86. }
  87. }
  88. wg.Wait()
  89. printLocalNewerThanAUR(remote, aurdata)
  90. names := make(stringset.StringSet)
  91. for _, up := range develUp.Up {
  92. names.Set(up.Name)
  93. }
  94. for _, up := range aurUp.Up {
  95. if !names.Get(up.Name) {
  96. develUp.Up = append(develUp.Up, up)
  97. }
  98. }
  99. aurUp = develUp
  100. aurUp.Repos = []string{"aur", "devel"}
  101. repoUp = upgrade.UpSlice{Up: repoSlice, Repos: dbExecutor.Repos()}
  102. aurUp.Up = filterUpdateList(aurUp.Up, filter)
  103. repoUp.Up = filterUpdateList(repoUp.Up, filter)
  104. return aurUp, repoUp, errs.Return()
  105. }
  106. func printLocalNewerThanAUR(
  107. remote []alpm.IPackage, aurdata map[string]*aur.Pkg,
  108. ) {
  109. for _, pkg := range remote {
  110. aurPkg, ok := aurdata[pkg.Name()]
  111. if !ok {
  112. continue
  113. }
  114. left, right := upgrade.GetVersionDiff(pkg.Version(), aurPkg.Version)
  115. if !isDevelPackage(pkg) && db.VerCmp(pkg.Version(), aurPkg.Version) > 0 {
  116. text.Warnln(gotext.Get("%s: local (%s) is newer than AUR (%s)",
  117. text.Cyan(pkg.Name()),
  118. left, right,
  119. ))
  120. }
  121. }
  122. }
  123. func isDevelName(name string) bool {
  124. for _, suffix := range []string{"git", "svn", "hg", "bzr", "nightly", "insiders-bin"} {
  125. if strings.HasSuffix(name, "-"+suffix) {
  126. return true
  127. }
  128. }
  129. return strings.Contains(name, "-always-")
  130. }
  131. func isDevelPackage(pkg alpm.IPackage) bool {
  132. return isDevelName(pkg.Name()) || isDevelName(pkg.Base())
  133. }
  134. // upgradePkgsMenu handles updating the cache and installing updates.
  135. func upgradePkgsMenu(aurUp, repoUp upgrade.UpSlice) (stringset.StringSet, []string, error) {
  136. ignore := make(stringset.StringSet)
  137. targets := []string{}
  138. allUpLen := len(repoUp.Up) + len(aurUp.Up)
  139. if allUpLen == 0 {
  140. return ignore, nil, nil
  141. }
  142. if !config.UpgradeMenu {
  143. for _, pkg := range aurUp.Up {
  144. targets = append(targets, pkg.Repository+"/"+pkg.Name)
  145. }
  146. return ignore, targets, nil
  147. }
  148. sort.Sort(repoUp)
  149. sort.Sort(aurUp)
  150. allUp := upgrade.UpSlice{Up: append(repoUp.Up, aurUp.Up...), Repos: append(repoUp.Repos, aurUp.Repos...)}
  151. fmt.Printf("%s"+text.Bold(" %d ")+"%s\n", text.Bold(text.Cyan("::")), allUpLen, text.Bold(gotext.Get("Packages to upgrade.")))
  152. allUp.Print()
  153. text.Infoln(gotext.Get("Packages to exclude: (eg: \"1 2 3\", \"1-3\", \"^4\" or repo name)"))
  154. numbers, err := text.GetInput(config.AnswerUpgrade, settings.NoConfirm)
  155. if err != nil {
  156. return nil, nil, err
  157. }
  158. // upgrade menu asks you which packages to NOT upgrade so in this case
  159. // include and exclude are kind of swapped
  160. include, exclude, otherInclude, otherExclude := intrange.ParseNumberMenu(numbers)
  161. isInclude := len(exclude) == 0 && len(otherExclude) == 0
  162. for i, pkg := range repoUp.Up {
  163. if isInclude && otherInclude.Get(pkg.Repository) {
  164. ignore.Set(pkg.Name)
  165. }
  166. if isInclude && !include.Get(len(repoUp.Up)-i+len(aurUp.Up)) {
  167. targets = append(targets, pkg.Repository+"/"+pkg.Name)
  168. continue
  169. }
  170. if !isInclude && (exclude.Get(len(repoUp.Up)-i+len(aurUp.Up)) || otherExclude.Get(pkg.Repository)) {
  171. targets = append(targets, pkg.Repository+"/"+pkg.Name)
  172. continue
  173. }
  174. ignore.Set(pkg.Name)
  175. }
  176. for i, pkg := range aurUp.Up {
  177. if isInclude && otherInclude.Get(pkg.Repository) {
  178. continue
  179. }
  180. if isInclude && !include.Get(len(aurUp.Up)-i) {
  181. targets = append(targets, "aur/"+pkg.Name)
  182. }
  183. if !isInclude && (exclude.Get(len(aurUp.Up)-i) || otherExclude.Get(pkg.Repository)) {
  184. targets = append(targets, "aur/"+pkg.Name)
  185. }
  186. }
  187. return ignore, targets, err
  188. }
  189. // Targets for sys upgrade.
  190. func sysupgradeTargets(ctx context.Context, dbExecutor db.Executor,
  191. enableDowngrade bool,
  192. ) (stringset.StringSet, []string, error) {
  193. warnings := query.NewWarnings()
  194. aurUp, repoUp, err := upList(ctx, nil, warnings, dbExecutor, enableDowngrade,
  195. func(upgrade.Upgrade) bool { return true })
  196. if err != nil {
  197. return nil, nil, err
  198. }
  199. warnings.Print()
  200. return upgradePkgsMenu(aurUp, repoUp)
  201. }
  202. // Targets for sys upgrade.
  203. func sysupgradeTargetsV2(ctx context.Context,
  204. aurCache *metadata.Client,
  205. dbExecutor db.Executor,
  206. graph *topo.Graph[string, *dep.InstallInfo],
  207. enableDowngrade bool,
  208. ) (*topo.Graph[string, *dep.InstallInfo], stringset.StringSet, error) {
  209. warnings := query.NewWarnings()
  210. aurUp, repoUp, err := upList(ctx, aurCache, warnings, dbExecutor, enableDowngrade,
  211. func(upgrade.Upgrade) bool { return true })
  212. if err != nil {
  213. return graph, nil, err
  214. }
  215. warnings.Print()
  216. ignore := make(stringset.StringSet)
  217. allUpLen := len(repoUp.Up) + len(aurUp.Up)
  218. if allUpLen == 0 {
  219. return graph, ignore, nil
  220. }
  221. sort.Sort(repoUp)
  222. sort.Sort(aurUp)
  223. allUp := upgrade.UpSlice{Up: append(repoUp.Up, aurUp.Up...), Repos: append(repoUp.Repos, aurUp.Repos...)}
  224. fmt.Printf("%s"+text.Bold(" %d ")+"%s\n", text.Bold(text.Cyan("::")), allUpLen, text.Bold(gotext.Get("Packages to upgrade.")))
  225. allUp.Print()
  226. text.Infoln(gotext.Get("Packages to exclude: (eg: \"1 2 3\", \"1-3\", \"^4\" or repo name)"))
  227. numbers, err := text.GetInput(config.AnswerUpgrade, settings.NoConfirm)
  228. if err != nil {
  229. return nil, nil, err
  230. }
  231. // upgrade menu asks you which packages to NOT upgrade so in this case
  232. // include and exclude are kind of swapped
  233. include, exclude, otherInclude, otherExclude := intrange.ParseNumberMenu(numbers)
  234. isInclude := len(exclude) == 0 && len(otherExclude) == 0
  235. for i := range repoUp.Up {
  236. pkg := &repoUp.Up[i]
  237. if isInclude && otherInclude.Get(pkg.Repository) {
  238. ignore.Set(pkg.Name)
  239. }
  240. if isInclude && !include.Get(len(repoUp.Up)-i+len(aurUp.Up)) {
  241. dep.AddUpgradeToGraph(pkg, graph)
  242. continue
  243. }
  244. if !isInclude && (exclude.Get(len(repoUp.Up)-i+len(aurUp.Up)) || otherExclude.Get(pkg.Repository)) {
  245. dep.AddUpgradeToGraph(pkg, graph)
  246. continue
  247. }
  248. ignore.Set(pkg.Name)
  249. }
  250. for i := range aurUp.Up {
  251. pkg := &aurUp.Up[i]
  252. if isInclude && otherInclude.Get(pkg.Repository) {
  253. continue
  254. }
  255. if isInclude && !include.Get(len(aurUp.Up)-i) {
  256. dep.AddUpgradeToGraph(pkg, graph)
  257. }
  258. if !isInclude && (exclude.Get(len(aurUp.Up)-i) || otherExclude.Get(pkg.Repository)) {
  259. dep.AddUpgradeToGraph(pkg, graph)
  260. }
  261. }
  262. return graph, ignore, err
  263. }