1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package load
import (
log "github.com/sirupsen/logrus"
"context"
"errors"
"runtime"
"sync"
"github.com/isacikgoz/gitbatch/core/git"
"golang.org/x/sync/semaphore"
)
// AsyncAdd is interface to caller
type AsyncAdd func(r *git.Repository)
// RepositoryEntities initializes the go-git's repository obejcts with given
// slice of paths. since this job is done parallel, the order of the directories
// is not kept
func RepositoryEntities(directories []string) (entities []*git.Repository, err error) {
entities = make([]*git.Repository, 0)
var wg sync.WaitGroup
var mu sync.Mutex
for _, dir := range directories {
// increment wait counter by one because we run a single goroutine
// below
wg.Add(1)
go func(d string) {
// decrement the wait counter by one, we call it in a defer so it's
// called at the end of this goroutine
defer wg.Done()
entity, err := git.InitializeRepo(d)
if err != nil {
log.WithFields(log.Fields{
"directory": d,
}).Trace("Cannot load git repository.")
return
}
// lock so we don't get a race if multiple go routines try to add
// to the same entities
mu.Lock()
entities = append(entities, entity)
mu.Unlock()
}(dir)
}
// wait until the wait counter is zero, this happens if all goroutines have
// finished
wg.Wait()
if len(entities) == 0 {
return entities, errors.New("There are no git repositories at given path(s)")
}
return entities, nil
}
// AddRepositoryEntitiesAsync asynchronously adds to caller
func AddRepositoryEntitiesAsync(directories []string, add AsyncAdd) error {
ctx := context.TODO()
var (
maxWorkers = runtime.GOMAXPROCS(0)
sem = semaphore.NewWeighted(int64(maxWorkers))
)
var mx sync.Mutex
for _, dir := range directories {
if err := sem.Acquire(ctx, 1); err != nil {
log.Errorf("Failed to acquire semaphore: %v", err)
break
}
go func(d string) {
defer sem.Release(1)
entity, err := git.InitializeRepo(d)
if err != nil {
log.WithFields(log.Fields{
"directory": d,
}).Trace("Cannot load git repository.")
return
}
// lock so we don't get a race if multiple go routines try to add
// to the same entities
mx.Lock()
add(entity)
mx.Unlock()
}(dir)
}
return nil
}
|