summaryrefslogtreecommitdiff
path: root/git.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-02-16 20:36:31 -0600
committerJeff Carr <[email protected]>2024-02-16 20:36:31 -0600
commitc7aaea4ae117b95a894b775bf8ef2e661db6f0ea (patch)
tree2958448261f3cb98cc2cceca0a39288173bb3be3 /git.go
parent0b4f4d76866ccc500112cfe553240c47961258e2 (diff)
continue to redo the branch handling
Diffstat (limited to 'git.go')
-rw-r--r--git.go134
1 files changed, 57 insertions, 77 deletions
diff --git a/git.go b/git.go
index e47b828..0f5255a 100644
--- a/git.go
+++ b/git.go
@@ -2,6 +2,7 @@ package repostatus
import (
"errors"
+ "os/user"
"strings"
"time"
"unicode/utf8"
@@ -43,10 +44,6 @@ func (rs *RepoStatus) checkCurrentBranchName() string {
return out
}
-func (rs *RepoStatus) getCurrentBranchName() string {
- return rs.currentBranch.String()
-}
-
func (rs *RepoStatus) gitDescribeByHash(hash string) (string, error) {
if hash == "" {
return "", errors.New("hash was blank")
@@ -97,10 +94,6 @@ func (rs *RepoStatus) checkCurrentBranchVersion() string {
return out
}
-func (rs *RepoStatus) getCurrentBranchVersion() string {
- return rs.currentVersion.String()
-}
-
// this should get the most recent tag
func (rs *RepoStatus) setLastTagVersion() {
hash := run(rs.realPath.String(), "git", "rev-list --tags --max-count=1")
@@ -141,6 +134,11 @@ func (rs *RepoStatus) getBranches() []string {
return all
}
+// returns quickly based on the last time it was checked
+func (rs *RepoStatus) IsDirty() bool {
+ return rs.dirty
+}
+
func (rs *RepoStatus) CheckDirty() bool {
cmd := []string{"git", "status"}
path := rs.realPath.String()
@@ -153,6 +151,7 @@ func (rs *RepoStatus) CheckDirty() bool {
log.Warn("CheckDirty() status err =", err)
log.Error(err, "CheckDirty() git status error")
rs.dirtyLabel.SetValue("error")
+ rs.dirty = true
return true
}
@@ -162,6 +161,7 @@ func (rs *RepoStatus) CheckDirty() bool {
log.Log(INFO, "CheckDirty() b =", b, "path =", path, "out =", out)
log.Log(INFO, "CheckDirty() no", rs.realPath.String())
rs.dirtyLabel.SetValue("no")
+ rs.dirty = false
return false
}
// sometimes b gets exit status 1 when there isn't anything that has changed
@@ -174,6 +174,7 @@ func (rs *RepoStatus) CheckDirty() bool {
log.Log(INFO, "CheckDirty() is normal err =", err)
rs.dirtyLabel.SetValue("dirty")
+ rs.dirty = true
return true
}
@@ -210,21 +211,9 @@ func (rs *RepoStatus) CheckoutMaster() bool {
return false
}
mName := rs.GetMasterBranchName()
- cmd := []string{"git", "checkout", mName}
- err, b, output := RunCmd(rs.realPath.String(), cmd)
- if err != nil {
- log.Log(INFO, err, b, output)
- }
-
- realname := rs.getCurrentBranchName()
- realversion := rs.getCurrentBranchVersion()
- log.Log(INFO, rs.realPath.String(), "realname =", realname, "realversion =", realversion)
-
- if realname != mName {
- log.Log(INFO, "git checkout failed", rs.realPath.String(), mName, "!=", realname)
- return false
+ if rs.CheckoutBranch(mName) {
+ return true
}
- rs.masterBranchVersion.SetValue(realversion)
return true
}
@@ -261,8 +250,8 @@ func (rs *RepoStatus) CheckoutUser() bool {
log.Log(INFO, err, b, output)
}
- realname := rs.getCurrentBranchName()
- realversion := rs.getCurrentBranchVersion()
+ realname := rs.GetCurrentBranchName()
+ realversion := rs.GetCurrentBranchVersion()
log.Log(INFO, rs.realPath.String(), "realname =", realname, "realversion =", realversion)
if realname != bName {
@@ -281,13 +270,13 @@ func (rs *RepoStatus) checkoutBranch(level string, branch string) {
out := run(rs.realPath.String(), "git", "checkout "+branch)
log.Log(INFO, rs.realPath.String(), "git checkout "+branch, "returned", out)
- realname := rs.getCurrentBranchName()
- realversion := rs.getCurrentBranchVersion()
+ realname := rs.GetCurrentBranchName()
+ realversion := rs.GetCurrentBranchVersion()
log.Log(INFO, rs.realPath.String(), "realname =", realname, "realversion =", realversion)
switch level {
case "master":
- rs.masterBranchVersion.SetValue(realversion)
+ rs.mainBranchVersion.SetValue(realversion)
case "devel":
rs.develBranchVersion.SetValue(realversion)
case "user":
@@ -298,67 +287,58 @@ func (rs *RepoStatus) checkoutBranch(level string, branch string) {
// attempt's to guess at what master is.
// TODO: fix this properly
-func (rs *RepoStatus) setMainWorkingName(s string) {
- if rs == nil {
- log.Info("rs == nil", s)
+func (rs *RepoStatus) guessMainWorkingName() {
+ if !rs.Ready() {
return
}
- if rs.gitConfig == nil {
- log.Info("rs.gitConfig == nil", s)
- rs.mainWorkingName.SetValue(s)
+ if rs.TagExists("guimaster") {
+ rs.mainWorkingName.SetText("guimaster")
+ rs.mainBranchVersion.SetLabel("guimaster")
return
}
- if rs.gitConfig.branches == nil {
- log.Info("rs.gitConfig.branches == nil", s)
- rs.mainWorkingName.SetValue(s)
+ if rs.TagExists("master") {
+ rs.mainWorkingName.SetText("master")
+ rs.mainBranchVersion.SetLabel("master")
return
}
- _, ok := rs.gitConfig.branches[s]
- if ok {
- // log.Info("git branch", s, "seems to exist")
- rs.mainWorkingName.SetValue(s)
- return
- }
- s = "guimaster"
- _, ok = rs.gitConfig.branches[s]
- if ok {
- // log.Info("git branch", s, "seems to exist")
- rs.mainWorkingName.SetValue(s)
+ if rs.TagExists("main") {
+ rs.mainWorkingName.SetText("main")
+ rs.mainBranchVersion.SetLabel("main")
return
}
- s = "master"
- _, ok = rs.gitConfig.branches[s]
- if ok {
- // log.Info("git branch", s, "seems to exist")
- rs.mainWorkingName.SetValue(s)
- return
- }
+ // figure out what to do here
+ rs.mainWorkingName.SetText("FIXME")
+ rs.mainBranchVersion.SetLabel("FIXME")
+}
- s = "main"
- _, ok = rs.gitConfig.branches[s]
- if ok {
- // log.Info("git branch", s, "seems to exist")
- rs.mainWorkingName.SetValue(s)
+func (rs *RepoStatus) guessDevelWorkingName() {
+ if rs.TagExists("guidevel") {
+ rs.develWorkingName.SetValue("guidevel")
+ rs.develBranchVersion.SetLabel("guidevel")
return
}
-
- s = "TODO: read .git/config"
- log.Warn("git branch", s, "does not seem to exist. TODO: figure out the server default")
- for name, _ := range rs.gitConfig.branches {
- log.Warn("git branch found. use this?", name)
+ if rs.TagExists("devel") {
+ rs.develWorkingName.SetValue("devel")
+ rs.develBranchVersion.SetLabel("devel")
+ return
}
- rs.mainWorkingName.SetValue(s)
-}
-func (rs *RepoStatus) setDevelWorkingName(s string) {
- rs.develWorkingName.SetValue(s)
- rs.develBranchVersion.SetLabel(s)
+ // figure out what to do here
+ rs.develWorkingName.SetValue("develFIXME")
+ rs.develBranchVersion.SetLabel("develFIXME")
}
-func (rs *RepoStatus) setUserWorkingName(s string) {
- rs.userWorkingName.SetValue(s)
- rs.userBranchVersion.SetLabel(s)
+func (rs *RepoStatus) setUserWorkingName() {
+ usr, _ := user.Current()
+ uname := usr.Username
+ if rs.TagExists(uname) {
+ rs.userWorkingName.SetValue(uname)
+ rs.userBranchVersion.SetLabel(uname)
+ return
+ }
+ rs.userWorkingName.SetValue("need to create " + uname)
+ rs.userBranchVersion.SetLabel("need to create " + uname)
}
// returns "master", "devel", os.Username, etc
@@ -378,7 +358,7 @@ func (rs *RepoStatus) GetUserBranchName() string {
// returns the git versions like "1.3-2-laksdjf" or whatever
func (rs *RepoStatus) GetMasterVersion() string {
- name := rs.masterBranchVersion.String()
+ name := rs.mainBranchVersion.String()
return name
}
func (rs *RepoStatus) GetDevelVersion() string {
@@ -395,7 +375,7 @@ func (rs *RepoStatus) setMasterVersion(s string) {
if old == s {
return
}
- rs.masterBranchVersion.SetValue(s)
+ rs.mainBranchVersion.SetValue(s)
if old == "" {
return // don't note if there was nothing before
}
@@ -434,13 +414,13 @@ func (rs *RepoStatus) GetStatus() string {
log.Log(INFO, "CheckDirty() true")
return "dirty"
}
- if rs.userBranchVersion.String() != rs.develBranchVersion.String() {
+ if rs.GetUserVersion() != rs.GetDevelVersion() {
return "merge to devel"
}
- if rs.develBranchVersion.String() != rs.masterBranchVersion.String() {
+ if rs.GetDevelVersion() != rs.GetMasterVersion() {
return "merge to main"
}
- if rs.lasttag.String() != rs.masterBranchVersion.String() {
+ if rs.lasttag.String() != rs.GetMasterVersion() {
return "ready to tag version"
}