vcs.go 4.5 KB

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