vcs.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 := aurInfoPrint(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. tryParsesrcinfosFile(info, srcinfosStale, bases)
  39. for _, pkg := range info {
  40. pkgbuild, ok := srcinfosStale[pkg.PackageBase]
  41. if !ok {
  42. continue
  43. }
  44. for _, pkg := range bases[pkg.PackageBase] {
  45. updateVCSData(pkg.Name, pkgbuild.Source)
  46. }
  47. }
  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. if !(strings.Contains(source, "git://") ||
  54. strings.Contains(source, ".git") ||
  55. strings.Contains(source, "git+https://")) {
  56. return "", "", nil
  57. }
  58. split := strings.Split(source, "::")
  59. source = split[len(split)-1]
  60. split = strings.SplitN(source, "://", 2)
  61. if len(split) != 2 {
  62. return "", "", nil
  63. }
  64. protocols = strings.Split(split[0], "+")
  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. return
  82. }
  83. func updateVCSData(pkgName string, sources []string) {
  84. if savedInfo == nil {
  85. savedInfo = make(vcsInfo)
  86. }
  87. info := make(shaInfos)
  88. for _, source := range sources {
  89. url, branch, protocols := parseSource(source)
  90. if url == "" || branch == "" {
  91. continue
  92. }
  93. commit := getCommit(url, branch, protocols)
  94. if commit == "" {
  95. continue
  96. }
  97. info[url] = shaInfo{
  98. protocols,
  99. branch,
  100. commit,
  101. }
  102. savedInfo[pkgName] = info
  103. fmt.Println(bold(yellow(arrow)) + " Found git repo: " + cyan(url))
  104. saveVCSInfo()
  105. }
  106. }
  107. func getCommit(url string, branch string, protocols []string) string {
  108. for _, protocol := range protocols {
  109. var outbuf bytes.Buffer
  110. cmd := exec.Command(config.GitBin, "ls-remote", protocol+"://"+url, branch)
  111. cmd.Stdout = &outbuf
  112. err := cmd.Start()
  113. if err != nil {
  114. continue
  115. }
  116. //for some reason
  117. //git://bitbucket.org/volumesoffun/polyvox.git` hangs on my
  118. //machine but using http:// instead of git does not hang.
  119. //Introduce a time out so this can not hang
  120. timer := time.AfterFunc(5*time.Second, func() {
  121. cmd.Process.Kill()
  122. })
  123. err = cmd.Wait()
  124. timer.Stop()
  125. if err != nil {
  126. continue
  127. }
  128. err = cmd.Run()
  129. stdout := outbuf.String()
  130. split := strings.Fields(stdout)
  131. if len(split) < 2 {
  132. continue
  133. }
  134. commit := split[0]
  135. return commit
  136. }
  137. return ""
  138. }
  139. func (infos shaInfos) needsUpdate() bool {
  140. //used to signal we have gone through all sources and found nothing
  141. finished := make(chan struct{})
  142. alive := 0
  143. //if we find an update we use this to exit early and return true
  144. hasUpdate := make(chan struct{})
  145. checkHash := func(url string, info shaInfo) {
  146. hash := getCommit(url, info.Brach, info.Protocols)
  147. if hash != "" && hash != info.SHA {
  148. hasUpdate <- struct{}{}
  149. } else {
  150. finished <- struct{}{}
  151. }
  152. }
  153. for url, info := range infos {
  154. alive++
  155. go checkHash(url, info)
  156. }
  157. for {
  158. select {
  159. case <-hasUpdate:
  160. return true
  161. case <-finished:
  162. alive--
  163. if alive == 0 {
  164. return false
  165. }
  166. }
  167. }
  168. }
  169. func inStore(pkgName string) shaInfos {
  170. return savedInfo[pkgName]
  171. }
  172. func saveVCSInfo() error {
  173. marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
  174. if err != nil || string(marshalledinfo) == "null" {
  175. return err
  176. }
  177. in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  178. if err != nil {
  179. return err
  180. }
  181. defer in.Close()
  182. _, err = in.Write(marshalledinfo)
  183. if err != nil {
  184. return err
  185. }
  186. err = in.Sync()
  187. return err
  188. }