vcs.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/leonelquinteros/gotext"
  12. "github.com/Jguer/yay/v10/pkg/stringset"
  13. "github.com/Jguer/yay/v10/pkg/text"
  14. )
  15. // Info contains the last commit sha of a repo
  16. type vcsInfo map[string]shaInfos
  17. type shaInfos map[string]shaInfo
  18. type shaInfo struct {
  19. Protocols []string `json:"protocols"`
  20. Branch string `json:"branch"`
  21. SHA string `json:"sha"`
  22. }
  23. // createDevelDB forces yay to create a DB of the existing development packages
  24. func createDevelDB() error {
  25. var mux sync.Mutex
  26. var wg sync.WaitGroup
  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. bases := getBases(info)
  36. toSkip := pkgbuildsToSkip(bases, stringset.FromSlice(remoteNames))
  37. _, err = downloadPkgbuilds(bases, toSkip, config.BuildDir)
  38. if err != nil {
  39. return err
  40. }
  41. srcinfos, err := parseSrcinfoFiles(bases, false)
  42. if err != nil {
  43. return err
  44. }
  45. for i := range srcinfos {
  46. for iP := range srcinfos[i].Packages {
  47. wg.Add(1)
  48. go updateVCSData(srcinfos[i].Packages[iP].Pkgname, srcinfos[i].Source, &mux, &wg)
  49. }
  50. }
  51. wg.Wait()
  52. text.OperationInfoln(gotext.Get("GenDB finished. No packages were installed"))
  53. return err
  54. }
  55. // parseSource returns the git url, default branch and protocols it supports
  56. func parseSource(source string) (url, branch string, protocols []string) {
  57. split := strings.Split(source, "::")
  58. source = split[len(split)-1]
  59. split = strings.SplitN(source, "://", 2)
  60. if len(split) != 2 {
  61. return "", "", nil
  62. }
  63. protocols = strings.SplitN(split[0], "+", 2)
  64. git := false
  65. for _, protocol := range protocols {
  66. if protocol == "git" {
  67. git = true
  68. break
  69. }
  70. }
  71. protocols = protocols[len(protocols)-1:]
  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 url, branch, protocols
  94. }
  95. func updateVCSData(pkgName string, sources []gosrc.ArchString, mux sync.Locker, 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. mux.Lock()
  114. info[url] = shaInfo{
  115. protocols,
  116. branch,
  117. commit,
  118. }
  119. savedInfo[pkgName] = info
  120. text.Warnln(gotext.Get("Found git repo: %s", cyan(url)))
  121. err := saveVCSInfo()
  122. if err != nil {
  123. fmt.Fprintln(os.Stderr, err)
  124. }
  125. mux.Unlock()
  126. }
  127. for _, source := range sources {
  128. wg.Add(1)
  129. go checkSource(source)
  130. }
  131. }
  132. func getCommit(url, branch string, protocols []string) string {
  133. if len(protocols) > 0 {
  134. protocol := protocols[len(protocols)-1]
  135. var outbuf bytes.Buffer
  136. cmd := passToGit("", "ls-remote", protocol+"://"+url, branch)
  137. cmd.Stdout = &outbuf
  138. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  139. err := cmd.Start()
  140. if err != nil {
  141. return ""
  142. }
  143. // for some reason
  144. // git://bitbucket.org/volumesoffun/polyvox.git` hangs on my
  145. // machine but using http:// instead of git does not hang.
  146. // Introduce a time out so this can not hang
  147. timer := time.AfterFunc(5*time.Second, func() {
  148. err = cmd.Process.Kill()
  149. if err != nil {
  150. text.Errorln(err)
  151. }
  152. })
  153. err = cmd.Wait()
  154. timer.Stop()
  155. if err != nil {
  156. return ""
  157. }
  158. stdout := outbuf.String()
  159. split := strings.Fields(stdout)
  160. if len(split) < 2 {
  161. return ""
  162. }
  163. commit := split[0]
  164. return commit
  165. }
  166. return ""
  167. }
  168. func (infos shaInfos) needsUpdate() bool {
  169. // used to signal we have gone through all sources and found nothing
  170. finished := make(chan struct{})
  171. alive := 0
  172. // if we find an update we use this to exit early and return true
  173. hasUpdate := make(chan struct{})
  174. checkHash := func(url string, info shaInfo) {
  175. hash := getCommit(url, info.Branch, info.Protocols)
  176. if hash != "" && hash != info.SHA {
  177. hasUpdate <- struct{}{}
  178. } else {
  179. finished <- struct{}{}
  180. }
  181. }
  182. for url, info := range infos {
  183. alive++
  184. go checkHash(url, info)
  185. }
  186. for {
  187. select {
  188. case <-hasUpdate:
  189. return true
  190. case <-finished:
  191. alive--
  192. if alive == 0 {
  193. return false
  194. }
  195. }
  196. }
  197. }
  198. func saveVCSInfo() error {
  199. marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
  200. if err != nil || string(marshalledinfo) == "null" {
  201. return err
  202. }
  203. in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  204. if err != nil {
  205. return err
  206. }
  207. defer in.Close()
  208. _, err = in.Write(marshalledinfo)
  209. if err != nil {
  210. return err
  211. }
  212. err = in.Sync()
  213. return err
  214. }