diff --git a/config.go b/config.go index 4034f66..d86d05a 100644 --- a/config.go +++ b/config.go @@ -43,7 +43,6 @@ func (f *ForgeConfigs) ConfigSave() error { return err } log.Info("forgepb.ConfigSave() proto.Marshal() worked len", len(data)) - configWrite("forge.pb", data) s := f.FormatTEXT() configWrite("forge.text", []byte(s)) @@ -68,19 +67,6 @@ func (c *ForgeConfigs) ConfigLoad() error { return errors.New("It's not safe to run ConfigLoad() on a nil") } - if data, err := loadFile("forge.pb"); err == nil { - if data != nil { - if len(data) != 0 { - if err = c.Unmarshal(data); err == nil { - log.Info("forge.ConfigLoad()", len(c.ForgeConfigs), "entries in ~/.config/forge") - return nil - } - } - } - } - log.Warn("broken forge.pb config file") - - // forge.db doesn't exist. try forge.text // this lets the user hand edit the config if data, err := loadFile("forge.text"); err == nil { if data != nil { @@ -99,7 +85,8 @@ func (c *ForgeConfigs) ConfigLoad() error { } // forge.text doesn't exist. try forge.json - // this lets the user hand edit the config + // this lets the user hand edit the JSON config + // probably just deprecate this if data, err := loadFile("forge.json"); err != nil { if data != nil { // this means the forge.json file exists and was read @@ -154,13 +141,21 @@ func configWrite(filename string, data []byte) error { } if filename == "forge.text" { // add header - cfgfile.Write([]byte("# this file is automatically re-generated from forge.pb, however,\n")) - cfgfile.Write([]byte("# if you want to edit it by hand, you can:\n")) - cfgfile.Write([]byte("# stop forge; remove forge.pb; edit forge.text; start forge\n")) - cfgfile.Write([]byte("# this will cause the default behavior to fallback to parsing this file for the config\n")) cfgfile.Write([]byte("\n")) cfgfile.Write([]byte("# this file is intended to be used to customize settings on what\n")) cfgfile.Write([]byte("# git repos you have write access to. That is, where you can run 'git push'\n")) + cfgfile.Write([]byte("\n")) + } + if filename == "forge.json" { + // add header + cfgfile.Write([]byte("\n")) + cfgfile.Write([]byte("# this file is intended to be used to customize settings on what\n")) + cfgfile.Write([]byte("# git repos you have write access to. That is, where you can run 'git push'\n")) + cfgfile.Write([]byte("\n")) + cfgfile.Write([]byte("# this file is parsed only if forge.text is missing\n")) + cfgfile.Write([]byte("# also, these comment lines don't work in json files and have to be removed for Marshal() to work\n")) + cfgfile.Write([]byte("# probably, JSON syntax for this is just going to be deprecated for the TEXT syntax\n")) + cfgfile.Write([]byte("\n")) } cfgfile.Write(data) return nil diff --git a/configLookup.go b/configLookup.go index 9a61c2e..8715f40 100644 --- a/configLookup.go +++ b/configLookup.go @@ -11,6 +11,8 @@ package forgepb IsPrivate(repo) // repo can't be published to the pkg.go.dev system DebName() // for 'zookeeper' returns 'zookeeper-go' + + This code is practical, not perfect */ import ( @@ -227,3 +229,34 @@ func (fc *ForgeConfigs) IsWritable(thing string) bool { return match.Writable } + +// allows custom user branch names in the forge config +func (fc *ForgeConfigs) FindUserBranch(thing string) string { + var match *ForgeConfig + + all := fc.SortByGoPath() // get the list of repos + for all.Scan() { + r := all.Next() + if r.GoPath == thing { + if r.UserBranchName != "" { + return r.UserBranchName + } + } + base := filepath.Base(r.GoPath) + if base == thing { + if r.UserBranchName != "" { + return r.UserBranchName + } + } + if r.Directory { + if strings.HasPrefix(thing, r.GoPath) { + match = r + } + } + } + if match == nil { + return "" + } + + return match.UserBranchName +} diff --git a/forgeConfig.proto b/forgeConfig.proto index 70f9f1d..77c559e 100644 --- a/forgeConfig.proto +++ b/forgeConfig.proto @@ -38,4 +38,5 @@ message ForgeConfigs { // `autogenpb:marshal` string uuid = 1; // could be useful for /usr/share/file/magic someday? string version = 2; // could be used for protobuf schema change violations? repeated ForgeConfig ForgeConfigs = 3; + string username = 4; // what to use for the user branch (default ENV{USER}) } diff --git a/init.go b/init.go index 29f7af5..d3f95e4 100644 --- a/init.go +++ b/init.go @@ -2,6 +2,7 @@ package forgepb import ( "os" + "os/user" "path/filepath" "time" @@ -22,6 +23,11 @@ func Init() *Forge { if err := f.Machine.ConfigLoad(); err != nil { log.Warn("zoopb.ConfigLoad() failed", err) } + if f.Config.Username == "" { + usr, _ := user.Current() + f.Config.Username = usr.Username + f.SetConfigSave(true) + } f.Machine.InitWit() now := time.Now() diff --git a/repoNew.go b/repoNew.go index d234183..3630688 100644 --- a/repoNew.go +++ b/repoNew.go @@ -2,7 +2,6 @@ package forgepb import ( "fmt" - "os/user" "path/filepath" "strings" @@ -44,68 +43,77 @@ func (f *Forge) ValidGoVersion(ver string) (bool, error) { return true, nil } -func (f *Forge) VerifyBranchNames(newr *gitpb.Repo) { - // log.Info("init worked for", newr.GoPath) +// this is still in flux +func (f *Forge) VerifyBranchNames(repo *gitpb.Repo) { + // log.Info("init worked for", repo.GoPath) - if newr.GetMasterBranchName() == "" { + if repo.GetMasterBranchName() == "" { // try to guess what the 'master' branch is - if newr.IsBranch("guimaster") { - newr.SetMasterBranchName("guimaster") - } else if newr.IsBranch("master") { - newr.SetMasterBranchName("master") - } else if newr.IsBranch("main") { - newr.SetMasterBranchName("main") + if repo.IsBranch("guimaster") { + repo.SetMasterBranchName("guimaster") + } else if repo.IsBranch("master") { + repo.SetMasterBranchName("master") + } else if repo.IsBranch("main") { + repo.SetMasterBranchName("main") } else { // todo, figure out the name from git - newr.SetMasterBranchName("master") - if newr.CheckoutMaster() { + repo.SetMasterBranchName("master") + if repo.CheckoutMaster() { } else { cmd := []string{"git", "branch", "master"} - newr.Run(cmd) + repo.Run(cmd) } } } - if newr.GetDevelBranchName() == "" { - if newr.IsBranch("guidevel") { - newr.SetDevelBranchName("guidevel") - } else if newr.IsBranch("devel") { - newr.SetDevelBranchName("devel") + if repo.GetDevelBranchName() == "" { + if repo.IsBranch("guidevel") { + repo.SetDevelBranchName("guidevel") + } else if repo.IsBranch("devel") { + repo.SetDevelBranchName("devel") } else { // forcing for now. todo: warn users - newr.SetDevelBranchName("devel") - if newr.CheckoutDevel() { + repo.SetDevelBranchName("devel") + if repo.CheckoutDevel() { } else { cmd := []string{"git", "branch", "devel"} - newr.Run(cmd) + repo.Run(cmd) } } } - if newr.GetUserBranchName() == "" { - usr, _ := user.Current() - uname := usr.Username - if newr.IsBranch(uname) { - newr.SetUserBranchName(uname) + if repo.GetUserBranchName() == "" { + uname := f.configUserBranchName(repo) + if repo.IsBranch(uname) { + repo.SetUserBranchName(uname) } else { // forcing for now. todo: warn users - newr.SetUserBranchName(uname) - if newr.CheckoutUser() { + repo.SetUserBranchName(uname) + if repo.CheckoutUser() { } else { cmd := []string{"git", "branch", uname} - newr.Run(cmd) + repo.Run(cmd) } } } } -// todo: check the forge config +// what name should be used for the user branch? func (f *Forge) configUserBranchName(repo *gitpb.Repo) string { - if repo.GetUserBranchName() != "" { - return repo.GetUserBranchName() + // look in the repo record for the username + uname := repo.GetUserBranchName() + if uname != "" { + return uname } - usr, _ := user.Current() - uname := usr.Username + + // query the the forge config for a custom User Branch Name + uname = f.Config.FindUserBranch(repo.GetGoPath()) + if uname != "" { + return uname + } + + // use the os.Username + uname = f.Config.Username return uname }