vcs.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "sync"
  9. "time"
  10. gosrc "github.com/Morganamilo/go-srcinfo"
  11. rpc "github.com/mikkeloscar/aur"
  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. var mux sync.Mutex
  24. var wg sync.WaitGroup
  25. infoMap := make(map[string]*rpc.Pkg)
  26. srcinfosStale := make(map[string]*gosrc.Srcinfo)
  27. _, _, _, remoteNames, err := filterPackages()
  28. if err != nil {
  29. return err
  30. }
  31. info, err := aurInfoPrint(remoteNames)
  32. if err != nil {
  33. return err
  34. }
  35. for _, pkg := range info {
  36. infoMap[pkg.Name] = pkg
  37. }
  38. bases := getBases(infoMap)
  39. toSkip := pkgBuildsToSkip(info, sliceToStringSet(remoteNames))
  40. downloadPkgBuilds(info, bases, toSkip)
  41. tryParsesrcinfosFile(info, srcinfosStale, bases)
  42. for _, pkg := range info {
  43. pkgbuild, ok := srcinfosStale[pkg.PackageBase]
  44. if !ok {
  45. continue
  46. }
  47. for _, pkg := range bases[pkg.PackageBase] {
  48. wg.Add(1)
  49. go updateVCSData(pkg.Name, pkgbuild.Source, &mux, &wg)
  50. }
  51. }
  52. wg.Wait()
  53. fmt.Println(bold(yellow(arrow) + bold(" GenDB finished. No packages were installed")))
  54. return err
  55. }
  56. // parseSource returns the git url, default branch and protocols it supports
  57. func parseSource(source string) (url string, branch string, protocols []string) {
  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. git := false
  66. for _, protocol := range protocols {
  67. if protocol == "git" {
  68. git = true
  69. break
  70. }
  71. }
  72. if !git {
  73. return "", "", nil
  74. }
  75. split = strings.SplitN(split[1], "#", 2)
  76. if len(split) == 2 {
  77. secondSplit := strings.SplitN(split[1], "=", 2)
  78. if secondSplit[0] != "branch" {
  79. //source has #commit= or #tag= which makes them not vcs
  80. //packages because they reference a specific point
  81. return "", "", nil
  82. }
  83. if len(secondSplit) == 2 {
  84. url = split[0]
  85. branch = secondSplit[1]
  86. }
  87. } else {
  88. url = split[0]
  89. branch = "HEAD"
  90. }
  91. url = strings.Split(url, "?")[0]
  92. branch = strings.Split(branch, "?")[0]
  93. return
  94. }
  95. func updateVCSData(pkgName string, sources []gosrc.ArchString, mux *sync.Mutex, wg *sync.WaitGroup) {
  96. defer wg.Done()
  97. if savedInfo == nil {
  98. mux.Lock()
  99. savedInfo = make(vcsInfo)
  100. mux.Unlock()
  101. }
  102. info := make(shaInfos)
  103. checkSource := func(source gosrc.ArchString) {
  104. defer wg.Done()
  105. url, branch, protocols := parseSource(source.Value)
  106. if url == "" || branch == "" {
  107. return
  108. }
  109. commit := getCommit(url, branch, protocols)
  110. if commit == "" {
  111. return
  112. }
  113. info[url] = shaInfo{
  114. protocols,
  115. branch,
  116. commit,
  117. }
  118. mux.Lock()
  119. savedInfo[pkgName] = info
  120. fmt.Println(bold(yellow(arrow)) + " Found git repo: " + cyan(url))
  121. saveVCSInfo()
  122. mux.Unlock()
  123. }
  124. for _, source := range sources {
  125. wg.Add(1)
  126. go checkSource(source)
  127. }
  128. }
  129. func getCommit(url string, branch string, protocols []string) string {
  130. for _, protocol := range protocols {
  131. var outbuf bytes.Buffer
  132. cmd := passToGit("", "ls-remote", protocol+"://"+url, branch)
  133. cmd.Stdout = &outbuf
  134. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  135. err := cmd.Start()
  136. if err != nil {
  137. continue
  138. }
  139. //for some reason
  140. //git://bitbucket.org/volumesoffun/polyvox.git` hangs on my
  141. //machine but using http:// instead of git does not hang.
  142. //Introduce a time out so this can not hang
  143. timer := time.AfterFunc(5*time.Second, func() {
  144. cmd.Process.Kill()
  145. })
  146. err = cmd.Wait()
  147. timer.Stop()
  148. if err != nil {
  149. continue
  150. }
  151. stdout := outbuf.String()
  152. split := strings.Fields(stdout)
  153. if len(split) < 2 {
  154. continue
  155. }
  156. commit := split[0]
  157. return commit
  158. }
  159. return ""
  160. }
  161. func (infos shaInfos) needsUpdate() bool {
  162. //used to signal we have gone through all sources and found nothing
  163. finished := make(chan struct{})
  164. alive := 0
  165. //if we find an update we use this to exit early and return true
  166. hasUpdate := make(chan struct{})
  167. checkHash := func(url string, info shaInfo) {
  168. hash := getCommit(url, info.Branch, info.Protocols)
  169. if hash != "" && hash != info.SHA {
  170. hasUpdate <- struct{}{}
  171. } else {
  172. finished <- struct{}{}
  173. }
  174. }
  175. for url, info := range infos {
  176. alive++
  177. go checkHash(url, info)
  178. }
  179. for {
  180. select {
  181. case <-hasUpdate:
  182. return true
  183. case <-finished:
  184. alive--
  185. if alive == 0 {
  186. return false
  187. }
  188. }
  189. }
  190. }
  191. func saveVCSInfo() error {
  192. marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
  193. if err != nil || string(marshalledinfo) == "null" {
  194. return err
  195. }
  196. in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  197. if err != nil {
  198. return err
  199. }
  200. defer in.Close()
  201. _, err = in.Write(marshalledinfo)
  202. if err != nil {
  203. return err
  204. }
  205. err = in.Sync()
  206. return err
  207. }