continue cleaning up original version

This commit is contained in:
Jeff Carr 2024-02-16 11:41:29 -06:00
parent 88ca40bcfa
commit bd62a89a67
10 changed files with 219 additions and 73 deletions

View File

@ -1,13 +1,26 @@
package repostatus package repostatus
import ( import (
"strings" "path/filepath"
"go.wit.com/gui" "go.wit.com/gui"
"go.wit.com/lib/gadgets" "go.wit.com/lib/gadgets"
"go.wit.com/log" "go.wit.com/log"
) )
func (rs *RepoStatus) TagExists(findname string) bool {
allTags := rs.Tags.ListAll()
for _, t := range allTags {
tagname := t.TagString()
path, filename := filepath.Split(tagname)
if filename == findname {
log.Info("found tag:", path, filename, "from", rs.Path())
return true
}
}
return false
}
func (rs *RepoStatus) makeBranchesBox(parent *gui.Node) { func (rs *RepoStatus) makeBranchesBox(parent *gui.Node) {
rs.gitBranchesGroup = parent.NewGroup("branches") // `progname:"BRANCHES"` // can the toolkits use these for i18n support? rs.gitBranchesGroup = parent.NewGroup("branches") // `progname:"BRANCHES"` // can the toolkits use these for i18n support?
newgrid := rs.gitBranchesGroup.NewGrid("gridnuts", 0, 0) newgrid := rs.gitBranchesGroup.NewGrid("gridnuts", 0, 0)
@ -45,18 +58,20 @@ func (rs *RepoStatus) makeBranchesBox(parent *gui.Node) {
// rs.targetBranch.AddText("master") // rs.targetBranch.AddText("master")
newgrid.NextRow() newgrid.NextRow()
rs.showBranchesButton = newgrid.NewButton("scan branches()", func() { // runs "git branch --all"
err, out := rs.RunCmd([]string{"git", "branch", "--all"}) rs.gitBranchAll()
if err != nil {
log.Log(WARN, "git branch failed", rs.String()) rs.showBranchesButton = newgrid.NewButton("find jcarr and devel", func() {
if rs.TagExists("jcarr") {
log.Log(WARN, "tag jcarr exists")
} else {
log.Log(WARN, "tag jcarr does not exist")
} }
all := strings.Split(out, "\n") if rs.TagExists("devel") {
for i, s := range all { log.Log(WARN, "tag devel exists")
log.Log(WARN, "found branch", i, s) } else {
rs.targetBranch.AddText(s) log.Log(WARN, "tag devel does not exist")
} }
i := len(all)
log.Log(WARN, "branch count =", i)
}) })
newgrid.NextRow() newgrid.NextRow()

View File

@ -39,7 +39,6 @@ func (rs *RepoStatus) GetPath() string {
} }
*/ */
/* /*
func (rs *RepoStatus) Draw() { func (rs *RepoStatus) Draw() {
if !rs.Ready() { if !rs.Ready() {
@ -88,6 +87,16 @@ func (rs *RepoStatus) Ready() bool {
return rs.ready return rs.ready
} }
func (rs *RepoStatus) IsGoLang() bool {
if !rs.Ready() {
return false
}
if rs.isGoLang.String() == "true" {
return true
}
return false
}
/* /*
func (rs *RepoStatus) Initialized() bool { func (rs *RepoStatus) Initialized() bool {
log.Log(CHANGE, "checking Initialized()") log.Log(CHANGE, "checking Initialized()")
@ -98,6 +107,9 @@ func (rs *RepoStatus) Initialized() bool {
*/ */
func (rs *RepoStatus) RepoType() string { func (rs *RepoStatus) RepoType() string {
if !rs.IsGoLang() {
return ""
}
err, output := rs.RunCmd([]string{"go", "list", "-f", "'{{if eq .Name \"main\"}}binary{{else}}library{{end}}'"}) err, output := rs.RunCmd([]string{"go", "list", "-f", "'{{if eq .Name \"main\"}}binary{{else}}library{{end}}'"})
if err == nil { if err == nil {
output = strings.Trim(output, "'") output = strings.Trim(output, "'")
@ -117,6 +129,9 @@ func (rs *RepoStatus) BinaryName() string {
} }
func (rs *RepoStatus) Build() bool { func (rs *RepoStatus) Build() bool {
if !rs.IsGoLang() {
return false
}
name := rs.BinaryName() name := rs.BinaryName()
// removes the binary if it already exists // removes the binary if it already exists
rs.RunCmd([]string{"rm", "-f", name}) rs.RunCmd([]string{"rm", "-f", name})

View File

@ -19,7 +19,8 @@ func (rs *RepoStatus) drawGitStatus(box *gui.Node) {
rs.path = gadgets.NewOneLiner(newgrid, "path") rs.path = gadgets.NewOneLiner(newgrid, "path")
rs.goSrcPath = gadgets.NewOneLiner(newgrid, "~/go/src") rs.goSrcPath = gadgets.NewOneLiner(newgrid, "~/go/src")
rs.realPath = gadgets.NewOneLiner(newgrid, "full path") rs.realPath = gadgets.NewOneLiner(newgrid, "full path")
rs.realPath.Hide() rs.isGoLang = gadgets.NewOneLiner(newgrid, "Is GO Lang?")
rs.isGoLang.SetText("false")
rs.mainWorkingName = gadgets.NewOneLiner(newgrid, "main working branch") rs.mainWorkingName = gadgets.NewOneLiner(newgrid, "main working branch")
rs.mainWorkingName.SetValue("???") rs.mainWorkingName.SetValue("???")
rs.develWorkingName = gadgets.NewOneLiner(newgrid, "devel working branch") rs.develWorkingName = gadgets.NewOneLiner(newgrid, "devel working branch")
@ -66,7 +67,7 @@ func (rs *RepoStatus) RunDevelMergeB() bool {
log.Warn("RunDevelMergeB() SOMETHING WENT WRONG") log.Warn("RunDevelMergeB() SOMETHING WENT WRONG")
return false return false
} }
rs.Update() rs.UpdateNew()
log.Warn("RunDevelMergeB() THINGS SEEM OK runGitCommands() returned true.") log.Warn("RunDevelMergeB() THINGS SEEM OK runGitCommands() returned true.")
return true return true
} }
@ -80,7 +81,7 @@ func (rs *RepoStatus) runReleaseVersionB() bool {
log.Warn("MAKING A RELEASE AND VERSION") log.Warn("MAKING A RELEASE AND VERSION")
if !rs.runGitCommands(true) { if !rs.runGitCommands(true) {
log.Warn("SOMETHING WENT WRONG") log.Warn("SOMETHING WENT WRONG")
rs.Update() rs.UpdateNew()
rs.Enable() rs.Enable()
return false return false
} }

65
git.go
View File

@ -33,8 +33,29 @@ func (rs *RepoStatus) getCurrentBranchName() string {
return out return out
} }
func (rs *RepoStatus) gitDescribeTags(name string) (string, error) {
name = strings.TrimSpace(name)
if name == "" {
// git will return the current tag
err, out := rs.RunCmd([]string{"git", "describe", "--tags"})
if err != nil {
log.Warn("not in a git repo?", err, rs.Path())
return "", err
}
return out, err
}
err, out := rs.RunCmd([]string{"git", "describe", "--tags", name})
if err != nil {
log.Warn("not in a git repo or bad tag?", err, rs.Path())
return "", err
}
return out, err
}
// todo: don't run git every time?
func (rs *RepoStatus) getCurrentBranchVersion() string { func (rs *RepoStatus) getCurrentBranchVersion() string {
out := run(rs.realPath.String(), "git", "describe --tags") out, _ := rs.gitDescribeTags("")
log.Log(INFO, "getCurrentBranchVersion()", out) log.Log(INFO, "getCurrentBranchVersion()", out)
rs.currentVersion.SetValue(out) rs.currentVersion.SetValue(out)
return out return out
@ -159,6 +180,13 @@ func (rs *RepoStatus) CheckoutMaster() bool {
func (rs *RepoStatus) CheckoutDevel() bool { func (rs *RepoStatus) CheckoutDevel() bool {
devel := rs.develWorkingName.String() devel := rs.develWorkingName.String()
// user := rs.userWorkingName.String() // user := rs.userWorkingName.String()
if devel == "" {
return false
}
if rs.CheckDirty() {
log.Log(INFO, rs.realPath.String(), "is dirty")
return false
}
log.Log(INFO, "checkoutBranch", devel) log.Log(INFO, "checkoutBranch", devel)
rs.checkoutBranch("devel", devel) rs.checkoutBranch("devel", devel)
@ -167,6 +195,33 @@ func (rs *RepoStatus) CheckoutDevel() bool {
return true return true
} }
func (rs *RepoStatus) CheckoutUser() bool {
bName := rs.GetUserBranchName()
if bName == "" {
return false
}
if rs.CheckDirty() {
log.Log(INFO, rs.realPath.String(), "is dirty")
return false
}
cmd := []string{"git", "checkout", bName}
err, b, output := RunCmd(rs.realPath.String(), cmd)
if err != nil {
log.Log(INFO, err, b, output)
}
realname := rs.getCurrentBranchName()
realversion := rs.getCurrentBranchVersion()
log.Log(INFO, rs.realPath.String(), "realname =", realname, "realversion =", realversion)
if realname != bName {
log.Log(INFO, "git checkout failed", rs.realPath.String(), bName, "!=", realname)
return false
}
rs.userBranchVersion.SetValue(realversion)
return true
}
func (rs *RepoStatus) checkoutBranch(level string, branch string) { func (rs *RepoStatus) checkoutBranch(level string, branch string) {
if rs.CheckDirty() { if rs.CheckDirty() {
log.Log(INFO, "checkoutBranch() checkDirty() == true for repo", rs.realPath.String(), "looking for branch:", branch) log.Log(INFO, "checkoutBranch() checkDirty() == true for repo", rs.realPath.String(), "looking for branch:", branch)
@ -190,7 +245,9 @@ func (rs *RepoStatus) checkoutBranch(level string, branch string) {
} }
} }
func (rs *RepoStatus) SetMainWorkingName(s string) { // attempt's to guess at what master is.
// TODO: fix this properly
func (rs *RepoStatus) setMainWorkingName(s string) {
if rs == nil { if rs == nil {
log.Info("rs == nil", s) log.Info("rs == nil", s)
return return
@ -243,12 +300,12 @@ func (rs *RepoStatus) SetMainWorkingName(s string) {
rs.mainWorkingName.SetValue(s) rs.mainWorkingName.SetValue(s)
} }
func (rs *RepoStatus) SetDevelWorkingName(s string) { func (rs *RepoStatus) setDevelWorkingName(s string) {
rs.develWorkingName.SetValue(s) rs.develWorkingName.SetValue(s)
rs.develBranchVersion.SetLabel(s) rs.develBranchVersion.SetLabel(s)
} }
func (rs *RepoStatus) SetUserWorkingName(s string) { func (rs *RepoStatus) setUserWorkingName(s string) {
rs.userWorkingName.SetValue(s) rs.userWorkingName.SetValue(s)
rs.userBranchVersion.SetLabel(s) rs.userBranchVersion.SetLabel(s)
// rs.userDrop.SetText(s) // rs.userDrop.SetText(s)

View File

@ -79,6 +79,7 @@ func (rs *RepoStatus) readGitConfig() error {
filename := filepath.Join(rs.realPath.String(), "/.git/config") filename := filepath.Join(rs.realPath.String(), "/.git/config")
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
log.Log(WARN, "readGitConfig() failed for file:", filename)
filename = filepath.Join(rs.realPath.String(), "../.git/config") filename = filepath.Join(rs.realPath.String(), "../.git/config")
log.Log(WARN, "readGitConfig() trying up one directory instead", filename) log.Log(WARN, "readGitConfig() trying up one directory instead", filename)
file, err = os.Open(filename) file, err = os.Open(filename)
@ -224,19 +225,6 @@ func ScanGoSrc() {
} }
} }
func ScanGitConfig() {
/*
for i, path := range listGitDirectories() {
filename := filepath.Join(path, ".git/config")
_, err := readGitConfig(filename)
if err != nil {
log.Log(WARN, "repo =", i, path)
log.Log(WARN, "Error reading .git/config:", err)
}
}
*/
}
func (rs *RepoStatus) ScanGoSrc() { func (rs *RepoStatus) ScanGoSrc() {
if rs.ReadGoMod() { if rs.ReadGoMod() {
log.Log(INFO, "parsed go.mod", rs.realPath.String()) log.Log(INFO, "parsed go.mod", rs.realPath.String())
@ -250,6 +238,10 @@ func (rs *RepoStatus) ScanGoSrc() {
} }
} }
func (rs *RepoStatus) Writable() {
rs.readOnly.SetText("false")
}
func (rs *RepoStatus) ReadOnly() bool { func (rs *RepoStatus) ReadOnly() bool {
if rs.readOnly.String() == "true" { if rs.readOnly.String() == "true" {
return true return true

View File

@ -10,12 +10,12 @@ func (rs *RepoStatus) drawGitCommands(box *gui.Node) {
rs.gitCommandsGroup = box.NewGroup("modify") rs.gitCommandsGroup = box.NewGroup("modify")
newgrid := rs.gitCommandsGroup.NewGrid("gridnuts", 0, 0) newgrid := rs.gitCommandsGroup.NewGrid("gridnuts", 0, 0)
newgrid.NewButton("update", func() { newgrid.NewButton("UpdateNew()", func() {
rs.Update() rs.UpdateNew()
}) })
newgrid.NewButton("UpdateCurrent()", func() { newgrid.NewButton("updateOld()", func() {
rs.Update() rs.UpdateOld()
}) })
newgrid.NewButton("CheckDirty()", func() { newgrid.NewButton("CheckDirty()", func() {

25
new.go
View File

@ -2,6 +2,7 @@ package repostatus
import ( import (
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"strings" "strings"
@ -63,9 +64,9 @@ func NewRepoStatusWindow(path string) *RepoStatus {
_, err = os.Open(filename) _, err = os.Open(filename)
if err != nil { if err != nil {
log.Log(WARN, "Error reading .git/config:", filename, err) // log.Log(WARN, "Error reading .git/config:", filename, err)
log.Log(WARN, "TODO: find .git/config in parent directory") // log.Log(WARN, "TODO: find .git/config in parent directory")
// return nil return nil
} }
rs := &RepoStatus{ rs := &RepoStatus{
@ -113,6 +114,24 @@ func NewRepoStatusWindow(path string) *RepoStatus {
if strings.HasPrefix(path, "git.wit.org") { if strings.HasPrefix(path, "git.wit.org") {
rs.readOnly.SetValue("false") rs.readOnly.SetValue("false")
} }
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 windowMap[path] = rs
return rs return rs

View File

@ -23,6 +23,7 @@ type RepoStatus struct {
path *gadgets.OneLiner path *gadgets.OneLiner
goSrcPath *gadgets.OneLiner goSrcPath *gadgets.OneLiner
realPath *gadgets.OneLiner realPath *gadgets.OneLiner
isGoLang *gadgets.OneLiner
currentBranch *gadgets.OneLiner currentBranch *gadgets.OneLiner
currentVersion *gadgets.OneLiner currentVersion *gadgets.OneLiner

32
unix.go
View File

@ -242,30 +242,6 @@ func Exists(file string) bool {
return true return true
} }
func VerifyLocalGoRepo(gorepo string) bool {
// Get current user
usr, err := user.Current()
if err != nil {
log.Error(err, "VerifyLocalGoRepo() are you really POSIX compliant?")
return false
}
// Form the path to the home Git directory
gitDir := filepath.Join(usr.HomeDir, "go/src/", gorepo, ".git")
log.Log(INFO, "VerifyLocalGoRepo() checking directory:", gitDir)
if IsDirectory(gitDir) {
return true
}
goDir := filepath.Join(usr.HomeDir, "go/src/", gorepo)
gomod := goDir + "/go.mod"
log.Log(INFO, "VerifyLocalGoRepo() checking for go.mod :", gomod)
_, err = os.Stat(gomod)
if os.IsNotExist(err) {
return false
}
return true
}
func readFileToString(filename string) (string, error) { func readFileToString(filename string) (string, error) {
data, err := ioutil.ReadFile(filename) data, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
@ -280,7 +256,7 @@ func getGitDateStamp(gitdefault string) (time.Time, string, string) {
const gitLayout = "Mon Jan 2 15:04:05 2006 -0700" const gitLayout = "Mon Jan 2 15:04:05 2006 -0700"
tagTime, err := time.Parse(gitLayout, gitdefault) tagTime, err := time.Parse(gitLayout, gitdefault)
if err != nil { if err != nil {
log.Warn("GOT THIS IN PARSE AAA."+gitdefault+".AAA") log.Warn("GOT THIS IN PARSE AAA." + gitdefault + ".AAA")
log.Warn(err) log.Warn(err)
return time.Now(), "Feb 1 12:34:56 1978 -0600", "" return time.Now(), "Feb 1 12:34:56 1978 -0600", ""
} }
@ -321,8 +297,13 @@ func formatDuration(d time.Duration) string {
minutes := int(d.Minutes()) % 60 minutes := int(d.Minutes()) % 60
hours := int(d.Hours()) % 24 hours := int(d.Hours()) % 24
days := int(d.Hours()) / 24 days := int(d.Hours()) / 24
years := int(d.Hours()) / (24 * 365)
result := "" result := ""
if years > 0 {
result += fmt.Sprintf("%dy ", years)
return result
}
if days > 0 { if days > 0 {
result += fmt.Sprintf("%dd ", days) result += fmt.Sprintf("%dd ", days)
return result return result
@ -350,6 +331,7 @@ func (rs *RepoStatus) Xterm(cmdline string) {
func (rs *RepoStatus) XtermWait(cmdline string) { func (rs *RepoStatus) XtermWait(cmdline string) {
shell.XtermCmdWait(rs.Path(), []string{cmdline}) shell.XtermCmdWait(rs.Path(), []string{cmdline})
} }
/* /*
func (rs *RepoStatus) XtermNohup(args []string) { func (rs *RepoStatus) XtermNohup(args []string) {
var argsX = []string{"xterm", "-geometry", "120x40"} var argsX = []string{"xterm", "-geometry", "120x40"}

View File

@ -3,19 +3,32 @@ package repostatus
import ( import (
"errors" "errors"
"fmt" "fmt"
"strings"
"time" "time"
"go.wit.com/log" "go.wit.com/log"
) )
func (rs *RepoStatus) Update() { func (rs *RepoStatus) gitBranchAll() {
err, out := rs.RunCmd([]string{"git", "branch", "--all"})
if err != nil {
log.Log(WARN, "git branch failed", rs.String())
}
all := strings.Split(out, "\n")
for _, s := range all {
// log.Log(WARN, "found branch", i, s)
rs.targetBranch.AddText(s)
}
// i := len(all)
// log.Log(WARN, "branch count =", i)
}
func (rs *RepoStatus) UpdateNew() {
if !rs.Ready() { if !rs.Ready() {
log.Log(WARN, "can't update yet. ready is false") log.Log(WARN, "can't update yet. ready is false")
log.Error(errors.New("Update() is not ready yet")) log.Error(errors.New("Update() is not ready yet"))
return return
} }
log.Log(INFO, "Update() START")
duration := timeFunction(func() {
// do things that are safe even if the git tree is dirty // do things that are safe even if the git tree is dirty
// rs.path.SetValue(rs.repopath) // rs.path.SetValue(rs.repopath)
rs.getCurrentBranchName() rs.getCurrentBranchName()
@ -25,6 +38,57 @@ func (rs *RepoStatus) Update() {
rs.populateTags() rs.populateTags()
rs.CheckDirty() rs.CheckDirty()
// get the master branch version
mName := rs.GetMasterBranchName()
cmd := []string{"git", "describe", "--tags", mName}
err, out := rs.RunCmd(cmd)
if err == nil {
log.Log(INFO, "git cmd =", cmd, "worked =", out)
rs.SetMasterVersion(out)
} else {
log.Log(WARN, "git cmd =", cmd)
log.Log(WARN, "git err =", err)
log.Log(WARN, "git master failed", mName, rs.Path())
}
// get the devel branch version
dName := rs.GetDevelBranchName()
cmd = []string{"git", "describe", "--tags", dName}
err, out = rs.RunCmd(cmd)
if err == nil {
log.Log(INFO, "git cmd =", cmd, "worked =", out)
rs.SetDevelVersion(out)
} else {
log.Log(WARN, "git cmd =", cmd)
log.Log(WARN, "git err =", err)
log.Log(WARN, "git devel failed", dName, rs.Path())
}
// get the user branch version
uName := rs.GetUserBranchName()
cmd = []string{"git", "describe", "--tags", uName}
err, out = rs.RunCmd(cmd)
if err == nil {
log.Log(INFO, "git cmd =", cmd, "worked =", out)
rs.SetUserVersion(out)
} else {
log.Log(WARN, "git cmd =", cmd)
log.Log(WARN, "git err =", err)
log.Log(WARN, "git user failed", uName, rs.Path())
}
}
// deprecate / redo what is left of this
func (rs *RepoStatus) UpdateOld() {
if !rs.Ready() {
log.Log(WARN, "can't update yet. ready is false")
log.Error(errors.New("Update() is not ready yet"))
return
}
log.Log(INFO, "Update() START")
duration := timeFunction(func() {
rs.UpdateNew()
if rs.dirtyLabel.String() != "no" { if rs.dirtyLabel.String() != "no" {
log.Warn("dirty label != no. actual value:", rs.dirtyLabel.String()) log.Warn("dirty label != no. actual value:", rs.dirtyLabel.String())
rs.DisableEverything() rs.DisableEverything()