-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlibrary.go
More file actions
177 lines (141 loc) · 3.93 KB
/
library.go
File metadata and controls
177 lines (141 loc) · 3.93 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package git
import (
"context"
"fmt"
"os"
"sync"
"github.com/src-d/lookout"
"github.com/src-d/lookout/util/ctxlog"
billy "gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-billy.v4/util"
errors "gopkg.in/src-d/go-errors.v1"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
"gopkg.in/src-d/go-git.v4/storage"
"gopkg.in/src-d/go-git.v4/storage/filesystem"
)
var (
ErrRepositoryExists = errors.NewKind("repository %s already exists")
ErrRepositoryNotExists = errors.NewKind("repository %s not exists")
)
type ReposCollectionHandler interface {
GetOrInit(context.Context, *lookout.RepositoryInfo) (*git.Repository, error)
Init(context.Context, *lookout.RepositoryInfo) (*git.Repository, error)
Has(*lookout.RepositoryInfo) (bool, error)
Get(context.Context, *lookout.RepositoryInfo) (*git.Repository, error)
}
// Library controls the persistence of multiple git repositories.
type Library struct {
m sync.Mutex
fs billy.Filesystem
}
// NewLibrary creates a new Library based on the given filesystem.
func NewLibrary(fs billy.Filesystem) *Library {
return &Library{fs: fs}
}
// GetOrInit get the requested repository based on the given URL, or inits a
// new repository.
func (l *Library) GetOrInit(ctx context.Context, url *lookout.RepositoryInfo) (
*git.Repository, error) {
has, err := l.Has(url)
if err != nil {
return nil, err
}
if has {
return l.Get(ctx, url)
}
return l.Init(ctx, url)
}
// Init inits a new repository for the given URL.
func (l *Library) Init(ctx context.Context, url *lookout.RepositoryInfo) (*git.Repository, error) {
ctxlog.Get(ctx).Infof("creating local repository for: %s", url.CloneURL)
l.m.Lock()
defer l.m.Unlock()
return l.init(url)
}
func (l *Library) init(url *lookout.RepositoryInfo) (*git.Repository, error) {
has, err := l.Has(url)
if err != nil {
return nil, err
}
if has {
return nil, ErrRepositoryExists.New(url.CloneURL)
}
s, err := l.repositoryStorer(url)
if err != nil {
return nil, err
}
r, err := git.Init(s, nil)
if err != nil {
return nil, err
}
_, err = r.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{url.CloneURL},
})
if err != nil {
return nil, err
}
return r, nil
}
// Has returns true if a repository with the given URL exists.
func (l *Library) Has(url *lookout.RepositoryInfo) (bool, error) {
_, err := l.fs.Stat(l.repositoryPath(url))
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// Get get the requested repository based on the given URL.
func (l *Library) Get(ctx context.Context, url *lookout.RepositoryInfo) (*git.Repository, error) {
r, err := l.get(url)
// it can happen if the repository in a broken state
if err == git.ErrRepositoryNotExists {
return l.recreate(url)
}
return r, nil
}
func (l *Library) get(url *lookout.RepositoryInfo) (*git.Repository, error) {
has, err := l.Has(url)
if err != nil {
return nil, err
}
if !has {
return nil, ErrRepositoryNotExists.New(url.CloneURL)
}
s, err := l.repositoryStorer(url)
if err != nil {
return nil, err
}
return git.Open(s, nil)
}
func (l *Library) repositoryStorer(url *lookout.RepositoryInfo) (
storage.Storer, error) {
fs, err := l.fs.Chroot(l.repositoryPath(url))
if err != nil {
return nil, err
}
// TODO(carlosms) take cache size from config
cache := cache.NewObjectLRU(cache.DefaultMaxSize)
return filesystem.NewStorage(fs, cache), nil
}
func (l *Library) repositoryPath(url *lookout.RepositoryInfo) string {
return fmt.Sprintf("%s/%s", url.Host, url.FullName)
}
func (l *Library) recreate(url *lookout.RepositoryInfo) (*git.Repository, error) {
l.m.Lock()
defer l.m.Unlock()
// in case it was recreated already by another goroutine
r, err := l.get(url)
if err != git.ErrRepositoryNotExists {
return r, err
}
if err := util.RemoveAll(l.fs, l.repositoryPath(url)); err != nil {
return nil, err
}
return l.init(url)
}