vcs.go 5.3 KB

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