download.go 7.9 KB

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