ready to work on creating branches

This commit is contained in:
Jeff Carr 2024-02-16 17:55:13 -06:00
parent bd62a89a67
commit 0b4f4d7686
9 changed files with 184 additions and 138 deletions

View File

@ -1,26 +1,11 @@
package repostatus package repostatus
import ( import (
"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)
@ -51,7 +36,7 @@ func (rs *RepoStatus) makeBranchesBox(parent *gui.Node) {
} else { } else {
log.Info("branch switched to", bname, "failed") log.Info("branch switched to", bname, "failed")
} }
rs.UpdateCurrent() rs.UpdateNew()
}) })
rs.targetBranch = newgrid.NewDropdown() // `progname:"TARGET"` rs.targetBranch = newgrid.NewDropdown() // `progname:"TARGET"`

View File

@ -9,12 +9,23 @@ import (
// reports externally if something has changed // reports externally if something has changed
// since the last time it was asked about it // since the last time it was asked about it
func (rs *RepoStatus) Changed() bool { func (rs *RepoStatus) Changed() (string, bool) {
if !rs.Ready() { if !rs.Ready() {
return false return "", false
} }
return rs.changed return rs.Changes(), rs.changed
}
// keeps a human readable list of things that have
// changed
func (rs *RepoStatus) Changes() string {
return rs.changes
}
func (rs *RepoStatus) NoteChange(s string) {
rs.changed = true
rs.changes += s + "\n"
} }
// deprecate this. returns the gopath right now // deprecate this. returns the gopath right now

View File

@ -260,7 +260,6 @@ func (rs *RepoStatus) recommend() {
rs.develMergeB.SetLabel(label) rs.develMergeB.SetLabel(label)
return return
} }
rs.getLastTagVersion()
if rs.lasttag.String() != rs.masterBranchVersion.String() { if rs.lasttag.String() != rs.masterBranchVersion.String() {
log.Log(INFO, "master does not equal last tag") log.Log(INFO, "master does not equal last tag")
rs.incrementVersion() rs.incrementVersion()

147
git.go
View File

@ -1,6 +1,7 @@
package repostatus package repostatus
import ( import (
"errors"
"strings" "strings"
"time" "time"
"unicode/utf8" "unicode/utf8"
@ -26,53 +27,88 @@ func (rs *RepoStatus) GetLastTagVersion() string {
return rs.lasttag.String() return rs.lasttag.String()
} }
func (rs *RepoStatus) getCurrentBranchName() string { // stores the current branch name
func (rs *RepoStatus) checkCurrentBranchName() string {
currentname := rs.currentBranch.String()
out := run(rs.realPath.String(), "git", "branch --show-current") out := run(rs.realPath.String(), "git", "branch --show-current")
log.Log(INFO, "getCurrentBranchName() =", out) if currentname == out {
// nothing changed
return currentname
}
rs.currentBranch.SetValue(out) rs.currentBranch.SetValue(out)
if currentname == "" {
return out // don't note if there was nothing before
}
rs.NoteChange("current branch has changed from " + currentname + " to " + out)
return out return out
} }
func (rs *RepoStatus) gitDescribeTags(name string) (string, error) { func (rs *RepoStatus) getCurrentBranchName() string {
return rs.currentBranch.String()
}
func (rs *RepoStatus) gitDescribeByHash(hash string) (string, error) {
if hash == "" {
return "", errors.New("hash was blank")
}
err, out := rs.RunCmd([]string{"git", "describe", "--tags", "--always", hash})
if err != nil {
log.Warn("not in a git repo or bad hash?", err, rs.Path())
return "", err
}
out = strings.TrimSpace(out)
return out, err
}
func (rs *RepoStatus) gitDescribeByName(name string) (string, error) {
name = strings.TrimSpace(name) name = strings.TrimSpace(name)
if name == "" { if name == "" {
// git will return the current tag // git will return the current tag
err, out := rs.RunCmd([]string{"git", "describe", "--tags"}) err, out := rs.RunCmd([]string{"git", "describe", "--tags", "--always"})
if err != nil { if err != nil {
log.Warn("not in a git repo?", err, rs.Path()) log.Warn("not in a git repo?", err, rs.Path())
return "", err return "", err
} }
out = strings.TrimSpace(out)
return out, err return out, err
} }
err, out := rs.RunCmd([]string{"git", "describe", "--tags", name}) if !rs.LocalTagExists(name) {
// tag does not exist
return "", errors.New("git fatal: Not a valid object name")
}
cmd := []string{"git", "describe", "--tags", "--always", name}
err, out := rs.RunCmd(cmd)
if err != nil { if err != nil {
log.Warn("not in a git repo or bad tag?", err, rs.Path()) log.Warn("cmd =", cmd)
log.Warn("err =", err)
log.Warn("not in a git repo or bad tag?", rs.Path())
return "", err return "", err
} }
out = strings.TrimSpace(out)
return out, err return out, err
} }
// todo: don't run git every time? // todo: don't run git every time?
func (rs *RepoStatus) getCurrentBranchVersion() string { func (rs *RepoStatus) checkCurrentBranchVersion() string {
out, _ := rs.gitDescribeTags("") out, _ := rs.gitDescribeByName("")
log.Log(INFO, "getCurrentBranchVersion()", out) log.Log(INFO, "checkCurrentBranchVersion()", out)
rs.currentVersion.SetValue(out) rs.currentVersion.SetValue(out)
return out return out
} }
func (rs *RepoStatus) getLastTagVersion() string { func (rs *RepoStatus) getCurrentBranchVersion() string {
out := run(rs.realPath.String(), "git", "rev-list --tags --max-count=1") return rs.currentVersion.String()
log.Log(INFO, "getLastTagVersion()", out) }
// rs.lasttagrev = out
lastreal := "describe --tags " + out // this should get the most recent tag
// out = run(r.path, "git", "describe --tags c871d5ecf051a7dc4e3a77157cdbc0a457eb9ae1") func (rs *RepoStatus) setLastTagVersion() {
out = run(rs.realPath.String(), "git", lastreal) hash := run(rs.realPath.String(), "git", "rev-list --tags --max-count=1")
rs.lasttag.SetValue(out) log.Log(INFO, "getLastTagVersion()", hash)
// rs.tagsDrop.SetText(out)
// rs.lastLabel.SetText(out) name, _ := rs.gitDescribeByHash(hash)
return out rs.lasttag.SetText(name)
return
} }
func (rs *RepoStatus) populateTags() { func (rs *RepoStatus) populateTags() {
@ -141,17 +177,32 @@ func (rs *RepoStatus) CheckDirty() bool {
return true return true
} }
func (rs *RepoStatus) CheckoutBranch(bname string) bool {
/* if rs.CheckDirty() {
func (rs *RepoStatus) CheckoutBranch(branch string) (string, string) { log.Log(INFO, rs.realPath.String(), "is dirty")
// run(rs.realPath.String(), "git", "checkout " + branch) log.Info("bname is dirty", bname, rs.Path())
return false
realname := rs.getCurrentBranchName() }
realversion := rs.getCurrentBranchVersion() if !rs.TagExists(bname) {
log.Log(INFO, rs.realPath.String(), "realname =", realname, "realversion =", realversion) // tag does not exist
return realname, realversion log.Info("bname already exists", bname, rs.Path())
return false
}
cName := rs.GetCurrentBranchName()
if cName == bname {
// already on branch
return true
}
cmd := []string{"git", "checkout", bname}
err, b, output := RunCmd(rs.realPath.String(), cmd)
if err != nil {
log.Log(INFO, err, b, output)
return false
}
rs.checkCurrentBranchName()
rs.checkCurrentBranchVersion()
return true
} }
*/
func (rs *RepoStatus) CheckoutMaster() bool { func (rs *RepoStatus) CheckoutMaster() bool {
if rs.CheckDirty() { if rs.CheckDirty() {
@ -308,7 +359,6 @@ func (rs *RepoStatus) setDevelWorkingName(s string) {
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)
} }
// returns "master", "devel", os.Username, etc // returns "master", "devel", os.Username, etc
@ -340,31 +390,42 @@ func (rs *RepoStatus) GetUserVersion() string {
return name return name
} }
func (rs *RepoStatus) SetMasterVersion(s string) { func (rs *RepoStatus) setMasterVersion(s string) {
if rs.GetMasterVersion() == s { old := rs.GetMasterVersion()
if old == s {
return return
} }
rs.changed = true
log.Verbose("git", rs.GetMasterBranchName(), "is now version =", s)
rs.masterBranchVersion.SetValue(s) rs.masterBranchVersion.SetValue(s)
if old == "" {
return // don't note if there was nothing before
}
rs.NoteChange("master branch has been changed from " + old + " to " + s)
} }
func (rs *RepoStatus) SetDevelVersion(s string) { func (rs *RepoStatus) setDevelVersion(s string) {
if rs.GetDevelVersion() == s { old := rs.GetDevelVersion()
if old == s {
return return
} }
rs.changed = true if old == "" {
// don't note nothing
} else {
rs.NoteChange("devel branch has been changed from " + old + " to " + s)
}
rs.develBranchVersion.SetValue(s) rs.develBranchVersion.SetValue(s)
log.Verbose("git", rs.GetDevelBranchName(), "s now version =", s)
} }
func (rs *RepoStatus) SetUserVersion(s string) { func (rs *RepoStatus) setUserVersion(s string) {
if rs.GetUserVersion() == s { old := rs.GetUserVersion()
if old == s {
return return
} }
rs.changed = true if old == "" {
// don't note nothing
} else {
rs.NoteChange("user branch has been changed from " + old + " to " + s)
}
rs.userBranchVersion.SetValue(s) rs.userBranchVersion.SetValue(s)
// log.Verbose("git", rs.GetUserBranchName(), "is now version =", s)
} }
func (rs *RepoStatus) GetStatus() string { func (rs *RepoStatus) GetStatus() string {

View File

@ -269,10 +269,7 @@ func (rs *RepoStatus) processBranch(branch string) {
} }
} }
var cmd []string name, _ := rs.gitDescribeByHash(newhash)
cmd = append(cmd, "git", "describe", "--tags", newhash) rs.gitConfig.versions[newhash] = name
_, _, output := RunCmd(rs.realPath.String(), cmd) log.Log(INFO, " hash: version", name)
output = strings.TrimSpace(output)
rs.gitConfig.versions[newhash] = output
log.Log(INFO, " hash: version", output)
} }

View File

@ -10,14 +10,10 @@ 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("UpdateNew()", func() { newgrid.NewButton("Rescan Repo", func() {
rs.UpdateNew() rs.UpdateNew()
}) })
newgrid.NewButton("updateOld()", func() {
rs.UpdateOld()
})
newgrid.NewButton("CheckDirty()", func() { newgrid.NewButton("CheckDirty()", func() {
if rs.CheckDirty() { if rs.CheckDirty() {
log.Log(WARN, "is dirty") log.Log(WARN, "is dirty")

View File

@ -6,8 +6,11 @@ import (
) )
type RepoStatus struct { type RepoStatus struct {
ready bool ready bool
// keeps track of changes that might have happened
changed bool changed bool
changes string
tags map[string]string tags map[string]string

View File

@ -1,6 +1,7 @@
package repostatus package repostatus
import ( import (
"path/filepath"
"regexp" "regexp"
"slices" "slices"
"strings" "strings"
@ -281,3 +282,33 @@ func (rt *repoTag) Show() {
rt.subject.Show() rt.subject.Show()
rt.deleteB.Show() rt.deleteB.Show()
} }
func (rs *RepoStatus) TagExists(findname string) bool {
allTags := rs.Tags.ListAll()
for _, t := range allTags {
tagname := t.TagString()
_, filename := filepath.Split(tagname)
if filename == findname {
// log.Info("found tag:", path, filename, "from", rs.Path())
return true
}
}
return false
}
func (rs *RepoStatus) LocalTagExists(findname string) bool {
allTags := rs.Tags.ListAll()
for _, t := range allTags {
tagname := t.TagString()
if strings.HasPrefix(tagname, "refs/remotes") {
continue
}
path, filename := filepath.Split(tagname)
log.Log(INFO, "tag:", path, filename, "from", rs.Path())
if filename == findname {
log.Log(INFO, "found tag:", path, filename, "from", rs.Path())
return true
}
}
return false
}

View File

@ -29,55 +29,45 @@ func (rs *RepoStatus) UpdateNew() {
log.Error(errors.New("Update() is not ready yet")) log.Error(errors.New("Update() is not ready yet"))
return return
} }
// do things that are safe even if the git tree is dirty
// rs.path.SetValue(rs.repopath) // store the current checked out branch name and version
rs.getCurrentBranchName() rs.checkCurrentBranchName()
// rs.window.SetTitle(rs.repopath + " GO repo Details") rs.checkCurrentBranchVersion()
rs.getCurrentBranchVersion()
rs.getLastTagVersion() // read in the tags
rs.populateTags() rs.populateTags()
// record if the repo is dirty
rs.CheckDirty() rs.CheckDirty()
// get the master branch version // store the last tag version
rs.setLastTagVersion()
// store the master branch version
mName := rs.GetMasterBranchName() mName := rs.GetMasterBranchName()
cmd := []string{"git", "describe", "--tags", mName} out, _ := rs.gitDescribeByName(mName)
err, out := rs.RunCmd(cmd) rs.setMasterVersion(out)
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 // store the devel branch version
dName := rs.GetDevelBranchName() dName := rs.GetDevelBranchName()
cmd = []string{"git", "describe", "--tags", dName} if dName == "" {
err, out = rs.RunCmd(cmd) rs.setDevelVersion("")
if err == nil {
log.Log(INFO, "git cmd =", cmd, "worked =", out)
rs.SetDevelVersion(out)
} else { } else {
log.Log(WARN, "git cmd =", cmd) out, _ = rs.gitDescribeByName(dName)
log.Log(WARN, "git err =", err) rs.setDevelVersion(out)
log.Log(WARN, "git devel failed", dName, rs.Path())
} }
// get the user branch version // store the user branch version
uName := rs.GetUserBranchName() uName := rs.GetUserBranchName()
cmd = []string{"git", "describe", "--tags", uName} if uName == "" {
err, out = rs.RunCmd(cmd) rs.setUserVersion("")
if err == nil {
log.Log(INFO, "git cmd =", cmd, "worked =", out)
rs.SetUserVersion(out)
} else { } else {
log.Log(WARN, "git cmd =", cmd) out, _ = rs.gitDescribeByName(uName)
log.Log(WARN, "git err =", err) rs.setUserVersion(out)
log.Log(WARN, "git user failed", uName, rs.Path())
} }
} }
/*
// deprecate / redo what is left of this // deprecate / redo what is left of this
func (rs *RepoStatus) UpdateOld() { func (rs *RepoStatus) UpdateOld() {
if !rs.Ready() { if !rs.Ready() {
@ -120,6 +110,7 @@ func (rs *RepoStatus) UpdateOld() {
rs.setSpeed(duration) rs.setSpeed(duration)
log.Log(INFO, "Update() END") log.Log(INFO, "Update() END")
} }
*/
func (rs *RepoStatus) setSpeed(duration time.Duration) { func (rs *RepoStatus) setSpeed(duration time.Duration) {
s := fmt.Sprint(duration) s := fmt.Sprint(duration)
@ -194,31 +185,3 @@ func (rs *RepoStatus) EnableSelectTag() {
rs.releaseVersion.Enable() rs.releaseVersion.Enable()
} }
} }
// This doesn't switch branches
func (rs *RepoStatus) UpdateCurrent() {
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")
rs.getCurrentBranchName()
rs.getCurrentBranchVersion()
rs.getLastTagVersion()
rs.populateTags()
rs.CheckDirty()
// read in the .git/config each update
rs.readGitConfig()
// this looks into .git somewhat
rs.CheckBranches()
if rs.dirtyLabel.String() != "no" {
// the repo is dirty
log.Warn("dirty label != no. actual value:", rs.dirtyLabel.String())
rs.DisableEverything()
return
}
}