vcs.go 5.8 KB

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