-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlibrary.go
More file actions
165 lines (130 loc) · 3.32 KB
/
library.go
File metadata and controls
165 lines (130 loc) · 3.32 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
package git
import (
"fmt"
"os"
"sync"
"github.com/src-d/lookout"
"gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-errors.v1"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/storage"
"gopkg.in/src-d/go-git.v4/storage/filesystem"
log "gopkg.in/src-d/go-log.v1"
)
var (
ErrRepositoryExists = errors.NewKind("repository %s already exists")
ErrRepositoryNotExists = errors.NewKind("repository %s not exists")
)
// 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(url *lookout.RepositoryInfo) (
*git.Repository, error) {
has, err := l.Has(url)
if err != nil {
return nil, err
}
if has {
return l.Get(url)
}
return l.Init(url)
}
// Init inits a new repository for the given URL.
func (l *Library) Init(url *lookout.RepositoryInfo) (*git.Repository, error) {
log.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(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
}
return filesystem.NewStorage(fs)
}
func (l *Library) repositoryPath(url *lookout.RepositoryInfo) string {
return fmt.Sprintf("%s/%s", url.RepoHost, 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 := l.fs.Remove(l.repositoryPath(url)); err != nil {
return nil, err
}
return l.init(url)
}