vcs.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. })
  136. err = cmd.Wait()
  137. timer.Stop()
  138. if err != nil {
  139. continue
  140. }
  141. stdout := outbuf.String()
  142. split := strings.Fields(stdout)
  143. if len(split) < 2 {
  144. continue
  145. }
  146. commit := split[0]
  147. return commit
  148. }
  149. return ""
  150. }
  151. func (infos shaInfos) needsUpdate() bool {
  152. //used to signal we have gone through all sources and found nothing
  153. finished := make(chan struct{})
  154. alive := 0
  155. //if we find an update we use this to exit early and return true
  156. hasUpdate := make(chan struct{})
  157. checkHash := func(url string, info shaInfo) {
  158. hash := getCommit(url, info.Branch, info.Protocols)
  159. if hash != "" && hash != info.SHA {
  160. hasUpdate <- struct{}{}
  161. } else {
  162. finished <- struct{}{}
  163. }
  164. }
  165. for url, info := range infos {
  166. alive++
  167. go checkHash(url, info)
  168. }
  169. for {
  170. select {
  171. case <-hasUpdate:
  172. return true
  173. case <-finished:
  174. alive--
  175. if alive == 0 {
  176. return false
  177. }
  178. }
  179. }
  180. }
  181. func saveVCSInfo() error {
  182. marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
  183. if err != nil || string(marshalledinfo) == "null" {
  184. return err
  185. }
  186. in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  187. if err != nil {
  188. return err
  189. }
  190. defer in.Close()
  191. _, err = in.Write(marshalledinfo)
  192. if err != nil {
  193. return err
  194. }
  195. err = in.Sync()
  196. return err
  197. }