upgrade.go 8.4 KB

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