vcs.go 5.2 KB

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