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 {
|
2024-01-23 15:39:53 -06:00
|
|
|
return rs.path.String()
|
2024-01-18 05:01:54 -06:00
|
|
|
}
|
|
|
|
|
2024-01-23 15:39:53 -06:00
|
|
|
/*
|
2024-01-16 04:47:44 -06:00
|
|
|
func (rs *RepoStatus) GetPath() string {
|
2024-01-23 15:39:53 -06:00
|
|
|
return rs.path.String()
|
2024-01-16 04:47:44 -06:00
|
|
|
}
|
2024-01-23 15:39:53 -06:00
|
|
|
*/
|
2024-01-16 04:47:44 -06:00
|
|
|
|
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 {
|
2024-01-23 15:20:54 -06:00
|
|
|
out := run(rs.realPath.String(), "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 {
|
2024-01-23 15:20:54 -06:00
|
|
|
out := run(rs.realPath.String(), "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 {
|
2024-01-23 15:20:54 -06:00
|
|
|
out := run(rs.realPath.String(), "git", "rev-list --tags --max-count=1")
|
2024-01-23 10:52:17 -06:00
|
|
|
log.Log(INFO, "getLastTagVersion()", out)
|
2024-01-23 22:47:39 -06:00
|
|
|
// rs.lasttagrev = out
|
2024-01-09 15:34:53 -06:00
|
|
|
|
|
|
|
lastreal := "describe --tags " + out
|
|
|
|
// out = run(r.path, "git", "describe --tags c871d5ecf051a7dc4e3a77157cdbc0a457eb9ae1")
|
2024-01-23 15:20:54 -06:00
|
|
|
out = run(rs.realPath.String(), "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() {
|
2024-01-23 15:20:54 -06:00
|
|
|
tmp := rs.realPath.String() + "/.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
|
|
|
func (rs *RepoStatus) getBranches() []string {
|
|
|
|
var all []string
|
|
|
|
var heads []string
|
|
|
|
var remotes []string
|
2024-01-23 15:20:54 -06:00
|
|
|
heads = listFiles(rs.realPath.String() + "/.git/refs/heads")
|
|
|
|
remotes = listFiles(rs.realPath.String() + "/.git/refs/remotes")
|
2024-01-11 15:56:50 -06:00
|
|
|
|
|
|
|
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-23 15:20:54 -06:00
|
|
|
path := rs.realPath.String()
|
2024-01-15 23:41:04 -06:00
|
|
|
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)
|
2024-01-24 16:24:58 -06:00
|
|
|
log.Warn("CheckDirty() cmd =", cmd)
|
2024-01-15 23:41:04 -06:00
|
|
|
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)
|
2024-01-23 15:20:54 -06:00
|
|
|
log.Log(INFO, "CheckDirty() no", rs.realPath.String())
|
2024-01-19 12:36:52 -06:00
|
|
|
rs.dirtyLabel.SetValue("no")
|
2024-01-09 15:34:53 -06:00
|
|
|
return false
|
|
|
|
}
|
2024-01-26 02:02:56 -06:00
|
|
|
log.Log(INFO, "CheckDirty() true", rs.realPath.String())
|
|
|
|
log.Log(INFO, "CheckDirty() cmd =", cmd)
|
|
|
|
log.Log(INFO, "CheckDirty() b =", b)
|
|
|
|
log.Log(INFO, "CheckDirty() path =", path)
|
|
|
|
log.Log(INFO, "CheckDirty() out =", out)
|
|
|
|
log.Log(INFO, "CheckDirty() err =", err)
|
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) {
|
2024-01-23 15:20:54 -06:00
|
|
|
// run(rs.realPath.String(), "git", "checkout " + branch)
|
2024-01-16 04:47:44 -06:00
|
|
|
|
|
|
|
realname := rs.getCurrentBranchName()
|
|
|
|
realversion := rs.getCurrentBranchVersion()
|
2024-01-23 15:20:54 -06:00
|
|
|
log.Log(INFO, rs.realPath.String(), "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 15:20:54 -06:00
|
|
|
log.Log(INFO, "checkoutBranch() checkDirty() == true for repo", rs.realPath.String(), "looking for branch:", branch)
|
2024-01-09 15:34:53 -06:00
|
|
|
return
|
|
|
|
}
|
2024-01-23 15:20:54 -06:00
|
|
|
out := run(rs.realPath.String(), "git", "checkout "+branch)
|
|
|
|
log.Log(INFO, rs.realPath.String(), "git checkout "+branch, "returned", out)
|
2024-01-09 15:34:53 -06:00
|
|
|
|
|
|
|
realname := rs.getCurrentBranchName()
|
|
|
|
realversion := rs.getCurrentBranchVersion()
|
2024-01-23 15:20:54 -06:00
|
|
|
log.Log(INFO, rs.realPath.String(), "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) {
|
2024-01-25 02:23:56 -06:00
|
|
|
if rs == nil {
|
|
|
|
log.Info("rs == nil", s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if rs.gitConfig == nil {
|
|
|
|
log.Info("rs.gitConfig == nil", s)
|
|
|
|
rs.mainWorkingName.SetValue(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if rs.gitConfig.branches == nil {
|
|
|
|
log.Info("rs.gitConfig.branches == nil", s)
|
|
|
|
rs.mainWorkingName.SetValue(s)
|
|
|
|
return
|
|
|
|
}
|
2024-01-25 01:30:01 -06:00
|
|
|
_, ok := rs.gitConfig.branches[s]
|
|
|
|
if ok {
|
|
|
|
log.Info("git branch", s, "seems to exist")
|
|
|
|
rs.mainWorkingName.SetValue(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s = "master"
|
|
|
|
_, ok = rs.gitConfig.branches[s]
|
|
|
|
if ok {
|
|
|
|
log.Info("git branch", s, "seems to exist")
|
|
|
|
rs.mainWorkingName.SetValue(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s = "main"
|
|
|
|
_, ok = rs.gitConfig.branches[s]
|
|
|
|
if ok {
|
|
|
|
log.Info("git branch", s, "seems to exist")
|
|
|
|
rs.mainWorkingName.SetValue(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s = "notsure"
|
|
|
|
log.Warn("git branch", s, "does not seem to exist. TODO: figure out the server default")
|
|
|
|
for name, _ := range rs.gitConfig.branches {
|
|
|
|
log.Warn("git branch found. use this?", name)
|
|
|
|
}
|
2024-01-23 10:52:17 -06:00
|
|
|
rs.mainWorkingName.SetValue(s)
|
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-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-25 01:30:01 -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-25 01:30:01 -06:00
|
|
|
name := rs.mainWorkingName.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-25 01:30:01 -06:00
|
|
|
name := rs.develWorkingName.String()
|
2024-01-13 20:30:33 -06:00
|
|
|
return name
|
|
|
|
}
|
2024-01-25 01:30:01 -06:00
|
|
|
|
2024-01-19 18:25:37 -06:00
|
|
|
func (rs *RepoStatus) GetUserBranchName() string {
|
2024-01-25 01:30:01 -06:00
|
|
|
name := rs.userWorkingName.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-25 01:30:01 -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 15:39:53 -06:00
|
|
|
log.Log(INFO, rs.String(), "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()
|
2024-01-23 15:20:54 -06:00
|
|
|
path := rs.realPath.String() + "/.git/refs/"
|
2024-01-11 23:24:09 -06:00
|
|
|
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-23 15:20:54 -06:00
|
|
|
_, _, output := RunCmd(rs.realPath.String(), 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")
|
2024-01-25 22:59:49 -06:00
|
|
|
rs.versionCmdOutput.SetText("UNKNOWN BRANCHES")
|
2024-01-11 23:24:09 -06:00
|
|
|
perfect = false
|
|
|
|
parts := strings.Split(b, "/")
|
|
|
|
log.Warn("git push", parts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return perfect
|
|
|
|
}
|