user branch name settings
This commit is contained in:
parent
e9776796dd
commit
f4837c807c
33
config.go
33
config.go
|
@ -43,7 +43,6 @@ func (f *ForgeConfigs) ConfigSave() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Info("forgepb.ConfigSave() proto.Marshal() worked len", len(data))
|
log.Info("forgepb.ConfigSave() proto.Marshal() worked len", len(data))
|
||||||
configWrite("forge.pb", data)
|
|
||||||
|
|
||||||
s := f.FormatTEXT()
|
s := f.FormatTEXT()
|
||||||
configWrite("forge.text", []byte(s))
|
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")
|
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
|
// this lets the user hand edit the config
|
||||||
if data, err := loadFile("forge.text"); err == nil {
|
if data, err := loadFile("forge.text"); err == nil {
|
||||||
if data != nil {
|
if data != nil {
|
||||||
|
@ -99,7 +85,8 @@ func (c *ForgeConfigs) ConfigLoad() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// forge.text doesn't exist. try forge.json
|
// 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, err := loadFile("forge.json"); err != nil {
|
||||||
if data != nil {
|
if data != nil {
|
||||||
// this means the forge.json file exists and was read
|
// 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" {
|
if filename == "forge.text" {
|
||||||
// add header
|
// 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("\n"))
|
||||||
cfgfile.Write([]byte("# this file is intended to be used to customize settings on what\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("# 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)
|
cfgfile.Write(data)
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -11,6 +11,8 @@ package forgepb
|
||||||
|
|
||||||
IsPrivate(repo) // repo can't be published to the pkg.go.dev system
|
IsPrivate(repo) // repo can't be published to the pkg.go.dev system
|
||||||
DebName() // for 'zookeeper' returns 'zookeeper-go'
|
DebName() // for 'zookeeper' returns 'zookeeper-go'
|
||||||
|
|
||||||
|
This code is practical, not perfect
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -227,3 +229,34 @@ func (fc *ForgeConfigs) IsWritable(thing string) bool {
|
||||||
|
|
||||||
return match.Writable
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -38,4 +38,5 @@ message ForgeConfigs { // `autogenpb:marshal`
|
||||||
string uuid = 1; // could be useful for /usr/share/file/magic someday?
|
string uuid = 1; // could be useful for /usr/share/file/magic someday?
|
||||||
string version = 2; // could be used for protobuf schema change violations?
|
string version = 2; // could be used for protobuf schema change violations?
|
||||||
repeated ForgeConfig ForgeConfigs = 3;
|
repeated ForgeConfig ForgeConfigs = 3;
|
||||||
|
string username = 4; // what to use for the user branch (default ENV{USER})
|
||||||
}
|
}
|
||||||
|
|
6
init.go
6
init.go
|
@ -2,6 +2,7 @@ package forgepb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -22,6 +23,11 @@ func Init() *Forge {
|
||||||
if err := f.Machine.ConfigLoad(); err != nil {
|
if err := f.Machine.ConfigLoad(); err != nil {
|
||||||
log.Warn("zoopb.ConfigLoad() failed", err)
|
log.Warn("zoopb.ConfigLoad() failed", err)
|
||||||
}
|
}
|
||||||
|
if f.Config.Username == "" {
|
||||||
|
usr, _ := user.Current()
|
||||||
|
f.Config.Username = usr.Username
|
||||||
|
f.SetConfigSave(true)
|
||||||
|
}
|
||||||
f.Machine.InitWit()
|
f.Machine.InitWit()
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
76
repoNew.go
76
repoNew.go
|
@ -2,7 +2,6 @@ package forgepb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/user"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -44,68 +43,77 @@ func (f *Forge) ValidGoVersion(ver string) (bool, error) {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forge) VerifyBranchNames(newr *gitpb.Repo) {
|
// this is still in flux
|
||||||
// log.Info("init worked for", newr.GoPath)
|
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
|
// try to guess what the 'master' branch is
|
||||||
if newr.IsBranch("guimaster") {
|
if repo.IsBranch("guimaster") {
|
||||||
newr.SetMasterBranchName("guimaster")
|
repo.SetMasterBranchName("guimaster")
|
||||||
} else if newr.IsBranch("master") {
|
} else if repo.IsBranch("master") {
|
||||||
newr.SetMasterBranchName("master")
|
repo.SetMasterBranchName("master")
|
||||||
} else if newr.IsBranch("main") {
|
} else if repo.IsBranch("main") {
|
||||||
newr.SetMasterBranchName("main")
|
repo.SetMasterBranchName("main")
|
||||||
} else {
|
} else {
|
||||||
// todo, figure out the name from git
|
// todo, figure out the name from git
|
||||||
newr.SetMasterBranchName("master")
|
repo.SetMasterBranchName("master")
|
||||||
if newr.CheckoutMaster() {
|
if repo.CheckoutMaster() {
|
||||||
} else {
|
} else {
|
||||||
cmd := []string{"git", "branch", "master"}
|
cmd := []string{"git", "branch", "master"}
|
||||||
newr.Run(cmd)
|
repo.Run(cmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if newr.GetDevelBranchName() == "" {
|
if repo.GetDevelBranchName() == "" {
|
||||||
if newr.IsBranch("guidevel") {
|
if repo.IsBranch("guidevel") {
|
||||||
newr.SetDevelBranchName("guidevel")
|
repo.SetDevelBranchName("guidevel")
|
||||||
} else if newr.IsBranch("devel") {
|
} else if repo.IsBranch("devel") {
|
||||||
newr.SetDevelBranchName("devel")
|
repo.SetDevelBranchName("devel")
|
||||||
} else {
|
} else {
|
||||||
// forcing for now. todo: warn users
|
// forcing for now. todo: warn users
|
||||||
newr.SetDevelBranchName("devel")
|
repo.SetDevelBranchName("devel")
|
||||||
if newr.CheckoutDevel() {
|
if repo.CheckoutDevel() {
|
||||||
} else {
|
} else {
|
||||||
cmd := []string{"git", "branch", "devel"}
|
cmd := []string{"git", "branch", "devel"}
|
||||||
newr.Run(cmd)
|
repo.Run(cmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if newr.GetUserBranchName() == "" {
|
if repo.GetUserBranchName() == "" {
|
||||||
usr, _ := user.Current()
|
uname := f.configUserBranchName(repo)
|
||||||
uname := usr.Username
|
if repo.IsBranch(uname) {
|
||||||
if newr.IsBranch(uname) {
|
repo.SetUserBranchName(uname)
|
||||||
newr.SetUserBranchName(uname)
|
|
||||||
} else {
|
} else {
|
||||||
// forcing for now. todo: warn users
|
// forcing for now. todo: warn users
|
||||||
newr.SetUserBranchName(uname)
|
repo.SetUserBranchName(uname)
|
||||||
if newr.CheckoutUser() {
|
if repo.CheckoutUser() {
|
||||||
} else {
|
} else {
|
||||||
cmd := []string{"git", "branch", uname}
|
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 {
|
func (f *Forge) configUserBranchName(repo *gitpb.Repo) string {
|
||||||
if repo.GetUserBranchName() != "" {
|
// look in the repo record for the username
|
||||||
return repo.GetUserBranchName()
|
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
|
return uname
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue