vcs.go 5.0 KB

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