repostatus/git.go

294 lines
7.3 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"
2024-02-22 15:29:22 -06:00
"time"
"go.wit.com/lib/gui/shell"
2024-11-15 09:12:47 -06:00
"go.wit.com/log"
2024-01-09 15:34:53 -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 17:19:29 -06:00
func (rs *RepoStatus) LastGitPull() (time.Time, error) {
return rs.mtime(".git/FETCH_HEAD")
}
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 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) checkoutBranch(level string, branch string) {
if rs.CheckDirty() {
log.Log(REPO, "checkoutBranch() checkDirty() == true for repo", rs.realPath.String(), "looking for branch:", branch)
return
}
out := run(rs.realPath.String(), "git", "checkout "+branch)
log.Log(REPO, rs.realPath.String(), "git checkout "+branch, "returned", out)
realname := rs.GetCurrentBranchName()
realversion := rs.GetCurrentBranchVersion()
log.Log(REPO, rs.realPath.String(), "realname =", realname, "realversion =", realversion)
switch level {
case "master":
rs.mainBranchVersion.SetValue(realversion)
case "devel":
rs.develBranchVersion.SetValue(realversion)
case "user":
rs.userBranchVersion.SetValue(realversion)
default:
}
}
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()
out := run(rs.realPath.String(), "git", "branch --show-current")
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() {
hash := run(rs.realPath.String(), "git", "rev-list --tags --max-count=1")
log.Log(REPO, "getLastTagVersion()", hash)
2024-02-16 17:55:13 -06:00
name, _ := rs.gitDescribeByHash(hash)
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)
2024-01-09 15:34:53 -06:00
for _, tag := range listFiles(tmp) {
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
}
func (rs *RepoStatus) getBranches() []string {
var all []string
var heads []string
var remotes []string
heads = listFiles(rs.realPath.String() + "/.git/refs/heads")
remotes = listFiles(rs.realPath.String() + "/.git/refs/remotes")
all = heads
all = append(all, remotes...)
for _, branch := range all {
log.Log(REPO, "getBranches()", branch)
}
return all
}
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
}
2025-01-07 04:58:05 -06:00
/*
// return the list of dirty files (but ignores go.mod & go.sum)
func (rs *RepoStatus) DirtyList() []string {
var all []string
for _, line := range strings.Split(rs.dirtyList, "\n") {
line = strings.TrimSpace(line)
parts := strings.Split(line, " ")
if len(parts) != 2 {
continue
}
if parts[1] == "go.mod" {
continue
}
if parts[1] == "go.sum" {
continue
}
all = append(all, parts[1])
}
return all
}
2025-01-07 04:58:05 -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
}
2024-12-03 00:34:55 -06:00
2025-01-07 04:58:05 -06:00
/*
2024-02-16 17:55:13 -06:00
func (rs *RepoStatus) CheckoutBranch(bname string) bool {
if rs.CheckDirty() {
log.Log(REPO, rs.realPath.String(), "is dirty")
2024-02-22 05:24:31 -06:00
log.Info(bname, "is dirty", rs.Path())
2024-02-16 17:55:13 -06:00
return false
}
if !rs.TagExists(bname) {
// tag does not exist
log.Log(REPO, "repo does not have branch", bname, rs.Path())
2024-02-16 17:55:13 -06:00
return false
}
cName := rs.GetCurrentBranchName()
if cName == bname {
// already on branch
return true
}
cmd := []string{"git", "checkout", bname}
2024-11-08 06:43:33 -06:00
r := rs.Run(cmd)
if r.Error != nil {
log.Log(REPO, "git checkout error:", r.Error)
2024-02-16 17:55:13 -06:00
return false
}
rs.checkCurrentBranchName()
rs.checkCurrentBranchVersion()
return true
}
2025-01-07 04:58:05 -06:00
*/