cleanup checkdirty code

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-02-24 05:37:53 -06:00
parent ccee573364
commit cec5e7f3b4
2 changed files with 32 additions and 44 deletions

75
git.go
View File

@ -145,7 +145,7 @@ func (rs *RepoStatus) gitDescribeByName(name string) (string, error) {
// todo: don't run git every time? // todo: don't run git every time?
func (rs *RepoStatus) checkCurrentBranchVersion() string { func (rs *RepoStatus) checkCurrentBranchVersion() string {
out, _ := rs.gitDescribeByName("") out, _ := rs.gitDescribeByName("")
log.Log(INFO, "checkCurrentBranchVersion()", out) log.Log(REPO, "checkCurrentBranchVersion()", out)
rs.currentVersion.SetValue(out) rs.currentVersion.SetValue(out)
return out return out
} }
@ -153,7 +153,7 @@ func (rs *RepoStatus) checkCurrentBranchVersion() string {
// this should get the most recent tag // this should get the most recent tag
func (rs *RepoStatus) setLastTagVersion() { func (rs *RepoStatus) setLastTagVersion() {
hash := run(rs.realPath.String(), "git", "rev-list --tags --max-count=1") hash := run(rs.realPath.String(), "git", "rev-list --tags --max-count=1")
log.Log(INFO, "getLastTagVersion()", hash) log.Log(REPO, "getLastTagVersion()", hash)
name, _ := rs.gitDescribeByHash(hash) name, _ := rs.gitDescribeByHash(hash)
rs.lasttag.SetText(name) rs.lasttag.SetText(name)
@ -162,10 +162,10 @@ func (rs *RepoStatus) setLastTagVersion() {
func (rs *RepoStatus) populateTags() { func (rs *RepoStatus) populateTags() {
tmp := rs.realPath.String() + "/.git/refs/tags" tmp := rs.realPath.String() + "/.git/refs/tags"
log.Log(INFO, "populateTags() path =", tmp) log.Log(REPO, "populateTags() path =", tmp)
for _, tag := range listFiles(tmp) { for _, tag := range listFiles(tmp) {
if rs.tags[tag] == "" { if rs.tags[tag] == "" {
log.Log(INFO, "populateTags() Adding new tag", tag) log.Log(REPO, "populateTags() Adding new tag", tag)
// rs.tagsDrop.AddText(tag) // rs.tagsDrop.AddText(tag)
rs.tags[tag] = "origin" rs.tags[tag] = "origin"
} }
@ -185,25 +185,25 @@ func (rs *RepoStatus) getBranches() []string {
all = append(all, remotes...) all = append(all, remotes...)
for _, branch := range all { for _, branch := range all {
log.Log(INFO, "getBranches()", branch) log.Log(REPO, "getBranches()", branch)
} }
return all return all
} }
// returns quickly based on the last time it was checked // returns quickly based on the last time it was checked
func (rs *RepoStatus) IsDirty() bool { func (rs *RepoStatus) IsDirty() bool {
return rs.dirty if rs.dirtyLabel.String() == "no" {
return false
}
return true
} }
func (rs *RepoStatus) CheckDirty() bool { func (rs *RepoStatus) CheckDirty() bool {
var start string = rs.dirtyLabel.String() var start string = rs.dirtyLabel.String()
cmd := []string{"git", "status"} cmd := []string{"git", "status"}
path := rs.realPath.String() err, out := rs.RunCmd(cmd)
err, b, out := RunCmd(path, cmd)
if err != nil { if err != nil {
log.Warn("CheckDirty() status b =", b)
log.Warn("CheckDirty() status cmd =", cmd) log.Warn("CheckDirty() status cmd =", cmd)
log.Warn("CheckDirty() status path =", path)
log.Warn("CheckDirty() status out =", out) log.Warn("CheckDirty() status out =", out)
log.Warn("CheckDirty() status err =", err) log.Warn("CheckDirty() status err =", err)
log.Error(err, "CheckDirty() git status error") log.Error(err, "CheckDirty() git status error")
@ -211,48 +211,37 @@ func (rs *RepoStatus) CheckDirty() bool {
if start != "error" { if start != "error" {
rs.NoteChange("repo git status is in error " + fmt.Sprint(err)) rs.NoteChange("repo git status is in error " + fmt.Sprint(err))
} }
rs.dirty = true
return true return true
} }
last := out[strings.LastIndex(out, "\n")+1:] last := out[strings.LastIndex(out, "\n")+1:]
if last == "nothing to commit, working tree clean" { if last == "nothing to commit, working tree clean" {
log.Log(INFO, "CheckDirty() b =", b, "path =", path, "out =", out) log.Log(REPO, "CheckDirty() no", rs.realPath.String())
log.Log(INFO, "CheckDirty() no", rs.realPath.String())
rs.dirtyLabel.SetValue("no") rs.dirtyLabel.SetValue("no")
if start != "no" { if start != "no" {
rs.NoteChange("repo is no longer dirty") rs.NoteChange("repo is no longer dirty")
} }
rs.dirty = false
return false return false
} }
// sometimes b gets exit status 1 when there isn't anything that has changed
// run git status fixes that for some reason.
log.Log(INFO, "CheckDirty() is normal dirty", rs.realPath.String())
log.Log(INFO, "CheckDirty() is normal cmd =", cmd)
log.Log(INFO, "CheckDirty() is normal b =", b)
log.Log(INFO, "CheckDirty() is normal path =", path)
log.Log(INFO, "CheckDirty() is normal out =", out)
log.Log(INFO, "CheckDirty() is normal err =", err)
rs.dirtyLabel.SetValue("dirty") rs.dirtyLabel.SetValue("dirty")
if start != "dirty" { if start != "dirty" {
log.Log(REPOWARN, "repo is now dirty")
rs.NoteChange("repo is now dirty") rs.NoteChange("repo is now dirty")
} }
rs.dirty = true
return true return true
} }
func (rs *RepoStatus) CheckoutBranch(bname string) bool { func (rs *RepoStatus) CheckoutBranch(bname string) bool {
if rs.CheckDirty() { if rs.CheckDirty() {
log.Log(INFO, rs.realPath.String(), "is dirty") log.Log(REPO, rs.realPath.String(), "is dirty")
log.Info(bname, "is dirty", rs.Path()) log.Info(bname, "is dirty", rs.Path())
return false return false
} }
if !rs.TagExists(bname) { if !rs.TagExists(bname) {
// tag does not exist // tag does not exist
log.Log(INFO, "repo does not have branch", bname, rs.Path()) log.Log(REPO, "repo does not have branch", bname, rs.Path())
return false return false
} }
cName := rs.GetCurrentBranchName() cName := rs.GetCurrentBranchName()
@ -263,7 +252,7 @@ func (rs *RepoStatus) CheckoutBranch(bname string) bool {
cmd := []string{"git", "checkout", bname} cmd := []string{"git", "checkout", bname}
err, b, output := RunCmd(rs.realPath.String(), cmd) err, b, output := RunCmd(rs.realPath.String(), cmd)
if err != nil { if err != nil {
log.Log(INFO, err, b, output) log.Log(REPO, err, b, output)
return false return false
} }
rs.checkCurrentBranchName() rs.checkCurrentBranchName()
@ -273,7 +262,7 @@ func (rs *RepoStatus) CheckoutBranch(bname string) bool {
func (rs *RepoStatus) CheckoutMaster() bool { func (rs *RepoStatus) CheckoutMaster() bool {
if rs.CheckDirty() { if rs.CheckDirty() {
log.Log(INFO, rs.realPath.String(), "is dirty") log.Log(REPO, rs.realPath.String(), "is dirty")
return false return false
} }
mName := rs.GetMasterBranchName() mName := rs.GetMasterBranchName()
@ -290,13 +279,13 @@ func (rs *RepoStatus) CheckoutDevel() bool {
return false return false
} }
if rs.CheckDirty() { if rs.CheckDirty() {
log.Log(INFO, rs.realPath.String(), "is dirty") log.Log(REPO, rs.realPath.String(), "is dirty")
return false return false
} }
log.Log(INFO, "checkoutBranch", devel) log.Log(REPO, "checkoutBranch", devel)
rs.checkoutBranch("devel", devel) rs.checkoutBranch("devel", devel)
// log.Log(INFO, "checkoutBranch", user) // log.Log(REPO, "checkoutBranch", user)
// rs.checkoutBranch("user", user) // rs.checkoutBranch("user", user)
return true return true
} }
@ -307,21 +296,21 @@ func (rs *RepoStatus) CheckoutUser() bool {
return false return false
} }
if rs.CheckDirty() { if rs.CheckDirty() {
log.Log(INFO, rs.realPath.String(), "is dirty") log.Log(REPO, rs.realPath.String(), "is dirty")
return false return false
} }
cmd := []string{"git", "checkout", bName} cmd := []string{"git", "checkout", bName}
err, b, output := RunCmd(rs.realPath.String(), cmd) err, b, output := RunCmd(rs.realPath.String(), cmd)
if err != nil { if err != nil {
log.Log(INFO, err, b, output) log.Log(REPO, err, b, output)
} }
realname := rs.GetCurrentBranchName() realname := rs.GetCurrentBranchName()
realversion := rs.GetCurrentBranchVersion() realversion := rs.GetCurrentBranchVersion()
log.Log(INFO, rs.realPath.String(), "realname =", realname, "realversion =", realversion) log.Log(REPO, rs.realPath.String(), "realname =", realname, "realversion =", realversion)
if realname != bName { if realname != bName {
log.Log(INFO, "git checkout failed", rs.realPath.String(), bName, "!=", realname) log.Log(REPO, "git checkout failed", rs.realPath.String(), bName, "!=", realname)
return false return false
} }
rs.userBranchVersion.SetValue(realversion) rs.userBranchVersion.SetValue(realversion)
@ -330,15 +319,15 @@ func (rs *RepoStatus) CheckoutUser() bool {
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(REPO, "checkoutBranch() checkDirty() == true for repo", rs.realPath.String(), "looking for branch:", branch)
return return
} }
out := run(rs.realPath.String(), "git", "checkout "+branch) out := run(rs.realPath.String(), "git", "checkout "+branch)
log.Log(INFO, rs.realPath.String(), "git checkout "+branch, "returned", out) log.Log(REPO, rs.realPath.String(), "git checkout "+branch, "returned", out)
realname := rs.GetCurrentBranchName() realname := rs.GetCurrentBranchName()
realversion := rs.GetCurrentBranchVersion() realversion := rs.GetCurrentBranchVersion()
log.Log(INFO, rs.realPath.String(), "realname =", realname, "realversion =", realversion) log.Log(REPO, rs.realPath.String(), "realname =", realname, "realversion =", realversion)
switch level { switch level {
case "master": case "master":
@ -490,7 +479,7 @@ func (rs *RepoStatus) GetStatus() string {
func (rs *RepoStatus) setState() { func (rs *RepoStatus) setState() {
rs.changed = false rs.changed = false
if rs.CheckDirty() { if rs.CheckDirty() {
log.Log(INFO, "CheckDirty() true") log.Log(REPO, "CheckDirty() true")
rs.gitState.SetText("dirty") rs.gitState.SetText("dirty")
return return
} }
@ -508,11 +497,11 @@ func (rs *RepoStatus) setState() {
} }
if rs.CheckBranches() { if rs.CheckBranches() {
log.Log(INFO, "Branches are Perfect") log.Log(REPO, "Branches are Perfect")
rs.gitState.SetText("PERFECT") rs.gitState.SetText("PERFECT")
return return
} }
log.Log(INFO, rs.String(), "Branches are not Perfect") log.Log(REPO, rs.String(), "Branches are not Perfect")
rs.gitState.SetText("unknown branches") rs.gitState.SetText("unknown branches")
} }
@ -545,7 +534,7 @@ func (rs *RepoStatus) CheckBranches() bool {
// Slice the last 4 runes // Slice the last 4 runes
lastFour := runes[runeCount-4:] lastFour := runes[runeCount-4:]
if string(lastFour) == "HEAD" { if string(lastFour) == "HEAD" {
log.Log(INFO, "skip HEAD fullfile", fullfile) log.Log(REPO, "skip HEAD fullfile", fullfile)
continue continue
} }
@ -561,9 +550,9 @@ func (rs *RepoStatus) CheckBranches() bool {
// log.Log(WARN, "cmd failed", cmd, "err =", err, "in", rs.String()) // log.Log(WARN, "cmd failed", cmd, "err =", err, "in", rs.String())
} }
// git show -s --format=%ci <hash> will give you the time // git show -s --format=%ci <hash> will give you the time
// log.Log(INFO, fullfile) // log.Log(REPO, fullfile)
if hash == hashCheck { if hash == hashCheck {
log.Log(INFO, hash, output, b) log.Log(REPO, hash, output, b)
} else { } else {
// log.Log(WARN, rs.String(), hash, output, b) // log.Log(WARN, rs.String(), hash, output, b)
// log.Log(WARN, "UNKNOWN BRANCHES IN THIS REPO", cmd) // log.Log(WARN, "UNKNOWN BRANCHES IN THIS REPO", cmd)

View File

@ -9,7 +9,6 @@ type RepoStatus struct {
ready bool ready bool
changed bool // keeps track of changes that might have happened changed bool // keeps track of changes that might have happened
changes string changes string
dirty bool // updates each time CheckDirty() is run
tags map[string]string tags map[string]string
window *gadgets.BasicWindow // the main window for this repo window *gadgets.BasicWindow // the main window for this repo