download.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/leonelquinteros/gotext"
  8. )
  9. const gitDiffRefName = "AUR_SEEN"
  10. // Update the YAY_DIFF_REVIEW ref to HEAD. We use this ref to determine which diff were
  11. // reviewed by the user
  12. func gitUpdateSeenRef(path, name string) error {
  13. _, stderr, err := config.Runtime.CmdRunner.Capture(
  14. config.Runtime.CmdBuilder.BuildGitCmd(
  15. filepath.Join(path, name), "update-ref", gitDiffRefName, "HEAD"), 0)
  16. if err != nil {
  17. return fmt.Errorf("%s %s", stderr, err)
  18. }
  19. return nil
  20. }
  21. // Return wether or not we have reviewed a diff yet. It checks for the existence of
  22. // YAY_DIFF_REVIEW in the git ref-list
  23. func gitHasLastSeenRef(path, name string) bool {
  24. _, _, err := config.Runtime.CmdRunner.Capture(
  25. config.Runtime.CmdBuilder.BuildGitCmd(
  26. filepath.Join(path, name), "rev-parse", "--quiet", "--verify", gitDiffRefName), 0)
  27. return err == nil
  28. }
  29. // Returns the last reviewed hash. If YAY_DIFF_REVIEW exists it will return this hash.
  30. // If it does not it will return empty tree as no diff have been reviewed yet.
  31. func getLastSeenHash(path, name string) (string, error) {
  32. if gitHasLastSeenRef(path, name) {
  33. stdout, stderr, err := config.Runtime.CmdRunner.Capture(
  34. config.Runtime.CmdBuilder.BuildGitCmd(
  35. filepath.Join(path, name), "rev-parse", gitDiffRefName), 0)
  36. if err != nil {
  37. return "", fmt.Errorf("%s %s", stderr, err)
  38. }
  39. lines := strings.Split(stdout, "\n")
  40. return lines[0], nil
  41. }
  42. return gitEmptyTree, nil
  43. }
  44. // Check whether or not a diff exists between the last reviewed diff and
  45. // HEAD@{upstream}
  46. func gitHasDiff(path, name string) (bool, error) {
  47. if gitHasLastSeenRef(path, name) {
  48. stdout, stderr, err := config.Runtime.CmdRunner.Capture(
  49. config.Runtime.CmdBuilder.BuildGitCmd(filepath.Join(path, name), "rev-parse", gitDiffRefName, "HEAD@{upstream}"), 0)
  50. if err != nil {
  51. return false, fmt.Errorf("%s%s", stderr, err)
  52. }
  53. lines := strings.Split(stdout, "\n")
  54. lastseen := lines[0]
  55. upstream := lines[1]
  56. return lastseen != upstream, nil
  57. }
  58. // If YAY_DIFF_REVIEW does not exists, we have never reviewed a diff for this package
  59. // and should display it.
  60. return true, nil
  61. }
  62. func gitDownload(url, path, name string) (bool, error) {
  63. _, err := os.Stat(filepath.Join(path, name, ".git"))
  64. if os.IsNotExist(err) {
  65. cmd := config.Runtime.CmdBuilder.BuildGitCmd(path, "clone", "--no-progress", url, name)
  66. _, stderr, errCapture := config.Runtime.CmdRunner.Capture(cmd, 0)
  67. if errCapture != nil {
  68. return false, fmt.Errorf(gotext.Get("error cloning %s: %s", name, stderr))
  69. }
  70. return true, nil
  71. } else if err != nil {
  72. return false, fmt.Errorf(gotext.Get("error reading %s", filepath.Join(path, name, ".git")))
  73. }
  74. cmd := config.Runtime.CmdBuilder.BuildGitCmd(filepath.Join(path, name), "fetch")
  75. _, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
  76. if err != nil {
  77. return false, fmt.Errorf(gotext.Get("error fetching %s: %s", name, stderr))
  78. }
  79. return false, nil
  80. }
  81. func gitMerge(path, name string) error {
  82. _, stderr, err := config.Runtime.CmdRunner.Capture(
  83. config.Runtime.CmdBuilder.BuildGitCmd(
  84. filepath.Join(path, name), "reset", "--hard", "HEAD"), 0)
  85. if err != nil {
  86. return fmt.Errorf(gotext.Get("error resetting %s: %s", name, stderr))
  87. }
  88. _, stderr, err = config.Runtime.CmdRunner.Capture(
  89. config.Runtime.CmdBuilder.BuildGitCmd(
  90. filepath.Join(path, name), "merge", "--no-edit", "--ff"), 0)
  91. if err != nil {
  92. return fmt.Errorf(gotext.Get("error merging %s: %s", name, stderr))
  93. }
  94. return nil
  95. }