From 294119e7c2678b609aa8b66b6ffc219f1c399309 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 23 Jan 2024 10:52:17 -0600 Subject: [PATCH] buttons for scanning .git/config and go.sum common branch handling scan go.sum & .git/config on New() parse go & git config files cleaner debugging cleaning up logging Signed-off-by: Jeff Carr --- .gitignore | 1 + Makefile | 3 + args.go | 14 +-- common.go | 1 - draw.go | 161 +++++++++++++++++++++++++++-------- git.go | 57 +++++++------ gitConfig.go | 235 +++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 +- go.sum | 8 +- new.go | 33 +++++++- structs.go | 9 ++ unix.go | 34 ++++++-- update.go | 17 ++-- 13 files changed, 475 insertions(+), 102 deletions(-) create mode 100644 .gitignore create mode 100644 gitConfig.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/Makefile b/Makefile index 4a4686b..4a67a5a 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ all: @echo Run: make redomod @echo +goimports: + goimports -w *.go + redomod: rm -f go.* goimports -w *.go diff --git a/args.go b/args.go index 8cea172..8884f6c 100644 --- a/args.go +++ b/args.go @@ -8,25 +8,15 @@ import ( "go.wit.com/log" ) -var NOW *log.LogFlag var INFO *log.LogFlag - -var SPEW *log.LogFlag var WARN *log.LogFlag - var CHANGE *log.LogFlag -var STATUS *log.LogFlag func init() { - full := "go.wit.com/gui/gadgets/repostatus" + full := "go.wit.com/lib/gui/repostatus" short := "repostatus" - NOW = log.NewFlag("NOW", true, full, short, "temp debugging stuff") - INFO = log.NewFlag("INFO", false, full, short, "normal debugging stuff") - + INFO = log.NewFlag("INFO", false, full, short, "general repo things") WARN = log.NewFlag("WARN", true, full, short, "bad things") - SPEW = log.NewFlag("SPEW", false, full, short, "spew stuff") - CHANGE = log.NewFlag("CHANGE", true, full, short, "when repo changes") - STATUS = log.NewFlag("STATUS", false, full, short, "current status") } diff --git a/common.go b/common.go index f2536a1..6ba0949 100644 --- a/common.go +++ b/common.go @@ -56,7 +56,6 @@ func (rs *RepoStatus) Toggle() { } func (rs *RepoStatus) Ready() bool { - log.Log(SPEW, "Ready() maybe not ready? rs =", rs) if rs == nil { return false } diff --git a/draw.go b/draw.go index 8f52f68..dc2a9c1 100644 --- a/draw.go +++ b/draw.go @@ -1,6 +1,7 @@ package repostatus import ( + "path/filepath" "strconv" "strings" @@ -37,10 +38,20 @@ func (rs *RepoStatus) drawGitBranches() { rs.masterDrop = gadgets.NewBasicDropdown(newgrid, "main branch") rs.develDrop = gadgets.NewBasicDropdown(newgrid, "devel branch") rs.userDrop = gadgets.NewBasicDropdown(newgrid, "user branch") + + rs.lasttag = gadgets.NewOneLiner(newgrid, "last tag") + + rs.masterBranchVersion = gadgets.NewOneLiner(newgrid, "master") + rs.develBranchVersion = gadgets.NewOneLiner(newgrid, "devel") + rs.userBranchVersion = gadgets.NewOneLiner(newgrid, "user") + + rs.currentBranch = gadgets.NewOneLiner(newgrid, "current branch") + rs.currentVersion = gadgets.NewOneLiner(newgrid, "current version") + var master = "" all := rs.getBranches() for _, branch := range all { - log.Warn("getBranches()", branch) + log.Log(INFO, "getBranches()", branch) rs.masterDrop.AddText(branch) rs.develDrop.AddText(branch) rs.userDrop.AddText(branch) @@ -51,6 +62,7 @@ func (rs *RepoStatus) drawGitBranches() { master = "main" } } + // relabel the various gadgets with the right branch name rs.masterBranchVersion.SetLabel(master) @@ -65,11 +77,82 @@ func (rs *RepoStatus) drawGitBranches() { rs.checkBranchesButton = newgrid.NewButton("check branches", func() { if rs.CheckBranches() { - log.Warn("Branches are perfect") + log.Log(INFO, "Branches are perfect") } else { - log.Warn("Branches are not perfect") + log.Log(INFO, "Branches are not perfect") } }) + newgrid.NewButton("parse git and go config", func() { + ScanGoSrc() + }) + + newgrid.NewButton("show .git/config", func() { + if rs.gitConfig == nil { + log.Log(WARN, "Nonexistant or damaged .git/config", rs.GetPath()) + return + } + log.Log(WARN, ".git/config:", rs.realPath.String()) + + // The info: + for name, remote := range rs.gitConfig.remotes { + log.Log(WARN, " ", name, remote.url) + } + for name, branch := range rs.gitConfig.branches { + log.Log(WARN, " ", name, "remote:", branch.remote, "merge", branch.merge) + } + }) + newgrid.NewButton("check go.sum", func() { + if rs.ReadGoMod() { + log.Log(INFO, "parsed go.mod", rs.realPath.String()) + } else { + log.Log(WARN, "Something went wrong parsing go.mod", rs.realPath.String()) + } + + log.Log(WARN, "go.sum:", rs.realPath.String()) + for depname, version := range rs.goConfig { + log.Log(WARN, " ", depname, version) + } + + }) +} + +/* +func (rs *RepoStatus) ScanConfig(path string) { + log.Log(WARN, "repo =", path) + filename := filepath.Join(path, ".git/config") + + rs.gitConfig, err := ReadGitConfig(filename) + if err != nil { + log.Log(WARN, "Error reading .git/config:", err) + continue + } + + rs.goConfig, err := ReadGoMod(path) +} +*/ + +func ScanGoSrc() { + for i, path := range listGitDirectories() { + log.Log(WARN, "repo =", i, path) + filename := filepath.Join(path, ".git/config") + gitConfig, err := readGitConfig(filename) + if err != nil { + log.Log(WARN, "Error reading .git/config:", err) + continue + } + + // Example of printing the parsed config + for section, options := range gitConfig.branches { + log.Log(WARN, "\t", section, options) + } + /* + _, err = ReadGoMod(path) // map[string]string { + if err != nil { + log.Log(WARN, "\tgo.sum scan failed") + } + */ + + } } func (rs *RepoStatus) drawGitStatus() { @@ -79,23 +162,29 @@ func (rs *RepoStatus) drawGitStatus() { newgrid.Pad() rs.path = gadgets.NewOneLiner(newgrid, "path") - rs.currentBranch = gadgets.NewOneLiner(newgrid, "branch") - rs.lasttag = gadgets.NewOneLiner(newgrid, "last tag") - rs.currentVersion = gadgets.NewOneLiner(newgrid, "Version") - rs.tagsDrop = gadgets.NewBasicDropdown(newgrid, "existing tags") + rs.goSrcPath = gadgets.NewOneLiner(newgrid, "go/src") + rs.realPath = gadgets.NewOneLiner(newgrid, "fullpath") + rs.realPath.Hide() + rs.mainWorkingName = gadgets.NewOneLiner(newgrid, "main working branch") + rs.mainWorkingName.SetValue("???") + rs.develWorkingName = gadgets.NewOneLiner(newgrid, "devel working branch") + rs.develWorkingName.SetValue("devel") + rs.userWorkingName = gadgets.NewOneLiner(newgrid, "user working branch") + rs.userWorkingName.SetValue("uid") + rs.tagsDrop = gadgets.NewBasicDropdown(newgrid, "all releases") // git for-each-ref --sort=taggerdate --format '%(tag) ,,,_,,, %(subject)' refs/tags var cmd []string cmd = append(cmd, "git", "for-each-ref", "--sort=taggerdate", "--format", "%(tag) %(subject)", "refs/tags") _, _, output := RunCmd("/home/jcarr/go/src/"+rs.repopath, cmd) - log.Info(output) + log.Log(INFO, output) for _, line := range strings.Split(output, "\n") { rs.tagsDrop.AddText(line) } - rs.masterBranchVersion = gadgets.NewOneLiner(newgrid, "master") - rs.develBranchVersion = gadgets.NewOneLiner(newgrid, "devel") - rs.userBranchVersion = gadgets.NewOneLiner(newgrid, "user") +// rs.masterBranchVersion = gadgets.NewOneLiner(newgrid, "master") +// rs.develBranchVersion = gadgets.NewOneLiner(newgrid, "devel") +// rs.userBranchVersion = gadgets.NewOneLiner(newgrid, "user") rs.dirtyLabel = gadgets.NewOneLiner(newgrid, "dirty") @@ -187,8 +276,8 @@ func (rs *RepoStatus) setTag() bool { oldb, _ := strconv.Atoi(minor) oldc, _ := strconv.Atoi(revision) - log.Warn("current version here", lasttag) - log.Warn("current release a,b,c =", major, minor, revision) + log.Log(INFO, "current version here", lasttag) + log.Log(INFO, "current release a,b,c =", major, minor, revision) newa, _ := strconv.Atoi(rs.major.String()) @@ -199,7 +288,7 @@ func (rs *RepoStatus) setTag() bool { return false } if newa > olda { - log.Warn("new version ok", newver, "vs old version", lasttag) + log.Log(INFO, "new version ok", newver, "vs old version", lasttag) rs.newversion.SetLabel(newver) rs.minor.SetText("") rs.revision.SetText("") @@ -215,7 +304,7 @@ func (rs *RepoStatus) setTag() bool { } if newb > oldb { - log.Warn("new version ok", newver, "vs old version", lasttag) + log.Log(INFO, "new version ok", newver, "vs old version", lasttag) rs.newversion.SetLabel(newver) rs.revision.SetText("") return true @@ -228,7 +317,7 @@ func (rs *RepoStatus) setTag() bool { rs.newversion.SetLabel("bad") return false } - log.Warn("new version ok", newver, "vs old version", lasttag) + log.Log(INFO, "new version ok", newver, "vs old version", lasttag) rs.newversion.SetLabel(newver) return true } @@ -238,7 +327,7 @@ func (rs *RepoStatus) incrementVersion() { var major, minor, revision string major, minor, revision = splitVersion(lasttag) log.Warn("Should release version here", lasttag) - log.Warn("Should release a,b,c", major, minor, revision) + log.Log(INFO, "Should release a,b,c", major, minor, revision) a, _ := strconv.Atoi(major) b, _ := strconv.Atoi(minor) @@ -259,23 +348,23 @@ func (rs *RepoStatus) incrementVersion() { } func (rs *RepoStatus) recommend() { - log.Warn("Is repo dirty?", rs.dirtyLabel.String()) - log.Warn("list the known tags") + log.Log(INFO, "Is repo dirty?", rs.dirtyLabel.String()) + log.Log(INFO, "list the known tags") rs.DisableEverything() rs.populateTags() - log.Warn("Does devel == user?", rs.develBranchVersion.String(), rs.userBranchVersion.String()) + log.Log(INFO, "Does devel == user?", rs.develBranchVersion.String(), rs.userBranchVersion.String()) if rs.develBranchVersion.String() != rs.userBranchVersion.String() { - log.Warn("devel does not equal user") - log.Warn("merge or squash?") + log.Log(INFO, "devel does not equal user") + log.Log(INFO, "merge or squash?") rs.EnableMergeDevel() rs.setMergeUserCommands() label := "merge user into " + rs.GetDevelBranchName() rs.develMerge.SetLabel(label) return } - log.Warn("Does master == devel? ", rs.masterBranchVersion.String(), rs.develBranchVersion.String()) + log.Log(INFO, "Does master == devel? ", rs.masterBranchVersion.String(), rs.develBranchVersion.String()) if rs.masterBranchVersion.String() != rs.develBranchVersion.String() { - log.Warn("master does not equal devel. merge devel into master") + log.Log(INFO, "master does not equal devel. merge devel into master") rs.EnableMergeDevel() rs.setMergeDevelCommands() label := "merge devel into " + rs.GetMasterBranchName() @@ -284,13 +373,13 @@ func (rs *RepoStatus) recommend() { } rs.getLastTagVersion() if rs.lasttag.String() != rs.masterBranchVersion.String() { - log.Warn("master does not equal last tag") + log.Log(INFO, "master does not equal last tag") rs.incrementVersion() rs.EnableSelectTag() rs.setTag() return } - log.Warn("Is repo pushed upstream? git.wit.org or github?") + log.Log(INFO, "Is repo pushed upstream? git.wit.org or github?") } func (rs *RepoStatus) generateCmd() bool { @@ -300,17 +389,17 @@ func (rs *RepoStatus) generateCmd() bool { // aka: "Topsy", "Picasso", "Buzz", etc if !rs.setTag() { - log.Warn("tag sucked. fix your tag version") + log.Log(INFO, "tag sucked. fix your tag version") rs.versionMessage.SetLabel("tag message (bad version)") rs.releaseVersion.Disable() return false } - log.Warn("tag is valid!!!!") + log.Log(INFO, "tag is valid!!!!") rs.setGitCommands() if rs.versionMessage.String() == "" { - log.Warn("tag message is empty!!!!") + log.Log(INFO, "tag message is empty!!!!") rs.releaseVersion.Disable() return false } @@ -326,7 +415,7 @@ func (rs *RepoStatus) generateCmd() bool { func (rs *RepoStatus) runGitCommands() bool { for _, line := range rs.versionCmds { s := strings.Join(line, " ") - log.Warn("NEED TO RUN:", s) + log.Log(INFO, "NEED TO RUN:", s) rs.develMerge.SetText(s) err, b, output := runCmd(rs.repopath, line) if err != nil { @@ -335,9 +424,9 @@ func (rs *RepoStatus) runGitCommands() bool { log.Warn("output =", output) return false } - log.Warn("Returned with b =", b) - log.Warn("output was =", output) - log.Warn("RUN DONE") + log.Log(INFO, "Returned with b =", b) + log.Log(INFO, "output was =", output) + log.Log(INFO, "RUN DONE") } return true } @@ -360,7 +449,7 @@ func (rs *RepoStatus) setGitCommands() { // convert to displayable to the user text for _, line := range all { s := strings.Join(line, " ") - log.Warn("s =", s) + log.Log(INFO, "s =", s) tmp = append(tmp, s) } @@ -387,7 +476,7 @@ func (rs *RepoStatus) setMergeDevelCommands() { // convert to displayable to the user text for _, line := range all { s := strings.Join(line, " ") - log.Warn("s =", s) + log.Log(INFO, "s =", s) tmp = append(tmp, s) } @@ -414,7 +503,7 @@ func (rs *RepoStatus) setMergeUserCommands() { // convert to displayable to the user text for _, line := range all { s := strings.Join(line, " ") - log.Warn("s =", s) + log.Log(INFO, "s =", s) tmp = append(tmp, s) } diff --git a/git.go b/git.go index 9cfdeeb..3f1a69d 100644 --- a/git.go +++ b/git.go @@ -31,21 +31,21 @@ func (rs *RepoStatus) GetLastTagVersion() string { func (rs *RepoStatus) getCurrentBranchName() string { out := run(rs.repopath, "git", "branch --show-current") - log.Warn("getCurrentBranchName() =", out) + log.Log(INFO, "getCurrentBranchName() =", out) rs.currentBranch.SetValue(out) return out } func (rs *RepoStatus) getCurrentBranchVersion() string { out := run(rs.repopath, "git", "describe --tags") - log.Warn("getCurrentBranchVersion()", out) + log.Log(INFO, "getCurrentBranchVersion()", out) rs.currentVersion.SetValue(out) return out } func (rs *RepoStatus) getLastTagVersion() string { out := run(rs.repopath, "git", "rev-list --tags --max-count=1") - log.Warn("getLastTagVersion()", out) + log.Log(INFO, "getLastTagVersion()", out) rs.lasttagrev = out lastreal := "describe --tags " + out @@ -59,10 +59,10 @@ func (rs *RepoStatus) getLastTagVersion() string { func (rs *RepoStatus) populateTags() { tmp := fullpath(rs.repopath + "/.git/refs/tags") - log.Warn("populateTags() path =", tmp) + log.Log(INFO, "populateTags() path =", tmp) for _, tag := range listFiles(tmp) { if rs.tags[tag] == "" { - log.Warn("populateTags() Adding new tag", tag) + log.Log(INFO, "populateTags() Adding new tag", tag) rs.tagsDrop.AddText(tag) rs.tags[tag] = "origin" } @@ -94,7 +94,7 @@ func (rs *RepoStatus) getBranches() []string { all = append(all, remotes...) for _, branch := range all { - log.Warn("getBranches()", branch) + log.Log(INFO, "getBranches()", branch) } return all } @@ -113,12 +113,12 @@ func (rs *RepoStatus) CheckDirty() bool { return true } if b { - log.Warn("CheckDirty() b =", b, "path =", path, "out =", out) - log.Warn("CheckDirty() no", rs.repopath) + log.Log(INFO, "CheckDirty() b =", b, "path =", path, "out =", out) + log.Log(INFO, "CheckDirty() no", rs.repopath) rs.dirtyLabel.SetValue("no") return false } - log.Warn("CheckDirty() true", rs.repopath) + log.Log(INFO, "CheckDirty() true", rs.repopath) rs.dirtyLabel.SetValue("dirty") return true @@ -129,21 +129,21 @@ func (rs *RepoStatus) CheckoutBranch(branch string) (string, string) { realname := rs.getCurrentBranchName() realversion := rs.getCurrentBranchVersion() - log.Warn(rs.repopath, "realname =", realname, "realversion =", realversion) + log.Log(INFO, rs.repopath, "realname =", realname, "realversion =", realversion) return realname, realversion } func (rs *RepoStatus) checkoutBranch(level string, branch string) { if rs.CheckDirty() { - log.Warn("checkoutBranch() checkDirty() == true for repo", rs.repopath, "looking for branch:", branch) + log.Log(INFO, "checkoutBranch() checkDirty() == true for repo", rs.repopath, "looking for branch:", branch) return } out := run(rs.repopath, "git", "checkout "+branch) - log.Warn(rs.repopath, "git checkout "+branch, "returned", out) + log.Log(INFO, rs.repopath, "git checkout "+branch, "returned", out) realname := rs.getCurrentBranchName() realversion := rs.getCurrentBranchVersion() - log.Warn(rs.repopath, "realname =", realname, "realversion =", realversion) + log.Log(INFO, rs.repopath, "realname =", realname, "realversion =", realversion) switch level { case "master": @@ -156,20 +156,23 @@ func (rs *RepoStatus) checkoutBranch(level string, branch string) { } } -func (rs *RepoStatus) SetMasterBranchName(s string) { - rs.masterDrop.SetText(s) - rs.masterBranchVersion.SetLabel(s) - // rs.major.SetTitle(s) +func (rs *RepoStatus) SetMainWorkingName(s string) { + rs.mainWorkingName.SetValue(s) + rs.masterDrop.SetLabel(s) + rs.masterDrop.SetText("guimaster") } -func (rs *RepoStatus) SetDevelBranchName(s string) { - rs.develDrop.SetText(s) +func (rs *RepoStatus) SetDevelWorkingName(s string) { + rs.develWorkingName.SetValue(s) rs.develBranchVersion.SetLabel(s) + rs.develDrop.SetLabel(s) + rs.develDrop.SetText(s) } -func (rs *RepoStatus) SetUserBranchName(s string) { - rs.userDrop.SetText(s) +func (rs *RepoStatus) SetUserWorkingName(s string) { + rs.userWorkingName.SetValue(s) rs.userBranchVersion.SetLabel(s) + rs.userDrop.SetText(s) } // returns "master", "devel", os.Username, etc @@ -230,7 +233,7 @@ func (rs *RepoStatus) SetUserVersion(s string) { func (rs *RepoStatus) GetStatus() string { rs.changed = false if rs.CheckDirty() { - log.Warn("CheckDirty() true") + log.Log(INFO, "CheckDirty() true") return "dirty" } if rs.userBranchVersion.String() != rs.develBranchVersion.String() { @@ -244,10 +247,10 @@ func (rs *RepoStatus) GetStatus() string { } if rs.CheckBranches() { - log.Info("Branches are Perfect") + log.Log(INFO, "Branches are Perfect") return "PERFECT" } - log.Warn(rs.GetPath(), "Branches are not Perfect") + log.Log(INFO, rs.GetPath(), "Branches are not Perfect") return "unknown branches" } @@ -272,7 +275,7 @@ func (rs *RepoStatus) CheckBranches() bool { // Slice the last 4 runes lastFour := runes[runeCount-4:] if string(lastFour) == "HEAD" { - log.Warn("skip HEAD fullfile", fullfile) + log.Log(INFO, "skip HEAD fullfile", fullfile) continue } @@ -285,9 +288,9 @@ func (rs *RepoStatus) CheckBranches() bool { cmd = append(cmd, "git", "show", "-s", "--format=%ci", hash) _, _, output := RunCmd("/home/jcarr/go/src/"+rs.repopath, cmd) // git show -s --format=%ci will give you the time - // log.Warn(fullfile) + // log.Log(INFO, fullfile) if hash == hashCheck { - log.Warn(hash, output, b) + log.Log(INFO, hash, output, b) } else { log.Warn("UNKNOWN BRANCHES IN THIS REPO") rs.versionMessage.SetText("UNKNOWN BRANCHES") diff --git a/gitConfig.go b/gitConfig.go new file mode 100644 index 0000000..70568bb --- /dev/null +++ b/gitConfig.go @@ -0,0 +1,235 @@ +package repostatus + +import ( + "bufio" + "os" + "path/filepath" + "strings" + + "go.wit.com/log" +) + +// GitConfig represents the parsed .git/config data +// type GitConfig map[string]map[string]string + +type remote struct { + url string + fetch string +} + +type branch struct { + remote string + merge string +} + +type GitConfig struct { + core map[string]string // map[origin] = "https:/git.wit.org/gui/gadgets" + remotes map[string]*remote // map[origin] = "https:/git.wit.org/gui/gadgets" + branches map[string]*branch // map[guimaster] = origin guimaster +} + +type GoConfig map[string]string + +func listGitDirectories() []string { + var all []string + homeDir, err := os.UserHomeDir() + if err != nil { + log.Log(WARN, "Error getting home directory:", err) + return nil + } + + srcDir := filepath.Join(homeDir, "go/src") + + err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + log.Log(WARN, "Error accessing path:", path, err) + return nil + } + + // Check if the current path is a directory and has a .git subdirectory + if info.IsDir() && isGitDir(path) { + all = append(all, path) + // fmt.Println(path) + } + + return nil + }) + + if err != nil { + log.Log(WARN, "Error walking the path:", srcDir, err) + } + + return all +} + +// isGitDir checks if a .git directory exists inside the given directory +func isGitDir(dir string) bool { + gitDir := filepath.Join(dir, ".git") + info, err := os.Stat(gitDir) + if os.IsNotExist(err) { + return false + } + return info.IsDir() +} + +// readGitConfig reads and parses the .git/config file +func readGitConfig(filePath string) (*GitConfig, error) { + file, err := os.Open(filePath) + if err != nil { + return nil, err + } + defer file.Close() + + var currentSection string = "" + var currentName string = "" + + config := new(GitConfig) + config.core = make(map[string]string) + config.remotes = make(map[string]*remote) + config.branches = make(map[string]*branch) + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + + // Skip empty lines and comments + if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") { + continue + } + + // Check for section headers + if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { + line = strings.Trim(line, "[]") + parts := strings.Split(line, " ") + currentSection = parts[0] + + if len(parts) == 2 { + line = strings.Trim(line, "[]") + currentName = strings.Trim(parts[1], "[]") + } + continue + } + + partsNew := strings.SplitN(line, "=", 2) + if len(partsNew) != 2 { + log.Log(WARN, "error on config section:", currentSection, "line:", line) + } + + key := strings.TrimSpace(partsNew[0]) + value := strings.TrimSpace(partsNew[1]) + + switch currentSection { + case "core": + config.core[key] = value + case "remote": + test, ok := config.remotes[currentName] + if !ok { + test = new(remote) + config.remotes[currentName] = test + } + log.Log(INFO, "switch currentSection", currentSection, currentName) + switch key { + case "url": + if test.url == value { + continue + } + if test.url == "" { + test.url = value + continue + } + log.Log(WARN, "error url mismatch", test.url, value) + case "fetch": + if test.fetch == value { + continue + } + if test.fetch == "" { + test.fetch = value + continue + } + log.Log(WARN, "error fetch mismatch", test.fetch, value) + default: + log.Log(WARN, "error unknown remote:", currentSection, currentName, "key", key, "value", value) + } + case "branch": + test, ok := config.branches[currentName] + if !ok { + test = new(branch) + config.branches[currentName] = test + } + switch key { + case "remote": + config.branches[currentName].remote = value + case "merge": + config.branches[currentName].merge = value + default: + log.Log(WARN, "error unknown remote:", currentSection, currentName, key, value) + } + default: + log.Log(WARN, "error unknown currentSection", currentSection, "line:", line) + } + } + + if err := scanner.Err(); err != nil { + return nil, err + } + + return config, nil +} + +// readGoMod reads and parses the go.sum file (TODO: do the go.mod file) +func (rs *RepoStatus) ReadGoMod() bool { + tmp := filepath.Join(rs.realPath.String(), "go.sum") + gomod, err := os.Open(tmp) + if err != nil { + log.Log(WARN, "missing go.mod", rs.realPath.String()) + rs.goConfig = nil + return false + } + defer gomod.Close() + + tmp = filepath.Join(rs.realPath.String(), "go.sum") + gosum, err := os.Open(tmp) + if err != nil { + log.Log(WARN, "missing go.sum", rs.realPath.String()) + rs.goConfig = nil + return false + } + defer gosum.Close() + + var deps GoConfig + deps = make(GoConfig) + + scanner := bufio.NewScanner(gosum) + log.Log(INFO, "gosum:", tmp) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + + parts := strings.Split(line, " ") + if len(parts) == 3 { + godep := strings.TrimSpace(parts[0]) + version := strings.TrimSpace(parts[1]) + if strings.HasSuffix(version, "/go.mod") { + version = strings.TrimSuffix(version, "/go.mod") + } + currentversion, ok := deps[godep] + if ok { + if currentversion != version { + log.Log(WARN, "versions do not match!!!", deps[godep], version, currentversion) + } + } else { + deps[godep] = version + log.Log(INFO, "\t", godep, "=", version) + } + } else { + log.Log(WARN, "\t INVALID:", parts) + } + } + + if err := scanner.Err(); err != nil { + rs.goConfig = nil + return false + } + + rs.goConfig = deps + return true +} diff --git a/go.mod b/go.mod index fc81fb0..d86fd4e 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module go.wit.com/lib/gui/repostatus go 1.21.4 require ( - go.wit.com/gui v0.12.19 - go.wit.com/lib/gadgets v0.12.15 + go.wit.com/gui v0.12.20 + go.wit.com/lib/gadgets v0.12.16 go.wit.com/log v0.5.6 go.wit.com/widget v1.1.6 ) diff --git a/go.sum b/go.sum index 2939e18..2a3d645 100644 --- a/go.sum +++ b/go.sum @@ -4,10 +4,10 @@ go.wit.com/dev/alexflint/scalar v1.2.1 h1:loXOcbVnd+8YeJRLey+XXidecBiedMDO00zQ26 go.wit.com/dev/alexflint/scalar v1.2.1/go.mod h1:+rYsfxqdI2cwA8kJ7GCMwWbNJvfvWUurOCXLiwdTtSs= go.wit.com/dev/davecgh/spew v1.1.4 h1:C9hj/rjlUpdK+E6aroyLjCbS5MFcyNUOuP1ICLWdNek= go.wit.com/dev/davecgh/spew v1.1.4/go.mod h1:sihvWmnQ/09FWplnEmozt90CCVqBtGuPXM811tgfhFA= -go.wit.com/gui v0.12.19 h1:OEnsnZnec7Q2jZVjwl413V0wuVAAB4r2mGTY0IouBuw= -go.wit.com/gui v0.12.19/go.mod h1:v2VgnOL3dlZ13KclYeedZ1cd20nQdvwjyJTNKvFX3DA= -go.wit.com/lib/gadgets v0.12.15 h1:C9q6wc45Trh5SrizD8lOXOWoJLGq/ESWwzjCVylZrNY= -go.wit.com/lib/gadgets v0.12.15/go.mod h1:Fxc7F8hGskpkWVAsXKhs4ilqUlAnikVXj4yzumtTYa0= +go.wit.com/gui v0.12.20 h1:mIc2DKGcpQjZdgtAj5qzkBrBDiteWfIaEpLyMnIBkh8= +go.wit.com/gui v0.12.20/go.mod h1:v2VgnOL3dlZ13KclYeedZ1cd20nQdvwjyJTNKvFX3DA= +go.wit.com/lib/gadgets v0.12.16 h1:xHz8zZiTe8xiGvfWs3s9drYUbePTT/Te58u7WXHjx0s= +go.wit.com/lib/gadgets v0.12.16/go.mod h1:9779QoRZlk+G3/MCcX4Io1eH3HTLImE0AXdAMMdw+0U= go.wit.com/log v0.5.6 h1:rDC3ju95zfEads4f1Zm+QMkqjZ39CsYAT/UmQQs7VP4= go.wit.com/log v0.5.6/go.mod h1:BaJBfHFqcJSJLXGQ9RHi3XVhPgsStxSMZRlaRxW4kAo= go.wit.com/widget v1.1.6 h1:av2miF5vlohMfARA/QGPTPfgW/ADup1c+oeAOKgroPY= diff --git a/new.go b/new.go index 5da1fc1..c7c41d2 100644 --- a/new.go +++ b/new.go @@ -1,6 +1,9 @@ package repostatus import ( + "os" + "path/filepath" + "go.wit.com/gui" "go.wit.com/lib/gadgets" "go.wit.com/log" @@ -16,12 +19,40 @@ func ListAll() { func NewRepoStatusWindow(path string) *RepoStatus { if windowMap[path] == nil { - log.Warn("This doesn't exist yet for path", path) + log.Log(INFO, "NewRepoStatusWindow() adding new", path) } else { log.Warn("This already exists yet for path", path) log.Warn("should return windowMap[path] here") + return windowMap[path] } + + homeDir, err := os.UserHomeDir() + if err != nil { + log.Log(WARN, "Error getting home directory:", err) + return nil + } + + goSrcDir := filepath.Join(homeDir, "go/src") + + realpath := filepath.Join(goSrcDir, path) + filename := filepath.Join(realpath, ".git/config") + gitConfig, err := readGitConfig(filename) + + // if the .git/config file fails to load, just nil out man + if err != nil { + log.Log(WARN, "Error reading .git/config:", err) + return nil + } + rs := New(gui.TreeRoot(), path) + + // save ~/go/src & the whole path strings + rs.goSrcPath.SetValue(goSrcDir) + rs.realPath.SetValue(realpath) + + // save .git/config + rs.gitConfig = gitConfig + windowMap[path] = rs // todo check if a window already exists for this path diff --git a/structs.go b/structs.go index 5097c22..c6a55a2 100644 --- a/structs.go +++ b/structs.go @@ -23,6 +23,8 @@ type RepoStatus struct { // status *gadgets.OneLiner dirtyLabel *gadgets.OneLiner path *gadgets.OneLiner + goSrcPath *gadgets.OneLiner + realPath *gadgets.OneLiner currentBranch *gadgets.OneLiner currentVersion *gadgets.OneLiner @@ -33,6 +35,10 @@ type RepoStatus struct { develBranchVersion *gadgets.OneLiner userBranchVersion *gadgets.OneLiner + mainWorkingName *gadgets.OneLiner + develWorkingName *gadgets.OneLiner + userWorkingName *gadgets.OneLiner + develMerge *gui.Node releaseVersion *gui.Node @@ -60,4 +66,7 @@ type RepoStatus struct { speed *gadgets.OneLiner speedActual *gadgets.OneLiner + + gitConfig *GitConfig + goConfig GoConfig } diff --git a/unix.go b/unix.go index 6d3615c..def2c99 100644 --- a/unix.go +++ b/unix.go @@ -27,16 +27,17 @@ func run(path string, thing string, cmdline string) string { // Execute the command output, err := cmd.CombinedOutput() + tmp := string(output) + tmp = strings.TrimSpace(tmp) + if err != nil { + log.Log(WARN, "run()", path, thing, cmdline, "=", tmp) log.Error(err, "cmd error'd out", parts) return "" } - tmp := string(output) - tmp = strings.TrimSpace(tmp) - // Print the output - log.Log(WARN, "run()", path, thing, cmdline, "=", tmp) + log.Log(INFO, "run()", path, thing, cmdline, "=", tmp) return tmp } @@ -100,7 +101,7 @@ func normalizeVersion(s string) string { return parts[0] } clean := reg.ReplaceAllString(parts[0], "") - log.Log(WARN, "normalizeVersion() s =", clean) + log.Log(INFO, "normalizeVersion() s =", clean) return clean } @@ -134,8 +135,8 @@ func RunCmd(workingpath string, parts []string) (error, bool, string) { } thing := parts[0] parts = parts[1:] + log.Log(INFO, "working path =", workingpath, "thing =", thing, "cmdline =", parts) - log.Warn("working path =", workingpath, "thing =", thing, "cmdline =", parts) // Create the command cmd := exec.Command(thing, parts...) @@ -145,9 +146,25 @@ func RunCmd(workingpath string, parts []string) (error, bool, string) { // Execute the command output, err := cmd.CombinedOutput() if err != nil { + if thing == "git" { + log.Log(INFO, "git ERROR. maybe okay", workingpath, "thing =", thing, "cmdline =", parts) + log.Log(INFO, "git ERROR. maybe okay err =", err) + if err.Error() == "exit status 1" { + log.Log(INFO, "git ERROR. normal exit status 1") + if parts[0] == "diff-index" { + log.Log(INFO, "git normal diff-index when repo dirty") + return nil, false, "git diff-index exit status 1" + } + } + } + + log.Log(WARN, "ERROR working path =", workingpath, "thing =", thing, "cmdline =", parts) + log.Log(WARN, "ERROR working path =", workingpath, "thing =", thing, "cmdline =", parts) + log.Log(WARN, "ERROR working path =", workingpath, "thing =", thing, "cmdline =", parts) log.Error(err) log.Warn("output was", string(output)) log.Warn("cmd exited with error", err) + // panic("fucknuts") return err, false, string(output) } @@ -159,12 +176,11 @@ func RunCmd(workingpath string, parts []string) (error, bool, string) { } // Set the path to the package -// Replace this with the actual path to the github.com/coredns/coredns directory func getfiles(pathToPackage string) { // List files in the directory err := filepath.Walk(pathToPackage, nil) // compiles but crashes if err == nil { - log.Warn("directory ok", pathToPackage) + log.Log(INFO, "directory ok", pathToPackage) } else { log.Warn("directory wrong", pathToPackage) } @@ -189,6 +205,6 @@ func VerifyLocalGoRepo(gorepo string) bool { // Form the path to the home Git directory gitDir := filepath.Join(usr.HomeDir, "go/src/", gorepo, ".git") - log.Warn("go directory:", gitDir) + log.Log(INFO, "go directory:", gitDir) return IsDirectory(gitDir) } diff --git a/update.go b/update.go index a8e7c91..96aefc9 100644 --- a/update.go +++ b/update.go @@ -14,7 +14,7 @@ func (rs *RepoStatus) Update() { log.Error(errors.New("Update() is not ready yet")) return } - log.Log(WARN, "Update() START") + 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) @@ -37,27 +37,24 @@ func (rs *RepoStatus) Update() { user := rs.userDrop.String() // rs.CheckDirty() this runs - log.Log(WARN, "") - log.Log(WARN, "checkoutBranch", master) + log.Log(INFO, "checkoutBranch", master) rs.checkoutBranch("master", master) - log.Log(WARN, "") - log.Log(WARN, "checkoutBranch", devel) + log.Log(INFO, "checkoutBranch", devel) rs.checkoutBranch("devel", devel) - log.Log(WARN, "") - log.Log(WARN, "checkoutBranch", user) + log.Log(INFO, "checkoutBranch", user) rs.checkoutBranch("user", user) rs.recommend() rs.CheckBranches() }) rs.setSpeed(duration) - log.Log(WARN, "Update() END") + log.Log(INFO, "Update() END") } func (rs *RepoStatus) setSpeed(duration time.Duration) { s := fmt.Sprint(duration) if rs.speedActual == nil { - log.Log(WARN, "can't actually warn") + log.Log(WARN, "rs.speedActual == nil") return } rs.speedActual.SetValue(s) @@ -73,7 +70,7 @@ func (rs *RepoStatus) setSpeed(duration time.Duration) { // disable all things besides Update() button func (rs *RepoStatus) DisableEverything() { - log.Warn("DisableEverything()") + log.Log(INFO, "DisableEverything()") // choosing a major, minor or revision rs.major.Disable()