summaryrefslogtreecommitdiff
path: root/age.go
blob: 49eea1a3078166c03305aa2ae652c50b64664b8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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.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(WARN, "Mtime() os.Stat() error", pathf, err)
	return time.Now(), err
}