summaryrefslogtreecommitdiff
path: root/git.go
diff options
context:
space:
mode:
Diffstat (limited to 'git.go')
-rw-r--r--git.go147
1 files changed, 104 insertions, 43 deletions
diff --git a/git.go b/git.go
index 517e328..e47b828 100644
--- a/git.go
+++ b/git.go
@@ -1,6 +1,7 @@
package repostatus
import (
+ "errors"
"strings"
"time"
"unicode/utf8"
@@ -26,53 +27,88 @@ func (rs *RepoStatus) GetLastTagVersion() string {
return rs.lasttag.String()
}
-func (rs *RepoStatus) getCurrentBranchName() string {
+// stores the current branch name
+func (rs *RepoStatus) checkCurrentBranchName() string {
+ currentname := rs.currentBranch.String()
out := run(rs.realPath.String(), "git", "branch --show-current")
- log.Log(INFO, "getCurrentBranchName() =", out)
+ 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) gitDescribeTags(name string) (string, error) {
+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")
+ }
+ err, out := rs.RunCmd([]string{"git", "describe", "--tags", "--always", hash})
+ if err != nil {
+ log.Warn("not in a git repo or bad hash?", err, rs.Path())
+ return "", err
+ }
+ out = strings.TrimSpace(out)
+ return out, err
+}
+
+func (rs *RepoStatus) gitDescribeByName(name string) (string, error) {
name = strings.TrimSpace(name)
if name == "" {
// git will return the current tag
- err, out := rs.RunCmd([]string{"git", "describe", "--tags"})
+ err, out := rs.RunCmd([]string{"git", "describe", "--tags", "--always"})
if err != nil {
log.Warn("not in a git repo?", err, rs.Path())
return "", err
}
+ out = strings.TrimSpace(out)
return out, err
}
- err, out := rs.RunCmd([]string{"git", "describe", "--tags", name})
+ if !rs.LocalTagExists(name) {
+ // tag does not exist
+ return "", errors.New("git fatal: Not a valid object name")
+ }
+ cmd := []string{"git", "describe", "--tags", "--always", name}
+ err, out := rs.RunCmd(cmd)
if err != nil {
- log.Warn("not in a git repo or bad tag?", err, rs.Path())
+ log.Warn("cmd =", cmd)
+ log.Warn("err =", err)
+ log.Warn("not in a git repo or bad tag?", rs.Path())
return "", err
}
+ out = strings.TrimSpace(out)
return out, err
}
// todo: don't run git every time?
-func (rs *RepoStatus) getCurrentBranchVersion() string {
- out, _ := rs.gitDescribeTags("")
- log.Log(INFO, "getCurrentBranchVersion()", out)
+func (rs *RepoStatus) checkCurrentBranchVersion() string {
+ out, _ := rs.gitDescribeByName("")
+ log.Log(INFO, "checkCurrentBranchVersion()", out)
rs.currentVersion.SetValue(out)
return out
}
-func (rs *RepoStatus) getLastTagVersion() string {
- out := run(rs.realPath.String(), "git", "rev-list --tags --max-count=1")
- log.Log(INFO, "getLastTagVersion()", out)
- // rs.lasttagrev = out
+func (rs *RepoStatus) getCurrentBranchVersion() string {
+ return rs.currentVersion.String()
+}
- lastreal := "describe --tags " + out
- // out = run(r.path, "git", "describe --tags c871d5ecf051a7dc4e3a77157cdbc0a457eb9ae1")
- out = run(rs.realPath.String(), "git", lastreal)
- rs.lasttag.SetValue(out)
- // rs.tagsDrop.SetText(out)
- // rs.lastLabel.SetText(out)
- return out
+// 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(INFO, "getLastTagVersion()", hash)
+
+ name, _ := rs.gitDescribeByHash(hash)
+ rs.lasttag.SetText(name)
+ return
}
func (rs *RepoStatus) populateTags() {
@@ -141,17 +177,32 @@ func (rs *RepoStatus) CheckDirty() bool {
return true
}
-
-/*
-func (rs *RepoStatus) CheckoutBranch(branch string) (string, string) {
- // run(rs.realPath.String(), "git", "checkout " + branch)
-
- realname := rs.getCurrentBranchName()
- realversion := rs.getCurrentBranchVersion()
- log.Log(INFO, rs.realPath.String(), "realname =", realname, "realversion =", realversion)
- return realname, realversion
+func (rs *RepoStatus) CheckoutBranch(bname string) bool {
+ if rs.CheckDirty() {
+ log.Log(INFO, rs.realPath.String(), "is dirty")
+ log.Info("bname is dirty", bname, rs.Path())
+ return false
+ }
+ if !rs.TagExists(bname) {
+ // tag does not exist
+ log.Info("bname already exists", bname, rs.Path())
+ return false
+ }
+ cName := rs.GetCurrentBranchName()
+ if cName == bname {
+ // already on branch
+ return true
+ }
+ cmd := []string{"git", "checkout", bname}
+ err, b, output := RunCmd(rs.realPath.String(), cmd)
+ if err != nil {
+ log.Log(INFO, err, b, output)
+ return false
+ }
+ rs.checkCurrentBranchName()
+ rs.checkCurrentBranchVersion()
+ return true
}
-*/
func (rs *RepoStatus) CheckoutMaster() bool {
if rs.CheckDirty() {
@@ -308,7 +359,6 @@ func (rs *RepoStatus) setDevelWorkingName(s string) {
func (rs *RepoStatus) setUserWorkingName(s string) {
rs.userWorkingName.SetValue(s)
rs.userBranchVersion.SetLabel(s)
- // rs.userDrop.SetText(s)
}
// returns "master", "devel", os.Username, etc
@@ -340,31 +390,42 @@ func (rs *RepoStatus) GetUserVersion() string {
return name
}
-func (rs *RepoStatus) SetMasterVersion(s string) {
- if rs.GetMasterVersion() == s {
+func (rs *RepoStatus) setMasterVersion(s string) {
+ old := rs.GetMasterVersion()
+ if old == s {
return
}
- rs.changed = true
- log.Verbose("git", rs.GetMasterBranchName(), "is now version =", s)
rs.masterBranchVersion.SetValue(s)
+ if old == "" {
+ return // don't note if there was nothing before
+ }
+ rs.NoteChange("master branch has been changed from " + old + " to " + s)
}
-func (rs *RepoStatus) SetDevelVersion(s string) {
- if rs.GetDevelVersion() == s {
+func (rs *RepoStatus) setDevelVersion(s string) {
+ old := rs.GetDevelVersion()
+ if old == s {
return
}
- rs.changed = true
+ if old == "" {
+ // don't note nothing
+ } else {
+ rs.NoteChange("devel branch has been changed from " + old + " to " + s)
+ }
rs.develBranchVersion.SetValue(s)
- log.Verbose("git", rs.GetDevelBranchName(), "s now version =", s)
}
-func (rs *RepoStatus) SetUserVersion(s string) {
- if rs.GetUserVersion() == s {
+func (rs *RepoStatus) setUserVersion(s string) {
+ old := rs.GetUserVersion()
+ if old == s {
return
}
- rs.changed = true
+ if old == "" {
+ // don't note nothing
+ } else {
+ rs.NoteChange("user branch has been changed from " + old + " to " + s)
+ }
rs.userBranchVersion.SetValue(s)
- // log.Verbose("git", rs.GetUserBranchName(), "is now version =", s)
}
func (rs *RepoStatus) GetStatus() string {