diff options
| -rw-r--r-- | Load.go (renamed from forgeConfig.SaveVerbose.go) | 32 | ||||
| -rw-r--r-- | Save.go | 79 | ||||
| -rw-r--r-- | cache.go | 58 | ||||
| -rw-r--r-- | config.go | 4 | ||||
| -rw-r--r-- | doDirty.go | 1 | ||||
| -rw-r--r-- | init.go | 24 | ||||
| -rw-r--r-- | patchset.config.go | 17 | ||||
| -rw-r--r-- | rill.go | 2 | ||||
| -rw-r--r-- | scanRepoDir.go | 3 | ||||
| -rw-r--r-- | structs.go | 4 |
10 files changed, 88 insertions, 136 deletions
diff --git a/forgeConfig.SaveVerbose.go b/Load.go index 089ffe8..f69279e 100644 --- a/forgeConfig.SaveVerbose.go +++ b/Load.go @@ -10,8 +10,11 @@ package forgepb import ( + "errors" + "go.wit.com/lib/ENV" "go.wit.com/lib/config" + "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) @@ -26,3 +29,32 @@ func (pb *ForgeConfigs) loadConfig() error { // init new config here return err } + +func (f *Forge) reposCacheLoad() error { + if f.Repos != nil { + return errors.New("already loaded") + } + f.Repos = gitpb.NewRepos() + err := config.LoadAppnameCache(f.Repos, "forge") + ENV.SetGlobal("lib/forgepb", "ReposPB", f.Repos.Filename) + return err +} + +/* +func (f *Forge) loadPatchsets() error { + f.Patchsets = NewSets() + + data, err := os.ReadFile(config.Get("PatchPB")) + if err != nil { + return err + } + + err = f.Patchsets.Unmarshal(data) + if err != nil { + log.Infof("LoadPatchsets() proto.Marshal() error %v\n", err) + return err + } + // log.Infof("LoadPatchsets() found %d patches.\n", f.Patchsets.Len()) + return nil +} +*/ @@ -3,6 +3,8 @@ package forgepb import ( + "errors" + "go.wit.com/lib/ENV" "go.wit.com/lib/config" "go.wit.com/lib/protobuf/argvpb" @@ -10,52 +12,61 @@ import ( ) func (f *Forge) Save() error { + var allerr error var err error if f.Config == nil { + log.Info("forge.Config == nil. Init failed. die here(?)") return log.Errorf("forge.Config == nil") } - // THIS IS NOT RIGHT ANYMORE ? - if !config.HasChanged("forge") { - return nil - } - // MOVE THIS TO /lib/config ? if !(argvpb.GetAPPNAME() == "forge" || argvpb.GetAPPNAME() == "guireleaser") { log.Info("This is not forge") return log.Errorf("Only forge can save the forge config file") } - // migrate from the old gopath to "namespace" - for fc := range f.Config.IterAll() { - if fc.Namespace != "" { - continue - } - if fc.Namespace != fc.GoPath { - fc.Namespace = fc.GoPath + // THIS IS NOT RIGHT ANYMORE ? + if config.HasChanged("repos") { + log.Info("forgepb.Save() thinks repos changed") + if ENV.Verbose() { + log.Info("forgepb.Save() trying SaveVerbose()") + err = f.Repos.SaveVerbose() + } else { + log.Info("forgepb.Save() trying Save()") + err = f.Repos.SaveVerbose() + // err = f.Repos.Save() } - // todo: deprecate this - fc.GoPath = "" // I want to do this but it might be a bad idea at this point + allerr = errors.Join(allerr, err) } - log.Info("Okay, this is", argvpb.GetAPPNAME()) - err = f.Config.configSave() - if err != nil { - log.Info("forge.Config.configSave() error", err) - return err - } + if config.HasChanged("forge") { + log.Info("forgepb.Save() thinks nothing changed") - if ENV.Verbose() { - err = f.Repos.SaveVerbose() - } else { - err = f.Repos.Save() + // migrate from the old gopath to "namespace" + for fc := range f.Config.IterAll() { + if fc.Namespace != "" { + continue + } + if fc.Namespace != fc.GoPath { + fc.Namespace = fc.GoPath + } + // todo: deprecate this + fc.GoPath = "" // I want to do this but it might be a bad idea at this point + } + log.Info("Okay, this is", argvpb.GetAPPNAME()) + + err = f.Config.configSave() + if err != nil { + log.Info("forge.Config.configSave() error", err) + } + allerr = errors.Join(allerr, err) + return nil } - if err != nil { - log.Info("forge.Repos.Save() error", err) - return err + if allerr != nil { + log.Info("forge.Save() errors:", allerr) } - return nil + return allerr } func (f *Forge) saveRepos() error { @@ -84,6 +95,10 @@ func (cfg *ForgeConfigs) configSave() error { return config.SaveWithHeader(cfg, header) } +func (f *Forge) SetConfigSave(b bool) { + config.SetChanged("forge", b) +} + func (pb *ForgeConfigs) saveVerbose() error { err := config.SavePB(pb) if err == nil { @@ -130,3 +145,11 @@ func (f *Forge) configSave() error { } return nil } + +// name to simplify tracking of code flow +func (f *Forge) flush() error { + if err := f.Save(); err != nil { + return err + } + return nil +} diff --git a/cache.go b/cache.go deleted file mode 100644 index c139306..0000000 --- a/cache.go +++ /dev/null @@ -1,58 +0,0 @@ -// I put this here to look at and think about -// from mod/sumdb/cache.go - -// Parallel cache. -// This file is copied from cmd/go/internal/par. - -package forgepb - -import ( - "sync" - "sync/atomic" -) - -// parCache runs an action once per key and caches the result. -type parCache struct { - m sync.Map -} - -type cacheEntry struct { - done uint32 - mu sync.Mutex - result interface{} -} - -// Do calls the function f if and only if Do is being called for the first time with this key. -// No call to Do with a given key returns until the one call to f returns. -// Do returns the value returned by the one call to f. -func (c *parCache) Do(key interface{}, f func() interface{}) interface{} { - entryIface, ok := c.m.Load(key) - if !ok { - entryIface, _ = c.m.LoadOrStore(key, new(cacheEntry)) - } - e := entryIface.(*cacheEntry) - if atomic.LoadUint32(&e.done) == 0 { - e.mu.Lock() - if atomic.LoadUint32(&e.done) == 0 { - e.result = f() - atomic.StoreUint32(&e.done, 1) - } - e.mu.Unlock() - } - return e.result -} - -// Get returns the cached result associated with key. -// It returns nil if there is no such result. -// If the result for key is being computed, Get does not wait for the computation to finish. -func (c *parCache) Get(key interface{}) interface{} { - entryIface, ok := c.m.Load(key) - if !ok { - return nil - } - e := entryIface.(*cacheEntry) - if atomic.LoadUint32(&e.done) == 0 { - return nil - } - return e.result -} @@ -19,11 +19,11 @@ func (f *Forge) SetMode(newmode ForgeMode) error { f.mode = newmode err := ENV.Set("mode", newmode.String()) if err != nil { - panic("config.Set() doesn't work") + panic("ENV.Set() doesn't work") } err = ENV.Save() if err != nil { - panic("config.Save() doesn't work") + panic("ENV.Save() doesn't work") } return err } @@ -28,6 +28,7 @@ func (f *Forge) CheckDirtyQuiet() { } } if changed { + config.SetChanged("repos", true) f.Save() log.Printf("dirty check (%d dirty repos) (%d total repos) (%d changed) (%s)\n", end, f.Repos.Len(), diff, shell.FormatDuration(time.Since(now))) } @@ -3,7 +3,6 @@ package forgepb import ( - "errors" "os" "os/user" "path/filepath" @@ -11,7 +10,6 @@ import ( "go.wit.com/lib/ENV" "go.wit.com/lib/cobol" "go.wit.com/lib/config" - "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) @@ -110,32 +108,10 @@ func initFromConfig(cfg *ForgeConfigs) *Forge { return f } -func (f *Forge) reposCacheLoad() error { - if f.Repos != nil { - return errors.New("already loaded") - } - f.Repos = gitpb.NewRepos() - err := config.LoadAppnameCache(f.Repos, "forge") - ENV.SetGlobal("lib/forgepb", "ReposPB", f.Repos.Filename) - return err -} - -func (f *Forge) SetConfigSave(b bool) { - config.SetChanged("forge", b) -} - // saves the config if there have been changes func (f *Forge) Close() error { if err := f.Save(); err != nil { return err } - if f.Repos != nil { - if config.HasChanged("repos") { - if err := f.Repos.SaveVerbose(); err != nil { - log.Info("forge.Repos.ConfigSave() error", err) - return err - } - } - } return nil } diff --git a/patchset.config.go b/patchset.config.go index be13cd7..7b807d4 100644 --- a/patchset.config.go +++ b/patchset.config.go @@ -1,23 +1,6 @@ package forgepb /* -func (f *Forge) LoadPatchsets() error { - f.Patchsets = NewSets() - - data, err := os.ReadFile(config.Get("PatchPB")) - if err != nil { - return err - } - - err = f.Patchsets.Unmarshal(data) - if err != nil { - log.Infof("LoadPatchsets() proto.Marshal() error %v\n", err) - return err - } - // log.Infof("LoadPatchsets() found %d patches.\n", f.Patchsets.Len()) - return nil -} - func (f *Forge) InitPatchsets() error { if err := f.LoadPatchsets(); err == nil { return nil @@ -25,7 +25,6 @@ func (f *Forge) RillReload() int { if !repo.IsValidDir() { log.Printf("%s %-50s\n", "got an invalid repo in forgepb.RillReload()", repo.GetFullPath()) f.Repos.Delete(repo) - log.Info("reposSave = true") config.SetChanged("repos", true) continue } @@ -122,7 +121,6 @@ func (f *Forge) RillRepos(rillf func(*gitpb.Repo) error) map[string]*RillStats { if !repo.IsValidDir() { log.Printf("got an invalid repo in forgepb.RillRepos() %-50s\n", repo.GetFullPath()) f.Repos.Delete(repo) - log.Info("reposSave = true") config.SetChanged("repos", true) continue } diff --git a/scanRepoDir.go b/scanRepoDir.go index 21e411d..062a30b 100644 --- a/scanRepoDir.go +++ b/scanRepoDir.go @@ -42,8 +42,7 @@ func (f *Forge) RescanRepos() error { gopath := ENV.Get("gopath") log.Info("RescanRepos() running in", gopath) f.ScanRepoDir(gopath) - // f.SaveRepos() - f.Repos.SaveVerbose() + f.Save() return errors.New("ScanReposByMode() not implemented yet") } @@ -1,14 +1,11 @@ package forgepb import ( - sync "sync" - "go.wit.com/lib/protobuf/gitpb" ) // maybe an interface someday? type Forge struct { - once sync.Once // one-time initialized data Config *ForgeConfigs // config repos for readonly, private, etc Repos *gitpb.Repos // the repo protobufs Patches *Patches // patches that are in progress @@ -16,6 +13,7 @@ type Forge struct { hostname string // your hostname goWork bool // means the user is currently using a go.work file mode ForgeMode // what "mode" forge is in + // once sync.Once // one-time initialized data } func (f *Forge) IsGoWork() bool { |
