vcs.go 5.7 KB

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