forked from hound-search/hound
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvcs.go
77 lines (62 loc) · 1.77 KB
/
vcs.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
package vcs
import (
"fmt"
"log"
"os"
)
// A collection that maps vcs names to their underlying
// factory. A factory allows the vcs to have unserialized
// json config passed in to be parsed.
var drivers = make(map[string]func(c []byte) (Driver, error))
// A "plugin" for each vcs that supports the very limited set of vcs
// operations that hound needs.
type Driver interface {
// Clone a new working directory.
Clone(dir, url string) (string, error)
// Pull new changes from the server and update the working directory.
Pull(dir string) (string, error)
// Return the revision at the head of the vcs directory.
HeadRev(dir string) (string, error)
// Return a list of special filenames that should not be indexed.
SpecialFiles() []string
}
// An API to interact with a vcs working directory. This is
// what clients will interact with.
type WorkDir struct {
Driver
}
// Register a new vcs driver under 1 or more names.
func Register(fn func(c []byte) (Driver, error), names ...string) {
if fn == nil {
log.Panic("vcs: cannot register nil factory")
}
for _, name := range names {
drivers[name] = fn
}
}
// Create a new WorkDir from the name and configuration data.
func New(name string, cfg []byte) (*WorkDir, error) {
f := drivers[name]
if f == nil {
return nil, fmt.Errorf("vcs: %s is not a valid vcs driver.", name)
}
d, err := f(cfg)
if err != nil {
return nil, err
}
return &WorkDir{d}, nil
}
func exists(path string) bool {
if _, err := os.Stat(path); err != nil {
return false
}
return true
}
// A utility method that carries out the common operation of cloning
// if the working directory is absent and pulling otherwise.
func (w *WorkDir) PullOrClone(dir, url string) (string, error) {
if exists(dir) {
return w.Pull(dir)
}
return w.Clone(dir, url)
}