keep isolating use of os.Exec("git")

This commit is contained in:
Jeff Carr 2024-12-17 01:15:31 -06:00
parent c53da5a9a1
commit 4bc95ad268
9 changed files with 86 additions and 49 deletions

View File

@ -14,29 +14,6 @@ import (
"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.Log(GITPBWARN, "git LastTag() error:", result.Stdout)
return ""
}
hash := result.Stdout[0]
cmd = []string{"git", "describe", "--tags", "--always", hash}
result = repo.RunQuiet(cmd)
if len(result.Stdout) != 1 {
log.Log(GITPBWARN, "git LastTag() error:", result.Stdout)
return ""
}
return result.Stdout[0]
}
func (repo *Repo) GetMasterVersion() string {
bname := repo.GetMasterBranchName()
v, err := repo.gitVersionByName(bname)
@ -78,6 +55,8 @@ func (repo *Repo) GetUserVersion() string {
}
}
/*
now tracked in repo.Reload()
func (repo *Repo) GetCurrentBranchName() string {
r := repo.RunQuiet([]string{"git", "branch", "--show-current"})
output := strings.Join(r.Stdout, "\n")
@ -87,6 +66,7 @@ func (repo *Repo) GetCurrentBranchName() string {
}
return strings.TrimSpace(output)
}
*/
// this is used often. probably move everything to this
// returns things like
@ -105,21 +85,6 @@ func (repo *Repo) GetCurrentVersion() string {
return bver
}
// always spawns 'git' and always should spawn 'git'
func (repo *Repo) GetCurrentBranchVersion() string {
if repo == nil {
log.Info("repo.GetCurrentBranchVersion() repo == nil")
return ""
}
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")
@ -135,13 +100,7 @@ func (repo *Repo) gitDescribeByHash(hash string) (string, 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
return repo.LastTag
}
func (repo *Repo) DebianReleaseVersion() string {

View File

@ -1,14 +1,27 @@
package gitpb
import (
"strings"
"go.wit.com/log"
)
func (repo *Repo) Reload() error {
repo.Tags = new(GitTags)
repo.UpdateGitTags()
repo.reloadGitTags()
repo.GoDeps = new(GoDeps)
repo.ParseGoSum()
if repo.GoInfo != nil {
repo.ReloadGo()
}
repo.setLastTag()
repo.setCurrentBranchName()
// everything has been checked, now save the mtime's
repo.RepoChanged()
return nil
}
@ -37,3 +50,56 @@ func (repo *Repo) SetDevelBranchName(bname string) {
func (repo *Repo) SetUserBranchName(bname string) {
repo.UserBranchName = bname
}
// updates LastTag // todo, get this from the protobuf
func (repo *Repo) setLastTag() {
cmd := []string{"git", "rev-list", "--tags", "--max-count=1"}
result := repo.RunQuiet(cmd)
// log.Info("getLastTagVersion()", result.Stdout)
if len(result.Stdout) != 1 {
log.Log(GITPBWARN, "git LastTag() error:", result.Stdout)
repo.LastTag = ""
return
}
hash := result.Stdout[0]
cmd = []string{"git", "describe", "--tags", "--always", hash}
result = repo.RunQuiet(cmd)
if len(result.Stdout) != 1 {
log.Log(GITPBWARN, "git LastTag() error:", result.Stdout)
repo.LastTag = ""
return
}
repo.LastTag = result.Stdout[0]
}
func (repo *Repo) setCurrentBranchName() {
repo.CurrentBranchName = ""
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)
}
repo.CurrentBranchName = strings.TrimSpace(output)
}
// always spawns 'git' and always should spawn 'git'
func (repo *Repo) setCurrentBranchVersion() {
repo.CurrentBranchVersion = ""
if repo == nil {
log.Info("repo.GetCurrentBranchVersion() repo == nil")
return
}
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)
}
repo.CurrentBranchVersion = strings.TrimSpace(output)
}

View File

@ -6,9 +6,11 @@ package gitpb
import (
"fmt"
"strings"
"time"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
"google.golang.org/protobuf/types/known/timestamppb"
)
func (repo *Repo) NoteChange(s string) {
@ -51,6 +53,8 @@ func (repo *Repo) CheckDirty() bool {
}
}
pbnow := timestamppb.New(time.Now())
repo.Times.LastDirty = pbnow
return bad
}

View File

@ -37,6 +37,7 @@ func (repo *Repo) isIgnored(file string) (bool, error) {
// for now, check if this repo should be ignored
// TODO: go.mod and go.sum should be moved to git tag metadata
func (repo *Repo) RepoIgnoresGoMod() error {
repo.GoInfo.GitIgnoresGoSum = false
file := "go.mod"
if tracked, err := repo.isTracked(file); err != nil {
msg := fmt.Sprintf("%s Error checking if %s tracked: %v\n", repo.GoPath, file, err)
@ -59,6 +60,7 @@ func (repo *Repo) RepoIgnoresGoMod() error {
} else {
if ignored {
fmt.Printf("%s %s is ignored by Git.\n", repo.GoPath, file)
repo.GoInfo.GitIgnoresGoSum = true
return nil
}
}

View File

@ -11,8 +11,8 @@ import (
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
// Update repo.Refs from .git/
func (repo *Repo) UpdateGitTags() error {
// reload the tags
func (repo *Repo) reloadGitTags() error {
// todo: look for changes in the tags?
repo.Tags = new(GitTags)

View File

@ -27,6 +27,8 @@ func (all *Repos) NewGoRepo(fullpath string, gopath string) (*Repo, error) {
newr := Repo{
FullPath: fullpath,
}
newr.Times = new(GitTimes)
newr.GoInfo = new(GoInfo)
newr.GoInfo.GoPath = gopath

View File

@ -39,6 +39,9 @@ message Repo { // `autogenpb:marshal`
GitTimes times = 25; // store all the mtime values here. these are temporary
GoInfo goInfo = 26; // put all the go specifcs here
string stateChange = 27; // reason for state change
string lastTag = 28; // the oldest tag
string currentBranchName = 29; // the branch currently checked out
string currentBranchVersion = 30; // the branch currently checked out
}
message Repos { // `autogenpb:marshal`
@ -71,4 +74,5 @@ message GoInfo {
GoDeps published = 9; // the last published go.mod/go.sum
bytes goMod = 10; // the last go.mod file
bytes goSum = 11; // the last go.sum file
bool gitIgnoresGoSum = 12; // does .gitignore ignore go.mod & go.sum?
}

View File

@ -77,7 +77,7 @@ func (repo *Repo) Exists(filename string) bool {
return true
}
func (repo *Repo) IsValid() bool {
func (repo *Repo) IsValidDir() bool {
if repo == nil {
return false
}