From bd62a89a670eab24ff5fd7b1ed155b89dde08157 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 16 Feb 2024 11:41:29 -0600 Subject: [PATCH] continue cleaning up original version --- branchesBox.go | 37 ++++++++++++++++------- common.go | 17 ++++++++++- draw.go | 7 +++-- git.go | 65 ++++++++++++++++++++++++++++++++++++--- gitConfig.go | 18 +++-------- modifyBox.go | 8 ++--- new.go | 25 +++++++++++++-- structs.go | 1 + unix.go | 32 +++++--------------- update.go | 82 ++++++++++++++++++++++++++++++++++++++++++++------ 10 files changed, 219 insertions(+), 73 deletions(-) diff --git a/branchesBox.go b/branchesBox.go index c1de0e2..354fe81 100644 --- a/branchesBox.go +++ b/branchesBox.go @@ -1,13 +1,26 @@ package repostatus import ( - "strings" + "path/filepath" "go.wit.com/gui" "go.wit.com/lib/gadgets" "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) { rs.gitBranchesGroup = parent.NewGroup("branches") // `progname:"BRANCHES"` // can the toolkits use these for i18n support? newgrid := rs.gitBranchesGroup.NewGrid("gridnuts", 0, 0) @@ -45,18 +58,20 @@ func (rs *RepoStatus) makeBranchesBox(parent *gui.Node) { // rs.targetBranch.AddText("master") newgrid.NextRow() - rs.showBranchesButton = newgrid.NewButton("scan branches()", func() { - err, out := rs.RunCmd([]string{"git", "branch", "--all"}) - if err != nil { - log.Log(WARN, "git branch failed", rs.String()) + // runs "git branch --all" + rs.gitBranchAll() + + 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") - for i, s := range all { - log.Log(WARN, "found branch", i, s) - rs.targetBranch.AddText(s) + if rs.TagExists("devel") { + log.Log(WARN, "tag devel exists") + } else { + log.Log(WARN, "tag devel does not exist") } - i := len(all) - log.Log(WARN, "branch count =", i) }) newgrid.NextRow() diff --git a/common.go b/common.go index d725fbb..9df2b74 100644 --- a/common.go +++ b/common.go @@ -39,7 +39,6 @@ func (rs *RepoStatus) GetPath() string { } */ - /* func (rs *RepoStatus) Draw() { if !rs.Ready() { @@ -88,6 +87,16 @@ func (rs *RepoStatus) Ready() bool { 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 { log.Log(CHANGE, "checking Initialized()") @@ -98,6 +107,9 @@ func (rs *RepoStatus) Initialized() bool { */ 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}}'"}) if err == nil { output = strings.Trim(output, "'") @@ -117,6 +129,9 @@ func (rs *RepoStatus) BinaryName() string { } func (rs *RepoStatus) Build() bool { + if !rs.IsGoLang() { + return false + } name := rs.BinaryName() // removes the binary if it already exists rs.RunCmd([]string{"rm", "-f", name}) diff --git a/draw.go b/draw.go index 4790230..00707e7 100644 --- a/draw.go +++ b/draw.go @@ -19,7 +19,8 @@ func (rs *RepoStatus) drawGitStatus(box *gui.Node) { rs.path = gadgets.NewOneLiner(newgrid, "path") rs.goSrcPath = gadgets.NewOneLiner(newgrid, "~/go/src") 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.SetValue("???") rs.develWorkingName = gadgets.NewOneLiner(newgrid, "devel working branch") @@ -66,7 +67,7 @@ func (rs *RepoStatus) RunDevelMergeB() bool { log.Warn("RunDevelMergeB() SOMETHING WENT WRONG") return false } - rs.Update() + rs.UpdateNew() log.Warn("RunDevelMergeB() THINGS SEEM OK runGitCommands() returned true.") return true } @@ -80,7 +81,7 @@ func (rs *RepoStatus) runReleaseVersionB() bool { log.Warn("MAKING A RELEASE AND VERSION") if !rs.runGitCommands(true) { log.Warn("SOMETHING WENT WRONG") - rs.Update() + rs.UpdateNew() rs.Enable() return false } diff --git a/git.go b/git.go index 3a3fcdd..517e328 100644 --- a/git.go +++ b/git.go @@ -33,8 +33,29 @@ func (rs *RepoStatus) getCurrentBranchName() string { 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 { - out := run(rs.realPath.String(), "git", "describe --tags") + out, _ := rs.gitDescribeTags("") log.Log(INFO, "getCurrentBranchVersion()", out) rs.currentVersion.SetValue(out) return out @@ -159,6 +180,13 @@ func (rs *RepoStatus) CheckoutMaster() bool { func (rs *RepoStatus) CheckoutDevel() bool { devel := rs.develWorkingName.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) rs.checkoutBranch("devel", devel) @@ -167,6 +195,33 @@ func (rs *RepoStatus) CheckoutDevel() bool { 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) { if rs.CheckDirty() { 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 { log.Info("rs == nil", s) return @@ -243,12 +300,12 @@ func (rs *RepoStatus) SetMainWorkingName(s string) { rs.mainWorkingName.SetValue(s) } -func (rs *RepoStatus) SetDevelWorkingName(s string) { +func (rs *RepoStatus) setDevelWorkingName(s string) { rs.develWorkingName.SetValue(s) rs.develBranchVersion.SetLabel(s) } -func (rs *RepoStatus) SetUserWorkingName(s string) { +func (rs *RepoStatus) setUserWorkingName(s string) { rs.userWorkingName.SetValue(s) rs.userBranchVersion.SetLabel(s) // rs.userDrop.SetText(s) diff --git a/gitConfig.go b/gitConfig.go index cdafd47..9fc4737 100644 --- a/gitConfig.go +++ b/gitConfig.go @@ -79,6 +79,7 @@ func (rs *RepoStatus) readGitConfig() error { filename := filepath.Join(rs.realPath.String(), "/.git/config") file, err := os.Open(filename) if err != nil { + log.Log(WARN, "readGitConfig() failed for file:", filename) filename = filepath.Join(rs.realPath.String(), "../.git/config") log.Log(WARN, "readGitConfig() trying up one directory instead", 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() { if rs.ReadGoMod() { 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 { if rs.readOnly.String() == "true" { return true diff --git a/modifyBox.go b/modifyBox.go index 2a294b0..8dfd289 100644 --- a/modifyBox.go +++ b/modifyBox.go @@ -10,12 +10,12 @@ func (rs *RepoStatus) drawGitCommands(box *gui.Node) { rs.gitCommandsGroup = box.NewGroup("modify") newgrid := rs.gitCommandsGroup.NewGrid("gridnuts", 0, 0) - newgrid.NewButton("update", func() { - rs.Update() + newgrid.NewButton("UpdateNew()", func() { + rs.UpdateNew() }) - newgrid.NewButton("UpdateCurrent()", func() { - rs.Update() + newgrid.NewButton("updateOld()", func() { + rs.UpdateOld() }) newgrid.NewButton("CheckDirty()", func() { diff --git a/new.go b/new.go index 8d11c8d..289966e 100644 --- a/new.go +++ b/new.go @@ -2,6 +2,7 @@ package repostatus import ( "os" + "os/user" "path/filepath" "strings" @@ -63,9 +64,9 @@ func NewRepoStatusWindow(path string) *RepoStatus { _, 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 nil + // log.Log(WARN, "Error reading .git/config:", filename, err) + // log.Log(WARN, "TODO: find .git/config in parent directory") + return nil } rs := &RepoStatus{ @@ -113,6 +114,24 @@ func NewRepoStatusWindow(path string) *RepoStatus { if strings.HasPrefix(path, "git.wit.org") { 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 return rs diff --git a/structs.go b/structs.go index a8f4698..7b7bcd7 100644 --- a/structs.go +++ b/structs.go @@ -23,6 +23,7 @@ type RepoStatus struct { path *gadgets.OneLiner goSrcPath *gadgets.OneLiner realPath *gadgets.OneLiner + isGoLang *gadgets.OneLiner currentBranch *gadgets.OneLiner currentVersion *gadgets.OneLiner diff --git a/unix.go b/unix.go index b1fcff7..ef2a9ac 100644 --- a/unix.go +++ b/unix.go @@ -242,30 +242,6 @@ func Exists(file string) bool { 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) { data, err := ioutil.ReadFile(filename) 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" tagTime, err := time.Parse(gitLayout, gitdefault) if err != nil { - log.Warn("GOT THIS IN PARSE AAA."+gitdefault+".AAA") + log.Warn("GOT THIS IN PARSE AAA." + gitdefault + ".AAA") log.Warn(err) 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 hours := int(d.Hours()) % 24 days := int(d.Hours()) / 24 + years := int(d.Hours()) / (24 * 365) result := "" + if years > 0 { + result += fmt.Sprintf("%dy ", years) + return result + } if days > 0 { result += fmt.Sprintf("%dd ", days) return result @@ -350,6 +331,7 @@ func (rs *RepoStatus) Xterm(cmdline string) { func (rs *RepoStatus) XtermWait(cmdline string) { shell.XtermCmdWait(rs.Path(), []string{cmdline}) } + /* func (rs *RepoStatus) XtermNohup(args []string) { var argsX = []string{"xterm", "-geometry", "120x40"} diff --git a/update.go b/update.go index 35bee63..3cafce2 100644 --- a/update.go +++ b/update.go @@ -3,12 +3,83 @@ package repostatus import ( "errors" "fmt" + "strings" "time" "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() { + log.Log(WARN, "can't update yet. ready is false") + log.Error(errors.New("Update() is not ready yet")) + return + } + // do things that are safe even if the git tree is dirty + // rs.path.SetValue(rs.repopath) + rs.getCurrentBranchName() + // rs.window.SetTitle(rs.repopath + " GO repo Details") + rs.getCurrentBranchVersion() + rs.getLastTagVersion() + rs.populateTags() + 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")) @@ -16,14 +87,7 @@ func (rs *RepoStatus) Update() { } log.Log(INFO, "Update() START") duration := timeFunction(func() { - // do things that are safe even if the git tree is dirty - // rs.path.SetValue(rs.repopath) - rs.getCurrentBranchName() - // rs.window.SetTitle(rs.repopath + " GO repo Details") - rs.getCurrentBranchVersion() - rs.getLastTagVersion() - rs.populateTags() - rs.CheckDirty() + rs.UpdateNew() if rs.dirtyLabel.String() != "no" { log.Warn("dirty label != no. actual value:", rs.dirtyLabel.String())