repostatus/git.go

296 lines
7.2 KiB
Go

package repostatus
import (
"errors"
"strings"
"time"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
func (rs *RepoStatus) GetCurrentBranchName() string {
return rs.currentBranch.String()
}
func (rs *RepoStatus) GetCurrentBranchVersion() string {
return rs.currentVersion.String()
}
func (rs *RepoStatus) LastGitPull() (time.Time, error) {
return rs.mtime(".git/FETCH_HEAD")
}
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)
}
var ErrorMissingGitConfig error = errors.New("missing .git/config")
var ErrorGitPullOnLocal error = errors.New("git pull on local only branch")
// remove this everything
func (rs *RepoStatus) Path() string {
return rs.realPath.String()
}
/*
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() {
pb := rs.pb
rs.changed = false
if pb.CheckDirty() {
log.Log(REPO, "CheckDirty() true")
rs.gitState.SetText("dirty")
return
}
if pb.GetUserVersion() != pb.GetDevelVersion() {
rs.gitState.SetText("merge to devel")
return
}
if pb.GetDevelVersion() != pb.GetMasterVersion() {
rs.gitState.SetText("merge to main")
return
}
if pb.GetLastTag() != pb.GetMasterVersion() {
rs.gitState.SetText("unchanged")
return
}
if pb.CheckBranches() {
log.Log(REPO, "Branches are Perfect")
rs.gitState.SetText("PERFECT")
return
}
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")
rs.gitState.SetText("unknown branches")
}
func (rs *RepoStatus) GetLastTagVersion() string {
return rs.lasttag.String()
}
func (rs *RepoStatus) displayCurrentBranchName() string {
out := rs.pb.GetCurrentBranchName()
rs.currentBranch.SetValue(out)
return out
}
// stores the current branch name
func (rs *RepoStatus) checkCurrentBranchName() string {
currentname := rs.currentBranch.String()
out := rs.pb.GetCurrentBranchName()
if currentname == out {
// nothing changed
return currentname
}
rs.currentBranch.SetValue(out)
if currentname == "" {
return out // don't note if there was nothing before
}
rs.NoteChange("current branch has changed from " + currentname + " to " + out)
return out
}
func (rs *RepoStatus) gitDescribeByHash(hash string) (string, error) {
if hash == "" {
return "", errors.New("hash was blank")
}
r := shell.PathRunLog(rs.realPath.String(), []string{"git", "describe", "--tags", "--always", hash}, INFO)
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
}
return out, r.Error
}
func (rs *RepoStatus) gitDescribeByName(name string) (string, error) {
name = strings.TrimSpace(name)
if name == "" {
// git will return the current tag
r := shell.PathRunLog(rs.Path(), []string{"git", "describe", "--tags", "--always"}, INFO)
output := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Warn("gitDescribeByName() not in a git repo?", r.Error, rs.Path())
}
return strings.TrimSpace(output), r.Error
}
if !rs.LocalTagExists(name) {
// tag does not exist
return "", errors.New("gitDescribeByName() git fatal: Not a valid object name")
}
cmd := []string{"git", "describe", "--tags", "--always", name}
r := shell.PathRunLog(rs.Path(), cmd, INFO)
output := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Warn("cmd =", cmd)
log.Warn("err =", r.Error)
log.Warn("not in a git repo or bad tag?", rs.Path())
}
return strings.TrimSpace(output), r.Error
}
// todo: don't run git every time?
func (rs *RepoStatus) checkCurrentBranchVersion() string {
out := rs.pb.GetCurrentVersion()
rs.currentVersion.SetValue(out)
return out
}
// this should get the most recent tag
func (rs *RepoStatus) setLastTagVersion() {
name := rs.pb.GetLastTagVersion()
rs.lasttag.SetText(name)
return
}
func (rs *RepoStatus) populateTags() {
tmp := rs.realPath.String() + "/.git/refs/tags"
log.Log(REPO, "populateTags() path =", tmp)
for _, tag := range gitpb.ListFiles(tmp) {
if rs.tags[tag] == "" {
log.Log(REPO, "populateTags() Adding new tag", tag)
// rs.tagsDrop.AddText(tag)
rs.tags[tag] = "origin"
}
}
// rs.tagsDrop.SetText(rs.lasttagrev)
}
/*
func (rs *RepoStatus) getBranches() []string {
var all []string
var heads []string
var remotes []string
heads = gitpb.ListFiles(rs.realPath.String() + "/.git/refs/heads")
remotes = gitpb.ListFiles(rs.realPath.String() + "/.git/refs/remotes")
all = heads
all = append(all, remotes...)
for _, branch := range all {
log.Log(REPO, "getBranches()", branch)
}
return all
}
*/
// returns quickly based on the last time it was checked
func (rs *RepoStatus) IsDirty() bool {
return rs.pb.IsDirty()
}
/*
// 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
}
*/
func (rs *RepoStatus) CheckDirty() bool {
if rs.pb.IsDirty() {
rs.dirtyLabel.SetValue("dirty")
return true
}
rs.dirtyLabel.SetValue("")
return false
}
/*
func (rs *RepoStatus) CheckoutBranch(bname string) bool {
if rs.CheckDirty() {
log.Log(REPO, rs.realPath.String(), "is dirty")
log.Info(bname, "is dirty", rs.Path())
return false
}
if !rs.TagExists(bname) {
// tag does not exist
log.Log(REPO, "repo does not have branch", bname, rs.Path())
return false
}
cName := rs.GetCurrentBranchName()
if cName == bname {
// already on branch
return true
}
cmd := []string{"git", "checkout", bname}
r := rs.Run(cmd)
if r.Error != nil {
log.Log(REPO, "git checkout error:", r.Error)
return false
}
rs.checkCurrentBranchName()
rs.checkCurrentBranchVersion()
return true
}
*/