From cec5e7f3b4900bdb8486d2d77c2ba6493ef53316 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 24 Feb 2024 05:37:53 -0600 Subject: [PATCH] cleanup checkdirty code Signed-off-by: Jeff Carr --- git.go | 75 +++++++++++++++++++++++------------------------------- structs.go | 1 - 2 files changed, 32 insertions(+), 44 deletions(-) diff --git a/git.go b/git.go index 76a6fd5..bc7a986 100644 --- a/git.go +++ b/git.go @@ -145,7 +145,7 @@ func (rs *RepoStatus) gitDescribeByName(name string) (string, error) { // todo: don't run git every time? func (rs *RepoStatus) checkCurrentBranchVersion() string { out, _ := rs.gitDescribeByName("") - log.Log(INFO, "checkCurrentBranchVersion()", out) + log.Log(REPO, "checkCurrentBranchVersion()", out) rs.currentVersion.SetValue(out) return out } @@ -153,7 +153,7 @@ func (rs *RepoStatus) checkCurrentBranchVersion() string { // this should get the most recent tag func (rs *RepoStatus) setLastTagVersion() { 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) rs.lasttag.SetText(name) @@ -162,10 +162,10 @@ func (rs *RepoStatus) setLastTagVersion() { func (rs *RepoStatus) populateTags() { tmp := rs.realPath.String() + "/.git/refs/tags" - log.Log(INFO, "populateTags() path =", tmp) + log.Log(REPO, "populateTags() path =", tmp) for _, tag := range listFiles(tmp) { 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.tags[tag] = "origin" } @@ -185,25 +185,25 @@ func (rs *RepoStatus) getBranches() []string { all = append(all, remotes...) for _, branch := range all { - log.Log(INFO, "getBranches()", branch) + log.Log(REPO, "getBranches()", branch) } return all } // returns quickly based on the last time it was checked func (rs *RepoStatus) IsDirty() bool { - return rs.dirty + if rs.dirtyLabel.String() == "no" { + return false + } + return true } func (rs *RepoStatus) CheckDirty() bool { var start string = rs.dirtyLabel.String() cmd := []string{"git", "status"} - path := rs.realPath.String() - err, b, out := RunCmd(path, cmd) + err, out := rs.RunCmd(cmd) if err != nil { - log.Warn("CheckDirty() status b =", b) log.Warn("CheckDirty() status cmd =", cmd) - log.Warn("CheckDirty() status path =", path) log.Warn("CheckDirty() status out =", out) log.Warn("CheckDirty() status err =", err) log.Error(err, "CheckDirty() git status error") @@ -211,48 +211,37 @@ func (rs *RepoStatus) CheckDirty() bool { if start != "error" { rs.NoteChange("repo git status is in error " + fmt.Sprint(err)) } - rs.dirty = true return true } last := out[strings.LastIndex(out, "\n")+1:] if last == "nothing to commit, working tree clean" { - log.Log(INFO, "CheckDirty() b =", b, "path =", path, "out =", out) - log.Log(INFO, "CheckDirty() no", rs.realPath.String()) + log.Log(REPO, "CheckDirty() no", rs.realPath.String()) rs.dirtyLabel.SetValue("no") if start != "no" { rs.NoteChange("repo is no longer dirty") } - rs.dirty = 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") if start != "dirty" { + log.Log(REPOWARN, "repo is now dirty") rs.NoteChange("repo is now dirty") } - rs.dirty = true return true } func (rs *RepoStatus) CheckoutBranch(bname string) bool { 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()) return false } if !rs.TagExists(bname) { // 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 } cName := rs.GetCurrentBranchName() @@ -263,7 +252,7 @@ func (rs *RepoStatus) CheckoutBranch(bname string) bool { cmd := []string{"git", "checkout", bname} err, b, output := RunCmd(rs.realPath.String(), cmd) if err != nil { - log.Log(INFO, err, b, output) + log.Log(REPO, err, b, output) return false } rs.checkCurrentBranchName() @@ -273,7 +262,7 @@ func (rs *RepoStatus) CheckoutBranch(bname string) bool { func (rs *RepoStatus) CheckoutMaster() bool { if rs.CheckDirty() { - log.Log(INFO, rs.realPath.String(), "is dirty") + log.Log(REPO, rs.realPath.String(), "is dirty") return false } mName := rs.GetMasterBranchName() @@ -290,13 +279,13 @@ func (rs *RepoStatus) CheckoutDevel() bool { return false } if rs.CheckDirty() { - log.Log(INFO, rs.realPath.String(), "is dirty") + log.Log(REPO, rs.realPath.String(), "is dirty") return false } - log.Log(INFO, "checkoutBranch", devel) + log.Log(REPO, "checkoutBranch", devel) rs.checkoutBranch("devel", devel) - // log.Log(INFO, "checkoutBranch", user) + // log.Log(REPO, "checkoutBranch", user) // rs.checkoutBranch("user", user) return true } @@ -307,21 +296,21 @@ func (rs *RepoStatus) CheckoutUser() bool { return false } if rs.CheckDirty() { - log.Log(INFO, rs.realPath.String(), "is dirty") + log.Log(REPO, 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) + log.Log(REPO, err, b, output) } realname := rs.GetCurrentBranchName() 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 { - log.Log(INFO, "git checkout failed", rs.realPath.String(), bName, "!=", realname) + log.Log(REPO, "git checkout failed", rs.realPath.String(), bName, "!=", realname) return false } rs.userBranchVersion.SetValue(realversion) @@ -330,15 +319,15 @@ func (rs *RepoStatus) CheckoutUser() bool { 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) + log.Log(REPO, "checkoutBranch() checkDirty() == true for repo", rs.realPath.String(), "looking for branch:", branch) return } 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() 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 { case "master": @@ -490,7 +479,7 @@ func (rs *RepoStatus) GetStatus() string { func (rs *RepoStatus) setState() { rs.changed = false if rs.CheckDirty() { - log.Log(INFO, "CheckDirty() true") + log.Log(REPO, "CheckDirty() true") rs.gitState.SetText("dirty") return } @@ -508,11 +497,11 @@ func (rs *RepoStatus) setState() { } if rs.CheckBranches() { - log.Log(INFO, "Branches are Perfect") + log.Log(REPO, "Branches are Perfect") rs.gitState.SetText("PERFECT") return } - log.Log(INFO, rs.String(), "Branches are not Perfect") + log.Log(REPO, rs.String(), "Branches are not Perfect") rs.gitState.SetText("unknown branches") } @@ -545,7 +534,7 @@ func (rs *RepoStatus) CheckBranches() bool { // Slice the last 4 runes lastFour := runes[runeCount-4:] if string(lastFour) == "HEAD" { - log.Log(INFO, "skip HEAD fullfile", fullfile) + log.Log(REPO, "skip HEAD fullfile", fullfile) continue } @@ -561,9 +550,9 @@ func (rs *RepoStatus) CheckBranches() bool { // log.Log(WARN, "cmd failed", cmd, "err =", err, "in", rs.String()) } // git show -s --format=%ci will give you the time - // log.Log(INFO, fullfile) + // log.Log(REPO, fullfile) if hash == hashCheck { - log.Log(INFO, hash, output, b) + log.Log(REPO, hash, output, b) } else { // log.Log(WARN, rs.String(), hash, output, b) // log.Log(WARN, "UNKNOWN BRANCHES IN THIS REPO", cmd) diff --git a/structs.go b/structs.go index 7ce4c7d..5234e7e 100644 --- a/structs.go +++ b/structs.go @@ -9,7 +9,6 @@ type RepoStatus struct { ready bool changed bool // keeps track of changes that might have happened changes string - dirty bool // updates each time CheckDirty() is run tags map[string]string window *gadgets.BasicWindow // the main window for this repo