-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcs.go
223 lines (203 loc) · 5.14 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
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
221
222
223
package vcs
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"time"
)
const (
beginCommit = "\uffff"
endMessage = "\ufffe"
)
var (
// ErrNoVCS is returned when no VCS can be found at the given
// path.
ErrNoVCS = errors.New("could not find an VCS at the given path")
)
type Revision struct {
Identifier string
ShortIdentifier string
Subject string
Message string
Author string
Email string
Diff string
Timestamp time.Time
}
type Branch struct {
Name string
Revision string
}
type Tag struct {
Name string
Revision string
}
// VCS represents a VCS system on a given
// directory.
type VCS struct {
// Dir is the absolute path of the repository root.
Dir string
path string
iface Interface
}
func (v *VCS) cmd(args []string) ([]byte, error) {
return v.dirCmd(v.Dir, args)
}
func (v *VCS) dirCmd(dir string, args []string) ([]byte, error) {
var err error
if v.path == "" {
v.path, err = exec.LookPath(v.iface.Cmd())
if err != nil {
return nil, err
}
}
cmd := exec.Command(v.path, args...)
cmd.Dir = dir
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
// git show-ref will return exit status 1 when
// there are not tags. Workaround this issue.
if len(args) > 0 && args[0] == "show-ref" && stdout.Len() == 0 && stderr.Len() == 0 {
return stdout.Bytes(), nil
}
if stderr.Len() > 0 {
err = errors.New(stderr.String())
}
return nil, fmt.Errorf("command %s %s (at dir %s) failed with error: %s", v.path, args, dir, err)
}
return stdout.Bytes(), nil
}
// Last returns the last revision from the default branch, known
// as HEAD in git or tip in mercurial.
func (v *VCS) Last() (*Revision, error) {
return v.Revision(v.iface.Head())
}
// Revision returns the revision identified by the given id, which
// might be either a short or a long revision identifier.
func (v *VCS) Revision(id string) (*Revision, error) {
data, err := v.cmd(v.iface.Revision(id))
if err != nil {
return nil, err
}
revs, err := v.iface.ParseRevisions("", data)
if err != nil {
return nil, err
}
return revs[0], nil
}
// History returns all the revisions from the VCS which are
// newer than the revision identified by the since parameter,
// which might be either a short or a long revision identifier.
// If since is empty, all the history is returned.
func (v *VCS) History(since string) ([]*Revision, error) {
data, err := v.cmd(v.iface.History(since))
if err != nil {
return nil, err
}
return v.iface.ParseRevisions(since, data)
}
// Checkout discards all local changes in VCS and updates
// the working copy to the given revision. If no revision
// is given, the one returned by v.Last() will be checked
// out.
func (v *VCS) Checkout(rev string) error {
_, err := v.cmd(v.iface.Checkout(rev))
return err
}
// CheckoutAt works like Checkout, but creates a copy of the
// repository at the given directory before perforing the
// Checkout. The returned VCS is the new one created at dir.
func (v *VCS) CheckoutAt(rev string, dir string) (*VCS, error) {
// Create parent directory.
p, _ := filepath.Split(dir)
if err := os.MkdirAll(p, 0755); err != nil {
return nil, err
}
_, err := v.dirCmd("", v.iface.Clone(v.Dir, dir))
if err != nil {
return nil, err
}
vc, err := NewAt(dir)
if err != nil {
return nil, err
}
if err := vc.Checkout(rev); err != nil {
return nil, err
}
return vc, err
}
// Update updates the VCS from its upstream. If there's no
// upstream, an error will be returned.
func (v *VCS) Update() error {
_, err := v.cmd(v.iface.Update())
return err
}
// Name returns the name of the underlyng VCS interface (e.g.
// git, mercurial, ...).
func (v *VCS) Name() string {
return v.iface.Cmd()
}
// Branches returns the available branches in the VCS.
func (v *VCS) Branches() ([]*Branch, error) {
data, err := v.cmd(v.iface.Branches())
if err != nil {
return nil, err
}
return v.iface.ParseBranches(data)
}
// Branches returns the available tags in the VCS.
func (v *VCS) Tags() ([]*Tag, error) {
data, err := v.cmd(v.iface.Tags())
if err != nil {
return nil, err
}
return v.iface.ParseTags(data)
}
// New starts at the given directory and walks up
// until it finds an VCS. If no VCS could be found,
// an error is returned.
func New(dir string) (*VCS, error) {
cur := dir
for {
abs, err := filepath.Abs(cur)
if err != nil {
return nil, err
}
for _, v := range interfaces {
if tester, ok := v.(Tester); ok {
if tester.Test(abs) {
return &VCS{Dir: abs, iface: v}, nil
}
continue
}
d := filepath.Join(abs, v.Dir())
if st, err := os.Stat(d); err == nil && st.IsDir() {
return &VCS{Dir: abs, iface: v}, nil
}
}
if p := filepath.Dir(cur); p != "" && p != cur {
cur = p
continue
}
break
}
return nil, ErrNoVCS
}
// NewAt works like New, but does not walk up into the the parent
// directories. Id est, it will only succeed if the given directory
// is the root directory of a VCS checkout.
func NewAt(dir string) (*VCS, error) {
s, err := New(dir)
if err == nil {
if s.Dir != dir {
s = nil
err = ErrNoVCS
}
}
return s, err
}