summaryrefslogtreecommitdiff
path: root/repo.Stats.go
diff options
context:
space:
mode:
Diffstat (limited to 'repo.Stats.go')
-rw-r--r--repo.Stats.go72
1 files changed, 71 insertions, 1 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()
}