vcs.go 4.6 KB

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