vcs.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "strings"
  8. )
  9. // branch contains the information of a repository branch
  10. type branch struct {
  11. Name string `json:"name"`
  12. Commit struct {
  13. SHA string `json:"sha"`
  14. URL string `json:"url"`
  15. } `json:"commit"`
  16. }
  17. type branches []branch
  18. // Info contains the last commit sha of a repo
  19. type Info struct {
  20. Package string `json:"pkgname"`
  21. URL string `json:"url"`
  22. SHA string `json:"sha"`
  23. }
  24. type infos []Info
  25. // createDevelDB forces yay to create a DB of the existing development packages
  26. func createDevelDB() error {
  27. _, _, _, remoteNames, err := filterPackages()
  28. if err != nil {
  29. return err
  30. }
  31. config.NoConfirm = true
  32. specialDBsauce = true
  33. err = aurInstall(remoteNames, nil)
  34. return err
  35. }
  36. // parseSource returns owner and repo from source
  37. func parseSource(source string) (owner string, repo string) {
  38. if !(strings.Contains(source, "git://") ||
  39. strings.Contains(source, ".git") ||
  40. strings.Contains(source, "git+https://")) {
  41. return
  42. }
  43. split := strings.Split(source, "github.com/")
  44. if len(split) > 1 {
  45. secondSplit := strings.Split(split[1], "/")
  46. if len(secondSplit) > 1 {
  47. owner = secondSplit[0]
  48. thirdSplit := strings.Split(secondSplit[1], ".git")
  49. if len(thirdSplit) > 0 {
  50. repo = thirdSplit[0]
  51. }
  52. }
  53. }
  54. return
  55. }
  56. func (info *Info) needsUpdate() bool {
  57. var newRepo branches
  58. r, err := http.Get(info.URL)
  59. if err != nil {
  60. fmt.Println(err)
  61. return false
  62. }
  63. defer r.Body.Close()
  64. err = json.NewDecoder(r.Body).Decode(&newRepo)
  65. if err != nil {
  66. fmt.Println(err)
  67. return false
  68. }
  69. for _, e := range newRepo {
  70. if e.Name == "master" {
  71. return e.Commit.SHA != info.SHA
  72. }
  73. }
  74. return false
  75. }
  76. func inStore(pkgName string) *Info {
  77. for i, e := range savedInfo {
  78. if pkgName == e.Package {
  79. return &savedInfo[i]
  80. }
  81. }
  82. return nil
  83. }
  84. // branchInfo updates saved information
  85. func branchInfo(pkgName string, owner string, repo string) (err error) {
  86. updated = true
  87. var newRepo branches
  88. url := "https://api.github.com/repos/" + owner + "/" + repo + "/branches"
  89. r, err := http.Get(url)
  90. if err != nil {
  91. return
  92. }
  93. defer r.Body.Close()
  94. _ = json.NewDecoder(r.Body).Decode(&newRepo)
  95. packinfo := inStore(pkgName)
  96. for _, e := range newRepo {
  97. if e.Name == "master" {
  98. if packinfo != nil {
  99. packinfo.Package = pkgName
  100. packinfo.URL = url
  101. packinfo.SHA = e.Commit.SHA
  102. } else {
  103. savedInfo = append(savedInfo, Info{Package: pkgName, URL: url, SHA: e.Commit.SHA})
  104. }
  105. }
  106. }
  107. return
  108. }
  109. func saveVCSInfo() error {
  110. marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
  111. if err != nil || string(marshalledinfo) == "null" {
  112. return err
  113. }
  114. in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE, 0755)
  115. if err != nil {
  116. return err
  117. }
  118. defer in.Close()
  119. _, err = in.Write(marshalledinfo)
  120. if err != nil {
  121. return err
  122. }
  123. err = in.Sync()
  124. return err
  125. }