start trying to make smarter tracking of git changes

This commit is contained in:
Jeff Carr 2024-12-16 00:21:08 -06:00
parent 01b332ceb8
commit cea5968b0f
1 changed files with 42 additions and 6 deletions

View File

@ -4,8 +4,10 @@ import (
"errors"
"os"
"path/filepath"
"time"
"go.wit.com/log"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
// scans in a new git repo. If it detects the repo is a golang project,
@ -41,10 +43,8 @@ func (all *Repos) NewGoPath(basepath string, gopath string, url string) (*Repo,
URL: url,
}
newr.Tags = new(GitTags)
// newr.UpdateGit()
newr.UpdateGitTags()
newr.GoDeps = new(GoDeps)
// newr.RedoGoMod()
switch newr.goListRepoType() {
case "plugin":
@ -57,14 +57,22 @@ func (all *Repos) NewGoPath(basepath string, gopath string, url string) (*Repo,
newr.GoBinary = true
}
lastpull, err := newr.LastGitPull()
if err == nil {
newr.LastPull = timestamppb.New(lastpull)
}
if all.AppendUniqueGoPath(&newr) {
// worked
return &newr, nil
} else {
// this is dumb, probably never happens. todo: use Repos.Lock()
if r := all.FindByGoPath(gopath); r != nil {
// already had this gopath
return r, errors.New("gitpb.NewGoPath() AppendUnique() failed but Find() worked" + gopath)
}
}
if r := all.FindByGoPath(gopath); r != nil {
// already had this gopath
return r, errors.New("gitpb.NewGoPath() AppendUnique() failed but Find() worked" + gopath)
}
// todo: use Repos.Lock()
return nil, errors.New("repo gitpb.NewGoPath() should never have gotten here " + gopath)
}
@ -75,3 +83,31 @@ func (repo *Repo) SetDevelBranchName(bname string) {
func (repo *Repo) SetUserBranchName(bname string) {
repo.UserBranchName = bname
}
func (repo *Repo) GitChanged() bool {
fullfile := filepath.Join(repo.FullPath, ".git/FETCH_HEAD")
lasttime, err := repo.LastGitPull()
if err == nil {
// if error, something is wrong, assume true
log.Info("gitpb:", fullfile, "changed")
return true
}
newtime := repo.LastPull.AsTime()
if lasttime == newtime {
return false
}
log.Info("gitpb:", fullfile, "changed")
return true
}
func (repo *Repo) GitPullAge() time.Duration {
lastpull, err := repo.LastGitPull()
if err == nil {
// if error, something is wrong, assume true
ltime := repo.LastPull.AsTime()
return time.Since(ltime)
}
return time.Since(lastpull)
}