forgepb/repoNew.go

166 lines
3.9 KiB
Go
Raw Normal View History

2024-11-30 01:31:54 -06:00
package forgepb
2024-11-30 12:45:07 -06:00
import (
2024-12-16 03:04:21 -06:00
"fmt"
2024-11-30 12:45:07 -06:00
"os/user"
2024-12-17 00:00:27 -06:00
"path/filepath"
2024-12-16 03:04:21 -06:00
"strings"
2024-11-30 12:45:07 -06:00
"go.wit.com/lib/protobuf/gitpb"
2024-12-16 03:04:21 -06:00
"go.wit.com/log"
2024-11-30 12:45:07 -06:00
)
2024-11-30 01:31:54 -06:00
2024-12-17 00:00:27 -06:00
func (f *Forge) NewGoRepo(gopath string, url string) (*gitpb.Repo, error) {
fullpath := filepath.Join(f.GetGoSrc(), gopath)
repo, err := f.Repos.NewGoRepo(fullpath, gopath)
2024-11-30 12:45:07 -06:00
if err != nil {
2024-12-17 06:37:00 -06:00
log.Info("WARNING. NEW FAILED", fullpath)
2024-12-01 22:36:36 -06:00
return nil, err
2024-11-30 12:45:07 -06:00
}
2024-12-17 00:00:27 -06:00
repo.URL = url
2024-12-03 00:35:33 -06:00
f.VerifyBranchNames(repo)
2024-12-17 06:37:00 -06:00
if f.Config.IsReadOnly(repo.GetGoPath()) {
repo.ReadOnly = true
}
2024-12-17 23:01:02 -06:00
repo.Reload()
2024-12-03 00:35:33 -06:00
return repo, nil
2024-11-30 01:31:54 -06:00
}
2024-12-01 12:53:08 -06:00
2024-12-16 03:04:21 -06:00
// golang versions MUST be vX.X.X
// it can not be vX.X and it also can not be v0.0.0
// verifies the version is format v3.2.1
func (f *Forge) ValidGoVersion(ver string) (bool, error) {
if ver == "v0.0.0" {
return false, fmt.Errorf("golang does not allow version v0.0.0")
}
if !strings.HasPrefix(ver, "v") {
return false, fmt.Errorf("(%s) invalid. golang versions must start with a 'v'", ver)
}
xver := ver[1:]
parts := strings.Split(xver, ".")
if len(parts) != 3 {
return false, fmt.Errorf("(%s) invalid. golang versions must have exactly 3 numbers (v1.2.3)", ver)
}
return true, nil
}
2024-12-01 12:53:08 -06:00
func (f *Forge) VerifyBranchNames(newr *gitpb.Repo) {
// log.Info("init worked for", newr.GoPath)
if newr.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")
} else {
2024-12-03 01:57:31 -06:00
// todo, figure out the name from git
newr.SetMasterBranchName("master")
if newr.CheckoutMaster() {
} else {
cmd := []string{"git", "branch", "master"}
newr.Run(cmd)
}
2024-12-01 12:53:08 -06:00
}
}
if newr.GetDevelBranchName() == "" {
if newr.IsBranch("guidevel") {
newr.SetDevelBranchName("guidevel")
} else if newr.IsBranch("devel") {
newr.SetDevelBranchName("devel")
} else {
2024-12-03 01:57:31 -06:00
// forcing for now. todo: warn users
newr.SetDevelBranchName("devel")
if newr.CheckoutDevel() {
} else {
cmd := []string{"git", "branch", "devel"}
newr.Run(cmd)
}
2024-12-01 12:53:08 -06:00
}
}
if newr.GetUserBranchName() == "" {
usr, _ := user.Current()
uname := usr.Username
if newr.IsBranch(uname) {
newr.SetUserBranchName(uname)
} else {
2024-12-03 01:57:31 -06:00
// forcing for now. todo: warn users
newr.SetUserBranchName(uname)
if newr.CheckoutUser() {
} else {
cmd := []string{"git", "branch", uname}
newr.Run(cmd)
}
2024-12-01 12:53:08 -06:00
}
}
}
2024-12-13 16:18:03 -06:00
// todo: check the forge config
func (f *Forge) configUserBranchName(repo *gitpb.Repo) string {
if repo.GetUserBranchName() != "" {
return repo.GetUserBranchName()
}
usr, _ := user.Current()
uname := usr.Username
return uname
}
// todo: check the forge config
func (f *Forge) configDevelBranchName(repo *gitpb.Repo) string {
if repo.GetDevelBranchName() != "" {
return repo.GetDevelBranchName()
}
if repo.IsBranch("guidevel") {
return "guidevel"
}
if repo.IsBranch("devel") {
return "devel"
}
return "devel"
}
2024-12-16 03:04:21 -06:00
func (f *Forge) AddFullPath(fulldir string) *gitpb.Repo {
repo := f.Repos.FindByFullPath(fulldir)
if repo != nil {
return repo
}
log.Info("don't know how to add full paths yet", fulldir)
return nil
}
2024-12-17 06:37:00 -06:00
func (f *Forge) FindByGoPath(gopath string) *gitpb.Repo {
fullpath := filepath.Join(f.GetGoSrc(), gopath)
return f.Repos.FindByFullPath(fullpath)
}
// tries any parent
func (f *Forge) FindAnyPath(dir string) *gitpb.Repo {
dir = filepath.Clean(dir)
repo := f.Repos.FindByFullPath(dir)
if repo != nil {
log.Info("FindAnyPath() worked = ", dir)
return repo
}
if dir == "" {
return nil
}
if dir == "/" {
return nil
}
basedir, newdir := filepath.Split(dir)
if newdir == "" {
// is this correct? not sure what stupid windows does
return nil
}
return f.FindAnyPath(basedir)
}
2024-12-17 06:37:00 -06:00
func (f *Forge) DeleteByGoPath(gopath string) bool {
fullpath := filepath.Join(f.GetGoSrc(), gopath)
return f.Repos.DeleteByFullPath(fullpath)
}