|
5 | 5 | "os" |
6 | 6 | "path/filepath" |
7 | 7 | "strconv" |
| 8 | + "strings" |
8 | 9 |
|
9 | 10 | "github.com/src-d/gitbase" |
10 | 11 | "github.com/src-d/gitbase/internal/function" |
@@ -34,8 +35,10 @@ const ( |
34 | 35 | // Server represents the `server` command of gitbase cli tool. |
35 | 36 | type Server struct { |
36 | 37 | Verbose bool `short:"v" description:"Activates the verbose mode"` |
37 | | - Git []string `short:"g" long:"git" description:"Path where the git repositories are located, multiple directories can be defined. Accepts globs."` |
38 | | - Siva []string `long:"siva" description:"Path where the siva repositories are located, multiple directories can be defined. Accepts globs."` |
| 38 | + Directories []string `short:"d" long:"directories" description:"Path where the git repositories are located (standard and siva), multiple directories can be defined. Accepts globs."` |
| 39 | + Depth int `long:"depth" default:"1000" description:"load repositories looking at less than <depth> nested subdirectories."` |
| 40 | + DisableGit bool `long:"no-git" description:"disable the load of git standard repositories."` |
| 41 | + DisableSiva bool `long:"no-siva" description:"disable the load of siva files."` |
39 | 42 | Host string `long:"host" default:"localhost" description:"Host where the server is going to listen"` |
40 | 43 | Port int `short:"p" long:"port" default:"3306" description:"Port where the server is going to listen"` |
41 | 44 | User string `short:"u" long:"user" default:"root" description:"User name used for connection"` |
@@ -196,52 +199,121 @@ func (c *Server) registerDrivers() error { |
196 | 199 | } |
197 | 200 |
|
198 | 201 | func (c *Server) addDirectories() error { |
199 | | - if len(c.Git) == 0 && len(c.Siva) == 0 { |
200 | | - logrus.Error("At least one git folder or siva folder should be provided.") |
| 202 | + if len(c.Directories) == 0 { |
| 203 | + logrus.Error("At least one folder should be provided.") |
201 | 204 | } |
202 | 205 |
|
203 | | - for _, pattern := range c.Git { |
204 | | - if err := c.addGitPattern(pattern); err != nil { |
205 | | - return err |
206 | | - } |
| 206 | + if c.DisableGit && c.DisableSiva { |
| 207 | + logrus.Warn("The load of git repositories and siva files are disabled," + |
| 208 | + " no repository will be added.") |
| 209 | + |
| 210 | + return nil |
207 | 211 | } |
208 | 212 |
|
209 | | - for _, pattern := range c.Siva { |
210 | | - if err := c.addSivaPattern(pattern); err != nil { |
| 213 | + if c.Depth < 1 { |
| 214 | + logrus.Warn("--depth flag set to a number less than 1," + |
| 215 | + " no repository will be added.") |
| 216 | + |
| 217 | + return nil |
| 218 | + } |
| 219 | + |
| 220 | + for _, directory := range c.Directories { |
| 221 | + if err := c.addDirectory(directory); err != nil { |
211 | 222 | return err |
212 | 223 | } |
213 | 224 | } |
214 | 225 |
|
215 | 226 | return nil |
216 | 227 | } |
217 | 228 |
|
218 | | -func (c *Server) addGitPattern(pattern string) error { |
219 | | - prefix, matches, err := gitbase.PatternMatches(pattern) |
| 229 | +func (c *Server) addDirectory(directory string) error { |
| 230 | + matches, err := gitbase.PatternMatches(directory) |
220 | 231 | if err != nil { |
221 | 232 | return err |
222 | 233 | } |
223 | 234 |
|
224 | | - for _, m := range matches { |
225 | | - logrus.WithField("dir", m).Debug("git repositories directory added") |
226 | | - if err := c.pool.AddDir(prefix, m); err != nil { |
227 | | - return err |
| 235 | + for _, match := range matches { |
| 236 | + if err := c.addMatch(match); err != nil { |
| 237 | + logrus.WithFields(logrus.Fields{ |
| 238 | + "path": match, |
| 239 | + "error": err, |
| 240 | + }).Error("path couldn't be inspected") |
228 | 241 | } |
229 | 242 | } |
230 | 243 |
|
231 | 244 | return nil |
232 | 245 | } |
233 | 246 |
|
234 | | -func (c *Server) addSivaPattern(pattern string) error { |
235 | | - matches, err := filepath.Glob(pattern) |
| 247 | +func (c *Server) addMatch(match string) error { |
| 248 | + root, err := filepath.Abs(match) |
236 | 249 | if err != nil { |
237 | 250 | return err |
238 | 251 | } |
239 | 252 |
|
240 | | - for _, m := range matches { |
241 | | - logrus.WithField("dir", m).Debug("siva repositories directory added") |
242 | | - if err := c.pool.AddSivaDir(m); err != nil { |
| 253 | + initDepth := strings.Count(root, string(os.PathSeparator)) |
| 254 | + return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { |
| 255 | + if err != nil { |
243 | 256 | return err |
244 | 257 | } |
| 258 | + |
| 259 | + if info.IsDir() { |
| 260 | + if err := c.addIfGitRepo(path); err != nil { |
| 261 | + return err |
| 262 | + } |
| 263 | + |
| 264 | + depth := strings.Count(path, string(os.PathSeparator)) - initDepth |
| 265 | + if depth >= c.Depth { |
| 266 | + return filepath.SkipDir |
| 267 | + } |
| 268 | + |
| 269 | + return nil |
| 270 | + } |
| 271 | + |
| 272 | + if !c.DisableSiva && |
| 273 | + info.Mode().IsRegular() && gitbase.IsSivaFile(info.Name()) { |
| 274 | + if err := c.pool.AddSivaFile(path); err != nil { |
| 275 | + logrus.WithFields(logrus.Fields{ |
| 276 | + "path": path, |
| 277 | + "error": err, |
| 278 | + }).Error("repository could not be addded") |
| 279 | + |
| 280 | + return nil |
| 281 | + } |
| 282 | + |
| 283 | + logrus.WithField("path", path).Debug("repository added") |
| 284 | + } |
| 285 | + |
| 286 | + return nil |
| 287 | + }) |
| 288 | +} |
| 289 | + |
| 290 | +func (c *Server) addIfGitRepo(path string) error { |
| 291 | + ok, err := gitbase.IsGitRepo(path) |
| 292 | + if err != nil { |
| 293 | + logrus.WithFields(logrus.Fields{ |
| 294 | + "path": path, |
| 295 | + "error": err, |
| 296 | + }).Error("path couldn't be inspected") |
| 297 | + |
| 298 | + return filepath.SkipDir |
| 299 | + } |
| 300 | + |
| 301 | + if ok { |
| 302 | + if !c.DisableGit { |
| 303 | + base := filepath.Base(path) |
| 304 | + if err := c.pool.AddGitWithID(base, path); err != nil { |
| 305 | + logrus.WithFields(logrus.Fields{ |
| 306 | + "id": base, |
| 307 | + "path": path, |
| 308 | + "error": err, |
| 309 | + }).Error("repository could not be added") |
| 310 | + } |
| 311 | + |
| 312 | + logrus.WithField("path", path).Debug("repository added") |
| 313 | + } |
| 314 | + |
| 315 | + // either the repository is added or not, the path must be skipped |
| 316 | + return filepath.SkipDir |
245 | 317 | } |
246 | 318 |
|
247 | 319 | return nil |
|
0 commit comments