gitpb/age.go

71 lines
1.5 KiB
Go

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.GetGoPath() + " 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.Times.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.Times.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
}