vcs.go 4.9 KB

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