vcs.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. checkHash := func(url string, info OriginInfo) {
  143. hash := v.getCommit(url, info.Branch, info.Protocols)
  144. if hash != "" && hash != info.SHA {
  145. hasUpdate <- struct{}{}
  146. } else {
  147. finished <- struct{}{}
  148. }
  149. }
  150. for url, info := range infos {
  151. alive++
  152. go checkHash(url, info)
  153. }
  154. for {
  155. select {
  156. case <-hasUpdate:
  157. return true
  158. case <-finished:
  159. alive--
  160. if alive == 0 {
  161. return false
  162. }
  163. }
  164. }
  165. }
  166. func (v *InfoStore) Save() error {
  167. marshalledinfo, err := json.MarshalIndent(v.OriginsByPackage, "", "\t")
  168. if err != nil || string(marshalledinfo) == "null" {
  169. return err
  170. }
  171. in, err := os.OpenFile(v.FilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
  172. if err != nil {
  173. return err
  174. }
  175. defer in.Close()
  176. _, err = in.Write(marshalledinfo)
  177. if err != nil {
  178. return err
  179. }
  180. err = in.Sync()
  181. return err
  182. }
  183. // RemovePackage removes package from VCS information
  184. func (v *InfoStore) RemovePackage(pkgs []string) {
  185. updated := false
  186. for _, pkgName := range pkgs {
  187. if _, ok := v.OriginsByPackage[pkgName]; ok {
  188. delete(v.OriginsByPackage, pkgName)
  189. updated = true
  190. }
  191. }
  192. if updated {
  193. if err := v.Save(); err != nil {
  194. fmt.Fprintln(os.Stderr, err)
  195. }
  196. }
  197. }
  198. // LoadStore reads a json file and populates a InfoStore structure
  199. func (v InfoStore) Load() error {
  200. vfile, err := os.Open(v.FilePath)
  201. if !os.IsNotExist(err) && err != nil {
  202. return fmt.Errorf("failed to open vcs file '%s': %s", v.FilePath, err)
  203. }
  204. defer vfile.Close()
  205. if !os.IsNotExist(err) {
  206. decoder := json.NewDecoder(vfile)
  207. if err = decoder.Decode(&v.OriginsByPackage); err != nil {
  208. return fmt.Errorf("failed to read vcs '%s': %s", v.FilePath, err)
  209. }
  210. }
  211. return nil
  212. }