repostatus/new.go

139 lines
3.0 KiB
Go
Raw Normal View History

2024-01-09 15:34:53 -06:00
package repostatus
import (
"os"
2024-02-16 11:41:29 -06:00
"os/user"
"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)
}
func ListAll() {
for path, rs := range windowMap {
log.Warn(rs.GetMasterVersion(), path)
}
}
2024-02-14 15:18:39 -06:00
// returns the object for the path
func FindPath(path string) *RepoStatus {
if windowMap[path] == nil {
log.Log(INFO, "FindPath() not initialized yet", path)
return nil
}
return windowMap[path]
}
func NewRepoStatusWindow(path string) *RepoStatus {
if windowMap[path] == nil {
log.Log(INFO, "NewRepoStatusWindow() adding new", path)
} else {
log.Warn("This already exists yet for path", path)
log.Warn("should return windowMap[path] here")
return windowMap[path]
}
var realpath string
homeDir, err := os.UserHomeDir()
if err != nil {
log.Log(WARN, "Error getting home directory:", err)
return nil
}
goSrcDir := filepath.Join(homeDir, "go/src")
// 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)
}
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")
return nil
}
rs := &RepoStatus{
2024-02-14 15:18:39 -06:00
ready: false,
}
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
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
rs.setMainWorkingName("master")
usr, _ := user.Current()
uname := usr.Username
if rs.TagExists(uname) {
rs.setUserWorkingName(uname)
} else {
rs.setUserWorkingName("")
}
if rs.TagExists("guidevel") {
rs.setDevelWorkingName("guidevel")
} else if rs.TagExists("devel") {
rs.setDevelWorkingName("devel")
} else {
log.Log(WARN, "tag devel does not exist")
rs.setDevelWorkingName("")
}
windowMap[path] = rs
return rs
2024-01-09 15:34:53 -06:00
}