vcs.go 4.8 KB

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