vcs.go 5.3 KB

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