vcs.go 5.0 KB

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