vcs.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "sync"
  8. gosrc "github.com/Morganamilo/go-srcinfo"
  9. "github.com/leonelquinteros/gotext"
  10. "github.com/Jguer/yay/v10/pkg/db"
  11. "github.com/Jguer/yay/v10/pkg/dep"
  12. "github.com/Jguer/yay/v10/pkg/query"
  13. "github.com/Jguer/yay/v10/pkg/stringset"
  14. "github.com/Jguer/yay/v10/pkg/text"
  15. )
  16. // Info contains the last commit sha of a repo
  17. type (
  18. vcsInfo map[string]shaInfos
  19. shaInfos map[string]shaInfo
  20. shaInfo struct {
  21. Protocols []string `json:"protocols"`
  22. Branch string `json:"branch"`
  23. SHA string `json:"sha"`
  24. }
  25. )
  26. // createDevelDB forces yay to create a DB of the existing development packages
  27. func createDevelDB(vcsFilePath string, dbExecutor db.Executor) error {
  28. var mux sync.Mutex
  29. var wg sync.WaitGroup
  30. _, remoteNames, err := query.GetPackageNamesBySource(dbExecutor)
  31. if err != nil {
  32. return err
  33. }
  34. info, err := query.AURInfoPrint(remoteNames, config.RequestSplitN)
  35. if err != nil {
  36. return err
  37. }
  38. bases := dep.GetBases(info)
  39. toSkip := pkgbuildsToSkip(bases, stringset.FromSlice(remoteNames))
  40. _, err = downloadPkgbuilds(bases, toSkip, config.BuildDir)
  41. if err != nil {
  42. return err
  43. }
  44. srcinfos, err := parseSrcinfoFiles(bases, false)
  45. if err != nil {
  46. return err
  47. }
  48. for i := range srcinfos {
  49. for iP := range srcinfos[i].Packages {
  50. wg.Add(1)
  51. go updateVCSData(vcsFilePath, srcinfos[i].Packages[iP].Pkgname, srcinfos[i].Source, &mux, &wg)
  52. }
  53. }
  54. wg.Wait()
  55. text.OperationInfoln(gotext.Get("GenDB finished. No packages were installed"))
  56. return err
  57. }
  58. // parseSource returns the git url, default branch and protocols it supports
  59. func parseSource(source string) (url, branch string, protocols []string) {
  60. split := strings.Split(source, "::")
  61. source = split[len(split)-1]
  62. split = strings.SplitN(source, "://", 2)
  63. if len(split) != 2 {
  64. return "", "", nil
  65. }
  66. protocols = strings.SplitN(split[0], "+", 2)
  67. git := false
  68. for _, protocol := range protocols {
  69. if protocol == "git" {
  70. git = true
  71. break
  72. }
  73. }
  74. protocols = protocols[len(protocols)-1:]
  75. if !git {
  76. return "", "", nil
  77. }
  78. split = strings.SplitN(split[1], "#", 2)
  79. if len(split) == 2 {
  80. secondSplit := strings.SplitN(split[1], "=", 2)
  81. if secondSplit[0] != "branch" {
  82. // source has #commit= or #tag= which makes them not vcs
  83. // packages because they reference a specific point
  84. return "", "", nil
  85. }
  86. if len(secondSplit) == 2 {
  87. url = split[0]
  88. branch = secondSplit[1]
  89. }
  90. } else {
  91. url = split[0]
  92. branch = "HEAD"
  93. }
  94. url = strings.Split(url, "?")[0]
  95. branch = strings.Split(branch, "?")[0]
  96. return url, branch, protocols
  97. }
  98. func updateVCSData(vcsFilePath, pkgName string, sources []gosrc.ArchString, mux sync.Locker, wg *sync.WaitGroup) {
  99. defer wg.Done()
  100. if savedInfo == nil {
  101. mux.Lock()
  102. savedInfo = make(vcsInfo)
  103. mux.Unlock()
  104. }
  105. info := make(shaInfos)
  106. checkSource := func(source gosrc.ArchString) {
  107. defer wg.Done()
  108. url, branch, protocols := parseSource(source.Value)
  109. if url == "" || branch == "" {
  110. return
  111. }
  112. commit := getCommit(url, branch, protocols)
  113. if commit == "" {
  114. return
  115. }
  116. mux.Lock()
  117. info[url] = shaInfo{
  118. protocols,
  119. branch,
  120. commit,
  121. }
  122. savedInfo[pkgName] = info
  123. text.Warnln(gotext.Get("Found git repo: %s", text.Cyan(url)))
  124. err := saveVCSInfo(vcsFilePath)
  125. if err != nil {
  126. fmt.Fprintln(os.Stderr, err)
  127. }
  128. mux.Unlock()
  129. }
  130. for _, source := range sources {
  131. wg.Add(1)
  132. go checkSource(source)
  133. }
  134. }
  135. func getCommit(url, branch string, protocols []string) string {
  136. if len(protocols) > 0 {
  137. protocol := protocols[len(protocols)-1]
  138. cmd := passToGit("", "ls-remote", protocol+"://"+url, branch)
  139. stdout, _, err := config.Runtime.CmdRunner.Capture(cmd, 5)
  140. if err != nil {
  141. text.Warnln(err)
  142. return ""
  143. }
  144. split := strings.Fields(stdout)
  145. if len(split) < 2 {
  146. return ""
  147. }
  148. commit := split[0]
  149. return commit
  150. }
  151. return ""
  152. }
  153. func (infos shaInfos) needsUpdate() bool {
  154. // used to signal we have gone through all sources and found nothing
  155. finished := make(chan struct{})
  156. alive := 0
  157. // if we find an update we use this to exit early and return true
  158. hasUpdate := make(chan struct{})
  159. checkHash := func(url string, info shaInfo) {
  160. hash := getCommit(url, info.Branch, info.Protocols)
  161. if hash != "" && hash != info.SHA {
  162. hasUpdate <- struct{}{}
  163. } else {
  164. finished <- struct{}{}
  165. }
  166. }
  167. for url, info := range infos {
  168. alive++
  169. go checkHash(url, info)
  170. }
  171. for {
  172. select {
  173. case <-hasUpdate:
  174. return true
  175. case <-finished:
  176. alive--
  177. if alive == 0 {
  178. return false
  179. }
  180. }
  181. }
  182. }
  183. func saveVCSInfo(vcsFilePath string) error {
  184. marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
  185. if err != nil || string(marshalledinfo) == "null" {
  186. return err
  187. }
  188. in, err := os.OpenFile(vcsFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
  189. if err != nil {
  190. return err
  191. }
  192. defer in.Close()
  193. _, err = in.Write(marshalledinfo)
  194. if err != nil {
  195. return err
  196. }
  197. err = in.Sync()
  198. return err
  199. }