download.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 string, 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 string, 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 string, 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 string, 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 string, path string, name string) (bool, error) {
  60. err := os.MkdirAll(path, 0755)
  61. if err != nil {
  62. return false, err
  63. }
  64. _, err = os.Stat(filepath.Join(path, name))
  65. if os.IsNotExist(err) {
  66. cmd := passToGit(path, "clone", "--no-progress", "--single-branch",
  67. "-b", "packages/"+name, url, name)
  68. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  69. _, stderr, err := capture(cmd)
  70. if err != nil {
  71. return false, fmt.Errorf("error cloning %s: %s", name, stderr)
  72. }
  73. return true, nil
  74. } else if err != nil {
  75. return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
  76. }
  77. cmd := passToGit(filepath.Join(path, name), "pull", "--ff-only")
  78. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  79. _, stderr, err := capture(cmd)
  80. if err != nil {
  81. return false, fmt.Errorf("error fetching %s: %s", name, stderr)
  82. }
  83. return true, nil
  84. }
  85. func gitDownload(url string, path string, name string) (bool, error) {
  86. _, err := os.Stat(filepath.Join(path, name, ".git"))
  87. if os.IsNotExist(err) {
  88. cmd := passToGit(path, "clone", "--no-progress", url, name)
  89. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  90. _, stderr, err := capture(cmd)
  91. if err != nil {
  92. return false, fmt.Errorf("error cloning %s: %s", name, stderr)
  93. }
  94. return true, nil
  95. } else if err != nil {
  96. return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
  97. }
  98. cmd := passToGit(filepath.Join(path, name), "fetch")
  99. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  100. _, stderr, err := capture(cmd)
  101. if err != nil {
  102. return false, fmt.Errorf("error fetching %s: %s", name, stderr)
  103. }
  104. return false, nil
  105. }
  106. func gitMerge(path string, name string) error {
  107. _, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
  108. if err != nil {
  109. return fmt.Errorf("error resetting %s: %s", name, stderr)
  110. }
  111. _, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
  112. if err != nil {
  113. return fmt.Errorf("error merging %s: %s", name, stderr)
  114. }
  115. return nil
  116. }
  117. func getPkgbuilds(pkgs []string) error {
  118. missing := false
  119. wd, err := os.Getwd()
  120. if err != nil {
  121. return err
  122. }
  123. pkgs = removeInvalidTargets(pkgs)
  124. aur, repo, err := packageSlices(pkgs)
  125. if err != nil {
  126. return err
  127. }
  128. for n := range aur {
  129. _, pkg := splitDBFromName(aur[n])
  130. aur[n] = pkg
  131. }
  132. info, err := aurInfoPrint(aur)
  133. if err != nil {
  134. return err
  135. }
  136. if len(repo) > 0 {
  137. missing, err = getPkgbuildsfromABS(repo, wd)
  138. if err != nil {
  139. return err
  140. }
  141. }
  142. if len(aur) > 0 {
  143. allBases := getBases(info)
  144. bases := make([]Base, 0)
  145. for _, base := range allBases {
  146. name := base.Pkgbase()
  147. _, err = os.Stat(filepath.Join(wd, name))
  148. switch {
  149. case err != nil && !os.IsNotExist(err):
  150. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  151. continue
  152. default:
  153. if err = os.RemoveAll(filepath.Join(wd, name)); err != nil {
  154. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  155. continue
  156. }
  157. }
  158. bases = append(bases, base)
  159. }
  160. if _, err = downloadPkgbuilds(bases, nil, wd); err != nil {
  161. return err
  162. }
  163. missing = missing || len(aur) != len(info)
  164. }
  165. if missing {
  166. err = fmt.Errorf("")
  167. }
  168. return err
  169. }
  170. // GetPkgbuild downloads pkgbuild from the ABS.
  171. func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
  172. var wg sync.WaitGroup
  173. var mux sync.Mutex
  174. var errs multierror.MultiError
  175. names := make(map[string]string)
  176. missing := make([]string, 0)
  177. downloaded := 0
  178. dbList, err := alpmHandle.SyncDBs()
  179. if err != nil {
  180. return false, err
  181. }
  182. for _, pkgN := range pkgs {
  183. var pkg *alpm.Package
  184. var err error
  185. var url string
  186. pkgDB, name := splitDBFromName(pkgN)
  187. if pkgDB != "" {
  188. if db, err := alpmHandle.SyncDBByName(pkgDB); err == nil {
  189. pkg = db.Pkg(name)
  190. }
  191. } else {
  192. _ = dbList.ForEach(func(db alpm.DB) error {
  193. if pkg = db.Pkg(name); pkg != nil {
  194. return fmt.Errorf("")
  195. }
  196. return nil
  197. })
  198. }
  199. if pkg == nil {
  200. missing = append(missing, name)
  201. continue
  202. }
  203. name = pkg.Base()
  204. if name == "" {
  205. name = pkg.Name()
  206. }
  207. // TODO: Check existence with ls-remote
  208. // https://git.archlinux.org/svntogit/packages.git
  209. switch pkg.DB().Name() {
  210. case "core", "extra", "testing":
  211. url = "https://git.archlinux.org/svntogit/packages.git"
  212. case "community", "multilib", "community-testing", "multilib-testing":
  213. url = "https://git.archlinux.org/svntogit/community.git"
  214. default:
  215. missing = append(missing, name)
  216. continue
  217. }
  218. _, err = os.Stat(filepath.Join(path, name))
  219. switch {
  220. case err != nil && !os.IsNotExist(err):
  221. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  222. continue
  223. case os.IsNotExist(err), cmdArgs.existsArg("f", "force"):
  224. if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
  225. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  226. continue
  227. }
  228. default:
  229. fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
  230. continue
  231. }
  232. names[name] = url
  233. }
  234. if len(missing) != 0 {
  235. fmt.Println(yellow(bold(smallArrow)), "Missing ABS packages: ", cyan(strings.Join(missing, " ")))
  236. }
  237. download := func(pkg string, url string) {
  238. defer wg.Done()
  239. if _, err := gitDownloadABS(url, config.ABSDir, pkg); err != nil {
  240. errs.Add(fmt.Errorf("%s Failed to get pkgbuild: %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(err.Error()))))
  241. return
  242. }
  243. _, stderr, err := capture(exec.Command("cp", "-r", filepath.Join(config.ABSDir, pkg, "trunk"), filepath.Join(path, pkg)))
  244. mux.Lock()
  245. downloaded++
  246. if err != nil {
  247. errs.Add(fmt.Errorf("%s Failed to link %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(stderr))))
  248. } else {
  249. fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", downloaded, len(names), cyan(pkg))
  250. }
  251. mux.Unlock()
  252. }
  253. count := 0
  254. for name, url := range names {
  255. wg.Add(1)
  256. go download(name, url)
  257. count++
  258. if count%25 == 0 {
  259. wg.Wait()
  260. }
  261. }
  262. wg.Wait()
  263. return len(missing) != 0, errs.Return()
  264. }