From c53da5a9a1da1b29db24d4e1ce2b294514d99ac2 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 17 Dec 2024 00:00:49 -0600 Subject: smarter and faster mtime logic --- age.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 age.go (limited to 'age.go') diff --git a/age.go b/age.go new file mode 100644 index 0000000..d70c863 --- /dev/null +++ b/age.go @@ -0,0 +1,70 @@ +package gitpb + +// functions that check the ages of files +// and track if the repo needs to be re-scanned + +import ( + "errors" + "os" + "path/filepath" + "time" + + "go.wit.com/log" +) + +func (repo *Repo) LastGitPull() (time.Time, error) { + return repo.oldMtime(".git/FETCH_HEAD") +} + +func (repo *Repo) GoSumAge() (time.Duration, error) { + var mtime time.Time + var err error + mtime, err = repo.oldMtime("go.sum") + if err == nil { + return time.Since(mtime), nil + } + mtime, err = repo.oldMtime("go.mod") + if err == nil { + return time.Since(mtime), nil + } + now := time.Now() + return time.Since(now), errors.New(repo.GoPath + " go.mod missing") +} + +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) +} + +func (repo *Repo) oldMtime(filename string) (time.Time, error) { + pathf := filepath.Join(repo.FullPath, filename) + statf, err := os.Stat(pathf) + if err == nil { + return statf.ModTime(), nil + } + log.Log(GITPBWARN, "Mtime() os.Stat() error", pathf, err) + return time.Now(), err +} -- cgit v1.2.3