-
Notifications
You must be signed in to change notification settings - Fork 60
/
repo.go
276 lines (224 loc) · 7.45 KB
/
repo.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Package vcs provides the ability to work with varying version control systems
// (VCS), also known as source control systems (SCM) though the same interface.
//
// This package includes a function that attempts to detect the repo type from
// the remote URL and return the proper type. For example,
//
// remote := "https://github.com/Masterminds/vcs"
// local, _ := ioutil.TempDir("", "go-vcs")
// repo, err := NewRepo(remote, local)
//
// In this case repo will be a GitRepo instance. NewRepo can detect the VCS for
// numerous popular VCS and from the URL. For example, a URL ending in .git
// that's not from one of the popular VCS will be detected as a Git repo and
// the correct type will be returned.
//
// If you know the repository type and would like to create an instance of a
// specific type you can use one of constructors for a type. They are NewGitRepo,
// NewSvnRepo, NewBzrRepo, and NewHgRepo. The definition and usage is the same
// as NewRepo.
//
// Once you have an object implementing the Repo interface the operations are
// the same no matter which VCS you're using. There are some caveats. For
// example, each VCS has its own version formats that need to be respected and
// checkout out branches, if a branch is being worked with, is different in
// each VCS.
package vcs
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"strings"
"time"
)
// Logger is where you can provide a logger, implementing the log.Logger interface,
// where verbose output from each VCS will be written. The default logger does
// not log data. To log data supply your own logger or change the output location
// of the provided logger.
var Logger *log.Logger
func init() {
// Initialize the logger to one that does not actually log anywhere. This is
// to be overridden by the package user by setting vcs.Logger to a different
// logger.
Logger = log.New(ioutil.Discard, "go-vcs", log.LstdFlags)
}
const longForm = "2006-01-02 15:04:05 -0700"
// Type describes the type of VCS
type Type string
// VCS types
const (
NoVCS Type = ""
Git Type = "git"
Svn Type = "svn"
Bzr Type = "bzr"
Hg Type = "hg"
)
// Repo provides an interface to work with repositories using different source
// control systems such as Git, Bzr, Mercurial, and SVN. For implementations
// of this interface see BzrRepo, GitRepo, HgRepo, and SvnRepo.
type Repo interface {
// Vcs retrieves the underlying VCS being implemented.
Vcs() Type
// Remote retrieves the remote location for a repo.
Remote() string
// LocalPath retrieves the local file system location for a repo.
LocalPath() string
// Get is used to perform an initial clone/checkout of a repository.
Get() error
// Initializes a new repository locally.
Init() error
// Update performs an update to an existing checkout of a repository.
Update() error
// UpdateVersion sets the version of a package of a repository.
UpdateVersion(string) error
// Version retrieves the current version.
Version() (string, error)
// Current retrieves the current version-ish. This is different from the
// Version method. The output could be a branch name if on the tip of a
// branch (git), a tag if on a tag, a revision if on a specific revision
// that's not the tip of the branch. The values here vary based on the VCS.
Current() (string, error)
// Date retrieves the date on the latest commit.
Date() (time.Time, error)
// CheckLocal verifies the local location is of the correct VCS type
CheckLocal() bool
// Branches returns a list of available branches on the repository.
Branches() ([]string, error)
// Tags returns a list of available tags on the repository.
Tags() ([]string, error)
// IsReference returns if a string is a reference. A reference can be a
// commit id, branch, or tag.
IsReference(string) bool
// IsDirty returns if the checkout has been modified from the checked
// out reference.
IsDirty() bool
// CommitInfo retrieves metadata about a commit.
CommitInfo(string) (*CommitInfo, error)
// TagsFromCommit retrieves tags from a commit id.
TagsFromCommit(string) ([]string, error)
// Ping returns if remote location is accessible.
Ping() bool
// RunFromDir executes a command from repo's directory.
RunFromDir(cmd string, args ...string) ([]byte, error)
// CmdFromDir creates a new command that will be executed from repo's
// directory.
CmdFromDir(cmd string, args ...string) *exec.Cmd
// ExportDir exports the current revision to the passed in directory.
ExportDir(string) error
}
// NewRepo returns a Repo based on trying to detect the source control from the
// remote and local locations. The appropriate implementation will be returned
// or an ErrCannotDetectVCS if the VCS type cannot be detected.
// Note, this function may make calls to the Internet to determind help determine
// the VCS.
func NewRepo(remote, local string) (Repo, error) {
vtype, remote, err := detectVcsFromRemote(remote)
// From the remote URL the VCS could not be detected. See if the local
// repo contains enough information to figure out the VCS. The reason the
// local repo is not checked first is because of the potential for VCS type
// switches which will be detected in each of the type builders.
if err == ErrCannotDetectVCS {
vtype, err = DetectVcsFromFS(local)
}
if err != nil {
return nil, err
}
switch vtype {
case Git:
return NewGitRepo(remote, local)
case Svn:
return NewSvnRepo(remote, local)
case Hg:
return NewHgRepo(remote, local)
case Bzr:
return NewBzrRepo(remote, local)
}
// Should never fall through to here but just in case.
return nil, ErrCannotDetectVCS
}
// CommitInfo contains metadata about a commit.
type CommitInfo struct {
// The commit id
Commit string
// Who authored the commit
Author string
// Date of the commit
Date time.Time
// Commit message
Message string
}
type base struct {
remote, local string
Logger *log.Logger
}
func (b *base) log(v interface{}) {
b.Logger.Printf("%s", v)
}
// Remote retrieves the remote location for a repo.
func (b *base) Remote() string {
return b.remote
}
// LocalPath retrieves the local file system location for a repo.
func (b *base) LocalPath() string {
return b.local
}
func (b *base) setRemote(remote string) {
b.remote = remote
}
func (b *base) setLocalPath(local string) {
b.local = local
}
func (b base) run(cmd string, args ...string) ([]byte, error) {
out, err := exec.Command(cmd, args...).CombinedOutput()
b.log(out)
if err != nil {
err = fmt.Errorf("%s: %s", out, err)
}
return out, err
}
func (b *base) CmdFromDir(cmd string, args ...string) *exec.Cmd {
c := exec.Command(cmd, args...)
c.Dir = b.local
c.Env = envForDir(c.Dir)
return c
}
func (b *base) RunFromDir(cmd string, args ...string) ([]byte, error) {
c := b.CmdFromDir(cmd, args...)
out, err := c.CombinedOutput()
return out, err
}
func (b *base) referenceList(c, r string) []string {
var out []string
re := regexp.MustCompile(r)
for _, m := range re.FindAllStringSubmatch(c, -1) {
out = append(out, m[1])
}
return out
}
func envForDir(dir string) []string {
env := os.Environ()
return mergeEnvLists([]string{"PWD=" + dir}, env)
}
func mergeEnvLists(in, out []string) []string {
NextVar:
for _, inkv := range in {
k := strings.SplitAfterN(inkv, "=", 2)[0]
for i, outkv := range out {
if strings.HasPrefix(outkv, k) {
out[i] = inkv
continue NextVar
}
}
out = append(out, inkv)
}
return out
}
func depInstalled(name string) bool {
if _, err := exec.LookPath(name); err != nil {
return false
}
return true
}