2024-11-28 08:35:39 -06:00
|
|
|
package forgepb
|
|
|
|
|
|
|
|
import (
|
2024-11-30 12:45:07 -06:00
|
|
|
"errors"
|
2024-12-02 05:14:52 -06:00
|
|
|
"fmt"
|
2024-11-28 08:35:39 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2024-11-30 12:45:07 -06:00
|
|
|
"github.com/destel/rill"
|
|
|
|
"go.wit.com/lib/protobuf/gitpb"
|
2024-11-28 08:35:39 -06:00
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f *Forge) ScanGoSrc() (bool, error) {
|
2024-12-02 05:14:52 -06:00
|
|
|
dirs, err := gitDirectoriesNew(f.goSrc)
|
2024-11-28 08:35:39 -06:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2024-11-30 12:45:07 -06:00
|
|
|
var gopaths []string
|
2024-11-28 08:35:39 -06:00
|
|
|
for _, dir := range dirs {
|
2024-12-03 00:35:33 -06:00
|
|
|
// log.Info("forge.ScanGoSrc()", dir)
|
2024-11-28 08:35:39 -06:00
|
|
|
if strings.HasPrefix(dir, f.goSrc) {
|
|
|
|
gopath := strings.TrimPrefix(dir, f.goSrc)
|
|
|
|
gopath = strings.Trim(gopath, "/")
|
2024-12-17 06:37:00 -06:00
|
|
|
if r := f.FindByGoPath(gopath); r != nil {
|
2024-12-03 00:35:33 -06:00
|
|
|
// log.Info("already have", gopath)
|
|
|
|
continue
|
|
|
|
}
|
2024-11-30 12:45:07 -06:00
|
|
|
gopaths = append(gopaths, gopath)
|
2024-11-28 08:35:39 -06:00
|
|
|
} else {
|
2024-11-28 08:54:30 -06:00
|
|
|
log.Log(FORGEPBWARN, "ScanGoSrc() bad:", dir)
|
2024-11-30 12:45:07 -06:00
|
|
|
return false, errors.New("forgepb.ScanGoSrc() bad dir: " + dir)
|
2024-11-28 08:35:39 -06:00
|
|
|
}
|
|
|
|
}
|
2024-12-03 00:35:33 -06:00
|
|
|
newcount, err := f.rillScanDirs(gopaths)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("go src dir problem. exit for now?", err)
|
2024-12-14 16:29:39 -06:00
|
|
|
return false, err
|
2024-12-03 00:35:33 -06:00
|
|
|
}
|
|
|
|
if newcount != 0 {
|
|
|
|
log.Info("forge go src scan found", newcount, "repos")
|
|
|
|
f.Repos.ConfigSave()
|
|
|
|
}
|
2024-12-02 05:14:52 -06:00
|
|
|
return true, err
|
|
|
|
}
|
2024-11-30 12:45:07 -06:00
|
|
|
|
2024-12-02 05:14:52 -06:00
|
|
|
// doesn't enter the directory any further when it finds a .git/
|
|
|
|
// not stupid like my old version
|
|
|
|
func gitDirectoriesNew(srcDir string) ([]string, error) {
|
|
|
|
var all []string
|
2024-12-12 23:50:38 -06:00
|
|
|
var trip bool
|
2024-12-02 05:14:52 -06:00
|
|
|
err := filepath.WalkDir(srcDir, func(path string, d os.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
// Handle possible errors, like permission issues
|
|
|
|
fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
|
|
|
|
return err
|
2024-11-30 12:45:07 -06:00
|
|
|
}
|
2024-12-02 05:14:52 -06:00
|
|
|
|
2024-12-03 00:35:33 -06:00
|
|
|
if d.IsDir() {
|
|
|
|
// log.Info("path is dir", path)
|
|
|
|
} else {
|
2024-12-13 02:22:13 -06:00
|
|
|
_, fname := filepath.Split(path)
|
|
|
|
switch fname {
|
|
|
|
case "repos.pb":
|
|
|
|
case "go.work":
|
2024-12-15 08:48:06 -06:00
|
|
|
case "go.work.last":
|
2024-12-13 02:22:13 -06:00
|
|
|
default:
|
2024-12-12 23:50:38 -06:00
|
|
|
// todo: figure out a way to do padding for init()
|
2024-12-17 00:00:27 -06:00
|
|
|
if trip == false {
|
|
|
|
log.Info("WARNING:")
|
|
|
|
}
|
2024-12-12 23:50:38 -06:00
|
|
|
log.Info("WARNING: you have an untracked file outside of any .git repository:", path)
|
|
|
|
trip = true
|
|
|
|
}
|
2024-12-03 00:35:33 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-02 05:14:52 -06:00
|
|
|
gitdir := filepath.Join(path, ".git")
|
|
|
|
_, err2 := os.Stat(gitdir)
|
|
|
|
if !os.IsNotExist(err2) {
|
|
|
|
all = append(all, path)
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2024-12-12 23:50:38 -06:00
|
|
|
//
|
|
|
|
// probably always leave this here forever
|
|
|
|
// this check, along with CheckDirty() makes sure you can safely delete ~/go/src or the go.work directory
|
|
|
|
// because everything is either checked in or deleted. An important thing to know!
|
|
|
|
if trip {
|
|
|
|
log.Info("WARNING:")
|
|
|
|
log.Info("WARNING: there isn't a way to disable this warning yet")
|
|
|
|
log.Info("WARNING: probably this is a good thing however. you don't want to leave files outside of git repos here")
|
|
|
|
log.Info("WARNING: so this warning should probably stay")
|
|
|
|
log.Info("WARNING:")
|
|
|
|
log.Info("WARNING: this also might mean you put these files here because you are actively working on them")
|
|
|
|
log.Info("WARNING: and you don't want to forget about them")
|
|
|
|
log.Info("WARNING:")
|
|
|
|
}
|
2024-12-02 05:14:52 -06:00
|
|
|
return all, err
|
2024-11-28 08:35:39 -06:00
|
|
|
}
|
|
|
|
|
2024-12-02 05:14:52 -06:00
|
|
|
func gitDirectoriesOld(srcDir string) ([]string, error) {
|
2024-11-28 08:35:39 -06:00
|
|
|
var all []string
|
|
|
|
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
log.Log(FORGEPBWARN, "Error accessing path:", path, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the path is a directory and has a .git subdirectory
|
|
|
|
if info.IsDir() && IsGitDir(path) {
|
|
|
|
all = append(all, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Log(FORGEPBWARN, "Error walking the path:", srcDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return all, err
|
|
|
|
}
|
|
|
|
|
2024-11-29 21:50:55 -06:00
|
|
|
// IsGitDir checks if a .git directory exists inside the given directory
|
|
|
|
func IsGitDir(dir string) bool {
|
|
|
|
gitDir := filepath.Join(dir, ".git")
|
|
|
|
info, err := os.Stat(gitDir)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return info.IsDir()
|
|
|
|
}
|
|
|
|
|
2024-11-30 12:45:07 -06:00
|
|
|
// rill is awesome. long live rill
|
|
|
|
// attempt scan with rill
|
2024-12-03 00:35:33 -06:00
|
|
|
func (f *Forge) rillScanDirs(gopaths []string) (int, error) {
|
2024-11-30 12:45:07 -06:00
|
|
|
// 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) {
|
2024-12-17 06:37:00 -06:00
|
|
|
return f.checkpath(id, "")
|
2024-11-30 12:45:07 -06:00
|
|
|
})
|
|
|
|
|
2024-12-03 00:35:33 -06:00
|
|
|
var counter int
|
2024-11-30 12:45:07 -06:00
|
|
|
// Activate users.
|
|
|
|
// Concurrency = 10
|
|
|
|
err := rill.ForEach(dirs, 10, func(repo *gitpb.Repo) error {
|
2024-12-03 00:35:33 -06:00
|
|
|
counter += 1
|
2024-11-30 12:45:07 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2024-12-03 00:35:33 -06:00
|
|
|
return counter, err
|
2024-11-30 12:45:07 -06:00
|
|
|
}
|
2024-12-03 13:24:41 -06:00
|
|
|
|
2024-12-17 06:37:00 -06:00
|
|
|
func (f *Forge) checkpath(gopath string, url string) (*gitpb.Repo, error) {
|
|
|
|
fullpath := filepath.Join(f.GetGoSrc(), gopath)
|
|
|
|
log.Info("checkpath()", gopath, fullpath)
|
|
|
|
repo, err := f.NewGoRepo(gopath, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Info("checkpath()", gopath, err)
|
|
|
|
}
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
2024-12-03 13:24:41 -06:00
|
|
|
func (f *Forge) RillRedoGoMod() int {
|
|
|
|
var all []*gitpb.Repo
|
2024-12-17 06:37:00 -06:00
|
|
|
tmp := f.Repos.SortByFullPath()
|
2024-12-11 19:31:37 -06:00
|
|
|
for tmp.Scan() {
|
|
|
|
repo := tmp.Next()
|
2024-12-17 01:15:17 -06:00
|
|
|
if !repo.IsValidDir() {
|
2024-12-17 06:37:00 -06:00
|
|
|
log.Printf("%10s %-50s", "why am I in RillRedoGoMod? old?", repo.GetGoPath())
|
2024-12-03 13:24:41 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
all = append(all, repo)
|
|
|
|
}
|
|
|
|
// Convert a slice of user IDs into a channel
|
|
|
|
ids := rill.FromSlice(all, nil)
|
|
|
|
|
|
|
|
var counter int
|
|
|
|
// Read users from the API.
|
|
|
|
// Concurrency = 20
|
|
|
|
dirs := rill.Map(ids, 50, func(id *gitpb.Repo) (*gitpb.Repo, error) {
|
|
|
|
return id, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
err := rill.ForEach(dirs, 20, func(repo *gitpb.Repo) error {
|
|
|
|
counter += 1
|
2024-12-12 23:15:19 -06:00
|
|
|
// repo.RedoGoMod()
|
2024-12-03 13:24:41 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Info("rill.ForEach() error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return counter
|
|
|
|
}
|