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