2024-11-17 14:42:49 -06:00
|
|
|
package repostatus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.wit.com/lib/gui/shell"
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// guess the paths. returns
|
|
|
|
|
|
|
|
// realpath : the actual path on the filesystem
|
|
|
|
// goSrcPath : this could be ~/go/src, or where the go.work file is
|
|
|
|
|
|
|
|
// goPath : go.wit.com/lib/gui/repostatus (for example)
|
|
|
|
// true/false if the repo is a golang repo
|
|
|
|
func guessPaths(path string) (string, string, string, bool, error) {
|
|
|
|
var realpath, goSrcDir string
|
|
|
|
var isGoLang bool = false
|
|
|
|
|
|
|
|
homeDir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Log(WARN, "Error getting home directory:", err)
|
|
|
|
return path, realpath, goSrcDir, false, err
|
|
|
|
}
|
|
|
|
goSrcDir = filepath.Join(homeDir, "go/src")
|
|
|
|
|
|
|
|
// allow arbitrary paths, otherwise, assume the repo is in ~/go/src
|
|
|
|
// unless REPO_WORK_PATH was set. to over-ride ~/go/src
|
|
|
|
// todo, look for go.work
|
|
|
|
if os.Getenv("REPO_WORK_PATH") == "" {
|
|
|
|
os.Setenv("REPO_WORK_PATH", goSrcDir)
|
|
|
|
} else {
|
|
|
|
goSrcDir = os.Getenv("REPO_WORK_PATH")
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: this is dumb
|
|
|
|
if strings.HasPrefix(path, "/") {
|
|
|
|
realpath = path
|
|
|
|
} else if strings.HasPrefix(path, "~") {
|
|
|
|
tmp := strings.TrimPrefix(path, "~")
|
|
|
|
realpath = filepath.Join(homeDir, tmp)
|
|
|
|
} else {
|
|
|
|
realpath = filepath.Join(goSrcDir, path)
|
|
|
|
isGoLang = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("REPO_AUTO_CLONE") == "true" {
|
|
|
|
err := Clone(goSrcDir, path)
|
|
|
|
if err != nil {
|
|
|
|
// directory doesn't exist. exit with nil and error nil
|
|
|
|
return path, realpath, goSrcDir, false, errors.New("git clone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !IsDirectory(realpath) {
|
|
|
|
log.Log(REPOWARN, "directory doesn't exist", realpath)
|
|
|
|
// directory doesn't exist. exit with nil and error nil
|
|
|
|
return path, realpath, goSrcDir, false, errors.New(realpath + " does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := filepath.Join(realpath, ".git/config")
|
|
|
|
|
|
|
|
_, err = os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
// log.Log(WARN, "Error reading .git/config:", filename, err)
|
|
|
|
// log.Log(WARN, "TODO: find .git/config in parent directory")
|
|
|
|
return path, realpath, goSrcDir, false, err
|
|
|
|
}
|
|
|
|
return path, realpath, goSrcDir, isGoLang, nil
|
|
|
|
}
|
|
|
|
|
2024-11-17 16:07:39 -06:00
|
|
|
// TODO: make some config file for things like this
|
|
|
|
// can be used to work around temporary problems
|
2024-11-22 22:30:08 -06:00
|
|
|
func clonePathHack(dirname string, basedir string, gopath string) error {
|
|
|
|
// newdir = helloworld
|
|
|
|
// basedir = /home/jcarr/go/src/go.wit.com/apps
|
|
|
|
// giturl = https://gitea.wit.com/gui/helloworld
|
|
|
|
// func cloneActual(newdir, basedir, giturl string) error {
|
|
|
|
|
2024-11-17 16:07:39 -06:00
|
|
|
switch gopath {
|
|
|
|
case "golang.org/x/crypto":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/crypto")
|
2024-11-17 16:07:39 -06:00
|
|
|
case "golang.org/x/mod":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/mod")
|
2024-11-17 16:07:39 -06:00
|
|
|
case "golang.org/x/net":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/net")
|
2024-11-17 16:07:39 -06:00
|
|
|
case "golang.org/x/sys":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/sys")
|
2024-11-17 16:07:39 -06:00
|
|
|
case "golang.org/x/sync":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/sync")
|
2024-11-17 16:07:39 -06:00
|
|
|
case "golang.org/x/term":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/term")
|
2024-11-17 16:07:39 -06:00
|
|
|
case "golang.org/x/text":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/text")
|
2024-11-17 16:07:39 -06:00
|
|
|
case "golang.org/x/tools":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/tools")
|
2024-11-17 16:07:39 -06:00
|
|
|
case "golang.org/x/xerrors":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/xerrors")
|
2024-11-22 20:48:02 -06:00
|
|
|
case "google.golang.org/protobuf":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/protobuf")
|
2024-11-22 20:48:02 -06:00
|
|
|
case "google.golang.org/genproto":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/genproto")
|
2024-11-22 20:48:02 -06:00
|
|
|
case "google.golang.org/api":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/api")
|
2024-11-22 20:48:02 -06:00
|
|
|
case "google.golang.org/grpc":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/grpc")
|
2024-11-22 20:48:02 -06:00
|
|
|
case "google.golang.org/appengine":
|
2024-11-22 22:30:08 -06:00
|
|
|
return cloneActual(dirname, basedir, "https://" + "go.googlesource.com/appengine")
|
2024-11-17 16:07:39 -06:00
|
|
|
}
|
|
|
|
|
2024-11-22 22:30:08 -06:00
|
|
|
return errors.New("no gopath override here")
|
2024-11-17 14:42:49 -06:00
|
|
|
}
|
|
|
|
|
2024-11-17 16:07:39 -06:00
|
|
|
// attempt to git clone if the go path doesn't exist
|
2024-11-17 14:42:49 -06:00
|
|
|
// does a git clone, if it works, returns true
|
|
|
|
// workdir = /home/jcarr/go/src/
|
2024-11-17 16:07:39 -06:00
|
|
|
// gopath = go.wit.com/apps/helloworld
|
|
|
|
func Clone(workdir, gopath string) error {
|
2024-11-17 14:42:49 -06:00
|
|
|
fullpath := filepath.Join(workdir, gopath)
|
|
|
|
dirname := filepath.Base(fullpath)
|
|
|
|
|
|
|
|
basedir := strings.TrimSuffix(fullpath, dirname)
|
|
|
|
|
|
|
|
var err error
|
2024-11-24 23:35:36 -06:00
|
|
|
url := "https://" + gopath
|
2024-11-17 14:42:49 -06:00
|
|
|
log.Info("trying git clone")
|
|
|
|
log.Info("gopath =", gopath)
|
|
|
|
|
2024-11-22 22:30:08 -06:00
|
|
|
|
2024-11-17 14:42:49 -06:00
|
|
|
// try a direct git clone against the gopath
|
2024-11-24 23:35:36 -06:00
|
|
|
// cloneActual("helloworld", "/home/jcarr/go/src/go.wit.com/apps", "https://go.wit.com/apps/helloworld")
|
2024-11-17 14:42:49 -06:00
|
|
|
if err = cloneActual(dirname, basedir, url); err == nil {
|
|
|
|
// git clone worked!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Info("direct attempt at git clone failed", url)
|
|
|
|
|
2024-11-17 16:07:39 -06:00
|
|
|
// 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 {
|
2024-11-22 22:30:08 -06:00
|
|
|
log.Info("findGoImport() DID NOT WORK", url)
|
|
|
|
log.Info("findGoImport() DID NOT WORK", err)
|
|
|
|
} else {
|
|
|
|
if err := cloneActual(dirname, basedir, url); err == nil {
|
|
|
|
// git clone worked!
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-17 16:07:39 -06:00
|
|
|
}
|
2024-11-22 22:30:08 -06:00
|
|
|
log.Info("git clone from 'go-import' info failed", url)
|
2024-11-17 16:07:39 -06:00
|
|
|
|
2024-11-17 14:42:49 -06:00
|
|
|
// 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 {
|
2024-11-17 16:07:39 -06:00
|
|
|
log.Info("go list failed", err)
|
2024-11-22 22:30:08 -06:00
|
|
|
} else {
|
|
|
|
if err := cloneActual(dirname, basedir, url); err == nil {
|
|
|
|
// git clone worked!
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-17 14:42:49 -06:00
|
|
|
}
|
2024-11-22 22:30:08 -06:00
|
|
|
log.Info("git clone from 'git list' info failed", url)
|
2024-11-17 14:42:49 -06:00
|
|
|
|
|
|
|
// try to parse a redirect
|
|
|
|
|
2024-11-22 22:30:08 -06:00
|
|
|
if err = clonePathHack(dirname, basedir, gopath); err == nil {
|
|
|
|
// WTF didn't go-import or go list work?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-17 14:42:49 -06:00
|
|
|
return errors.New("can not find git sources for gopath " + gopath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newdir = helloworld
|
|
|
|
// basedir = /home/jcarr/go/src/go.wit.com/apps
|
|
|
|
// giturl = https://gitea.wit.com/gui/helloworld
|
|
|
|
func cloneActual(newdir, basedir, giturl string) error {
|
2024-11-17 16:07:39 -06:00
|
|
|
log.Info("cloneActual() newdir =", newdir)
|
|
|
|
log.Info("cloneActual() basedir =", basedir)
|
|
|
|
log.Info("cloneActual() giturl =", giturl)
|
2024-11-17 14:42:49 -06:00
|
|
|
if !IsDirectory(basedir) {
|
|
|
|
os.MkdirAll(basedir, 0750)
|
|
|
|
}
|
|
|
|
err := os.Chdir(basedir)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("chdir failed", basedir, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-17 17:19:09 -06:00
|
|
|
cmd := []string{"git", "clone", "--verbose", "--progress", giturl, newdir}
|
2024-11-17 16:07:39 -06:00
|
|
|
log.Info("Running:", cmd)
|
|
|
|
r := shell.PathRunRealtime(basedir, cmd)
|
2024-11-17 14:42:49 -06:00
|
|
|
if r.Error != nil {
|
|
|
|
log.Warn("git clone error", r.Error)
|
|
|
|
return r.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
fullpath := filepath.Join(basedir, newdir)
|
|
|
|
if !IsDirectory(fullpath) {
|
|
|
|
log.Info("git clone failed", giturl)
|
|
|
|
return errors.New("git clone failed " + giturl)
|
|
|
|
}
|
|
|
|
gitdir := filepath.Join(fullpath, ".git")
|
|
|
|
if IsDirectory(gitdir) {
|
2024-11-17 16:07:39 -06:00
|
|
|
log.Info("git cloned worked to", fullpath)
|
2024-11-17 14:42:49 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// git clone didn't really work but did make a directory
|
|
|
|
log.Info("fullpath is probably empty", fullpath)
|
|
|
|
panic("crapnuts. rmdir fullpath here?")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the server for the current go path to git url mapping
|
|
|
|
// for example:
|
|
|
|
// This will check go.wit.com for "go.wit.com/apps/go-clone"
|
|
|
|
// and return where the URL to do git clone should be
|
|
|
|
func findGoImport(url string) (string, error) {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp := string(bodyBytes)
|
|
|
|
parts := strings.Split(tmp, "go-import")
|
|
|
|
if len(parts) < 2 {
|
|
|
|
return "", errors.New("missing go-import")
|
|
|
|
}
|
|
|
|
// this is terrible, it doesn't even look for 'content='
|
|
|
|
// but again, this is just a hack for my own code to be
|
|
|
|
// usuable after the removal in go v1.22 of the go get clone behavior that was in v1.21
|
|
|
|
parts = strings.Split(parts[1], "\"")
|
|
|
|
var newurl string
|
|
|
|
for {
|
|
|
|
if len(parts) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
tmp := strings.TrimSpace(parts[0])
|
|
|
|
fields := strings.Split(tmp, " ")
|
|
|
|
// log.Info("FIELDS:", fields)
|
|
|
|
if len(fields) == 3 {
|
|
|
|
newurl = fields[2]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
parts = parts[1:]
|
|
|
|
}
|
|
|
|
if newurl == "" {
|
|
|
|
return "", errors.New("missing git content string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return newurl, nil
|
|
|
|
}
|