vcs.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.SplitN(split[0], "+", 2)
  55. git := false
  56. for _, protocol := range protocols {
  57. if protocol == "git" {
  58. git = true
  59. break
  60. }
  61. }
  62. protocols = protocols[len(protocols)-1:]
  63. if !git {
  64. return "", "", nil
  65. }
  66. split = strings.SplitN(split[1], "#", 2)
  67. if len(split) == 2 {
  68. secondSplit := strings.SplitN(split[1], "=", 2)
  69. if secondSplit[0] != "branch" {
  70. //source has #commit= or #tag= which makes them not vcs
  71. //packages because they reference a specific point
  72. return "", "", nil
  73. }
  74. if len(secondSplit) == 2 {
  75. url = split[0]
  76. branch = secondSplit[1]
  77. }
  78. } else {
  79. url = split[0]
  80. branch = "HEAD"
  81. }
  82. url = strings.Split(url, "?")[0]
  83. branch = strings.Split(branch, "?")[0]
  84. return
  85. }
  86. func updateVCSData(pkgName string, sources []gosrc.ArchString, mux *sync.Mutex, wg *sync.WaitGroup) {
  87. defer wg.Done()
  88. if savedInfo == nil {
  89. mux.Lock()
  90. savedInfo = make(vcsInfo)
  91. mux.Unlock()
  92. }
  93. info := make(shaInfos)
  94. checkSource := func(source gosrc.ArchString) {
  95. defer wg.Done()
  96. url, branch, protocols := parseSource(source.Value)
  97. if url == "" || branch == "" {
  98. return
  99. }
  100. commit := getCommit(url, branch, protocols)
  101. if commit == "" {
  102. return
  103. }
  104. mux.Lock()
  105. info[url] = shaInfo{
  106. protocols,
  107. branch,
  108. commit,
  109. }
  110. savedInfo[pkgName] = info
  111. fmt.Println(bold(yellow(arrow)) + " Found git repo: " + cyan(url))
  112. saveVCSInfo()
  113. mux.Unlock()
  114. }
  115. for _, source := range sources {
  116. wg.Add(1)
  117. go checkSource(source)
  118. }
  119. }
  120. func getCommit(url string, branch string, protocols []string) string {
  121. if len(protocols) > 0 {
  122. protocol := protocols[len(protocols)-1]
  123. var outbuf bytes.Buffer
  124. cmd := passToGit("", "ls-remote", protocol+"://"+url, branch)
  125. cmd.Stdout = &outbuf
  126. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  127. err := cmd.Start()
  128. if err != nil {
  129. return ""
  130. }
  131. //for some reason
  132. //git://bitbucket.org/volumesoffun/polyvox.git` hangs on my
  133. //machine but using http:// instead of git does not hang.
  134. //Introduce a time out so this can not hang
  135. timer := time.AfterFunc(5*time.Second, func() {
  136. cmd.Process.Kill()
  137. })
  138. err = cmd.Wait()
  139. timer.Stop()
  140. if err != nil {
  141. return ""
  142. }
  143. stdout := outbuf.String()
  144. split := strings.Fields(stdout)
  145. if len(split) < 2 {
  146. return ""
  147. }
  148. commit := split[0]
  149. return commit
  150. }
  151. return ""
  152. }
  153. func (infos shaInfos) needsUpdate() bool {
  154. //used to signal we have gone through all sources and found nothing
  155. finished := make(chan struct{})
  156. alive := 0
  157. //if we find an update we use this to exit early and return true
  158. hasUpdate := make(chan struct{})
  159. checkHash := func(url string, info shaInfo) {
  160. hash := getCommit(url, info.Branch, info.Protocols)
  161. if hash != "" && hash != info.SHA {
  162. hasUpdate <- struct{}{}
  163. } else {
  164. finished <- struct{}{}
  165. }
  166. }
  167. for url, info := range infos {
  168. alive++
  169. go checkHash(url, info)
  170. }
  171. for {
  172. select {
  173. case <-hasUpdate:
  174. return true
  175. case <-finished:
  176. alive--
  177. if alive == 0 {
  178. return false
  179. }
  180. }
  181. }
  182. }
  183. func saveVCSInfo() error {
  184. marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
  185. if err != nil || string(marshalledinfo) == "null" {
  186. return err
  187. }
  188. in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  189. if err != nil {
  190. return err
  191. }
  192. defer in.Close()
  193. _, err = in.Write(marshalledinfo)
  194. if err != nil {
  195. return err
  196. }
  197. err = in.Sync()
  198. return err
  199. }