repostatus/git.go

198 lines
4.9 KiB
Go
Raw Normal View History

2024-01-09 15:34:53 -06:00
package repostatus
import (
2024-02-16 17:55:13 -06:00
"errors"
"strings"
"go.wit.com/lib/gui/shell"
2025-01-07 18:24:17 -06:00
"go.wit.com/lib/protobuf/gitpb"
2024-11-15 09:12:47 -06:00
"go.wit.com/log"
2024-01-09 15:34:53 -06:00
)
2025-01-07 19:09:25 -06:00
/*
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-02-22 15:29:22 -06:00
func (rs *RepoStatus) Age() time.Duration {
var t *Tag
t = rs.NewestTag()
if t != nil {
log.Log(REPO, "newest tag:", t.date.String(), t.tag.String(), t.Name())
return t.Age()
}
const gitLayout = "Mon Jan 2 15:04:05 2006 -0700"
const madeuptime = "Mon Jun 3 15:04:05 2013 -0700"
tagTime, _ := time.Parse(gitLayout, madeuptime)
return time.Since(tagTime)
}
2024-02-29 19:16:45 -06:00
var ErrorMissingGitConfig error = errors.New("missing .git/config")
var ErrorGitPullOnLocal error = errors.New("git pull on local only branch")
2025-01-07 19:09:25 -06:00
*/
2024-02-29 19:16:45 -06:00
2025-01-07 04:58:05 -06:00
// remove this everything
func (rs *RepoStatus) Path() string {
return rs.realPath.String()
2024-02-22 17:19:29 -06:00
}
2024-12-03 00:34:55 -06:00
func (rs *RepoStatus) GitState() string {
return rs.gitState.String()
}
func (rs *RepoStatus) CheckGitState() string {
rs.setState()
return rs.gitState.String()
}
func (rs *RepoStatus) GetStatus() string {
return rs.gitState.String()
}
func (rs *RepoStatus) setState() {
2025-01-07 04:58:05 -06:00
pb := rs.pb
2024-12-03 00:34:55 -06:00
rs.changed = false
2025-01-07 04:58:05 -06:00
if pb.CheckDirty() {
2024-12-03 00:34:55 -06:00
log.Log(REPO, "CheckDirty() true")
rs.gitState.SetText("dirty")
return
}
2025-01-07 04:58:05 -06:00
if pb.GetUserVersion() != pb.GetDevelVersion() {
2024-12-03 00:34:55 -06:00
rs.gitState.SetText("merge to devel")
return
}
2025-01-07 04:58:05 -06:00
if pb.GetDevelVersion() != pb.GetMasterVersion() {
2024-12-03 00:34:55 -06:00
rs.gitState.SetText("merge to main")
return
}
2025-01-07 04:58:05 -06:00
if pb.GetLastTag() != pb.GetMasterVersion() {
2024-12-03 00:34:55 -06:00
rs.gitState.SetText("unchanged")
return
}
2025-01-07 04:58:05 -06:00
if pb.CheckBranches() {
2024-12-03 00:34:55 -06:00
log.Log(REPO, "Branches are Perfect")
rs.gitState.SetText("PERFECT")
return
}
2025-01-07 04:58:05 -06:00
log.Log(REPO, "FIND THIS IN REPO STATUS Branches are not Perfect")
log.Log(REPO, "FIND THIS IN REPO STATUS Branches are not Perfect")
log.Log(REPO, "FIND THIS IN REPO STATUS Branches are not Perfect")
log.Log(REPO, "FIND THIS IN REPO STATUS Branches are not Perfect")
2024-12-03 00:34:55 -06:00
rs.gitState.SetText("unknown branches")
}
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
}
2025-01-07 04:58:05 -06:00
func (rs *RepoStatus) displayCurrentBranchName() string {
out := rs.pb.GetCurrentBranchName()
rs.currentBranch.SetValue(out)
return out
}
2024-02-16 17:55:13 -06:00
// stores the current branch name
func (rs *RepoStatus) checkCurrentBranchName() string {
currentname := rs.currentBranch.String()
2025-01-07 18:18:38 -06:00
out := rs.pb.GetCurrentBranchName()
2024-02-16 17:55:13 -06:00
if currentname == out {
// nothing changed
return currentname
}
rs.currentBranch.SetValue(out)
2024-02-16 17:55:13 -06:00
if currentname == "" {
return out // don't note if there was nothing before
}
rs.NoteChange("current branch has changed from " + currentname + " to " + out)
2024-01-09 15:34:53 -06:00
return out
}
2024-02-16 17:55:13 -06:00
func (rs *RepoStatus) gitDescribeByHash(hash string) (string, error) {
if hash == "" {
return "", errors.New("hash was blank")
}
2025-01-07 04:58:05 -06:00
r := shell.PathRunLog(rs.realPath.String(), []string{"git", "describe", "--tags", "--always", hash}, INFO)
2024-11-08 06:43:33 -06:00
out := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Warn("not in a git repo or bad hash?", r.Error, rs.Path())
return out, r.Error
2024-02-16 17:55:13 -06:00
}
2024-11-08 06:43:33 -06:00
return out, r.Error
2024-02-16 17:55:13 -06:00
}
func (rs *RepoStatus) gitDescribeByName(name string) (string, error) {
2024-02-16 11:41:29 -06:00
name = strings.TrimSpace(name)
if name == "" {
// git will return the current tag
r := shell.PathRunLog(rs.Path(), []string{"git", "describe", "--tags", "--always"}, INFO)
2024-11-08 06:43:33 -06:00
output := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Warn("gitDescribeByName() not in a git repo?", r.Error, rs.Path())
2024-02-16 11:41:29 -06:00
}
2024-11-08 06:43:33 -06:00
return strings.TrimSpace(output), r.Error
2024-02-16 11:41:29 -06:00
}
2024-02-16 17:55:13 -06:00
if !rs.LocalTagExists(name) {
// tag does not exist
2024-11-08 06:43:33 -06:00
return "", errors.New("gitDescribeByName() git fatal: Not a valid object name")
2024-02-16 17:55:13 -06:00
}
cmd := []string{"git", "describe", "--tags", "--always", name}
r := shell.PathRunLog(rs.Path(), cmd, INFO)
2024-11-08 06:43:33 -06:00
output := strings.Join(r.Stdout, "\n")
if r.Error != nil {
2024-02-16 17:55:13 -06:00
log.Warn("cmd =", cmd)
2024-11-08 06:43:33 -06:00
log.Warn("err =", r.Error)
2024-02-16 17:55:13 -06:00
log.Warn("not in a git repo or bad tag?", rs.Path())
2024-02-16 11:41:29 -06:00
}
2024-11-08 06:43:33 -06:00
return strings.TrimSpace(output), r.Error
2024-02-16 11:41:29 -06:00
}
// todo: don't run git every time?
2024-02-16 17:55:13 -06:00
func (rs *RepoStatus) checkCurrentBranchVersion() string {
2025-01-07 04:58:05 -06:00
out := rs.pb.GetCurrentVersion()
rs.currentVersion.SetValue(out)
2024-01-09 15:34:53 -06:00
return out
}
2024-02-16 17:55:13 -06:00
// this should get the most recent tag
func (rs *RepoStatus) setLastTagVersion() {
2025-01-07 18:18:38 -06:00
name := rs.pb.GetLastTagVersion()
2024-02-16 17:55:13 -06:00
rs.lasttag.SetText(name)
return
2024-01-09 15:34:53 -06:00
}
func (rs *RepoStatus) populateTags() {
tmp := rs.realPath.String() + "/.git/refs/tags"
log.Log(REPO, "populateTags() path =", tmp)
2025-01-07 18:24:17 -06:00
for _, tag := range gitpb.ListFiles(tmp) {
2024-01-09 15:34:53 -06:00
if rs.tags[tag] == "" {
log.Log(REPO, "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
}
2024-02-16 20:36:31 -06:00
// returns quickly based on the last time it was checked
func (rs *RepoStatus) IsDirty() bool {
2025-01-07 04:58:05 -06:00
return rs.pb.IsDirty()
2024-02-16 20:36:31 -06:00
}
2024-01-13 20:30:33 -06:00
func (rs *RepoStatus) CheckDirty() bool {
2025-01-07 04:58:05 -06:00
if rs.pb.IsDirty() {
rs.dirtyLabel.SetValue("dirty")
return true
}
2025-01-07 04:58:05 -06:00
rs.dirtyLabel.SetValue("")
return false
2024-01-09 15:34:53 -06:00
}