/
repositories.go
220 lines (189 loc) · 4.81 KB
/
repositories.go
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package regression
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/ghodss/yaml"
"gopkg.in/src-d/go-log.v1"
)
// RepoDescription holds the information about a single repository
type RepoDescription struct {
Name string `json:"name"`
URL string `json:"url"`
Description string `json:"description"`
Complexity int `json:"complexity"`
}
var defaultRepos = []RepoDescription{
{
Name: "cangallo",
URL: "git://github.com/jfontan/cangallo.git",
Description: "Small repository that should be fast to clone",
Complexity: 0,
},
{
Name: "octoprint-tft",
URL: "https://github.com/mcuadros/OctoPrint-TFT",
Description: "Small repository that should be fast to clone",
Complexity: 0,
},
{
Name: "upsilon",
URL: "git://github.com/upsilonproject/upsilon-common.git",
Description: "Average repository",
Complexity: 1,
},
{
Name: "numpy",
URL: "git://github.com/numpy/numpy.git",
Description: "Average repository",
Complexity: 2,
},
{
Name: "tensorflow",
URL: "git://github.com/tensorflow/tensorflow.git",
Description: "Average repository",
Complexity: 3,
},
{
Name: "bismuth",
URL: "git://github.com/hclivess/Bismuth.git",
Description: "Big files repo (100Mb)",
Complexity: 4,
},
}
// Repositories struct has the information about a set of repositories and
// functionality to download them.
type Repositories struct {
Repos []RepoDescription
config GitServerConfig
}
// NewRepositories creates a new Repositories set. If config.RepositoriesFile
// is set it will try to load it and use as a load it as the repositories
// list.
func NewRepositories(config GitServerConfig) (*Repositories, error) {
repos := defaultRepos
if config.RepositoriesFile != "" {
var err error
repos, err = loadReposYaml(config.RepositoriesFile)
if err != nil {
return nil, err
}
}
return &Repositories{
Repos: repos,
config: config,
}, nil
}
// Download clones all repositories in the list that have equal or lower
// complexity specified in config.
func (r *Repositories) Download() error {
for _, repo := range r.Repos {
if repo.Complexity > r.config.Complexity {
continue
}
logger := log.New(log.Fields{"name": repo.Name})
path := filepath.Join(r.config.RepositoriesCache, repo.Name)
exist, err := fileExist(path)
if err != nil {
return err
}
if exist {
logger.Debugf("Repository already downloaded")
continue
}
logger = logger.New(log.Fields{
"url": repo.URL,
"path": path,
})
logger.Debugf("Downloading repository")
err = os.MkdirAll(r.config.RepositoriesCache, 0755)
if err != nil {
return err
}
err = downloadRepo(logger, repo.URL, path)
if err != nil {
logger.Errorf(err, "Could not download repository")
return err
}
}
return nil
}
// Path returns the repository cache path.
func (r *Repositories) Path() string {
return r.config.RepositoriesCache
}
// Names returns the names of repositories within concurrency level.
func (r *Repositories) Names() []string {
names := make([]string, 0, len(r.Repos))
for _, repo := range r.Repos {
if repo.Complexity <= r.config.Complexity {
names = append(names, repo.Name)
}
}
return names
}
// LinksDir returns a path of a temporary directory with repos within
// the config complexity.
func (r *Repositories) LinksDir() (string, error) {
dir, err := CreateTempDir()
if err != nil {
return "", err
}
for _, name := range r.Names() {
from := filepath.Join(r.config.RepositoriesCache, name)
to := filepath.Join(dir, name)
err = RecursiveCopy(from, to)
if err != nil {
os.RemoveAll(dir)
return "", err
}
}
return dir, err
}
// ShowRepos prints information about all repositories.
func (r *Repositories) ShowRepos() {
for _, repo := range r.Repos {
fmt.Printf("* Name: %s\n", repo.Name)
fmt.Printf(" URL: %s\n", repo.URL)
fmt.Printf(" Complexity: %d\n", repo.Complexity)
}
}
func downloadRepo(l log.Logger, url, path string) error {
downloadPath := fmt.Sprintf("%s.download", path)
exist, err := fileExist(downloadPath)
if err != nil {
return err
}
if exist {
err = os.RemoveAll(downloadPath)
if err != nil {
return err
}
}
clone, err := NewExecutor("git", "clone", "--mirror", url, downloadPath)
if err != nil {
l.Errorf(err, "Could not create executor")
return err
}
err = clone.Run()
if err != nil {
out, _ := clone.Out()
l.New(log.Fields{"output": out}).Errorf(err, "Could not execute git clone")
return err
}
err = os.Rename(downloadPath, path)
return err
}
func loadReposYaml(file string) ([]RepoDescription, error) {
text, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
var repos []RepoDescription
err = yaml.Unmarshal(text, &repos)
if err != nil {
return nil, err
}
return repos, nil
}