gitpb/repo.new.go

65 lines
1.7 KiB
Go

package gitpb
import (
"errors"
"go.wit.com/log"
)
// 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.
// todo: use Repos.Lock() ?
func (all *Repos) NewGoRepo(fullpath string, gopath string) (*Repo, error) {
if gopath == "" {
return nil, errors.New("blank gopath")
}
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)
// already had this gopath
return r, errors.New("gitpb.NewGoPath() duplicate gopath " + gopath)
}
// add a new one here
newr := Repo{
FullPath: fullpath,
}
newr.Times = new(GitTimes)
newr.GoInfo = new(GoInfo)
newr.GoInfo.GoPath = gopath
// everything happens in here
newr.Reload()
if all.AppendUniqueFullPath(&newr) {
// worked
return &newr, nil
} else {
// this is dumb, probably never happens. todo: use Repos.Lock()
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()
return nil, errors.New("repo gitpb.NewGoPath() should never have gotten here " + gopath)
}
// 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
}