vcs.go 4.6 KB

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