fixing Init()

This commit is contained in:
Jeff Carr 2024-12-03 00:35:33 -06:00
parent 189d305fee
commit cbdcfa0589
4 changed files with 140 additions and 61 deletions

92
gitCheckout.go Normal file
View File

@ -0,0 +1,92 @@
package forgepb
import (
"errors"
"go.wit.com/lib/gui/repostatus"
"go.wit.com/log"
)
func (f *Forge) GitPull() bool {
var localonly int
var badmap int
log.Log(FORGEPBWARN, "running git pull everywhere")
var failed int = 0
loop := f.Repos.SortByGoPath()
for loop.Scan() {
repo := loop.Next()
if out, err := repo.GitPull(); err == nil {
log.Log(FORGEPBWARN, "Ran git pull ok", repo.GetGoPath(), out)
} else {
failed += 1
// repo.DumpTags()
if errors.Is(repostatus.ErrorGitPullOnLocal, err) {
localonly += 1
continue
}
badmap += 1
log.Log(FORGEPBWARN, "bad unknown git error", repo.GetGoPath(), out, err)
}
}
log.Log(FORGEPBWARN, "Ran git pull in all repos. failure count =", failed)
log.Log(FORGEPBWARN, "Ran git pull in all repos. bad errors =", badmap)
if localonly != 0 {
log.Log(FORGEPBWARN, "Ran git pull in all repos. ignored local only branches =", localonly)
}
return true
}
func (f *Forge) CheckoutDevel() bool {
log.Log(FORGEPBWARN, "running git checkout devel everwhere")
var failed int = 0
var count int = 0
loop := f.Repos.SortByGoPath()
for loop.Scan() {
repo := loop.Next()
count += 1
if repo.CheckoutDevel() {
// checkout ok
} else {
failed += 1
}
}
log.Log(FORGEPBWARN, "Ran git checkout in", count, "repos. failure count =", failed)
return true
}
func (f *Forge) CheckoutMaster() bool {
log.Log(FORGEPBWARN, "running git checkout master everwhere")
var failed int = 0
var count int = 0
loop := f.Repos.SortByGoPath()
for loop.Scan() {
repo := loop.Next()
count += 1
if repo.CheckoutMaster() {
// checkout ok
} else {
failed += 1
}
}
log.Log(FORGEPBWARN, "Ran git checkout in", count, "repos. failure count =", failed)
return true
}
func (f *Forge) CheckoutUser() bool {
log.Log(FORGEPBWARN, "running git checkout master everwhere")
var failed int = 0
var count int = 0
loop := f.Repos.SortByGoPath()
for loop.Scan() {
repo := loop.Next()
count += 1
if repo.CheckoutUser() {
// checkout ok
} else {
failed += 1
}
}
log.Log(FORGEPBWARN, "Ran git checkout in", count, "repos. failure count =", failed)
return true
}

View File

