gitpb/reload.go

113 lines
2.8 KiB
Go
Raw Normal View History

2024-12-17 00:00:49 -06:00
package gitpb
2024-12-17 01:15:31 -06:00
import (
"strings"
2025-01-08 02:38:50 -06:00
"time"
2024-12-17 01:15:31 -06:00
"go.wit.com/log"
2025-01-08 02:38:50 -06:00
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
2024-12-17 01:15:31 -06:00
)
2025-01-18 23:26:41 -06:00
// does a fast check with os.Stat()
// if the mtimes changed, does a full repo.Reload()
func (repo *Repo) ReloadCheck() error {
if !repo.DidRepoChange() {
return nil
}
// f.configSave = true
err := repo.Reload()
return err
}
// TODO: clean this up more, but it is working now more or less
2024-12-17 00:00:49 -06:00
func (repo *Repo) Reload() error {
2024-12-17 21:15:19 -06:00
// log.Info("in reload", repo.FullPath)
2024-12-17 00:00:49 -06:00
repo.Tags = new(GitTags)
2024-12-17 01:15:31 -06:00
repo.reloadGitTags()
2024-12-17 00:00:49 -06:00
repo.GoDeps = new(GoDeps)
2024-12-17 20:48:23 -06:00
if repo.GoInfo == nil {
repo.GoInfo = new(GoInfo)
}
2024-12-17 23:01:13 -06:00
repo.ParseGoSum() // also sets GoPrimitive
repo.reloadVersions()
2024-12-17 06:37:14 -06:00
repo.setRepoType()
2025-01-08 02:38:50 -06:00
// this is probably a good place & time to store these
repo.reloadMtimes()
repo.CheckDirty()
repo.setRepoState()
2024-12-17 01:15:31 -06:00
2025-01-18 10:34:58 -06:00
if repo.GitConfig == nil {
if err := repo.updateGitConfig(); err != nil {
return err
}
}
2025-01-08 02:38:50 -06:00
// LastUpdate should always be the newest time
repo.Times.LastUpdate = timestamppb.New(time.Now())
2024-12-17 00:00:49 -06:00
return nil
}
func (repo *Repo) SetDevelBranchName(bname string) {
repo.DevelBranchName = bname
}
func (repo *Repo) SetUserBranchName(bname string) {
repo.UserBranchName = bname
}
2024-12-17 01:15:31 -06:00
// 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 {
2025-01-19 03:19:06 -06:00
// log.Log(WARN, "no gitpb.LastTag() repo is broken. ignore this.", repo.GetGoPath())
2024-12-17 01:15:31 -06:00
repo.LastTag = ""
return
}
hash := result.Stdout[0]
cmd = []string{"git", "describe", "--tags", "--always", hash}
result = repo.RunQuiet(cmd)
if len(result.Stdout) != 1 {
2025-01-19 03:19:06 -06:00
log.Log(WARN, "git LastTag() error:", result.Stdout, "hash =", hash)
2024-12-17 01:15:31 -06:00
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 {
2025-01-08 03:13:09 -06:00
log.Log(WARN, "GetCurrentBranchName() not in a git repo?", r.Error, repo.GetGoPath())
log.Log(WARN, "GetCurrentBranchName() output might have worked anyway:", output)
2024-12-17 01:15:31 -06:00
}
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 {
2025-01-08 03:13:09 -06:00
log.Log(WARN, "GetCurrentBranchVersion() not in a git repo?", r.Error, repo.GetGoPath())
log.Log(WARN, "GetCurrentBranchVersion() output might have worked anyway:", output)
2024-12-17 01:15:31 -06:00
}
repo.CurrentBranchVersion = strings.TrimSpace(output)
}