vcs.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package vcs
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "sync"
  8. gosrc "github.com/Morganamilo/go-srcinfo"
  9. "github.com/leonelquinteros/gotext"
  10. "github.com/Jguer/yay/v10/pkg/settings/exe"
  11. "github.com/Jguer/yay/v10/pkg/text"
  12. )
  13. // InfoStore is a collection of OriginInfoByURL by Package.
  14. // Containing a map of last commit SHAs of a repo
  15. type InfoStore struct {
  16. OriginsByPackage map[string]OriginInfoByURL
  17. FilePath string
  18. Runner exe.Runner
  19. CmdBuilder *exe.CmdBuilder
  20. }
  21. // OriginInfoByURL stores the OriginInfo of each origin URL provided
  22. type OriginInfoByURL map[string]OriginInfo
  23. // OriginInfo contains the last commit sha of a repo
  24. // Example:
  25. // "github.com/Jguer/yay.git": {
  26. // "protocols": [
  27. // "https"
  28. // ],
  29. // "branch": "next",
  30. // "sha": "c1171d41467c68ffd3c46748182a16366aaaf87b"
  31. // }
  32. type OriginInfo struct {
  33. Protocols []string `json:"protocols"`
  34. Branch string `json:"branch"`
  35. SHA string `json:"sha"`
  36. }
  37. func NewInfoStore(filePath string, runner exe.Runner, cmdBuilder *exe.CmdBuilder) *InfoStore {
  38. infoStore := &InfoStore{
  39. CmdBuilder: cmdBuilder,
  40. FilePath: filePath,
  41. OriginsByPackage: map[string]OriginInfoByURL{},
  42. Runner: runner,
  43. }
  44. return infoStore
  45. }
  46. // GetCommit parses HEAD commit from url and branch
  47. func (v *InfoStore) getCommit(url, branch string, protocols []string) string {
  48. if len(protocols) > 0 {
  49. protocol := protocols[len(protocols)-1]
  50. cmd := v.CmdBuilder.BuildGitCmd("", "ls-remote", protocol+"://"+url, branch)
  51. stdout, _, err := v.Runner.Capture(cmd, 5)
  52. if err != nil {
  53. text.Warnln(err)
  54. return ""
  55. }
  56. split := strings.Fields(stdout)
  57. if len(split) < 2 {
  58. return ""
  59. }
  60. commit := split[0]
  61. return commit
  62. }
  63. return ""
  64. }
  65. func (v *InfoStore) Update(pkgName string, sources []gosrc.ArchString, mux sync.Locker, wg *sync.WaitGroup) {
  66. defer wg.Done()
  67. info := make(OriginInfoByURL)
  68. checkSource := func(source gosrc.ArchString) {
  69. defer wg.Done()
  70. url, branch, protocols := parseSource(source.Value)
  71. if url == "" || branch == "" {
  72. return
  73. }
  74. commit := v.getCommit(url, branch, protocols)
  75. if commit == "" {
  76. return
  77. }
  78. mux.Lock()
  79. info[url] = OriginInfo{
  80. protocols,
  81. branch,
  82. commit,
  83. }
  84. v.OriginsByPackage[pkgName] = info
  85. text.Warnln(gotext.Get("Found git repo: %s", text.Cyan(url)))
  86. if err := v.Save(); err != nil {
  87. fmt.Fprintln(os.Stderr, err)
  88. }
  89. mux.Unlock()
  90. }
  91. for _, source := range sources {
  92. wg.Add(1)
  93. go checkSource(source)
  94. }
  95. }
  96. // parseSource returns the git url, default branch and protocols it supports
  97. func parseSource(source string) (url, branch string, protocols []string) {
  98. split := strings.Split(source, "::")
  99. source = split[len(split)-1]
  100. split = strings.SplitN(source, "://", 2)
  101. if len(split) != 2 {
  102. return "", "", nil
  103. }
  104. protocols = strings.SplitN(split[0], "+", 2)
  105. git := false
  106. for _, protocol := range protocols {
  107. if protocol == "git" {
  108. git = true
  109. break
  110. }
  111. }
  112. protocols = protocols[len(protocols)-1:]
  113. if !git {
  114. return "", "", nil
  115. }
  116. split = strings.SplitN(split[1], "#", 2)
  117. if len(split) == 2 {
  118. secondSplit := strings.SplitN(split[1], "=", 2)
  119. if secondSplit[0] != "branch" {
  120. // source has #commit= or #tag= which makes them not vcs
  121. // packages because they reference a specific point
  122. return "", "", nil
  123. }
  124. if len(secondSplit) == 2 {
  125. url = split[0]
  126. branch = secondSplit[1]
  127. }
  128. } else {
  129. url = split[0]
  130. branch = "HEAD"
  131. }
  132. url = strings.Split(url, "?")[0]
  133. branch = strings.Split(branch, "?")[0]
  134. return url, branch, protocols
  135. }
  136. func (v *InfoStore) NeedsUpdate(infos OriginInfoByURL) bool {
  137. // used to signal we have gone through all sources and found nothing
  138. finished := make(chan struct{})
  139. alive := 0
  140. // if we find an update we use this to exit early and return true
  141. hasUpdate := make(chan struct{})
  142. closed := make(chan struct{})
  143. defer close(closed)
  144. checkHash := func(url string, info OriginInfo) {
  145. hash := v.getCommit(url, info.Branch, info.Protocols)
  146. var sendTo chan<- struct{}
  147. if hash != "" && hash != info.SHA {
  148. sendTo = hasUpdate
  149. } else {
  150. sendTo = finished
  151. }
  152. select {
  153. case sendTo <- struct{}{}:
  154. case <-closed:
  155. }
  156. }
  157. for url, info := range infos {
  158. alive++
  159. go checkHash(url, info)
  160. }
  161. for {
  162. select {
  163. case <-hasUpdate:
  164. return true
  165. case <-finished:
  166. alive--
  167. if alive == 0 {
  168. return false
  169. }
  170. }
  171. }
  172. }
  173. func (v *InfoStore) Save() error {
  174. marshalledinfo, err := json.MarshalIndent(v.OriginsByPackage, "", "\t")
  175. if err != nil || string(marshalledinfo) == "null" {
  176. return err
  177. }
  178. in, err := os.OpenFile(v.FilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
  179. if err != nil {
  180. return err
  181. }
  182. defer in.Close()
  183. _, err = in.Write(marshalledinfo)
  184. if err != nil {
  185. return err
  186. }
  187. err = in.Sync()
  188. return err
  189. }
  190. // RemovePackage removes package from VCS information
  191. func (v *InfoStore) RemovePackage(pkgs []string) {
  192. updated := false
  193. for _, pkgName := range pkgs {
  194. if _, ok := v.OriginsByPackage[pkgName]; ok {
  195. delete(v.OriginsByPackage, pkgName)
  196. updated = true
  197. }
  198. }
  199. if updated {
  200. if err := v.Save(); err != nil {
  201. fmt.Fprintln(os.Stderr, err)
  202. }
  203. }
  204. }
  205. // LoadStore reads a json file and populates a InfoStore structure
  206. func (v InfoStore) Load() error {
  207. vfile, err := os.Open(v.FilePath)
  208. if !os.IsNotExist(err) && err != nil {
  209. return fmt.Errorf("failed to open vcs file '%s': %s", v.FilePath, err)
  210. }
  211. defer vfile.Close()
  212. if !os.IsNotExist(err) {
  213. decoder := json.NewDecoder(vfile)
  214. if err = decoder.Decode(&v.OriginsByPackage); err != nil {
  215. return fmt.Errorf("failed to read vcs '%s': %s", v.FilePath, err)
  216. }
  217. }
  218. return nil
  219. }