mirror of
https://github.com/taigrr/mg.git
synced 2026-04-02 03:28:42 -07:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1030a8f3a9 | |||
| f0c6f3906a | |||
| 01c736b54e |
1
cmd/mg/.gitignore
vendored
Normal file
1
cmd/mg/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
main
|
||||||
@@ -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,46 +38,55 @@ 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.Remote, &(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()
|
||||||
} else if err == git.ErrRepositoryNotExists {
|
continue
|
||||||
log.Printf("attempting clone: %s\n", repo)
|
} else if err == git.ErrRepositoryNotExists {
|
||||||
_, err = git.PlainClone(repo.Path, false, &git.CloneOptions{
|
log.Printf("attempting clone: %s\n", repo.Path)
|
||||||
URL: repo.Remote,
|
parentPath := filepath.Dir(repo.Path)
|
||||||
})
|
if _, err := os.Stat(parentPath); err != nil {
|
||||||
if err != nil {
|
os.MkdirAll(parentPath, os.ModeDir)
|
||||||
mutex.Lock()
|
}
|
||||||
errs = append(errs, RepoError{Error: err, Repo: repo.Path})
|
_, err = git.PlainClone(repo.Path, false, &git.CloneOptions{
|
||||||
mutex.Unlock()
|
URL: repo.Remote,
|
||||||
log.Printf("clone failed for %s: %v\n", repo.Path, err)
|
})
|
||||||
wg.Done()
|
if err != nil {
|
||||||
continue
|
|
||||||
}
|
|
||||||
fmt.Printf("successfully cloned %s\n", repo)
|
|
||||||
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()
|
||||||
log.Printf("clone failed for %s: %v\n", repo, err)
|
log.Printf("clone failed for %s: %v\n", repo.Path, err)
|
||||||
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))
|
||||||
for _, repo := range conf.Repos {
|
for _, repo := range conf.Repos {
|
||||||
repoChan <- repo
|
repoChan <- repo
|
||||||
}
|
}
|
||||||
close(repoChan)
|
close(repoChan)
|
||||||
|
fmt.Println("waiting...")
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
for _, err := range errs {
|
for _, err := range errs {
|
||||||
log.Printf("error pulling %s: %s\n", err.Repo, err.Error)
|
log.Printf("error pulling %s: %s\n", err.Repo, err.Error)
|
||||||
|
|||||||
Reference in New Issue
Block a user