vcs.go 5.9 KB

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