vcs.go 4.9 KB

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