vcs.go 4.8 KB

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