repostatus/git.go

301 lines
7.5 KiB
Go
Raw Normal View History

2024-01-09 15:34:53 -06:00
package repostatus
import (
"strings"
"unicode/utf8"
"io/ioutil"
"go.wit.com/log"
2024-01-09 15:34:53 -06:00
)
func (rs *RepoStatus) String() string {
return rs.repopath
}
func (rs *RepoStatus) GetPath() string {
return rs.repopath
}
2024-01-13 20:30:33 -06:00
func (rs *RepoStatus) GetCurrentBranchName() string {
return rs.currentBranch.String()
2024-01-13 20:30:33 -06:00
}
2024-01-13 20:30:33 -06:00
func (rs *RepoStatus) GetCurrentBranchVersion() string {
return rs.currentVersion.String()
2024-01-13 20:30:33 -06:00
}
2024-01-13 20:30:33 -06:00
func (rs *RepoStatus) GetLastTagVersion() string {
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")
log.Warn("getCurrentBranchName() =", out)
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")
log.Warn("getCurrentBranchVersion()", out)
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")
log.Warn("getLastTagVersion()", out)
rs.lasttagrev = out
lastreal := "describe --tags " + out
// out = run(r.path, "git", "describe --tags c871d5ecf051a7dc4e3a77157cdbc0a457eb9ae1")
out = run(rs.repopath, "git", lastreal)
rs.lasttag.SetValue(out)
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")
log.Warn("populateTags() path =", tmp)
for _, tag := range listFiles(tmp) {
if rs.tags[tag] == "" {
log.Warn("populateTags() Adding new tag", tag)
rs.tagsDrop.AddText(tag)
2024-01-09 15:34:53 -06:00
rs.tags[tag] = "origin"
}
}
// rs.tagsDrop.SetText(rs.lasttagrev)
2024-01-09 15:34:53 -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 {
log.Warn("getBranches()", branch)
}
return all
}
2024-01-13 20:30:33 -06:00
func (rs *RepoStatus) CheckDirty() bool {
cmd := []string{"git", "diff-index", "--quiet", "HEAD"}
path := "/home/jcarr/go/src/" + rs.repopath
err, b, out := RunCmd(path, cmd)
if err != nil {
log.Warn("CheckDirty() b =", b)
log.Warn("CheckDirty() path =", path)
log.Warn("CheckDirty() out =", out)
log.Warn("CheckDirty() err =", err)
log.Error(err, "CheckDirty() error")
rs.dirtyLabel.SetValue("error")
return true
}
if b {
log.Warn("CheckDirty() b =", b, "path =", path, "out =", out)
2024-01-13 20:30:33 -06:00
log.Warn("CheckDirty() no", rs.repopath)
rs.dirtyLabel.SetValue("no")
2024-01-09 15:34:53 -06:00
return false
}
log.Warn("CheckDirty() true", rs.repopath)
rs.dirtyLabel.SetValue("dirty")
return true
2024-01-09 15:34:53 -06:00
}
func (rs *RepoStatus) CheckoutBranch(branch string) (string, string) {
// run(rs.repopath, "git", "checkout " + branch)
realname := rs.getCurrentBranchName()
realversion := rs.getCurrentBranchVersion()
log.Warn(rs.repopath, "realname =", realname, "realversion =", realversion)
return realname, realversion
}
func (rs *RepoStatus) checkoutBranch(level string, branch string) {
2024-01-13 20:30:33 -06:00
if rs.CheckDirty() {
2024-01-09 15:34:53 -06:00
log.Warn("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)
2024-01-09 15:34:53 -06:00
realname := rs.getCurrentBranchName()
realversion := rs.getCurrentBranchVersion()
log.Warn(rs.repopath, "realname =", realname, "realversion =", realversion)
switch level {
case "master":
rs.masterBranchVersion.SetValue(realversion)
case "devel":
rs.develBranchVersion.SetValue(realversion)
case "user":
rs.userBranchVersion.SetValue(realversion)
default:
2024-01-09 15:34:53 -06:00
}
}
func (rs *RepoStatus) SetMasterBranchName(s string) {
rs.masterDrop.SetText(s)
rs.masterBranchVersion.SetLabel(s)
// rs.major.SetTitle(s)
}
func (rs *RepoStatus) SetDevelBranchName(s string) {
rs.develDrop.SetText(s)
rs.develBranchVersion.SetLabel(s)
}
func (rs *RepoStatus) SetUserBranchName(s string) {
rs.userDrop.SetText(s)
rs.userBranchVersion.SetLabel(s)
}
2024-01-13 20:30:33 -06:00
// returns "master", "devel", os.Username, etc
func (rs *RepoStatus) GetMasterBranchName() string {
name := rs.masterDrop.String()
2024-01-13 20:30:33 -06:00
return name
}
func (rs *RepoStatus) GetDevelBranchName() string {
name := rs.develDrop.String()
2024-01-13 20:30:33 -06:00
return name
}
func (rs *RepoStatus) GetUserBranchName() string {
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 {
name := rs.masterBranchVersion.String()
2024-01-13 20:30:33 -06:00
return name
}
func (rs *RepoStatus) GetDevelVersion() string {
name := rs.develBranchVersion.String()
2024-01-13 20:30:33 -06:00
return name
}
func (rs *RepoStatus) GetUserVersion() string {
name := rs.userBranchVersion.String()
2024-01-13 20:30:33 -06:00
return name
}
func (rs *RepoStatus) SetMasterVersion(s string) {
if rs.GetMasterVersion() == s {
return
}
rs.changed = true
log.Verbose("git", rs.GetMasterBranchName(), "is now version =", s)
rs.masterBranchVersion.SetValue(s)
}
func (rs *RepoStatus) SetDevelVersion(s string) {
if rs.GetDevelVersion() == s {
return
}
rs.changed = true
rs.develBranchVersion.SetValue(s)
log.Verbose("git", rs.GetDevelBranchName(), "s now version =", s)
}
func (rs *RepoStatus) SetUserVersion(s string) {
if rs.GetUserVersion() == s {
return
}
rs.changed = true
rs.userBranchVersion.SetValue(s)
log.Verbose("git", rs.GetUserBranchName(), "is now version =", s)
}
func (rs *RepoStatus) GetStatus() string {
rs.changed = false
if rs.CheckDirty() {
log.Warn("CheckDirty() true")
return "dirty"
}
if rs.userBranchVersion.String() != rs.develBranchVersion.String() {
return "merge to devel"
}
if rs.userBranchVersion.String() != rs.masterBranchVersion.String() {
return "ready to release"
}
if rs.lasttag.String() != rs.masterBranchVersion.String() {
return "ready to tag version"
}
if rs.CheckBranches() {
log.Info("Branches are Perfect")
return "PERFECT"
}
log.Warn(rs.GetPath(), "Branches are not Perfect")
return "unknown branches"
}
// TODO: make this report the error somewhere
2024-01-13 20:30:33 -06:00
func (rs *RepoStatus) CheckBranches() bool {
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" {
log.Warn("skip HEAD fullfile", fullfile)
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)
_, _, output := RunCmd("/home/jcarr/go/src/"+rs.repopath, cmd)
// git show -s --format=%ci <hash> will give you the time
// log.Warn(fullfile)
if hash == hashCheck {
log.Warn(hash, output, b)
} else {
log.Warn("UNKNOWN BRANCHES IN THIS REPO")
rs.versionMessage.SetText("UNKNOWN BRANCHES")
perfect = false
parts := strings.Split(b, "/")
log.Warn("git push", parts)
}
}
return perfect
}