download.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. "sync"
  9. "github.com/leonelquinteros/gotext"
  10. "github.com/pkg/errors"
  11. "github.com/Jguer/yay/v10/pkg/db"
  12. "github.com/Jguer/yay/v10/pkg/dep"
  13. "github.com/Jguer/yay/v10/pkg/multierror"
  14. "github.com/Jguer/yay/v10/pkg/query"
  15. "github.com/Jguer/yay/v10/pkg/text"
  16. )
  17. const gitDiffRefName = "AUR_SEEN"
  18. // Update the YAY_DIFF_REVIEW ref to HEAD. We use this ref to determine which diff were
  19. // reviewed by the user
  20. func gitUpdateSeenRef(path, name string) error {
  21. _, stderr, err := config.Runtime.CmdRunner.Capture(
  22. config.Runtime.CmdBuilder.BuildGitCmd(
  23. filepath.Join(path, name), "update-ref", gitDiffRefName, "HEAD"), 0)
  24. if err != nil {
  25. return fmt.Errorf("%s %s", stderr, err)
  26. }
  27. return nil
  28. }
  29. // Return wether or not we have reviewed a diff yet. It checks for the existence of
  30. // YAY_DIFF_REVIEW in the git ref-list
  31. func gitHasLastSeenRef(path, name string) bool {
  32. _, _, err := config.Runtime.CmdRunner.Capture(
  33. config.Runtime.CmdBuilder.BuildGitCmd(
  34. filepath.Join(path, name), "rev-parse", "--quiet", "--verify", gitDiffRefName), 0)
  35. return err == nil
  36. }
  37. // Returns the last reviewed hash. If YAY_DIFF_REVIEW exists it will return this hash.
  38. // If it does not it will return empty tree as no diff have been reviewed yet.
  39. func getLastSeenHash(path, name string) (string, error) {
  40. if gitHasLastSeenRef(path, name) {
  41. stdout, stderr, err := config.Runtime.CmdRunner.Capture(
  42. config.Runtime.CmdBuilder.BuildGitCmd(
  43. filepath.Join(path, name), "rev-parse", gitDiffRefName), 0)
  44. if err != nil {
  45. return "", fmt.Errorf("%s %s", stderr, err)
  46. }
  47. lines := strings.Split(stdout, "\n")
  48. return lines[0], nil
  49. }
  50. return gitEmptyTree, nil
  51. }
  52. // Check whether or not a diff exists between the last reviewed diff and
  53. // HEAD@{upstream}
  54. func gitHasDiff(path, name string) (bool, error) {
  55. if gitHasLastSeenRef(path, name) {
  56. stdout, stderr, err := config.Runtime.CmdRunner.Capture(
  57. config.Runtime.CmdBuilder.BuildGitCmd(filepath.Join(path, name), "rev-parse", gitDiffRefName, "HEAD@{upstream}"), 0)
  58. if err != nil {
  59. return false, fmt.Errorf("%s%s", stderr, err)
  60. }
  61. lines := strings.Split(stdout, "\n")
  62. lastseen := lines[0]
  63. upstream := lines[1]
  64. return lastseen != upstream, nil
  65. }
  66. // If YAY_DIFF_REVIEW does not exists, we have never reviewed a diff for this package
  67. // and should display it.
  68. return true, nil
  69. }
  70. // TODO: yay-next passes args through the header, use that to unify ABS and AUR
  71. func gitDownloadABS(url, path, name string) (bool, error) {
  72. if err := os.MkdirAll(path, 0o755); err != nil {
  73. return false, err
  74. }
  75. if _, errExist := os.Stat(filepath.Join(path, name)); os.IsNotExist(errExist) {
  76. cmd := config.Runtime.CmdBuilder.BuildGitCmd(path, "clone", "--no-progress", "--single-branch",
  77. "-b", "packages/"+name, url, name)
  78. _, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
  79. if err != nil {
  80. return false, fmt.Errorf(gotext.Get("error cloning %s: %s", name, stderr))
  81. }
  82. return true, nil
  83. } else if errExist != nil {
  84. return false, fmt.Errorf(gotext.Get("error reading %s", filepath.Join(path, name, ".git")))
  85. }
  86. cmd := config.Runtime.CmdBuilder.BuildGitCmd(filepath.Join(path, name), "pull", "--ff-only")
  87. _, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
  88. if err != nil {
  89. return false, fmt.Errorf(gotext.Get("error fetching %s: %s", name, stderr))
  90. }
  91. return true, nil
  92. }
  93. func gitDownload(url, path, name string) (bool, error) {
  94. _, err := os.Stat(filepath.Join(path, name, ".git"))
  95. if os.IsNotExist(err) {
  96. cmd := config.Runtime.CmdBuilder.BuildGitCmd(path, "clone", "--no-progress", url, name)
  97. _, stderr, errCapture := config.Runtime.CmdRunner.Capture(cmd, 0)
  98. if errCapture != nil {
  99. return false, fmt.Errorf(gotext.Get("error cloning %s: %s", name, stderr))
  100. }
  101. return true, nil
  102. } else if err != nil {
  103. return false, fmt.Errorf(gotext.Get("error reading %s", filepath.Join(path, name, ".git")))
  104. }
  105. cmd := config.Runtime.CmdBuilder.BuildGitCmd(filepath.Join(path, name), "fetch")
  106. _, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
  107. if err != nil {
  108. return false, fmt.Errorf(gotext.Get("error fetching %s: %s", name, stderr))
  109. }
  110. return false, nil
  111. }
  112. func gitMerge(path, name string) error {
  113. _, stderr, err := config.Runtime.CmdRunner.Capture(
  114. config.Runtime.CmdBuilder.BuildGitCmd(
  115. filepath.Join(path, name), "reset", "--hard", "HEAD"), 0)
  116. if err != nil {
  117. return fmt.Errorf(gotext.Get("error resetting %s: %s", name, stderr))
  118. }
  119. _, stderr, err = config.Runtime.CmdRunner.Capture(
  120. config.Runtime.CmdBuilder.BuildGitCmd(
  121. filepath.Join(path, name), "merge", "--no-edit", "--ff"), 0)
  122. if err != nil {
  123. return fmt.Errorf(gotext.Get("error merging %s: %s", name, stderr))
  124. }
  125. return nil
  126. }
  127. func getPkgbuilds(pkgs []string, dbExecutor db.Executor, force bool) error {
  128. missing := false
  129. wd, err := os.Getwd()
  130. if err != nil {
  131. return err
  132. }
  133. pkgs = query.RemoveInvalidTargets(pkgs, config.Runtime.Mode)
  134. aur, repo := packageSlices(pkgs, dbExecutor)
  135. for n := range aur {
  136. _, pkg := text.SplitDBFromName(aur[n])
  137. aur[n] = pkg
  138. }
  139. info, err := query.AURInfoPrint(aur, config.RequestSplitN)
  140. if err != nil {
  141. return err
  142. }
  143. if len(repo) > 0 {
  144. missing, err = getPkgbuildsfromABS(repo, wd, dbExecutor, force)
  145. if err != nil {
  146. return err
  147. }
  148. }
  149. if len(aur) > 0 {
  150. allBases := dep.GetBases(info)
  151. bases := make([]dep.Base, 0)
  152. for _, base := range allBases {
  153. name := base.Pkgbase()
  154. pkgDest := filepath.Join(wd, name)
  155. _, err = os.Stat(pkgDest)
  156. if os.IsNotExist(err) {
  157. bases = append(bases, base)
  158. } else if err != nil {
  159. text.Errorln(err)
  160. continue
  161. } else {
  162. if force {
  163. if err = os.RemoveAll(pkgDest); err != nil {
  164. text.Errorln(err)
  165. continue
  166. }
  167. bases = append(bases, base)
  168. } else {
  169. text.Warnln(gotext.Get("%s already exists. Use -f/--force to overwrite", pkgDest))
  170. continue
  171. }
  172. }
  173. }
  174. if _, err = downloadPkgbuilds(bases, nil, wd); err != nil {
  175. return err
  176. }
  177. missing = missing || len(aur) != len(info)
  178. }
  179. if missing {
  180. err = fmt.Errorf("")
  181. }
  182. return err
  183. }
  184. // GetPkgbuild downloads pkgbuild from the ABS.
  185. func getPkgbuildsfromABS(pkgs []string, path string, dbExecutor db.Executor, force bool) (bool, error) {
  186. var wg sync.WaitGroup
  187. var mux sync.Mutex
  188. var errs multierror.MultiError
  189. names := make(map[string]string)
  190. missing := make([]string, 0)
  191. downloaded := 0
  192. for _, pkgN := range pkgs {
  193. var pkg db.RepoPackage
  194. var err error
  195. var url string
  196. pkgDB, name := text.SplitDBFromName(pkgN)
  197. if pkgDB != "" {
  198. pkg = dbExecutor.SatisfierFromDB(name, pkgDB)
  199. } else {
  200. pkg = dbExecutor.SyncSatisfier(name)
  201. }
  202. if pkg == nil {
  203. missing = append(missing, name)
  204. continue
  205. }
  206. name = pkg.Base()
  207. if name == "" {
  208. name = pkg.Name()
  209. }
  210. // TODO: Check existence with ls-remote
  211. // https://git.archlinux.org/svntogit/packages.git
  212. switch pkg.DB().Name() {
  213. case "core", "extra", "testing":
  214. url = "https://git.archlinux.org/svntogit/packages.git"
  215. case "community", "multilib", "community-testing", "multilib-testing":
  216. url = "https://git.archlinux.org/svntogit/community.git"
  217. default:
  218. missing = append(missing, name)
  219. continue
  220. }
  221. _, err = os.Stat(filepath.Join(path, name))
  222. switch {
  223. case err != nil && !os.IsNotExist(err):
  224. text.Errorln(err)
  225. continue
  226. case os.IsNotExist(err), force:
  227. if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
  228. text.Errorln(err)
  229. continue
  230. }
  231. default:
  232. text.Warn(gotext.Get("%s already downloaded -- use -f to overwrite", text.Cyan(name)))
  233. continue
  234. }
  235. names[name] = url
  236. }
  237. if len(missing) != 0 {
  238. text.Warnln(gotext.Get("Missing ABS packages:"),
  239. text.Cyan(strings.Join(missing, ", ")))
  240. }
  241. download := func(pkg string, url string) {
  242. defer wg.Done()
  243. if _, err := gitDownloadABS(url, config.ABSDir, pkg); err != nil {
  244. errs.Add(errors.New(gotext.Get("failed to get pkgbuild: %s: %s", text.Cyan(pkg), err.Error())))
  245. return
  246. }
  247. _, stderr, err := config.Runtime.CmdRunner.Capture(
  248. exec.Command(
  249. "cp", "-r",
  250. filepath.Join(config.ABSDir, pkg, "trunk"),
  251. filepath.Join(path, pkg)), 0)
  252. mux.Lock()
  253. downloaded++
  254. if err != nil {
  255. errs.Add(errors.New(gotext.Get("failed to link %s: %s", text.Cyan(pkg), stderr)))
  256. } else {
  257. fmt.Fprintln(os.Stdout, gotext.Get("(%d/%d) Downloaded PKGBUILD from ABS: %s", downloaded, len(names), text.Cyan(pkg)))
  258. }
  259. mux.Unlock()
  260. }
  261. count := 0
  262. for name, url := range names {
  263. wg.Add(1)
  264. go download(name, url)
  265. count++
  266. if count%25 == 0 {
  267. wg.Wait()
  268. }
  269. }
  270. wg.Wait()
  271. return len(missing) != 0, errs.Return()
  272. }