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
71
72
73
74
75
|
package gitpb
import (
"errors"
"os"
"path/filepath"
sync "sync"
"go.wit.com/lib/config"
)
// an experiment to see if this is useful
// it seems lke the right thing already and I haven't even used it
var repoStatsMu sync.RWMutex
var statsMap map[string]*Stats
func initStats() {
if statsMap == nil {
statsMap = make(map[string]*Stats)
}
}
func (r *Repo) LoadStats() (*Stats, error) {
filename := filepath.Join(r.FullPath, ".git/", "stats.pb")
if fileExists(filename) {
newfilename := filepath.Join(r.FullPath, ".git/origin.pb")
os.Rename(filename, newfilename)
}
return r.Stats(), nil
}
func (r *Repo) Stats() *Stats {
repoStatsMu.Lock()
defer repoStatsMu.Unlock()
if statsMap == nil {
initStats()
}
// by default, assume "origin"
lookup := r.Namespace + " origin"
origin := statsMap[lookup]
if origin == nil {
// load or initialize the file
origin = NewStats()
origin.Filename = filepath.Join(r.FullPath, ".git/origin.pb")
if fileExists(origin.Filename) {
config.ReLoad(origin)
} else {
config.Save(origin)
}
// add PB to the lookup map
statsMap[lookup] = origin
}
return origin
}
// todo: move this somewhere
// fileExists checks if a file exists and is not a directory.
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if err != nil {
// If the error is that the file doesn't exist, it's not an error for us.
if errors.Is(err, os.ErrNotExist) {
return false
}
// A different error occurred (e.g., permission error).
// We'll treat this as "does not exist" for simplicity,
// but in a real app, you might want to log this error.
return false
}
// The path exists, but we also want to ensure it's not a directory.
return !info.IsDir()
}
|