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() }