@ -13,26 +13,36 @@ import (
)
func (f *Forge) ScanGoSrc() (bool, error) {
log.Info("pre dir walk")
dirs, err := gitDirectoriesNew(f.goSrc)
log.Info("post dir walk", len(dirs))
if err != nil {
return false, err
}
var gopaths []string
for _, dir := range dirs {
// log.Info("forge.ScanGoSrc()", dir)
if strings.HasPrefix(dir, f.goSrc) {
gopath := strings.TrimPrefix(dir, f.goSrc)
gopath = strings.Trim(gopath, "/")
if r := f.Repos.FindByGoPath(gopath); r != nil {
// log.Info("already have", gopath)
continue
}
gopaths = append(gopaths, gopath)
} else {
log.Log(FORGEPBWARN, "ScanGoSrc() bad:", dir)
return false, errors.New("forgepb.ScanGoSrc() bad dir: " + dir)
}
}
log.Info("pre rill")
f.rillScanDirs(gopaths)
newcount, err := f.rillScanDirs(gopaths)
if err != nil {
log.Info("go src dir problem. exit for now?", err)
os.Exit(-1)
}
if newcount != 0 {
log.Info("forge go src scan found", newcount, "repos")
f.Repos.ConfigSave()
}
return true, err
}
@ -47,6 +57,13 @@ func gitDirectoriesNew(srcDir string) ([]string, error) {
return err
}
if d.IsDir() {
// log.Info("path is dir", path)
} else {
log.Info("warning: you have an untracked file:", path)
return nil
}
gitdir := filepath.Join(path, ".git")
_, err2 := os.Stat(gitdir)
if !os.IsNotExist(err2) {
@ -118,24 +135,23 @@ func rillAddDirs(gopaths []string) {
// rill is awesome. long live rill
// attempt scan with rill
func (f *Forge) rillScanDirs(gopaths []string) error {
func (f *Forge) rillScanDirs(gopaths []string) (int, error) {
// Convert a slice of user IDs into a channel
ids := rill.FromSlice(gopaths, nil)
// Read users from the API.
// Concurrency = 20
dirs := rill.Map(ids, 20, func(id string) (*gitpb.Repo, error) {
return f.Repos.NewGoPath(f.goSrc, id)
return f.NewGoPathRepo(id)
})
var counter int
// Activate users.
// Concurrency = 10
err := rill.ForEach(dirs, 10, func(repo *gitpb.Repo) error {
// could do something here
// fmt.Printf("Repo found : %s\n", repo.GoPath)
// repo.Run([]string{"git", "pull"})
counter += 1
return nil
})
return err
return counter, err
}

View File

@ -80,7 +80,7 @@ func (f *Forge) guessPaths(path string) (string, string, string, bool, error) {
// TODO: make some config file for things like this
// can be used to work around temporary problems
func clonePathHack(dirname string, basedir string, gopath string) error {
func clonePathHack(dirname string, basedir string, gopath string) (string, error) {
// newdir = helloworld
// basedir = /home/jcarr/go/src/go.wit.com/apps
// giturl = https://gitea.wit.com/gui/helloworld
@ -117,7 +117,7 @@ func clonePathHack(dirname string, basedir string, gopath string) error {
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/appengine")
}
return errors.New("no gopath override here")
return "", errors.New("no gopath override here")
}
// attempt to git clone if the go path doesn't exist
@ -126,7 +126,7 @@ func clonePathHack(dirname string, basedir string, gopath string) error {
// gopath = go.wit.com/apps/helloworld
func (f *Forge) Clone(gopath string) (*gitpb.Repo, error) {
var err error
pb, err := f.Repos.NewGoPath(f.goSrc, gopath)
pb, err := f.Repos.NewGoPath(f.goSrc, gopath, "")
if err == nil {
return pb, err
}
@ -142,9 +142,9 @@ func (f *Forge) Clone(gopath string) (*gitpb.Repo, error) {
// try a direct git clone against the gopath
// cloneActual("helloworld", "/home/jcarr/go/src/go.wit.com/apps", "https://go.wit.com/apps/helloworld")
if err = cloneActual(dirname, basedir, url); err == nil {
if finalurl, err := cloneActual(dirname, basedir, url); err == nil {
// git clone worked!
return f.Repos.NewGoPath(f.goSrc, gopath)
return f.Repos.NewGoPath(f.goSrc, gopath, finalurl)
}
log.Info("direct attempt at git clone failed", url)
@ -155,9 +155,9 @@ func (f *Forge) Clone(gopath string) (*gitpb.Repo, error) {
log.Info("findGoImport() DID NOT WORK", url)
log.Info("findGoImport() DID NOT WORK", err)
} else {
if err := cloneActual(dirname, basedir, url); err == nil {
if finalurl, err := cloneActual(dirname, basedir, url); err == nil {
// git clone worked!
return f.Repos.NewGoPath(f.goSrc, gopath)
return f.Repos.NewGoPath(f.goSrc, gopath, finalurl)
}
}
log.Info("git clone from 'go-import' info failed", url)
@ -167,18 +167,18 @@ func (f *Forge) Clone(gopath string) (*gitpb.Repo, error) {
if url, err = runGoList(gopath); err != nil {
log.Info("go list failed", err)
} else {
if err := cloneActual(dirname, basedir, url); err == nil {
if finalurl, err := cloneActual(dirname, basedir, url); err == nil {
// git clone worked!
return f.Repos.NewGoPath(f.goSrc, gopath)
return f.Repos.NewGoPath(f.goSrc, gopath, finalurl)
}
}
log.Info("git clone from 'git list' info failed", url)
// try to parse a redirect
if err = clonePathHack(dirname, basedir, gopath); err == nil {
if finalurl, err := clonePathHack(dirname, basedir, gopath); err == nil {
// WTF didn't go-import or go list work?
return f.Repos.NewGoPath(f.goSrc, gopath)
return f.Repos.NewGoPath(f.goSrc, gopath, finalurl)
}
return nil, errors.New("can not find git sources for gopath " + gopath)
@ -187,7 +187,7 @@ func (f *Forge) Clone(gopath string) (*gitpb.Repo, error) {
// newdir = helloworld
// basedir = /home/jcarr/go/src/go.wit.com/apps
// giturl = https://gitea.wit.com/gui/helloworld
func cloneActual(newdir, basedir, giturl string) error {
func cloneActual(newdir, basedir, giturl string) (string, error) {
log.Info("cloneActual() newdir =", newdir)
log.Info("cloneActual() basedir =", basedir)
log.Info("cloneActual() giturl =", giturl)
@ -197,7 +197,7 @@ func cloneActual(newdir, basedir, giturl string) error {
err := os.Chdir(basedir)
if err != nil {
log.Warn("chdir failed", basedir, err)
return err
return giturl, err
}
cmd := []string{"git", "clone", "--verbose", "--progress", giturl, newdir}
@ -205,22 +205,22 @@ func cloneActual(newdir, basedir, giturl string) error {
r := shell.PathRunRealtime(basedir, cmd)
if r.Error != nil {
log.Warn("git clone error", r.Error)
return r.Error
return giturl, r.Error
}
fullpath := filepath.Join(basedir, newdir)
if !IsDirectory(fullpath) {
log.Info("git clone failed", giturl)
return errors.New("git clone failed " + giturl)
return giturl, errors.New("git clone failed " + giturl)
}
gitdir := filepath.Join(fullpath, ".git")
if IsDirectory(gitdir) {
log.Info("git cloned worked to", fullpath)
return nil
return giturl, nil
}
// git clone didn't really work but did make a directory
log.Info("fullpath is probably empty", fullpath)
return errors.New("crapnuts. rmdir fullpath here? " + fullpath)
return giturl, errors.New("crapnuts. rmdir fullpath here? " + fullpath)
}
// check the server for the current go path to git url mapping

View File

@ -6,45 +6,16 @@ import (
"go.wit.com/lib/protobuf/gitpb"
)
/*
func (f *Forge) NewGoPath(gopath string) (*gitpb.Repo, error) {
newr, err := f.Repos.NewGoPath(f.goSrc, gopath)
func (f *Forge) NewGoPathRepo(gopath string) (*gitpb.Repo, error) {
repo, err := f.Repos.NewGoPath(f.GetGoSrc(), gopath, "")
if err != nil {
log.Log(FORGEPBWARN, "init failed", err)
return nil, err
}
log.Info("init worked for", newr.GoPath)
// 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 {
newr.SetMasterBranchName("masterFIXME")
f.VerifyBranchNames(repo)
repo.ParseGoSum()
return repo, nil
}
if newr.IsBranch("guidevel") {
newr.SetDevelBranchName("guidevel")
} else if newr.IsBranch("devel") {
newr.SetDevelBranchName("devel")
} else {
newr.SetDevelBranchName("develFIXME")
}
usr, _ := user.Current()
uname := usr.Username
if newr.IsBranch(uname) {
newr.SetUserBranchName(uname)
} else {
newr.SetUserBranchName(uname + "FIXME")
}
f.Repos.ConfigSave()
return newr, err
}
*/
func (f *Forge) VerifyBranchNames(newr *gitpb.Repo) {
// log.Info("init worked for", newr.GoPath)