2024-01-09 15:34:53 -06:00
|
|
|
package repostatus
|
|
|
|
|
|
|
|
import (
|
2024-01-11 23:24:09 -06:00
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2024-01-18 00:57:43 -06:00
|
|
|
"io/ioutil"
|
2024-01-18 16:20:11 -06:00
|
|
|
|
|
|
|
"go.wit.com/log"
|
2024-01-09 15:34:53 -06:00
|
|
|
)
|
|
|
|
|
2024-01-18 05:01:54 -06:00
|
|
|
func (rs *RepoStatus) String() string {
|
|
|
|
return rs.repopath
|
|
|
|
}
|
|
|
|
|
2024-01-16 04:47:44 -06:00
|
|
|
func (rs *RepoStatus) GetPath() string {
|
|
|
|
return rs.repopath
|
|
|
|
}
|
|
|
|
|
2024-01-13 20:30:33 -06:00
|
|
|
func (rs *RepoStatus) GetCurrentBranchName() string {
|
2024-01-17 03:59:13 -06:00
|
|
|
return rs.currentBranch.String()
|
2024-01-13 20:30:33 -06:00
|
|
|
}
|
2024-01-16 04:47:44 -06:00
|
|
|
|
2024-01-13 20:30:33 -06:00
|
|
|
func (rs *RepoStatus) GetCurrentBranchVersion() string {
|
2024-01-17 03:59:13 -06:00
|
|
|
return rs.currentVersion.String()
|
2024-01-13 20:30:33 -06:00
|
|
|
}
|
2024-01-16 04:47:44 -06:00
|
|
|
|
2024-01-13 20:30:33 -06:00
|
|
|
func (rs *RepoStatus) GetLastTagVersion() string {
|
2024-01-17 03:59:13 -06:00
|
|
|
return rs.lasttag.String()
|
2024-01-13 20:30:33 -06:00
|
|
|
}
|
|
|
|
|
2024-01-09 15:34:53 -06:00
|
|
|
func (rs *RepoStatus) getCurrentBranchName() string {
|
|
|
|
out := run(rs.repopath, "git", "branch --show-current")
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "getCurrentBranchName() =", out)
|
2024-01-19 12:36:52 -06:00
|
|
|
rs.currentBranch.SetValue(out)
|
2024-01-09 15:34:53 -06:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RepoStatus) getCurrentBranchVersion() string {
|
|
|
|
out := run(rs.repopath, "git", "describe --tags")
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "getCurrentBranchVersion()", out)
|
2024-01-19 12:36:52 -06:00
|
|
|
rs.currentVersion.SetValue(out)
|
2024-01-09 15:34:53 -06:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RepoStatus) getLastTagVersion() string {
|
|
|
|
out := run(rs.repopath, "git", "rev-list --tags --max-count=1")
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "getLastTagVersion()", out)
|
2024-01-09 15:34:53 -06:00
|
|
|
rs.lasttagrev = out
|
|
|
|
|
|
|
|
lastreal := "describe --tags " + out
|
|
|
|
// out = run(r.path, "git", "describe --tags c871d5ecf051a7dc4e3a77157cdbc0a457eb9ae1")
|
|
|
|
out = run(rs.repopath, "git", lastreal)
|
2024-01-19 12:36:52 -06:00
|
|
|
rs.lasttag.SetValue(out)
|
2024-01-17 03:59:13 -06:00
|
|
|
rs.tagsDrop.SetText(out)
|
|
|
|
// rs.lastLabel.SetText(out)
|
2024-01-09 15:34:53 -06:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RepoStatus) populateTags() {
|
|
|
|
tmp := fullpath(rs.repopath + "/.git/refs/tags")
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "populateTags() path =", tmp)
|
2024-01-09 15:34:53 -06:00
|
|
|
for _, tag := range listFiles(tmp) {
|
|
|
|
if rs.tags[tag] == "" {
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "populateTags() Adding new tag", tag)
|
2024-01-17 03:59:13 -06:00
|
|
|
rs.tagsDrop.AddText(tag)
|
2024-01-09 15:34:53 -06:00
|
|
|
rs.tags[tag] = "origin"
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 03:59:13 -06:00
|
|
|
// rs.tagsDrop.SetText(rs.lasttagrev)
|
2024-01-09 15:34:53 -06:00
|
|
|
}
|
|
|
|
|
2024-01-11 15:56:50 -06:00
|
|
|
/*
|
|
|
|
.git/refs/remotes
|
|
|
|
.git/refs/remotes/github
|
|
|
|
.git/refs/remotes/github/devel
|
|
|
|
.git/refs/remotes/github/main
|
|
|
|
.git/refs/remotes/origin
|
|
|
|
.git/refs/remotes/origin/devel
|
|
|
|
.git/refs/remotes/origin/main
|
|
|
|
.git/refs/remotes/origin/jcarr
|
|
|
|
.git/refs/heads
|
|
|
|
*/
|
|
|
|
|
|
|
|
func (rs *RepoStatus) getBranches() []string {
|
|
|
|
var all []string
|
|
|
|
var heads []string
|
|
|
|
var remotes []string
|
|
|
|
heads = listFiles(fullpath(rs.repopath + "/.git/refs/heads"))
|
|
|
|
remotes = listFiles(fullpath(rs.repopath + "/.git/refs/remotes"))
|
|
|
|
|
|
|
|
all = heads
|
|
|
|
|
|
|
|
all = append(all, remotes...)
|
|
|
|
|
|
|
|
for _, branch := range all {
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "getBranches()", branch)
|
2024-01-11 15:56:50 -06:00
|
|
|
}
|
|
|
|
return all
|
|
|
|
}
|
|
|
|
|
2024-01-13 20:30:33 -06:00
|
|
|
func (rs *RepoStatus) CheckDirty() bool {
|
2024-01-15 22:55:19 -06:00
|
|
|
cmd := []string{"git", "diff-index", "--quiet", "HEAD"}
|
2024-01-15 23:41:04 -06:00
|
|
|
path := "/home/jcarr/go/src/" + rs.repopath
|
|
|
|
err, b, out := RunCmd(path, cmd)
|
2024-01-15 22:55:19 -06:00
|
|
|
if err != nil {
|
2024-01-15 23:41:04 -06:00
|
|
|
log.Warn("CheckDirty() b =", b)
|
|
|
|
log.Warn("CheckDirty() path =", path)
|
|
|
|
log.Warn("CheckDirty() out =", out)
|
2024-01-15 22:55:19 -06:00
|
|
|
log.Warn("CheckDirty() err =", err)
|
|
|
|
log.Error(err, "CheckDirty() error")
|
2024-01-19 12:36:52 -06:00
|
|
|
rs.dirtyLabel.SetValue("error")
|
2024-01-15 22:55:19 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if b {
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "CheckDirty() b =", b, "path =", path, "out =", out)
|
|
|
|
log.Log(INFO, "CheckDirty() no", rs.repopath)
|
2024-01-19 12:36:52 -06:00
|
|
|
rs.dirtyLabel.SetValue("no")
|
2024-01-09 15:34:53 -06:00
|
|
|
return false
|
|
|
|
}
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "CheckDirty() true", rs.repopath)
|
2024-01-19 12:36:52 -06:00
|
|
|
rs.dirtyLabel.SetValue("dirty")
|
2024-01-15 22:55:19 -06:00
|
|
|
return true
|
2024-01-09 15:34:53 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-16 04:47:44 -06:00
|
|
|
func (rs *RepoStatus) CheckoutBranch(branch string) (string, string) {
|
|
|
|
// run(rs.repopath, "git", "checkout " + branch)
|
|
|
|
|
|
|
|
realname := rs.getCurrentBranchName()
|
|
|
|
realversion := rs.getCurrentBranchVersion()
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, rs.repopath, "realname =", realname, "realversion =", realversion)
|
2024-01-16 04:47:44 -06:00
|
|
|
return realname, realversion
|
|
|
|
}
|
|
|
|
|
2024-01-11 15:56:50 -06:00
|
|
|
func (rs *RepoStatus) checkoutBranch(level string, branch string) {
|
2024-01-13 20:30:33 -06:00
|
|
|
if rs.CheckDirty() {
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "checkoutBranch() checkDirty() == true for repo", rs.repopath, "looking for branch:", branch)
|
2024-01-09 15:34:53 -06:00
|
|
|
return
|
|
|
|
}
|
2024-01-18 00:57:43 -06:00
|
|
|
out := run(rs.repopath, "git", "checkout "+branch)
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, rs.repopath, "git checkout "+branch, "returned", out)
|
2024-01-09 15:34:53 -06:00
|
|
|
|
|
|
|
realname := rs.getCurrentBranchName()
|
|
|
|
realversion := rs.getCurrentBranchVersion()
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, rs.repopath, "realname =", realname, "realversion =", realversion)
|
2024-01-11 15:56:50 -06:00
|
|
|
|
|
|
|
switch level {
|
|
|
|
case "master":
|
2024-01-19 12:36:52 -06:00
|
|
|
rs.masterBranchVersion.SetValue(realversion)
|
2024-01-11 15:56:50 -06:00
|
|
|
case "devel":
|
2024-01-19 12:36:52 -06:00
|
|
|
rs.develBranchVersion.SetValue(realversion)
|
2024-01-11 15:56:50 -06:00
|
|
|
case "user":
|
2024-01-19 12:36:52 -06:00
|
|
|
rs.userBranchVersion.SetValue(realversion)
|
2024-01-11 15:56:50 -06:00
|
|
|
default:
|
2024-01-09 15:34:53 -06:00
|
|
|
}
|
|
|
|
}
|
2024-01-11 23:24:09 -06:00
|
|
|
|
2024-01-23 10:52:17 -06:00
|
|
|
func (rs *RepoStatus) SetMainWorkingName(s string) {
|
|
|
|
rs.mainWorkingName.SetValue(s)
|
|
|
|
rs.masterDrop.SetLabel(s)
|
|
|
|
rs.masterDrop.SetText("guimaster")
|
2024-01-13 23:29:27 -06:00
|
|
|
}
|
|
|
|
|
2024-01-23 10:52:17 -06:00
|
|
|
func (rs *RepoStatus) SetDevelWorkingName(s string) {
|
|
|
|
rs.develWorkingName.SetValue(s)
|
2024-01-14 13:21:40 -06:00
|
|
|
rs.develBranchVersion.SetLabel(s)
|
2024-01-23 10:52:17 -06:00
|
|
|
rs.develDrop.SetLabel(s)
|
|
|
|
rs.develDrop.SetText(s)
|
2024-01-13 23:29:27 -06:00
|
|
|
}
|
|
|
|
|
2024-01-23 10:52:17 -06:00
|
|
|
func (rs *RepoStatus) SetUserWorkingName(s string) {
|
|
|
|
rs.userWorkingName.SetValue(s)
|
2024-01-14 13:21:40 -06:00
|
|
|
rs.userBranchVersion.SetLabel(s)
|
2024-01-23 10:52:17 -06:00
|
|
|
rs.userDrop.SetText(s)
|
2024-01-13 23:29:27 -06:00
|
|
|
}
|
|
|
|
|
2024-01-13 20:30:33 -06:00
|
|
|
// returns "master", "devel", os.Username, etc
|
2024-01-19 18:25:37 -06:00
|
|
|
func (rs *RepoStatus) GetMasterBranchName() string {
|
2024-01-17 03:59:13 -06:00
|
|
|
name := rs.masterDrop.String()
|
2024-01-13 20:30:33 -06:00
|
|
|
return name
|
|
|
|
}
|
2024-01-19 18:25:37 -06:00
|
|
|
func (rs *RepoStatus) GetDevelBranchName() string {
|
2024-01-17 03:59:13 -06:00
|
|
|
name := rs.develDrop.String()
|
2024-01-13 20:30:33 -06:00
|
|
|
return name
|
|
|
|
}
|
2024-01-19 18:25:37 -06:00
|
|
|
func (rs *RepoStatus) GetUserBranchName() string {
|
2024-01-17 03:59:13 -06:00
|
|
|
name := rs.userDrop.String()
|
2024-01-13 20:30:33 -06:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the git versions like "1.3-2-laksdjf" or whatever
|
|
|
|
func (rs *RepoStatus) GetMasterVersion() string {
|
2024-01-17 03:59:13 -06:00
|
|
|
name := rs.masterBranchVersion.String()
|
2024-01-13 20:30:33 -06:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
func (rs *RepoStatus) GetDevelVersion() string {
|
2024-01-17 03:59:13 -06:00
|
|
|
name := rs.develBranchVersion.String()
|
2024-01-13 20:30:33 -06:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
func (rs *RepoStatus) GetUserVersion() string {
|
2024-01-17 03:59:13 -06:00
|
|
|
name := rs.userBranchVersion.String()
|
2024-01-13 20:30:33 -06:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2024-01-19 18:25:37 -06:00
|
|
|
func (rs *RepoStatus) SetMasterVersion(s string) {
|
|
|
|
if rs.GetMasterVersion() == s {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rs.changed = true
|
2024-01-21 03:18:07 -06:00
|
|
|
log.Verbose("git", rs.GetMasterBranchName(), "is now version =", s)
|
2024-01-19 18:25:37 -06:00
|
|
|
rs.masterBranchVersion.SetValue(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RepoStatus) SetDevelVersion(s string) {
|
|
|
|
if rs.GetDevelVersion() == s {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rs.changed = true
|
|
|
|
rs.develBranchVersion.SetValue(s)
|
2024-01-21 03:18:07 -06:00
|
|
|
log.Verbose("git", rs.GetDevelBranchName(), "s now version =", s)
|
2024-01-19 18:25:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RepoStatus) SetUserVersion(s string) {
|
|
|
|
if rs.GetUserVersion() == s {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rs.changed = true
|
|
|
|
rs.userBranchVersion.SetValue(s)
|
2024-01-21 03:18:07 -06:00
|
|
|
log.Verbose("git", rs.GetUserBranchName(), "is now version =", s)
|
2024-01-19 18:25:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RepoStatus) GetStatus() string {
|
|
|
|
rs.changed = false
|
|
|
|
if rs.CheckDirty() {
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "CheckDirty() true")
|
2024-01-19 18:25:37 -06:00
|
|
|
return "dirty"
|
|
|
|
}
|
|
|
|
if rs.userBranchVersion.String() != rs.develBranchVersion.String() {
|
|
|
|
return "merge to devel"
|
|
|
|
}
|
|
|
|
if rs.userBranchVersion.String() != rs.masterBranchVersion.String() {
|
2024-01-20 21:18:03 -06:00
|
|
|
return "ready to release"
|
|
|
|
}
|
|
|
|
if rs.lasttag.String() != rs.masterBranchVersion.String() {
|
|
|
|
return "ready to tag version"
|
2024-01-19 18:25:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if rs.CheckBranches() {
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "Branches are Perfect")
|
2024-01-19 18:25:37 -06:00
|
|
|
return "PERFECT"
|
|
|
|
}
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, rs.GetPath(), "Branches are not Perfect")
|
2024-01-19 18:25:37 -06:00
|
|
|
return "unknown branches"
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:37:50 -06:00
|
|
|
// TODO: make this report the error somewhere
|
2024-01-13 20:30:33 -06:00
|
|
|
func (rs *RepoStatus) CheckBranches() bool {
|
2024-01-11 23:24:09 -06:00
|
|
|
var hashCheck string
|
|
|
|
var perfect bool = true
|
|
|
|
all := rs.getBranches()
|
|
|
|
path := fullpath(rs.repopath + "/.git/refs/")
|
|
|
|
for _, b := range all {
|
|
|
|
parts := strings.Split(b, "/")
|
|
|
|
rdir := "heads"
|
|
|
|
if len(parts) == 2 {
|
|
|
|
rdir = "remotes"
|
|
|
|
}
|
|
|
|
fullfile := path + "/" + rdir + "/" + b
|
|
|
|
|
|
|
|
// check if the ref name is "HEAD". if so, skip
|
|
|
|
runeCount := utf8.RuneCountInString(fullfile)
|
|
|
|
// Convert the string to a slice of runes
|
|
|
|
runes := []rune(fullfile)
|
|
|
|
// Slice the last 4 runes
|
|
|
|
lastFour := runes[runeCount-4:]
|
|
|
|
if string(lastFour) == "HEAD" {
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "skip HEAD fullfile", fullfile)
|
2024-01-11 23:24:09 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
content, _ := ioutil.ReadFile(fullfile)
|
|
|
|
hash := strings.TrimSpace(string(content))
|
|
|
|
if hashCheck == "" {
|
|
|
|
hashCheck = hash
|
|
|
|
}
|
|
|
|
var cmd []string
|
|
|
|
cmd = append(cmd, "git", "show", "-s", "--format=%ci", hash)
|
2024-01-18 00:57:43 -06:00
|
|
|
_, _, output := RunCmd("/home/jcarr/go/src/"+rs.repopath, cmd)
|
2024-01-11 23:24:09 -06:00
|
|
|
// git show -s --format=%ci <hash> will give you the time
|
2024-01-23 10:52:17 -06:00
|
|
|
// log.Log(INFO, fullfile)
|
2024-01-11 23:24:09 -06:00
|
|
|
if hash == hashCheck {
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, hash, output, b)
|
2024-01-11 23:24:09 -06:00
|
|
|
} else {
|
2024-01-18 17:37:50 -06:00
|
|
|
log.Warn("UNKNOWN BRANCHES IN THIS REPO")
|
|
|
|
rs.versionMessage.SetText("UNKNOWN BRANCHES")
|
2024-01-11 23:24:09 -06:00
|
|
|
perfect = false
|
|
|
|
parts := strings.Split(b, "/")
|
|
|
|
log.Warn("git push", parts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return perfect
|
|
|
|
}
|