-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcs.go
93 lines (78 loc) · 1.82 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"path/filepath"
)
type vcs struct {
name string
dlArgs,
getRevArgs,
getRemoteArgs,
getRootArgs,
toRevArgs []string
}
func args(a ...string) []string {
return a
}
var (
vcses = []*vcs{
&vcs{
name: "git",
dlArgs: args("clone"),
getRevArgs: args("log", "--pretty=format:%H", "-n", "1", "HEAD"),
getRemoteArgs: args("config", "--get", "remote.origin.url"),
getRootArgs: args("rev-parse", "--show-toplevel"),
toRevArgs: args("checkout"),
},
&vcs{
name: "hg",
dlArgs: args("clone"),
getRevArgs: args("log", "--template={node}", "--rev=."),
getRemoteArgs: args("paths", "default"),
getRootArgs: args("root"),
toRevArgs: args("update"),
},
}
)
func selectVCS(name string) *vcs {
for _, v := range vcses {
if v.name == name {
return v
}
}
return nil
}
func detectVCS(dir string) *vcs {
for _, v := range vcses {
if root := tryRun(dir, v.name, v.getRootArgs...); len(root) > 0 {
return v
}
}
return nil
}
func (v *vcs) getAbsRoot(dir string) string {
return tryRun(dir, v.name, v.getRootArgs...)
}
// TODO: re-consider later
func (v *vcs) getRelRoot(dir string) string {
absRoot := run(dir, v.name, v.getRootArgs...)
gopath := goPaths()
for _, gopath := range gopath {
prefix := filepath.Join(gopath, "src")
if relRoot, err := filepath.Rel(prefix, absRoot); err == nil {
return relRoot
}
}
return ""
}
func (v *vcs) download(remote, dir string) string {
return run(".", v.name, append(v.dlArgs, remote, dir)...)
}
func (v *vcs) getRev(dir string) string {
return run(dir, v.name, v.getRevArgs...)
}
func (v *vcs) getRemote(dir string) string {
return run(dir, v.name, v.getRemoteArgs...)
}
func (v *vcs) toRev(dir, rev string) string {
return run(dir, v.name, append(v.toRevArgs, rev)...)
}