repostatus/new.go

151 lines
3.5 KiB
Go
Raw Permalink Normal View History

2024-01-09 15:34:53 -06:00
package repostatus
import (
2024-02-23 11:00:33 -06:00
"errors"
"os"
"path/filepath"
"strings"
"go.wit.com/lib/gadgets"
"go.wit.com/log"
2024-01-09 15:34:53 -06:00
)
var windowMap map[string]*RepoStatus
func init() {
windowMap = make(map[string]*RepoStatus)
}
2024-02-18 15:09:48 -06:00
// deprecate this
func ListAllOld() {
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-02-18 07:25:48 -06:00
func NewRepoStatusWindow(path string) (error, *RepoStatus) {
2024-02-19 14:42:59 -06:00
var realpath string
var isGoLang bool = false
if windowMap[path] == nil {
log.Log(INFO, "NewRepoStatusWindow() adding new", path)
} else {
2024-02-23 11:00:33 -06:00
log.Warn("This already exists for path", path)
log.Warn("should return windowMap[path] here")
2024-02-18 07:25:48 -06:00
return nil, windowMap[path]
}
homeDir, err := os.UserHomeDir()
if err != nil {
log.Log(WARN, "Error getting home directory:", err)
2024-02-18 07:25:48 -06:00
return err, nil
}
goSrcDir := filepath.Join(homeDir, "go/src")
2024-02-19 14:42:59 -06:00
rs := &RepoStatus{
ready: false,
}
// allow arbitrary paths, otherwise, assume the repo is in ~/go/src
if strings.HasPrefix(path, "/") {
realpath = path
} else if strings.HasPrefix(path, "~") {
// TODO: example this to homedir
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-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
return errors.New(realpath + " does not exist"), nil
}
filename := filepath.Join(realpath, ".git/config")
_, err = os.Open(filename)
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-02-18 07:25:48 -06:00
return err, nil
}
rs.tags = make(map[string]string)
rs.window = gadgets.RawBasicWindow("GO Repo Details " + path)
rs.window.Horizontal()
rs.window.Make()
basebox := rs.window.Box()
group := basebox.NewGroup("stuff")
primarybox := group.Box()
primarybox.Horizontal()
box2 := group.Box()
rs.ready = true
rs.window.Custom = func() {
rs.Hide()
log.Warn("repostatus user closed the window()")
}
// 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)
// save ~/go/src & the whole path strings
rs.path.SetValue(path)
rs.goSrcPath.SetValue(goSrcDir)
rs.realPath.SetValue(realpath)
// add all the tags
rs.makeTagBox(box2)
rs.readGitConfig()
rs.readOnly.SetValue("true")
// ignore everything else for now
2024-02-23 11:00:33 -06:00
// todo: move this logic to cfgfile.go
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-02-19 14:42:59 -06:00
if isGoLang {
rs.isGoLang.SetText("true")
rs.goPath.SetText(path)
}
windowMap[path] = rs
2024-02-18 07:25:48 -06:00
return nil, rs
2024-01-09 15:34:53 -06:00
}