main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // depCheckout clones overriden packages from a dep project (Gopkg.toml) into os.Args[1]
  2. // it `go get`s the originals, adds the `source` as a git remote named fork.
  3. // then it fetches them and resets to the specified `revision``
  4. package main
  5. import (
  6. "log"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. "github.com/cryptix/go/logging"
  11. "github.com/golang/dep"
  12. "github.com/golang/dep/gps"
  13. "github.com/pkg/errors"
  14. "golang.org/x/tools/go/vcs"
  15. )
  16. var check = logging.CheckFatal
  17. func main() {
  18. wd, err := os.Getwd()
  19. check(errors.Wrap(err, "GetWD failed"))
  20. log.Print("wd: ", wd)
  21. gp := os.Getenv("GOPATH")
  22. log.Print("GOPATH:", gp)
  23. if len(os.Args) < 2 {
  24. check(errors.Errorf("usage: %s <forkPath>", os.Args[0]))
  25. }
  26. forkPath := os.Args[1]
  27. ctx := &dep.Ctx{
  28. WorkingDir: wd,
  29. GOPATH: gp,
  30. GOPATHs: strings.Split(gp, ":"),
  31. ExplicitRoot: wd,
  32. Out: log.New(os.Stderr, "", 0),
  33. Err: log.New(os.Stderr, "", 0),
  34. Verbose: true,
  35. }
  36. proj, err := ctx.LoadProject()
  37. check(errors.Wrap(err, "load proj"))
  38. for pr, prop := range proj.Manifest.Ovr {
  39. rev, ok := prop.Constraint.(gps.Revision)
  40. if prop.Source != "" && ok {
  41. log.Print("Package: ", pr)
  42. log.Print("Fork Source: ", prop.Source)
  43. pkg := string(pr)
  44. loc, err := getPkgLocation(forkPath, pkg)
  45. if err != nil {
  46. log.Print("could not locate forked:", err)
  47. check(goGet(forkPath, pkg))
  48. if pkg == "github.com/qor/auth_themes" {
  49. pkg = "github.com/qor/auth_themes/clean"
  50. }
  51. loc, err = getPkgLocation(forkPath, pkg)
  52. check(errors.Wrap(err, "2nd pkg locate failed"))
  53. }
  54. log.Print("OS location:", loc)
  55. remoteURL, err := gitRemoteGetURL(loc, "fork")
  56. if err != nil {
  57. log.Println("no remote fork:", err)
  58. repoRoot, err := vcs.RepoRootForImportDynamic(prop.Source, true)
  59. check(errors.Wrap(err, "vcs.RepoFromImport failed"))
  60. err = gitRemoteAdd(loc, "fork", repoRoot.Repo)
  61. check(errors.Wrap(err, "failed to add fork"))
  62. } else {
  63. log.Println("Remote:", remoteURL)
  64. }
  65. err = gitFetch(loc, "fork")
  66. check(err)
  67. err = gitResetHard(loc, rev)
  68. check(err)
  69. log.Print("Done")
  70. log.Print()
  71. }
  72. }
  73. }
  74. func getPkgLocation(gopath, pkg string) (string, error) {
  75. cmd := exec.Command("go", "list", "-f", `{{.Dir}}`, pkg)
  76. cmd.Env = []string{"GOPATH=" + gopath}
  77. out, err := cmd.CombinedOutput()
  78. return strings.TrimSpace(string(out)), errors.Wrapf(err, "pkg(%s) not located", pkg)
  79. }
  80. func goGet(gopath, pkg string) error {
  81. cmd := exec.Command("go", "get", "-d", pkg+"/...")
  82. cmd.Env = []string{"GOPATH=" + gopath}
  83. cmd.Stdout = os.Stderr
  84. cmd.Stderr = os.Stderr
  85. return errors.Wrap(cmd.Run(), "goGet failed")
  86. }
  87. func gitRemoteGetURL(path, remote string) (string, error) {
  88. cmd := exec.Command("git", "remote", "get-url", remote)
  89. cmd.Dir = path
  90. out, err := cmd.CombinedOutput()
  91. return string(out), errors.Wrap(err, "git remote get-url failed")
  92. }
  93. func gitRemoteAdd(path, remote, url string) error {
  94. cmd := exec.Command("git", "remote", "add", remote, url)
  95. cmd.Dir = path
  96. return errors.Wrap(cmd.Run(), "git remote add failed")
  97. }
  98. func gitFetch(path, remote string) error {
  99. cmd := exec.Command("git", "fetch", "-v", remote)
  100. cmd.Dir = path
  101. cmd.Stdout = os.Stderr
  102. cmd.Stderr = os.Stderr
  103. return errors.Wrap(cmd.Run(), "gitFetch failed")
  104. }
  105. func gitResetHard(path string, rev gps.Revision) error {
  106. cmd := exec.Command("git", "reset", "--hard", string(rev))
  107. cmd.Dir = path
  108. cmd.Stdout = os.Stderr
  109. cmd.Stderr = os.Stderr
  110. return errors.Wrap(cmd.Run(), "gitFetch failed")
  111. }