diff options
| -rw-r--r-- | repo.Stats.go | 72 | ||||
| -rw-r--r-- | stat.OpenStats.go | 24 |
2 files changed, 71 insertions, 25 deletions
diff --git a/repo.Stats.go b/repo.Stats.go index 228b3a8..2bad926 100644 --- a/repo.Stats.go +++ b/repo.Stats.go @@ -1,5 +1,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 { - return nil + 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.LoadPB(origin) + } else { + config.SavePB(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() } diff --git a/stat.OpenStats.go b/stat.OpenStats.go deleted file mode 100644 index 9e53fa2..0000000 --- a/stat.OpenStats.go +++ /dev/null @@ -1,24 +0,0 @@ -// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. -// go install go.wit.com/apps/autogenpb@latest -// -// This file was autogenerated with autogenpb: -// autogenpb v0.5.24 Built on 2025/10/14 23:24:11 ( 3 h) -// Theese sort.pb.go and marshal.pb.go files are autogenerated -// The autogenpb sources have example .proto files with instructions -// - -package gitpb - -import ( - "path/filepath" - - "go.wit.com/lib/config" -) - -func (r *Repo) LoadStats() (*Stats, error) { - filename := filepath.Join(r.FullPath, ".git/", "stats.pb") - pb := NewStats() - pb.Filename = filename - err := config.LoadPB(pb) - return pb, err -} |
