vcs.go 3.0 KB

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