repostatus/git.go

249 lines
6.2 KiB
Go

package repostatus
import (
"strings"
"unicode/utf8"
"io/ioutil"
"go.wit.com/log"
)
func (rs *RepoStatus) GetPath() string {
return rs.repopath
}
func (rs *RepoStatus) GetCurrentBranchName() string {
return rs.currentBranch.Get()
}
func (rs *RepoStatus) GetCurrentBranchVersion() string {
return rs.currentVersion.Get()
}
func (rs *RepoStatus) GetLastTagVersion() string {
return rs.lasttag.Get()
}
func (rs *RepoStatus) getCurrentBranchName() string {
out := run(rs.repopath, "git", "branch --show-current")
log.Warn("getCurrentBranchName() =", out)
rs.currentBranch.Set(out)
return out
}
func (rs *RepoStatus) getCurrentBranchVersion() string {
out := run(rs.repopath, "git", "describe --tags")
log.Warn("getCurrentBranchVersion()", out)
rs.currentVersion.Set(out)
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.Set(out)
rs.tagsDrop.Set(out)
// rs.lastLabel.Set(out)
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.Add(tag)
rs.tags[tag] = "origin"
}
}
// rs.tagsDrop.Set(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.Warn("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.Set("error")
return true
}
if b {
log.Warn("CheckDirty() b =", b, "path =", path, "out =", out)
log.Warn("CheckDirty() no", rs.repopath)
rs.dirtyLabel.Set("no")
return false
}
log.Warn("CheckDirty() true", rs.repopath)
rs.dirtyLabel.Set("dirty")
return true
}
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) {
if rs.CheckDirty() {
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)
realname := rs.getCurrentBranchName()
realversion := rs.getCurrentBranchVersion()
log.Warn(rs.repopath, "realname =", realname, "realversion =", realversion)
switch level {
case "master":
rs.masterBranchVersion.Set(realversion)
case "devel":
rs.develBranchVersion.Set(realversion)
case "user":
rs.userBranchVersion.Set(realversion)
default:
}
}
func (rs *RepoStatus) SetMasterName(s string) {
rs.masterDrop.Set(s)
rs.masterBranchVersion.SetLabel(s)
// rs.major.SetTitle(s)
}
func (rs *RepoStatus) SetDevelName(s string) {
rs.develDrop.Set(s)
rs.develBranchVersion.SetLabel(s)
}
func (rs *RepoStatus) SetUserName(s string) {
rs.userDrop.Set(s)
rs.userBranchVersion.SetLabel(s)
}
// returns "master", "devel", os.Username, etc
func (rs *RepoStatus) GetMasterName() string {
name := rs.masterDrop.Get()
log.Warn("GetMasterName() =", name)
return name
}
func (rs *RepoStatus) GetDevelName() string {
name := rs.develDrop.Get()
log.Warn("GetDevelName() =", name)
return name
}
func (rs *RepoStatus) GetUserName() string {
name := rs.userDrop.Get()
log.Warn("GetUserName() =", name)
return name
}
// returns the git versions like "1.3-2-laksdjf" or whatever
func (rs *RepoStatus) GetMasterVersion() string {
name := rs.masterBranchVersion.Get()
log.Warn("GetMasterVersion() =", name)
return name
}
func (rs *RepoStatus) GetDevelVersion() string {
name := rs.develBranchVersion.Get()
log.Warn("GetBranchVersion() =", name)
return name
}
func (rs *RepoStatus) GetUserVersion() string {
name := rs.userBranchVersion.Get()
log.Warn("GetUserVersion() =", name)
return name
}
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(hash, output, b, "NOT THE SAME")
perfect = false
parts := strings.Split(b, "/")
log.Warn("git push", parts)
}
}
return perfect
}