gitpb/gitTags.query.go

123 lines
2.8 KiB
Go

package gitpb
// runs git, parses output
// types faster than you can
import (
"errors"
"path/filepath"
"strings"
"go.wit.com/log"
)
func (repo *Repo) GetLastTag() string {
cmd := []string{"git", "rev-list", "--tags", "--max-count=1"}
result := repo.RunQuiet(cmd)
log.Info("getLastTagVersion()", result.Stdout)
if len(result.Stdout) != 1 {
log.Info("git LastTag() error:", result.Stdout)
return "error"
}
hash := result.Stdout[0]
cmd = []string{"git", "describe", "--tags", "--always", hash}
result = repo.RunQuiet(cmd)
if len(result.Stdout) != 1 {
log.Info("git LastTag() error:", result.Stdout)
return "error"
}
return result.Stdout[0]
}
func (repo *Repo) GitMasterVersion() string {
v, err := repo.gitVersionByName("master")
/*
count := repo.LenGitTags()
log.Info(repo.GoPath, "tag count", count)
repo.UpdateGitTags()
count = repo.LenGitTags()
log.Info(repo.GoPath, "tag count", count)
*/
if err == nil {
return v
} else {
log.Info("GitMasterVersion() error", err)
return "error"
}
}
func (repo *Repo) GitDevelVersion() string {
v, err := repo.gitVersionByName("devel")
if err == nil {
return v
} else {
log.Info("GitMasterVersion() error", err)
return "error"
}
}
func (repo *Repo) GitUserVersion() string {
v, err := repo.gitVersionByName("jcarr")
if err == nil {
return v
} else {
log.Info("GitMasterVersion() error", err)
return "error"
}
}
func (repo *Repo) gitVersionByName(name string) (string, error) {
name = strings.TrimSpace(name)
if name == "" {
// git will return the current tag
r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always"})
output := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Warn("gitDescribeByName() not in a git repo?", r.Error, repo.GoPath)
}
return strings.TrimSpace(output), r.Error
}
if !repo.LocalTagExists(name) {
// tag does not exist
log.Warn("LocalTagExists()", name, "did not exist")
return "", errors.New("gitDescribeByName() git fatal: Not a valid object name")
}
cmd := []string{"git", "describe", "--tags", "--always", name}
result := repo.RunQuiet(cmd)
output := strings.Join(result.Stdout, "\n")
if result.Error != nil {
log.Warn("cmd =", cmd)
log.Warn("err =", result.Error)
log.Warn("not in a git repo or bad tag?", repo.GoPath)
}
return strings.TrimSpace(output), result.Error
}
func (repo *Repo) LocalTagExists(findname string) bool {
loop := repo.AllTags()
for loop.Scan() {
t := loop.Next()
log.Info("tag:", t.Refname)
tagname := t.Refname
if strings.HasPrefix(tagname, "refs/remotes") {
continue
}
path, filename := filepath.Split(tagname)
log.Log(GITPB, "tag:", path, filename, "from", repo.GoPath)
if filename == findname {
log.Log(GITPBWARN, "found tag:", path, filename, "from", repo.GoPath)
return true
}
}
return false
}