repostatus/git.go

304 lines
7.7 KiB
Go

package repostatus
import (
"strings"
"unicode/utf8"
"io/ioutil"
"go.wit.com/log"
)
func (rs *RepoStatus) String() string {
return rs.repopath
}
func (rs *RepoStatus) GetPath() string {
return rs.repopath
}
func (rs *RepoStatus) GetCurrentBranchName() string {
return rs.currentBranch.String()
}
func (rs *RepoStatus) GetCurrentBranchVersion() string {
return rs.currentVersion.String()
}
func (rs *RepoStatus) GetLastTagVersion() string {
return rs.lasttag.String()
}
func (rs *RepoStatus) getCurrentBranchName() string {
out := run(rs.repopath, "git", "branch --show-current")
log.Log(INFO, "getCurrentBranchName() =", out)
rs.currentBranch.SetValue(out)
return out
}
func (rs *RepoStatus) getCurrentBranchVersion() string {
out := run(rs.repopath, "git", "describe --tags")
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.Log(INFO, "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)
return out
}
func (rs *RepoStatus) populateTags() {
tmp := fullpath(rs.repopath + "/.git/refs/tags")
log.Log(INFO, "populateTags() path =", tmp)
for _, tag := range listFiles(tmp) {
if rs.tags[tag] == "" {
log.Log(INFO, "populateTags() Adding new tag", tag)
rs.tagsDrop.AddText(tag)
rs.tags[tag] = "origin"
}
}
// rs.tagsDrop.SetText(rs.lasttagrev)
}
/*
.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.Log(INFO, "getBranches()", branch)
}
return all
}
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.Log(INFO, "CheckDirty() b =", b, "path =", path, "out =", out)
log.Log(INFO, "CheckDirty() no", rs.repopath)
rs.dirtyLabel.SetValue("no")
return false
}
log.Log(INFO, "CheckDirty() true", rs.repopath)
rs.dirtyLabel.SetValue("dirty")
return true
}
func (rs *RepoStatus) CheckoutBranch(branch string) (string, string) {
// run(rs.repopath, "git", "checkout " + branch)
realname := rs.getCurrentBranchName()
realversion := rs.getCurrentBranchVersion()
log.Log(INFO, rs.repopath, "realname =", realname, "realversion =", realversion)
return realname, realversion
}
func (rs *RepoStatus) checkoutBranch(level string, branch string) {
if rs.CheckDirty() {
log.Log(INFO, "checkoutBranch() checkDirty() == true for repo", rs.repopath, "looking for branch:", branch)
return
}
out := run(rs.repopath, "git", "checkout "+branch)
log.Log(INFO, rs.repopath, "git checkout "+branch, "returned", out)
realname := rs.getCurrentBranchName()
realversion := rs.getCurrentBranchVersion()
log.Log(INFO, 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:
}
}
func (rs *RepoStatus) SetMainWorkingName(s string) {
rs.mainWorkingName.SetValue(s)
rs.masterDrop.SetLabel(s)
rs.masterDrop.SetText("guimaster")
}
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) SetUserWorkingName(s string) {
rs.userWorkingName.SetValue(s)
rs.userBranchVersion.SetLabel(s)
rs.userDrop.SetText(s)
}
// returns "master", "devel", os.Username, etc
func (rs *RepoStatus) GetMasterBranchName() string {
name := rs.masterDrop.String()
return name
}
func (rs *RepoStatus) GetDevelBranchName() string {
name := rs.develDrop.String()
return name
}
func (rs *RepoStatus) GetUserBranchName() string {
name := rs.userDrop.String()
return name
}
// returns the git versions like "1.3-2-laksdjf" or whatever
func (rs *RepoStatus) GetMasterVersion() string {
name := rs.masterBranchVersion.String()
return name
}
func (rs *RepoStatus) GetDevelVersion() string {
name := rs.develBranchVersion.String()
return name
}
func (rs *RepoStatus) GetUserVersion() string {
name := rs.userBranchVersion.String()
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.Log(INFO, "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.Log(INFO, "Branches are Perfect")
return "PERFECT"
}
log.Log(INFO, rs.GetPath(), "Branches are not Perfect")
return "unknown branches"
}
// TODO: make this report the error somewhere
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.Log(INFO, "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.Log(INFO, fullfile)
if hash == hashCheck {
log.Log(INFO, 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
}