vcs.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "time"
  9. gosrc "github.com/Morganamilo/go-srcinfo"
  10. rpc "github.com/mikkeloscar/aur"
  11. )
  12. // Info contains the last commit sha of a repo
  13. type vcsInfo map[string]shaInfos
  14. type shaInfos map[string]shaInfo
  15. type shaInfo struct {
  16. Protocols []string `json:"protocols"`
  17. Branch string `json:"branch"`
  18. SHA string `json:"sha"`
  19. }
  20. // createDevelDB forces yay to create a DB of the existing development packages
  21. func createDevelDB() error {
  22. infoMap := make(map[string]*rpc.Pkg)
  23. srcinfosStale := make(map[string]*gosrc.Srcinfo)
  24. _, _, _, remoteNames, err := filterPackages()
  25. if err != nil {
  26. return err
  27. }
  28. info, err := aurInfoPrint(remoteNames)
  29. if err != nil {
  30. return err
  31. }
  32. for _, pkg := range info {
  33. infoMap[pkg.Name] = pkg
  34. }
  35. bases := getBases(infoMap)
  36. toSkip := pkgBuildsToSkip(info, sliceToStringSet(remoteNames))
  37. downloadPkgBuilds(info, bases, toSkip)
  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. url = strings.Split(url, "?")[0]
  82. branch = strings.Split(branch, "?")[0]
  83. return
  84. }
  85. func updateVCSData(pkgName string, sources []gosrc.ArchString) {
  86. if savedInfo == nil {
  87. savedInfo = make(vcsInfo)
  88. }
  89. info := make(shaInfos)
  90. for _, source := range sources {
  91. url, branch, protocols := parseSource(source.Value)
  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 := passToGit("", "ls-remote", protocol+"://"+url, branch)
  113. cmd.Stdout = &outbuf
  114. cmd.Env = append(os.Environ(), "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. }