From 94e98f21456a4758f8f0083ffa9cc47dcf40a9e3 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 17 Nov 2024 16:07:39 -0600 Subject: [PATCH] fix clone logic --- clone.go | 123 +++++++++++++++++++++---------------------------------- 1 file changed, 47 insertions(+), 76 deletions(-) diff --git a/clone.go b/clone.go index 39a6aed..fe7354b 100644 --- a/clone.go +++ b/clone.go @@ -75,82 +75,40 @@ func guessPaths(path string) (string, string, string, bool, error) { return path, realpath, goSrcDir, isGoLang, nil } -// attempt to git clone if the go path doesn't exist -// wdir = /home/jcarr/go/src/ -// path = go.wit.com/apps/helloworld -func Clone(wdir string, path string) error { - fullpath := filepath.Join(wdir, path) - if IsDirectory(fullpath) { - // directory already exists - return nil - } - err := os.Chdir(wdir) - if err != nil { - return err +// TODO: make some config file for things like this +// can be used to work around temporary problems +func pathHack(gopath string) string { + switch gopath { + case "golang.org/x/crypto": + return "go.googlesource.com/crypto" + case "golang.org/x/mod": + return "go.googlesource.com/mod" + case "golang.org/x/net": + return "go.googlesource.com/net" + case "golang.org/x/sys": + return "go.googlesource.com/sys" + case "golang.org/x/sync": + return "go.googlesource.com/sync" + case "golang.org/x/term": + return "go.googlesource.com/term" + case "golang.org/x/text": + return "go.googlesource.com/text" + case "golang.org/x/tools": + return "go.googlesource.com/tools" + case "golang.org/x/xerrors": + return "go.googlesource.com/xerrors" } - fulldir := filepath.Join(wdir, filepath.Dir(path)) - /* - base := filepath.Base(path) - os.MkdirAll(fulldir, 0750) - err = os.Chdir(fulldir) - if err != nil { - return err - } - */ - - return CloneNew(fulldir, path) - - /* - // TODO: this should go in a config file if needed - // todo: move this to a resources/ text file in go-clone - // go get golang.org/x/term - // is now supposed to be: - // git clone https://go.googlesource.com/term - // if url, err = findGoImport("http://" + path); err != nil { - switch path { - case "golang.org/x/crypto": - path = "go.googlesource.com/crypto" - case "golang.org/x/mod": - path = "go.googlesource.com/mod" - case "golang.org/x/net": - path = "go.googlesource.com/net" - case "golang.org/x/sys": - path = "go.googlesource.com/sys" - case "golang.org/x/sync": - path = "go.googlesource.com/sync" - case "golang.org/x/term": - path = "go.googlesource.com/term" - case "golang.org/x/text": - path = "go.googlesource.com/text" - case "golang.org/x/tools": - path = "go.googlesource.com/tools" - case "golang.org/x/xerrors": - path = "go.googlesource.com/xerrors" - } - */ - - /* - var url string - - // this creates a valid URL for git clone - if url, err = runGoList(path); err != nil { - return err - } - log.Info("URL:", url) - shell.PathRunRealtime(fulldir, []string{"git", "clone", url, base}) - if IsDirectory(fullpath) { - // clone worked - return nil - } - return errors.New("resolve go import") - */ + return gopath } +// attempt to git clone if the go path doesn't exist // does a git clone, if it works, returns true // workdir = /home/jcarr/go/src/ -// gopath = go.wit.com/apps/helloworld -func CloneNew(workdir, gopath string) error { +// gopath = go.wit.com/apps/helloworld +func Clone(workdir, gopath string) error { + // temp hack to fix paths. + gopath = pathHack(gopath) fullpath := filepath.Join(workdir, gopath) dirname := filepath.Base(fullpath) @@ -160,9 +118,6 @@ func CloneNew(workdir, gopath string) error { url := "http://" + gopath log.Info("trying git clone") log.Info("gopath =", gopath) - log.Info("dirname =", dirname) - log.Info("basedir =", basedir) - log.Info("url =", url) // try a direct git clone against the gopath // cloneActual("helloworld", "/home/jcarr/go/src/go.wit.com/apps", "http://go.wit.com/apps/helloworld") @@ -172,10 +127,21 @@ func CloneNew(workdir, gopath string) error { } log.Info("direct attempt at git clone failed", url) + // if direct git clone doesn't work, look for a redirect + // go directly to the URL as that is autoritive. If that failes + // try the go package system as maybe the git site no longer exists + if url, err = findGoImport(url); err != nil { + return err + } + if err := cloneActual(dirname, basedir, url); err == nil { + // git clone worked! + return nil + } + // query the golang package system for the last known location // NOTE: take time to thank the go developers and google for designing this wonderful system if url, err = runGoList(gopath); err != nil { - return err + log.Info("go list failed", err) } if err := cloneActual(dirname, basedir, url); err == nil { // git clone worked! @@ -192,6 +158,9 @@ func CloneNew(workdir, gopath string) error { // basedir = /home/jcarr/go/src/go.wit.com/apps // giturl = https://gitea.wit.com/gui/helloworld func cloneActual(newdir, basedir, giturl string) error { + log.Info("cloneActual() newdir =", newdir) + log.Info("cloneActual() basedir =", basedir) + log.Info("cloneActual() giturl =", giturl) if !IsDirectory(basedir) { os.MkdirAll(basedir, 0750) } @@ -201,7 +170,9 @@ func cloneActual(newdir, basedir, giturl string) error { return err } - r := shell.PathRunRealtime(basedir, []string{"git", "clone", "--verbose", "--progress", giturl}) + cmd := []string{"git", "clone", "--verbose", "--progress", giturl} + log.Info("Running:", cmd) + r := shell.PathRunRealtime(basedir, cmd) if r.Error != nil { log.Warn("git clone error", r.Error) return r.Error @@ -214,7 +185,7 @@ func cloneActual(newdir, basedir, giturl string) error { } gitdir := filepath.Join(fullpath, ".git") if IsDirectory(gitdir) { - log.Info("git clone worked", gitdir) + log.Info("git cloned worked to", fullpath) return nil } // git clone didn't really work but did make a directory