2024-01-09 15:34:53 -06:00
|
|
|
package repostatus
|
|
|
|
|
2024-01-18 00:57:43 -06:00
|
|
|
import (
|
2024-02-23 11:00:33 -06:00
|
|
|
"errors"
|
2024-03-08 15:09:46 -06:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2024-01-23 10:52:17 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-01-23 22:47:39 -06:00
|
|
|
"strings"
|
2024-01-23 10:52:17 -06:00
|
|
|
|
2024-01-18 00:57:43 -06:00
|
|
|
"go.wit.com/lib/gadgets"
|
2024-03-07 19:31:52 -06:00
|
|
|
"go.wit.com/lib/gui/shell"
|
2024-01-18 00:57:43 -06:00
|
|
|
"go.wit.com/log"
|
2024-01-09 15:34:53 -06:00
|
|
|
)
|
|
|
|
|
2024-01-21 03:18:07 -06:00
|
|
|
var windowMap map[string]*RepoStatus
|
|
|
|
|
2024-02-15 22:50:50 -06:00
|
|
|
func init() {
|
|
|
|
windowMap = make(map[string]*RepoStatus)
|
|
|
|
}
|
|
|
|
|
2024-02-18 15:09:48 -06:00
|
|
|
// deprecate this
|
|
|
|
func ListAllOld() {
|
2024-01-21 03:18:07 -06:00
|
|
|
for path, rs := range windowMap {
|
|
|
|
log.Warn(rs.GetMasterVersion(), path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 15:18:39 -06:00
|
|
|
// returns the object for the path
|
2024-02-18 15:09:48 -06:00
|
|
|
// deprecate this
|
|
|
|
func FindPathOld(path string) *RepoStatus {
|
2024-02-14 15:18:39 -06:00
|
|
|
if windowMap[path] == nil {
|
|
|
|
log.Log(INFO, "FindPath() not initialized yet", path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return windowMap[path]
|
|
|
|
}
|
|
|
|
|
2024-02-22 15:29:22 -06:00
|
|
|
// makes a window of the status of the repo
|
|
|
|
// don't worry, you can think of it like Sierpinski carpet
|
|
|
|
// it's doesn't need to be displayed so it'll work fine even in an embedded space
|
|
|
|
func New(path string) (*RepoStatus, error) {
|
|
|
|
err, r := NewRepoStatusWindow(path)
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
2024-03-07 19:31:52 -06:00
|
|
|
func SetWorkPath(path string) {
|
|
|
|
os.Setenv("REPO_WORK_PATH", path)
|
|
|
|
}
|
2024-02-19 14:42:59 -06:00
|
|
|
|
2024-03-07 19:31:52 -06:00
|
|
|
// 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
|
2024-01-23 10:52:17 -06:00
|
|
|
|
|
|
|
homeDir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Log(WARN, "Error getting home directory:", err)
|
2024-03-07 19:31:52 -06:00
|
|
|
return path, realpath, goSrcDir, false, err
|
2024-01-21 03:18:07 -06:00
|
|
|
}
|
2024-03-07 19:31:52 -06:00
|
|
|
goSrcDir = filepath.Join(homeDir, "go/src")
|
2024-01-23 10:52:17 -06:00
|
|
|
|
2024-03-07 19:31:52 -06:00
|
|
|
// 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")
|
2024-02-19 14:42:59 -06:00
|
|
|
}
|
|
|
|
|
2024-02-15 22:50:50 -06:00
|
|
|
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)
|
2024-02-19 14:42:59 -06:00
|
|
|
isGoLang = true
|
2024-02-15 22:50:50 -06:00
|
|
|
}
|
2024-03-07 22:08:32 -06:00
|
|
|
|
|
|
|
if os.Getenv("REPO_AUTO_CLONE") == "true" {
|
|
|
|
clone(goSrcDir, path)
|
|
|
|
}
|
|
|
|
|
2024-02-23 11:00:33 -06:00
|
|
|
if !IsDirectory(realpath) {
|
|
|
|
log.Log(REPOWARN, "directory doesn't exist", realpath)
|
|
|
|
// directory doesn't exist. exit with nil and error nil
|
2024-03-07 19:31:52 -06:00
|
|
|
return path, realpath, goSrcDir, false, errors.New(realpath + " does not exist")
|
2024-02-23 11:00:33 -06:00
|
|
|
}
|
2024-02-15 22:50:50 -06:00
|
|
|
|
|
|
|
filename := filepath.Join(realpath, ".git/config")
|
2024-01-23 22:47:39 -06:00
|
|
|
|
|
|
|
_, err = os.Open(filename)
|
2024-01-23 10:52:17 -06:00
|
|
|
if err != nil {
|
2024-02-16 11:41:29 -06:00
|
|
|
// log.Log(WARN, "Error reading .git/config:", filename, err)
|
|
|
|
// log.Log(WARN, "TODO: find .git/config in parent directory")
|
2024-03-07 19:31:52 -06:00
|
|
|
return path, realpath, goSrcDir, false, err
|
|
|
|
}
|
|
|
|
return path, realpath, goSrcDir, isGoLang, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to git clone if the go path doesn't exist
|
2024-03-08 15:09:46 -06:00
|
|
|
func clone(wdir string, path string) error {
|
2024-03-07 19:31:52 -06:00
|
|
|
fullpath := filepath.Join(wdir, path)
|
|
|
|
if IsDirectory(fullpath) {
|
2024-03-08 15:09:46 -06:00
|
|
|
// directory already exists
|
|
|
|
return nil
|
2024-03-07 19:31:52 -06:00
|
|
|
}
|
|
|
|
err := os.Chdir(wdir)
|
|
|
|
if err != nil {
|
2024-03-08 15:09:46 -06:00
|
|
|
return err
|
2024-03-07 19:31:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
base := filepath.Join(wdir, filepath.Dir(path))
|
2024-03-07 22:08:32 -06:00
|
|
|
os.MkdirAll(base, 0750)
|
2024-03-08 15:09:46 -06:00
|
|
|
err = os.Chdir(base)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-07 19:31:52 -06:00
|
|
|
shell.RunPath(base, []string{"git", "clone", "http://" + path})
|
2024-03-08 15:09:46 -06:00
|
|
|
if IsDirectory(fullpath) {
|
|
|
|
// clone worked
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var url string
|
|
|
|
if url, err = findGoImport("http://" + path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info("URL:", url)
|
|
|
|
shell.RunPath(base, []string{"git", "clone", url})
|
|
|
|
if IsDirectory(fullpath) {
|
|
|
|
// clone worked
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("resolve go import")
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a terrible hack and really
|
|
|
|
// shouldn't be here. Hopefully this can
|
|
|
|
// be removed and the GO compiler code can
|
|
|
|
// be involked directly (it appears to currently
|
|
|
|
// be an internal/ function in the GO sources
|
|
|
|
// so it can't be imported from outside the compiler
|
|
|
|
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
|
2024-03-07 19:31:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRepoStatusWindow(path string) (error, *RepoStatus) {
|
|
|
|
path, realpath, goSrcDir, isGoLang, err := guessPaths(path)
|
|
|
|
if err != nil {
|
2024-02-18 07:25:48 -06:00
|
|
|
return err, nil
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
|
2024-03-07 19:31:52 -06:00
|
|
|
if windowMap[path] == nil {
|
|
|
|
log.Log(INFO, "NewRepoStatusWindow() adding new", path)
|
|
|
|
} else {
|
|
|
|
log.Warn("This already exists for path", path)
|
|
|
|
log.Warn("should return windowMap[path] here")
|
|
|
|
return nil, windowMap[path]
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := &RepoStatus{
|
|
|
|
ready: false,
|
|
|
|
}
|
|
|
|
|
2024-02-14 02:35:47 -06:00
|
|
|
rs.tags = make(map[string]string)
|
|
|
|
rs.window = gadgets.RawBasicWindow("GO Repo Details " + path)
|
|
|
|
rs.window.Horizontal()
|
|
|
|
rs.window.Make()
|
2024-02-15 22:50:50 -06:00
|
|
|
basebox := rs.window.Box()
|
|
|
|
group := basebox.NewGroup("stuff")
|
|
|
|
primarybox := group.Box()
|
|
|
|
primarybox.Horizontal()
|
|
|
|
box2 := group.Box()
|
2024-02-14 02:35:47 -06:00
|
|
|
rs.ready = true
|
|
|
|
rs.window.Custom = func() {
|
|
|
|
rs.Hide()
|
|
|
|
log.Warn("repostatus user closed the window()")
|
|
|
|
}
|
2024-02-15 22:50:50 -06:00
|
|
|
|
|
|
|
// display the status of the git repository
|
|
|
|
rs.drawGitStatus(primarybox)
|
|
|
|
|
|
|
|
// display the git branches and options
|
|
|
|
rs.makeBranchesBox(primarybox)
|
|
|
|
|
|
|
|
// show standard git commit and merge controls
|
|
|
|
rs.drawGitCommands(primarybox)
|
2024-01-23 10:52:17 -06:00
|
|
|
|
|
|
|
// save ~/go/src & the whole path strings
|
2024-01-23 22:47:39 -06:00
|
|
|
rs.path.SetValue(path)
|
2024-01-23 10:52:17 -06:00
|
|
|
rs.goSrcPath.SetValue(goSrcDir)
|
|
|
|
rs.realPath.SetValue(realpath)
|
|
|
|
|
2024-02-15 22:50:50 -06:00
|
|
|
// add all the tags
|
|
|
|
rs.makeTagBox(box2)
|
|
|
|
|
2024-01-23 22:47:39 -06:00
|
|
|
rs.readGitConfig()
|
|
|
|
|
|
|
|
rs.readOnly.SetValue("true")
|
2024-02-14 02:35:47 -06:00
|
|
|
// ignore everything else for now
|
2024-02-23 11:00:33 -06:00
|
|
|
// todo: move this logic to cfgfile.go
|
2024-01-23 22:47:39 -06:00
|
|
|
if strings.HasPrefix(path, "go.wit.com") {
|
|
|
|
rs.readOnly.SetValue("false")
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(path, "git.wit.org") {
|
|
|
|
rs.readOnly.SetValue("false")
|
|
|
|
}
|
2024-02-16 11:41:29 -06:00
|
|
|
|
2024-02-16 20:36:31 -06:00
|
|
|
// tries 'master', 'main', etc.
|
|
|
|
rs.guessMainWorkingName()
|
|
|
|
// tries 'devel', etc
|
|
|
|
rs.guessDevelWorkingName()
|
|
|
|
// sets this to os.Username
|
|
|
|
rs.setUserWorkingName()
|
2024-01-23 10:52:17 -06:00
|
|
|
|
2024-02-19 14:42:59 -06:00
|
|
|
if isGoLang {
|
|
|
|
rs.isGoLang.SetText("true")
|
|
|
|
rs.goPath.SetText(path)
|
|
|
|
}
|
2024-01-21 03:18:07 -06:00
|
|
|
windowMap[path] = rs
|
2024-02-18 07:25:48 -06:00
|
|
|
return nil, rs
|
2024-01-09 15:34:53 -06:00
|
|
|
}
|