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"
|
2024-12-12 02:07:25 -06:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2024-11-29 02:01:25 -06:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2024-12-17 23:01:13 -06:00
|
|
|
func (repo *Repo) reloadVersions() {
|
2024-12-17 06:37:14 -06:00
|
|
|
repo.setMasterVersion()
|
|
|
|
repo.setDevelVersion()
|
|
|
|
repo.setUserVersion()
|
2024-12-17 23:01:13 -06:00
|
|
|
repo.setLastTag()
|
|
|
|
repo.setCurrentBranchName()
|
|
|
|
repo.setCurrentBranchVersion()
|
2024-12-17 06:37:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repo) setMasterVersion() {
|
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()
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Info(repo.GetGoPath(), "tag count", count)
|
2024-11-29 15:47:23 -06:00
|
|
|
repo.UpdateGitTags()
|
|
|
|
count = repo.LenGitTags()
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Info(repo.GetGoPath(), "tag count", count)
|
2024-11-29 02:01:25 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
if err == nil {
|
2024-12-17 06:37:14 -06:00
|
|
|
repo.MasterVersion = v
|
2024-11-29 02:01:25 -06:00
|
|
|
} else {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "gitpb.GitMasterVersion() error:", err)
|
2024-12-17 06:37:14 -06:00
|
|
|
repo.MasterVersion = "giterr"
|
2024-11-29 02:01:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 06:37:14 -06:00
|
|
|
func (repo *Repo) setDevelVersion() {
|
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 {
|
2024-12-17 06:37:14 -06:00
|
|
|
repo.DevelVersion = v
|
2024-11-29 02:01:25 -06:00
|
|
|
} else {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "gitpb.GitDevelVersion() error:", err)
|
2024-12-17 06:37:14 -06:00
|
|
|
repo.DevelVersion = "deverr"
|
2024-11-29 02:01:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 06:37:14 -06:00
|
|
|
func (repo *Repo) setUserVersion() {
|
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 {
|
2024-12-17 06:37:14 -06:00
|
|
|
repo.UserVersion = v
|
2024-11-29 02:01:25 -06:00
|
|
|
} else {
|
2024-11-29 15:47:23 -06:00
|
|
|
log.Log(GITPBWARN, "gitpb.GitUserVersion() error:", err)
|
2024-12-17 06:37:14 -06:00
|
|
|
repo.UserVersion = "uerr"
|
2024-11-29 02:01:25 -06:00
|
|
|
}
|
|
|
|
}
|
2024-12-16 03:04:39 -06:00
|
|
|
|
2024-12-17 01:15:31 -06:00
|
|
|
/*
|
|
|
|
now tracked in repo.Reload()
|
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 {
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Log(GITPBWARN, "GetCurrentBranchName() not in a git repo?", r.Error, repo.GetGoPath())
|
2024-12-01 00:49:25 -06:00
|
|
|
log.Log(GITPBWARN, "GetCurrentBranchName() output might have worked anyway:", output)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(output)
|
|
|
|
}
|
2024-12-17 01:15:31 -06:00
|
|
|
*/
|
2024-12-01 00:49:25 -06:00
|
|
|
|
2024-12-16 03:04:39 -06:00
|
|
|
// this is used often. probably move everything to this
|
|
|
|
// returns things like
|
|
|
|
// v0.2.2
|
|
|
|
// v0.22.39-1-g2141737
|
|
|
|
// v0.23-dirty
|
|
|
|
// mystuff
|
|
|
|
func (repo *Repo) GetCurrentVersion() string {
|
|
|
|
if repo == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
bver := repo.GetCurrentBranchVersion()
|
|
|
|
if repo.CheckDirty() {
|
|
|
|
bver = bver + "-dirty"
|
|
|
|
}
|
|
|
|
return bver
|
|
|
|
}
|
|
|
|
|
2024-12-01 00:49:25 -06:00
|
|
|
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 {
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Warn("not in a git repo or bad hash?", r.Error, repo.GetGoPath())
|
2024-12-01 00:49:25 -06:00
|
|
|
return out, r.Error
|
|
|
|
}
|
|
|
|
return out, r.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// this should get the most recent tag
|
|
|
|
func (repo *Repo) GetLastTagVersion() string {
|
2024-12-17 01:15:31 -06:00
|
|
|
return repo.LastTag
|
2024-12-01 00:49:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Log(GITPBWARN, "gitDescribeByName() not in a git repo?", r.Error, repo.GetGoPath())
|
2024-11-29 15:47:23 -06:00
|
|
|
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)
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Log(GITPBWARN, "not in a git repo or bad tag?", repo.GetGoPath())
|
2024-11-29 15:47:23 -06:00
|
|
|
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-12-17 06:37:14 -06:00
|
|
|
log.Log(GITPB, "gitpb.IsBranch() tag:", path, filename, "from", repo.GetGoPath())
|
2024-11-29 01:23:51 -06:00
|
|
|
if filename == findname {
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Log(GITPB, "gitpb.IsBranch() found tag:", path, filename, "from", repo.GetGoPath())
|
2024-12-13 16:18:25 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Log(GITPB, "did not find tag:", findname, "in", repo.GetGoPath())
|
2024-12-13 16:18:25 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: redo this and above. both are messed up. ignore for now until things are stable
|
|
|
|
func (repo *Repo) IsLocalBranch(findname string) bool {
|
|
|
|
loop := repo.Tags.All()
|
|
|
|
for loop.Scan() {
|
|
|
|
t := loop.Next()
|
|
|
|
// log.Info("LocalTagExists() tag:", t.Refname)
|
|
|
|
|
|
|
|
tagname := t.Refname
|
|
|
|
if strings.HasPrefix(tagname, "refs/heads") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path, filename := filepath.Split(tagname)
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Log(GITPB, "gitpb.IsBranch() tag:", path, filename, "from", repo.GetGoPath())
|
2024-12-13 16:18:25 -06:00
|
|
|
if filename == findname {
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Log(GITPB, "gitpb.IsBranch() found tag:", path, filename, "from", repo.GetGoPath())
|
2024-11-29 01:23:51 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2024-12-17 06:37:14 -06:00
|
|
|
log.Log(GITPB, "did not find tag:", findname, "in", repo.GetGoPath())
|
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 ""
|
|
|
|
}
|
2024-12-12 02:07:25 -06:00
|
|
|
|
|
|
|
func normalizeVersion(s string) string {
|
|
|
|
// reg, err := regexp.Compile("[^a-zA-Z0-9]+")
|
|
|
|
parts := strings.Split(s, "-")
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
reg, err := regexp.Compile("[^0-9.]+")
|
|
|
|
if err != nil {
|
|
|
|
log.Log(GITPBWARN, "normalizeVersion() regexp.Compile() ERROR =", err)
|
|
|
|
return parts[0]
|
|
|
|
}
|
|
|
|
clean := reg.ReplaceAllString(parts[0], "")
|
|
|
|
log.Log(GITPB, "normalizeVersion() s =", clean)
|
|
|
|
return clean
|
|
|
|
}
|
|
|
|
|
|
|
|
// golang doesn't seem to really support v0.1 and seems to want v0.1.0
|
|
|
|
// todo: confirm this
|
|
|
|
func splitVersion(version string) (a, b, c string) {
|
|
|
|
tmp := normalizeVersion(version)
|
|
|
|
parts := strings.Split(tmp, ".")
|
|
|
|
switch len(parts) {
|
|
|
|
case 1:
|
|
|
|
return parts[0], "", ""
|
|
|
|
case 2:
|
|
|
|
return parts[0], parts[1], ""
|
|
|
|
default:
|
|
|
|
return parts[0], parts[1], parts[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// changes the target minor. v0.1.3 becomes v0.2.0
|
|
|
|
func (repo *Repo) IncrementTargetMinor() {
|
|
|
|
lasttag := repo.GetLastTag()
|
|
|
|
var major, minor, revision string
|
|
|
|
major, minor, revision = splitVersion(lasttag)
|
|
|
|
|
|
|
|
olda, _ := strconv.Atoi(major)
|
|
|
|
oldb, _ := strconv.Atoi(minor)
|
|
|
|
oldc, _ := strconv.Atoi(revision)
|
|
|
|
|
|
|
|
oldb += 1
|
|
|
|
oldc = 0
|
|
|
|
|
|
|
|
newa := strconv.Itoa(olda)
|
|
|
|
newb := strconv.Itoa(oldb)
|
|
|
|
newc := strconv.Itoa(oldc)
|
|
|
|
|
|
|
|
repo.SetTargetVersion("v" + newa + "." + newb + "." + newc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// changes the target revision. v0.1.3 becomes v0.1.4
|
|
|
|
func (repo *Repo) IncrementTargetRevision() {
|
|
|
|
lasttag := repo.GetLastTag()
|
|
|
|
var major, minor, revision string
|
|
|
|
major, minor, revision = splitVersion(lasttag)
|
|
|
|
|
|
|
|
olda, _ := strconv.Atoi(major)
|
|
|
|
oldb, _ := strconv.Atoi(minor)
|
|
|
|
oldc, _ := strconv.Atoi(revision)
|
|
|
|
|
|
|
|
oldc += 1
|
|
|
|
|
|
|
|
newa := strconv.Itoa(olda)
|
|
|
|
newb := strconv.Itoa(oldb)
|
|
|
|
newc := strconv.Itoa(oldc)
|
|
|
|
|
|
|
|
repo.SetTargetVersion("v" + newa + "." + newb + "." + newc)
|
|
|
|
}
|