2024-11-29 01:23:51 -06:00
|
|
|
package gitpb
|
|
|
|
|
|
|
|
// runs git, parses output
|
|
|
|
// types faster than you can
|
|
|
|
|
|
|
|
import (
|
2024-11-29 02:01:25 -06:00
|
|
|
"errors"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2024-12-01 00:49:25 -06:00
|
|
|
"unicode"
|
2024-11-29 02:01:25 -06:00
|
|
|
|
2024-11-29 01:23:51 -06:00
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (repo *Repo) GetLastTag() string {
|
|
|
|
cmd := []string{"git", "rev-list", "--tags", "--max-count=1"}
|
|
|
|
result := repo.RunQuiet(cmd)
|
2024-11-29 23:32:19 -06:00
|
|
|
// log.Info("getLastTagVersion()", result.Stdout)
|
2024-11-29 01:23:51 -06:00
|
|
|
|
|
|
|
if len(result.Stdout) != 1 {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "git LastTag() error:", result.Stdout)
|
|
|
|
return ""
|
2024-11-29 01:23:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
hash := result.Stdout[0]
|
|
|
|
|
|
|
|
cmd = []string{"git", "describe", "--tags", "--always", hash}
|
|
|
|
result = repo.RunQuiet(cmd)
|
|
|
|
|
|
|
|
if len(result.Stdout) != 1 {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "git LastTag() error:", result.Stdout)
|
|
|
|
return ""
|
2024-11-29 01:23:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return result.Stdout[0]
|
|
|
|
}
|
|
|
|
|
2024-12-01 15:10:55 -06:00
|
|
|
func (repo *Repo) GetMasterVersion() string {
|
2024-11-29 21:51:30 -06:00
|
|
|
bname := repo.GetMasterBranchName()
|
|
|
|
v, err := repo.gitVersionByName(bname)
|
2024-11-29 02:01:25 -06:00
|
|
|
/*
|
2024-11-29 15:47:23 -06:00
|
|
|
count := repo.LenGitTags()
|
|
|
|
log.Info(repo.GoPath, "tag count", count)
|
|
|
|
repo.UpdateGitTags()
|
|
|
|
count = repo.LenGitTags()
|
|
|
|
log.Info(repo.GoPath, "tag count", count)
|
2024-11-29 02:01:25 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return v
|
|
|
|
} else {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "gitpb.GitMasterVersion() error:", err)
|
|
|
|
return ""
|
2024-11-29 02:01:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 15:10:55 -06:00
|
|
|
func (repo *Repo) GetDevelVersion() string {
|
2024-11-29 21:51:30 -06:00
|
|
|
bname := repo.GetDevelBranchName()
|
|
|
|
v, err := repo.gitVersionByName(bname)
|
2024-11-29 02:01:25 -06:00
|
|
|
if err == nil {
|
|
|
|
return v
|
|
|
|
} else {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "gitpb.GitDevelVersion() error:", err)
|
|
|
|
return ""
|
2024-11-29 02:01:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 15:10:55 -06:00
|
|
|
func (repo *Repo) GetUserVersion() string {
|
2024-11-29 21:51:30 -06:00
|
|
|
bname := repo.GetUserBranchName()
|
|
|
|
v, err := repo.gitVersionByName(bname)
|
2024-11-29 02:01:25 -06:00
|
|
|
if err == nil {
|
|
|
|
return v
|
|
|
|
} else {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "gitpb.GitUserVersion() error:", err)
|
|
|
|
return ""
|
2024-11-29 02:01:25 -06:00
|
|
|
}
|
|
|
|
}
|
2024-12-01 00:49:25 -06:00
|
|
|
func (repo *Repo) GetCurrentBranchName() string {
|
|
|
|
r := repo.RunQuiet([]string{"git", "branch", "--show-current"})
|
|
|
|
output := strings.Join(r.Stdout, "\n")
|
|
|
|
if r.Error != nil {
|
|
|
|
log.Log(GITPBWARN, "GetCurrentBranchName() not in a git repo?", r.Error, repo.GoPath)
|
|
|
|
log.Log(GITPBWARN, "GetCurrentBranchName() output might have worked anyway:", output)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repo) GetCurrentBranchVersion() string {
|
2024-12-07 16:50:26 -06:00
|
|
|
if repo == nil {
|
|
|
|
log.Info("repo.GetCurrentBranchVersion() repo == nil")
|
|
|
|
return ""
|
|
|
|
}
|
2024-12-01 00:49:25 -06:00
|
|
|
r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always"})
|
|
|
|
output := strings.Join(r.Stdout, "\n")
|
|
|
|
if r.Error != nil {
|
|
|
|
log.Log(GITPBWARN, "GetCurrentBranchVersion() not in a git repo?", r.Error, repo.GoPath)
|
|
|
|
log.Log(GITPBWARN, "GetCurrentBranchVersion() output might have worked anyway:", output)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repo) gitDescribeByHash(hash string) (string, error) {
|
|
|
|
if hash == "" {
|
|
|
|
return "", errors.New("hash was blank")
|
|
|
|
}
|
|
|
|
r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always", hash})
|
|
|
|
out := strings.Join(r.Stdout, "\n")
|
|
|
|
if r.Error != nil {
|
|
|
|
log.Warn("not in a git repo or bad hash?", r.Error, repo.GoPath)
|
|
|
|
return out, r.Error
|
|
|
|
}
|
|
|
|
return out, r.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// this should get the most recent tag
|
|
|
|
func (repo *Repo) GetLastTagVersion() string {
|
|
|
|
r := repo.RunQuiet([]string{"git", "rev-list", "--tags", "--max-count=1"})
|
|
|
|
hash := strings.Join(r.Stdout, "\n")
|
|
|
|
hash = strings.TrimSpace(hash)
|
|
|
|
log.Log(GITPB, "getLastTagVersion()", hash)
|
|
|
|
|
|
|
|
name, _ := repo.gitDescribeByHash(hash)
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repo) DebianReleaseVersion() string {
|
|
|
|
lasttag := repo.GetLastTagVersion()
|
|
|
|
newv := trimNonNumericFromStart(lasttag)
|
|
|
|
if newv == "" {
|
|
|
|
newv = "0.0"
|
|
|
|
if lasttag != "" {
|
|
|
|
newv += "-" + lasttag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repo) DebianCurrentVersion() string {
|
|
|
|
cbversion := repo.GetCurrentBranchVersion()
|
|
|
|
|
|
|
|
newv := trimNonNumericFromStart(cbversion)
|
|
|
|
if newv == "" {
|
|
|
|
newv = "0.0"
|
|
|
|
}
|
|
|
|
if repo.CheckDirty() {
|
|
|
|
newv += "-dirty"
|
|
|
|
}
|
|
|
|
return newv
|
|
|
|
}
|
2024-11-29 02:01:25 -06:00
|
|
|
|
|
|
|
func (repo *Repo) gitVersionByName(name string) (string, error) {
|
2024-11-29 01:23:51 -06:00
|
|
|
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 {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "gitDescribeByName() output might have worked anyway:", output)
|
|
|
|
log.Log(GITPBWARN, "gitDescribeByName() not in a git repo?", r.Error, repo.GoPath)
|
|
|
|
return "", r.Error
|
2024-11-29 01:23:51 -06:00
|
|
|
}
|
2024-11-29 15:47:23 -06:00
|
|
|
return strings.TrimSpace(output), nil
|
2024-11-29 01:23:51 -06:00
|
|
|
}
|
2024-11-29 23:19:09 -06:00
|
|
|
if !repo.IsBranch(name) {
|
2024-11-29 01:23:51 -06:00
|
|
|
// tag does not exist
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "LocalTagExists()", name, "did not exist")
|
|
|
|
return "", errors.New("gitDescribeByName() git fatal: Not a valid object name: " + name)
|
2024-11-29 01:23:51 -06:00
|
|
|
}
|
|
|
|
cmd := []string{"git", "describe", "--tags", "--always", name}
|
2024-11-29 02:01:25 -06:00
|
|
|
result := repo.RunQuiet(cmd)
|
|
|
|
output := strings.Join(result.Stdout, "\n")
|
|
|
|
if result.Error != nil {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "cmd =", cmd)
|
|
|
|
log.Log(GITPBWARN, "err =", result.Error)
|
|
|
|
log.Log(GITPBWARN, "output (might have worked with error?) =", output)
|
|
|
|
log.Log(GITPBWARN, "not in a git repo or bad tag?", repo.GoPath)
|
|
|
|
return "", result.Error
|
2024-11-29 01:23:51 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 15:47:23 -06:00
|
|
|
return strings.TrimSpace(output), nil
|
2024-11-29 01:23:51 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 15:47:23 -06:00
|
|
|
// find a branch name
|
|
|
|
// will find "master" or "devel"
|
|
|
|
// will also find "v0.1.1"
|
|
|
|
// or will find "patches-from-foo"
|
|
|
|
// will return *any* match on any git branch because it doesn't
|
|
|
|
// matter much here yet
|
|
|
|
// eventually this will be worked out by forge in some future code that hasn't been made yet
|
|
|
|
func (repo *Repo) IsBranch(findname string) bool {
|
2024-11-29 18:15:25 -06:00
|
|
|
loop := repo.Tags.All()
|
2024-11-29 02:01:25 -06:00
|
|
|
for loop.Scan() {
|
|
|
|
t := loop.Next()
|
2024-11-29 02:20:44 -06:00
|
|
|
// log.Info("LocalTagExists() tag:", t.Refname)
|
2024-11-29 02:01:25 -06:00
|
|
|
|
|
|
|
tagname := t.Refname
|
2024-11-29 01:23:51 -06:00
|
|
|
if strings.HasPrefix(tagname, "refs/remotes") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path, filename := filepath.Split(tagname)
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPB, "gitpb.IsBranch() tag:", path, filename, "from", repo.GoPath)
|
2024-11-29 01:23:51 -06:00
|
|
|
if filename == findname {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPB, "gitpb.IsBranch() found tag:", path, filename, "from", repo.GoPath)
|
2024-11-29 01:23:51 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2024-11-29 21:51:30 -06:00
|
|
|
log.Log(GITPB, "did not find tag:", findname, "in", repo.GoPath)
|
2024-11-29 01:23:51 -06:00
|
|
|
return false
|
|
|
|
}
|
2024-12-01 00:49:25 -06:00
|
|
|
|
|
|
|
func trimNonNumericFromStart(s string) string {
|
|
|
|
for i, r := range s {
|
|
|
|
if unicode.IsDigit(r) {
|
|
|
|
return s[i:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|