vcs.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. 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. 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. toSkip := pkgBuildsToSkip(info, sliceToStringSet(remoteNames))
  38. downloadPkgBuilds(info, bases, toSkip)
  39. tryParsesrcinfosFile(info, srcinfosStale, bases)
  40. for _, pkg := range info {
  41. pkgbuild, ok := srcinfosStale[pkg.PackageBase]
  42. if !ok {
  43. continue
  44. }
  45. for _, pkg := range bases[pkg.PackageBase] {
  46. updateVCSData(pkg.Name, pkgbuild.Source)
  47. }
  48. }
  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. if !(strings.Contains(source, "git://") ||
  55. strings.Contains(source, ".git") ||
  56. strings.Contains(source, "git+https://")) {
  57. return "", "", nil
  58. }
  59. split := strings.Split(source, "::")
  60. source = split[len(split)-1]
  61. split = strings.SplitN(source, "://", 2)
  62. if len(split) != 2 {
  63. return "", "", nil
  64. }
  65. protocols = strings.Split(split[0], "+")
  66. split = strings.SplitN(split[1], "#", 2)
  67. if len(split) == 2 {
  68. secondSplit := strings.SplitN(split[1], "=", 2)
  69. if secondSplit[0] != "branch" {
  70. //source has #commit= or #tag= which makes them not vcs
  71. //packages because they reference a specific point
  72. return "", "", nil
  73. }
  74. if len(secondSplit) == 2 {
  75. url = split[0]
  76. branch = secondSplit[1]
  77. }
  78. } else {
  79. url = split[0]
  80. branch = "HEAD"
  81. }
  82. url = strings.Split(url, "?")[0]
  83. branch = strings.Split(branch, "?")[0]
  84. return
  85. }
  86. func updateVCSData(pkgName string, sources []string) {
  87. if savedInfo == nil {
  88. savedInfo = make(vcsInfo)
  89. }
  90. info := make(shaInfos)
  91. for _, source := range sources {
  92. url, branch, protocols := parseSource(source)
  93. if url == "" || branch == "" {
  94. continue
  95. }
  96. commit := getCommit(url, branch, protocols)
  97. if commit == "" {
  98. continue
  99. }
  100. info[url] = shaInfo{
  101. protocols,
  102. branch,
  103. commit,
  104. }
  105. savedInfo[pkgName] = info
  106. fmt.Println(bold(yellow(arrow)) + " Found git repo: " + cyan(url))
  107. saveVCSInfo()
  108. }
  109. }
  110. func getCommit(url string, branch string, protocols []string) string {
  111. for _, protocol := range protocols {
  112. var outbuf bytes.Buffer
  113. cmd := exec.Command(config.GitBin, "ls-remote", protocol+"://"+url, branch)
  114. cmd.Stdout = &outbuf
  115. cmd.Env = append(cmd.Env, "GIT_TERMINAL_PROMPT=0")
  116. err := cmd.Start()
  117. if err != nil {
  118. continue
  119. }
  120. //for some reason
  121. //git://bitbucket.org/volumesoffun/polyvox.git` hangs on my
  122. //machine but using http:// instead of git does not hang.
  123. //Introduce a time out so this can not hang
  124. timer := time.AfterFunc(5*time.Second, func() {
  125. cmd.Process.Kill()
  126. })
  127. err = cmd.Wait()
  128. timer.Stop()
  129. if err != nil {
  130. continue
  131. }
  132. stdout := outbuf.String()
  133. split := strings.Fields(stdout)
  134. if len(split) < 2 {
  135. continue
  136. }
  137. commit := split[0]
  138. return commit
  139. }
  140. return ""
  141. }
  142. func (infos shaInfos) needsUpdate() bool {
  143. //used to signal we have gone through all sources and found nothing
  144. finished := make(chan struct{})
  145. alive := 0
  146. //if we find an update we use this to exit early and return true
  147. hasUpdate := make(chan struct{})
  148. checkHash := func(url string, info shaInfo) {
  149. hash := getCommit(url, info.Branch, info.Protocols)
  150. if hash != "" && hash != info.SHA {
  151. hasUpdate <- struct{}{}
  152. } else {
  153. finished <- struct{}{}
  154. }
  155. }
  156. for url, info := range infos {
  157. alive++
  158. go checkHash(url, info)
  159. }
  160. for {
  161. select {
  162. case <-hasUpdate:
  163. return true
  164. case <-finished:
  165. alive--
  166. if alive == 0 {
  167. return false
  168. }
  169. }
  170. }
  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. }