gitpb/repo.new.go

65 lines
1.7 KiB
Go
Raw Permalink Normal View History

2024-11-27 14:41:57 -06:00
package gitpb
import (
"errors"
"go.wit.com/log"
2024-11-27 14:41:57 -06:00
)
// scans in a new git repo. If it detects the repo is a golang project,
// then it parses the go.mod/go.sum files
// TODO: try adding python, rails, perl, rust, other language things?
// I probably will never have time to try that, but I'd take patches for anyone
// that might see this note and feel so inclined.
2024-12-17 00:00:49 -06:00
// todo: use Repos.Lock() ?
func (all *Repos) NewGoRepo(fullpath string, gopath string) (*Repo, error) {
if gopath == "" {
return nil, errors.New("blank gopath")
}
2024-12-17 06:37:14 -06:00
if r := all.FindByFullPath(fullpath); r != nil {
log.Info("gitpb.NewGoPath() already has gopath", r.GetGoPath())
log.Info("gitpb.NewGoPath() already has FullPath", r.FullPath)
2024-11-27 14:41:57 -06:00
// already had this gopath
2024-12-07 16:50:26 -06:00
return r, errors.New("gitpb.NewGoPath() duplicate gopath " + gopath)
}
2024-11-27 14:41:57 -06:00
// add a new one here
newr := Repo{
2024-12-17 00:00:49 -06:00
FullPath: fullpath,
}
2024-12-17 01:15:31 -06:00
newr.Times = new(GitTimes)
2024-12-17 00:00:49 -06:00
newr.GoInfo = new(GoInfo)
newr.GoInfo.GoPath = gopath
2024-12-17 06:37:14 -06:00
// everything happens in here
newr.Reload()
2024-12-17 06:37:14 -06:00
if all.AppendUniqueFullPath(&newr) {
// worked
return &newr, nil
} else {
// this is dumb, probably never happens. todo: use Repos.Lock()
2024-12-17 06:37:14 -06:00
if r := all.FindByFullPath(fullpath); r != nil {
// already had this gopath
return r, errors.New("gitpb.NewGoPath() AppendUnique() failed but Find() worked" + gopath)
}
}
// todo: use Repos.Lock()
2024-12-07 16:50:26 -06:00
return nil, errors.New("repo gitpb.NewGoPath() should never have gotten here " + gopath)
2024-11-29 21:51:30 -06:00
}
2024-12-17 06:37:14 -06:00
// enforces GoPath is unique
func (all *Repos) AppendUniqueGoPath(newr *Repo) bool {
all.Lock.RLock()
defer all.Lock.RUnlock()
for _, r := range all.Repos {
if r.GoInfo.GoPath == newr.GoInfo.GoPath {
return false
}
}
all.Repos = append(all.Repos, newr)
return true
}