vcs.go 4.6 KB

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