repostatus/git.go

207 lines
5.1 KiB
Go
Raw Normal View History

2024-01-09 15:34:53 -06:00
package repostatus
import (
"strings"
"unicode/utf8"
"io/ioutil"
2024-01-09 15:34:53 -06:00
"go.wit.com/log"
)
2024-01-13 20:30:33 -06:00
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()
}
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.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)
2024-01-09 15:34:53 -06:00
// 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)
2024-01-09 15:34:53 -06:00
rs.tags[tag] = "origin"
}
}
// rs.tagsDrop.Set(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 {
2024-01-09 15:34:53 -06:00
out := run(rs.repopath, "git", "diff-index HEAD")
if out == "" {
2024-01-13 20:30:33 -06:00
log.Warn("CheckDirty() no", rs.repopath)
2024-01-09 15:34:53 -06:00
rs.dirtyLabel.Set("no")
return false
} else {
2024-01-13 20:30:33 -06:00
log.Warn("CheckDirty() true", rs.repopath)
2024-01-09 15:34:53 -06:00
rs.dirtyLabel.Set("dirty")
return true
}
}
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)
realname := rs.getCurrentBranchName()
realversion := rs.getCurrentBranchVersion()
log.Warn(rs.repopath, "realname =", realname, "realversion =", realversion)
switch level {
case "master":
2024-01-13 20:30:33 -06:00
rs.masterBranchVersion.Set(realversion)
case "devel":
2024-01-13 20:30:33 -06:00
rs.develBranchVersion.Set(realversion)
case "user":
2024-01-13 20:30:33 -06:00
rs.jcarrBranchVersion.Set(realversion)
default:
2024-01-09 15:34:53 -06:00
}
}
2024-01-13 20:30:33 -06:00
// 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.jcarrBranchVersion.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(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
}