2024-12-15 08:31:42 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-12-15 15:51:27 -06:00
|
|
|
"fmt"
|
2024-12-15 08:31:42 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"go.wit.com/lib/gui/shell"
|
|
|
|
"go.wit.com/lib/protobuf/gitpb"
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func clone(gopath string) (*gitpb.Repo, error) {
|
|
|
|
// if the user defined a repo, attempt to download it now
|
|
|
|
if gopath == "" {
|
|
|
|
// nothing to clone
|
|
|
|
// user probably wants to --recursive on current working dir
|
|
|
|
return nil, errors.New("gopath was blank")
|
|
|
|
}
|
|
|
|
os.Setenv("REPO_AUTO_CLONE", "true")
|
|
|
|
// pb, _ := forge.NewGoPath(gopath)
|
2024-12-18 03:34:43 -06:00
|
|
|
check := forge.FindByGoPath(gopath)
|
2024-12-15 08:31:42 -06:00
|
|
|
if check != nil {
|
2024-12-17 01:55:09 -06:00
|
|
|
if check.IsValidDir() {
|
2024-12-15 08:31:42 -06:00
|
|
|
// repo already exists and is valid
|
|
|
|
return check, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pb, err := forge.Clone(gopath)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("clone() could not download err:", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-12-15 12:14:22 -06:00
|
|
|
if err := makeValidGoSum(pb); err != nil {
|
2024-12-15 08:31:42 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// double check it actually downloaded
|
|
|
|
fullgitdir := filepath.Join(forge.GetGoSrc(), gopath, ".git")
|
|
|
|
if !shell.IsDir(fullgitdir) {
|
|
|
|
log.Info("repo cloned failed", filepath.Join(forge.GetGoSrc(), gopath))
|
|
|
|
return nil, errors.New(fullgitdir + " was not created")
|
|
|
|
}
|
2024-12-15 17:03:41 -06:00
|
|
|
log.Info("go-clone clone() onward and upward")
|
2024-12-15 08:31:42 -06:00
|
|
|
return pb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// really only does go.sum things
|
|
|
|
// so not 'really' recursive
|
|
|
|
// but that is because go.sum is supposed
|
|
|
|
// to have everything required in it
|
|
|
|
func recursiveClone(check *gitpb.Repo) error {
|
|
|
|
var good int
|
|
|
|
var bad int
|
|
|
|
|
2024-12-15 08:45:44 -06:00
|
|
|
badmap := make(map[string]error)
|
|
|
|
|
2024-12-15 08:31:42 -06:00
|
|
|
if check == nil {
|
|
|
|
return errors.New("repo was nil")
|
|
|
|
}
|
2024-12-17 07:02:45 -06:00
|
|
|
log.Info("STARTING RECURSIVE CLONE", check.GetGoPath())
|
|
|
|
log.Info("STARTING RECURSIVE CLONE", check.GetGoPath())
|
|
|
|
if check.GoInfo.GoPrimitive {
|
|
|
|
log.Info("repo is a primitive", check.GetGoPath())
|
2024-12-15 08:31:42 -06:00
|
|
|
// go primitive repos are "pure"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if just cloned, parse the go.sum file for deps
|
|
|
|
check.ParseGoSum()
|
|
|
|
|
|
|
|
if check.GoDeps == nil {
|
2024-12-17 07:02:45 -06:00
|
|
|
log.Info("repo godeps == nil", check.GetGoPath())
|
2024-12-15 08:31:42 -06:00
|
|
|
return errors.New("go.sum is missing?")
|
|
|
|
}
|
|
|
|
|
|
|
|
// probably this should never be 0 because GoPrimitive should have been true otherwise
|
|
|
|
if check.GoDeps.Len() == 0 {
|
2024-12-17 07:02:45 -06:00
|
|
|
log.Info("repo len(godeps) == 0", check.GetGoPath())
|
2024-12-15 08:31:42 -06:00
|
|
|
return errors.New("go.sum never parsed?")
|
|
|
|
}
|
|
|
|
|
2024-12-17 07:02:45 -06:00
|
|
|
log.Info("deps for", check.GetGoPath(), "len()", check.GoDeps.Len())
|
2024-12-15 08:31:42 -06:00
|
|
|
deps := check.GoDeps.SortByGoPath()
|
|
|
|
for deps.Scan() {
|
|
|
|
depRepo := deps.Next()
|
2024-12-17 07:02:45 -06:00
|
|
|
log.Info("download:", depRepo.GetGoPath())
|
|
|
|
_, err := clone(depRepo.GetGoPath())
|
2024-12-15 08:31:42 -06:00
|
|
|
if err != nil {
|
2024-12-17 07:02:45 -06:00
|
|
|
log.Info("recursiveClone() could not download", depRepo.GetGoPath())
|
2024-12-15 08:31:42 -06:00
|
|
|
log.Info("err:", err)
|
|
|
|
bad += 1
|
2024-12-17 07:02:45 -06:00
|
|
|
badmap[depRepo.GetGoPath()] = err
|
2024-12-15 08:31:42 -06:00
|
|
|
} else {
|
2024-12-17 07:02:45 -06:00
|
|
|
log.Info("downloaded", depRepo.GetGoPath())
|
2024-12-15 08:31:42 -06:00
|
|
|
good += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Info("got", good, "repos", "failed on", bad, "repos")
|
|
|
|
if bad != 0 {
|
2024-12-15 15:51:27 -06:00
|
|
|
log.Info("clone() ERROR len(badmap)", len(badmap))
|
2024-12-15 08:45:44 -06:00
|
|
|
for gopath, err := range badmap {
|
2024-12-15 15:51:27 -06:00
|
|
|
log.Info("clone() ERROR", gopath, err)
|
|
|
|
}
|
|
|
|
if !argv.Ignore {
|
|
|
|
return errors.New("clone failed on some repos")
|
2024-12-15 08:45:44 -06:00
|
|
|
}
|
2024-12-15 08:31:42 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-12-15 12:14:22 -06:00
|
|
|
|
|
|
|
func makeValidGoSum(check *gitpb.Repo) error {
|
2024-12-15 15:51:27 -06:00
|
|
|
if check.Exists("go.mod") {
|
2024-12-15 17:03:41 -06:00
|
|
|
log.Info("makeValidGoSum() attempt SetPrimitive()")
|
2024-12-15 15:51:27 -06:00
|
|
|
if err := check.SetPrimitive(); err != nil {
|
2024-12-15 17:03:41 -06:00
|
|
|
log.Info("SetPrimitive() failed", err)
|
2024-12-15 15:51:27 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-12-17 07:02:45 -06:00
|
|
|
if check.GoInfo.GoPrimitive {
|
|
|
|
log.Info(check.GetGoPath(), "is a golang primitive! no need to parse go.sum because there is not one!")
|
2024-12-15 15:51:27 -06:00
|
|
|
return nil
|
|
|
|
}
|
2024-12-15 21:10:25 -06:00
|
|
|
// attempt to grab the notes
|
|
|
|
check.Run([]string{"git", "fetch", "origin", "refs/notes/*:refs/notes/*"})
|
|
|
|
|
2024-12-15 12:14:22 -06:00
|
|
|
// first try to generate go.mod & go.sum with go-mod-clean
|
|
|
|
if err := check.ValidGoSum(); err != nil {
|
|
|
|
log.Info("try running go-mod-clean")
|
|
|
|
// update go.sum and go.mod
|
|
|
|
if err := check.RunStrict([]string{"go-mod-clean"}); err != nil {
|
|
|
|
log.Info("")
|
|
|
|
log.Info("Do you have go-mod-clean? Otherwise:")
|
|
|
|
log.Info(" go install go.wit.com/apps/go-mod-clean@latest")
|
|
|
|
log.Info("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if this fails, just use go mod
|
|
|
|
if err := check.ValidGoSum(); err != nil {
|
2024-12-17 07:02:45 -06:00
|
|
|
cmd := []string{"go", "mod", "init", check.GetGoPath()}
|
2024-12-15 12:14:22 -06:00
|
|
|
log.Info("try running", cmd)
|
|
|
|
if err := check.RunStrict(cmd); err != nil {
|
|
|
|
log.Info("go mod init failed", err)
|
|
|
|
}
|
2024-12-15 15:51:27 -06:00
|
|
|
if check.Exists("go.mod") {
|
|
|
|
if err := check.SetPrimitive(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-12-17 07:02:45 -06:00
|
|
|
if check.GoInfo.GoPrimitive {
|
|
|
|
log.Info(check.GetGoPath(), "is a golang primitive! no need to parse go.sum because there is not one!")
|
2024-12-15 15:51:27 -06:00
|
|
|
return nil
|
|
|
|
}
|
2024-12-15 12:14:22 -06:00
|
|
|
if err := check.RunStrict([]string{"go", "mod", "tidy"}); err != nil {
|
|
|
|
log.Info("go mod tidy failed", err)
|
|
|
|
}
|
2024-12-15 15:51:27 -06:00
|
|
|
panic("fucknuts")
|
2024-12-15 12:14:22 -06:00
|
|
|
}
|
|
|
|
if err := check.ValidGoSum(); err != nil {
|
|
|
|
// have to give up. can't recursive clone without go.mod file
|
|
|
|
log.Info("could not generate valid go.sum file")
|
2024-12-15 15:51:27 -06:00
|
|
|
return errors.New(fmt.Sprintf("could have been %v", err))
|
2024-12-15 12:14:22 -06:00
|
|
|
}
|
|
|
|
check.ParseGoSum()
|
|
|
|
return nil
|
|
|
|
}
|