vcs.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. return infoStore
  44. }
  45. // GetCommit parses HEAD commit from url and branch
  46. func (v *InfoStore) getCommit(url, branch string, protocols []string) string {
  47. if len(protocols) > 0 {
  48. protocol := protocols[len(protocols)-1]
  49. cmd := v.CmdBuilder.BuildGitCmd("", "ls-remote", protocol+"://"+url, branch)
  50. stdout, _, err := v.Runner.Capture(cmd, 5)
  51. if err != nil {
  52. text.Warnln(err)
  53. return ""
  54. }
  55. split := strings.Fields(stdout)
  56. if len(split) < 2 {
  57. return ""
  58. }
  59. commit := split[0]
  60. return commit
  61. }
  62. return ""
  63. }
  64. func (v *InfoStore) Update(pkgName string, sources []gosrc.ArchString, mux sync.Locker, wg *sync.WaitGroup) {
  65. defer wg.Done()
  66. info := make(OriginInfoByURL)
  67. checkSource := func(source gosrc.ArchString) {
  68. defer wg.Done()
  69. url, branch, protocols := parseSource(source.Value)
  70. if url == "" || branch == "" {
  71. return
  72. }
  73. commit := v.getCommit(url, branch, protocols)
  74. if commit == "" {
  75. return
  76. }
  77. mux.Lock()
  78. info[url] = OriginInfo{
  79. protocols,
  80. branch,
  81. commit,
  82. }
  83. v.OriginsByPackage[pkgName] = info
  84. text.Warnln(gotext.Get("Found git repo: %s", text.Cyan(url)))
  85. if err := v.Save(); err != nil {
  86. fmt.Fprintln(os.Stderr, err)
  87. }
  88. mux.Unlock()
  89. }
  90. for _, source := range sources {
  91. wg.Add(1)
  92. go checkSource(source)
  93. }
  94. }
  95. // parseSource returns the git url, default branch and protocols it supports
  96. func parseSource(source string) (url, branch string, protocols []string) {
  97. split := strings.Split(source, "::")
  98. source = split[len(split)-1]
  99. split = strings.SplitN(source, "://", 2)
  100. if len(split) != 2 {
  101. return "", "", nil
  102. }
  103. protocols = strings.SplitN(split[0], "+", 2)
  104. git := false
  105. for _, protocol := range protocols {
  106. if protocol == "git" {
  107. git = true
  108. break
  109. }
  110. }
  111. protocols = protocols[len(protocols)-1:]
  112. if !git {
  113. return "", "", nil
  114. }
  115. split = strings.SplitN(split[1], "#", 2)
  116. if len(split) == 2 {
  117. secondSplit := strings.SplitN(split[1], "=", 2)
  118. if secondSplit[0] != "branch" {
  119. // source has #commit= or #tag= which makes them not vcs
  120. // packages because they reference a specific point
  121. return "", "", nil
  122. }
  123. if len(secondSplit) == 2 {
  124. url = split[0]
  125. branch = secondSplit[1]
  126. }
  127. } else {
  128. url = split[0]
  129. branch = "HEAD"
  130. }
  131. url = strings.Split(url, "?")[0]
  132. branch = strings.Split(branch, "?")[0]
  133. return url, branch, protocols
  134. }
  135. func (v *InfoStore) NeedsUpdate(infos OriginInfoByURL) bool {
  136. // used to signal we have gone through all sources and found nothing
  137. finished := make(chan struct{})
  138. alive := 0
  139. // if we find an update we use this to exit early and return true
  140. hasUpdate := make(chan struct{})
  141. checkHash := func(url string, info OriginInfo) {
  142. hash := v.getCommit(url, info.Branch, info.Protocols)
  143. if hash != "" && hash != info.SHA {
  144. hasUpdate <- struct{}{}
  145. } else {
  146. finished <- struct{}{}
  147. }
  148. }
  149. for url, info := range infos {
  150. alive++
  151. go checkHash(url, info)
  152. }
  153. for {
  154. select {
  155. case <-hasUpdate:
  156. return true
  157. case <-finished:
  158. alive--
  159. if alive == 0 {
  160. return false
  161. }
  162. }
  163. }
  164. }
  165. func (v *InfoStore) Save() error {
  166. marshalledinfo, err := json.MarshalIndent(v, "", "\t")
  167. if err != nil || string(marshalledinfo) == "null" {
  168. return err
  169. }
  170. in, err := os.OpenFile(v.FilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
  171. if err != nil {
  172. return err
  173. }
  174. defer in.Close()
  175. _, err = in.Write(marshalledinfo)
  176. if err != nil {
  177. return err
  178. }
  179. err = in.Sync()
  180. return err
  181. }
  182. // RemovePackage removes package from VCS information
  183. func (v *InfoStore) RemovePackage(pkgs []string) {
  184. updated := false
  185. for _, pkgName := range pkgs {
  186. if _, ok := v.OriginsByPackage[pkgName]; ok {
  187. delete(v.OriginsByPackage, pkgName)
  188. updated = true
  189. }
  190. }
  191. if updated {
  192. if err := v.Save(); err != nil {
  193. fmt.Fprintln(os.Stderr, err)
  194. }
  195. }
  196. }
  197. // LoadStore reads a json file and populates a InfoStore structure
  198. func (v InfoStore) Load() error {
  199. vfile, err := os.Open(v.FilePath)
  200. if !os.IsNotExist(err) && err != nil {
  201. return fmt.Errorf("failed to open vcs file '%s': %s", v.FilePath, err)
  202. }
  203. defer vfile.Close()
  204. if !os.IsNotExist(err) {
  205. decoder := json.NewDecoder(vfile)
  206. if err = decoder.Decode(&v.OriginsByPackage); err != nil {
  207. return fmt.Errorf("failed to read vcs '%s': %s", v.FilePath, err)
  208. }
  209. }
  210. return nil
  211. }