vcs.go 5.9 KB

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