add cloning

This commit is contained in:
2024-09-15 13:01:55 -07:00
parent f0c6f3906a
commit 1030a8f3a9

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath"
"sync" "sync"
git "github.com/go-git/go-git/v5" git "github.com/go-git/go-git/v5"
@@ -37,34 +38,26 @@ var (
mutex := sync.Mutex{} mutex := sync.Mutex{}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(len(conf.Repos)) wg.Add(len(conf.Repos))
for i := 0; i < jobs; i++ { cloneFunc := func() {
go func() { for repo := range repoChan {
for repo := range repoChan { _, err := git.PlainOpenWithOptions(repo.Path, &(git.PlainOpenOptions{DetectDotGit: true}))
_, err := git.PlainOpenWithOptions(repo.Path, &(git.PlainOpenOptions{DetectDotGit: true})) if err == nil {
if err == nil { log.Printf("already cloned: %s\n", repo.Path)
log.Printf("already cloned: %s\n", repo.Path) mutex.Lock()
mutex.Lock() alreadyCloned++
alreadyCloned++ mutex.Unlock()
mutex.Unlock() wg.Done()
wg.Done() continue
continue } else if err == git.ErrRepositoryNotExists {
} else if err == git.ErrRepositoryNotExists { log.Printf("attempting clone: %s\n", repo.Path)
log.Printf("attempting clone: %s\n", repo.Path) parentPath := filepath.Dir(repo.Path)
_, err = git.PlainClone(repo.Path, false, &git.CloneOptions{ if _, err := os.Stat(parentPath); err != nil {
URL: repo.Remote, os.MkdirAll(parentPath, os.ModeDir)
}) }
if err != nil { _, err = git.PlainClone(repo.Path, false, &git.CloneOptions{
mutex.Lock() URL: repo.Remote,
errs = append(errs, RepoError{Error: err, Repo: repo.Path}) })
mutex.Unlock() if err != nil {
log.Printf("clone failed for %s: %v\n", repo.Path, err)
wg.Done()
continue
}
fmt.Printf("successfully cloned %s\n", repo.Path)
wg.Done()
continue
} else {
mutex.Lock() mutex.Lock()
errs = append(errs, RepoError{Error: err, Repo: repo.Path}) errs = append(errs, RepoError{Error: err, Repo: repo.Path})
mutex.Unlock() mutex.Unlock()
@@ -72,8 +65,21 @@ var (
wg.Done() wg.Done()
continue continue
} }
fmt.Printf("successfully cloned %s\n", repo.Path)
wg.Done()
continue
} else {
mutex.Lock()
errs = append(errs, RepoError{Error: err, Repo: repo.Path})
mutex.Unlock()
log.Printf("clone failed for %s: %v\n", repo.Path, err)
wg.Done()
continue
} }
}() }
}
for i := 0; i < jobs; i++ {
go cloneFunc()
} }
fmt.Println(len(conf.Repos)) fmt.Println(len(conf.Repos))
for _, repo := range conf.Repos { for _, repo := range conf.Repos {