forked from Masterminds/vcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hg.go
284 lines (267 loc) · 9.56 KB
/
hg.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
277
278
279
280
281
282
283
284
package vcs
import (
"os"
"regexp"
"strings"
"github.com/dvln/out"
"github.com/dvln/util/dir"
"github.com/dvln/util/url"
)
var hgDetectURL = regexp.MustCompile("default = (?P<foo>.+)\n")
var defaultHgSchemes []string
// set up default hg remote URL schemes and a search order (for any remote
// that doesn't have a full URL), eg: https, http, ssh
func init() {
SetDefaultHgSchemes(nil)
}
// HgGet is used to perform an initial clone of a repository.
func HgGet(g *HgGetter, rev ...Rev) (Resulter, error) {
results := newResults()
var result *Result
var err error
if rev == nil || (rev != nil && rev[0] == "") {
result, err = run(hgTool, "clone", "-U", g.Remote(), g.LocalRepoPath())
} else {
result, err = run(hgTool, "clone", "-u", string(rev[0]), "-U", g.Remote(), g.LocalRepoPath())
}
results.add(result)
return results, err
}
// HgUpdate performs a Mercurial pull (like git fetch) + mercurial update (like git pull)
// into the workspace checkout. Note that one can optionally identify a specific rev
// to update/merge to. It will return any output of the cmd and an error that occurs.
// Note that there will be a pull and a merge class of functionality in dvln but
// pull is likely Mercurial pull (ie: git fetch) and merge is similar to git/hg,
// whereas update is like a fetch/merge in git or pull/upd(/merge) in hg.
func HgUpdate(u *HgUpdater, rev ...Rev) (Resulter, error) {
//FIXME: should support a "date:<datestr>" class of rev,
// if that is passed in use "-d <date>" for update, so should
// all other VCS's that can support it... other option is to
// switch to a *Revision as the param but a special revision
// that only has *1* revision set (raw, tags, branches, semver,
// time)... and a silly routine to get that rev (then no need
// to mark up the 'Rev' type (which is a string), but a strong
// need to pass in the right thing of course if that is done. ;)
results := newResults()
result, err := runFromLocalRepoDir(u.LocalRepoPath(), hgTool, "pull")
results.add(result)
if err != nil {
return results, err
}
var updResult *Result
if rev == nil || (rev != nil && rev[0] == "") {
updResult, err = runFromLocalRepoDir(u.LocalRepoPath(), hgTool, "update")
} else {
updResult, err = runFromLocalRepoDir(u.LocalRepoPath(), hgTool, "update", "-r", string(rev[0]))
}
results.add(updResult)
return results, err
}
// HgRevSet sets the local repo rev of a pkg currently checked out via Hg.
// Note that a single specific revision must be given vs a generic
// Revision structure (since it may have <N> different valid rev's
// that reference the revision, this one decides exactly the one
// the client wishes to "set" or checkout in the local repo).
func HgRevSet(r RevSetter, rev Rev) (Resulter, error) {
results := newResults()
if rev == "" {
result, err := runFromLocalRepoDir(r.LocalRepoPath(), hgTool, "update")
results.add(result)
return results, err
}
result, err := runFromLocalRepoDir(r.LocalRepoPath(), hgTool, "update", "-r", string(rev))
results.add(result)
return results, err
}
// HgRevRead retrieves the given or current local repo rev. A Revision struct
// pointer is returned (how filled out depends upon if the read is just the
// basic core/raw VCS revision or full data for the given VCS which will
// include tags, branches, timestamp info, author/committer, date, comment).
// Note: this reads one version but that could be expanded to take <N>
// revisions or a range, eg HgRevRead(reader, <scope>, rev1, "..", rev2),
// without changing this methods params or return signature (but code
// changes would be needed)
func HgRevRead(r RevReader, scope ReadScope, vcsRev ...Rev) ([]Revisioner, Resulter, error) {
results := newResults()
oldDir, err := os.Getwd()
if err != nil {
return nil, nil, err
}
specificRev := ""
if vcsRev != nil && vcsRev[0] != "" {
specificRev = string(vcsRev[0])
}
err = os.Chdir(r.LocalRepoPath())
if err != nil {
return nil, nil, err
}
defer os.Chdir(oldDir)
rev := &Revision{}
var revs []Revisioner
if scope == CoreRev {
// client just wants the core/base VCS revision only..
var result *Result
if specificRev != "" {
result, err = run(hgTool, "identify", "-r", specificRev)
} else {
result, err = run(hgTool, "identify")
}
results.add(result)
if err != nil {
return nil, results, err
}
parts := strings.SplitN(result.Output, " ", 2)
sha := strings.TrimSpace(parts[0])
rev.SetCore(Rev(sha))
revs = append(revs, rev)
} else {
//FIXME: implement more extensive hg data grab
// if the client has asked for extra data (vs speed)
/* Here is how to get the details, if no "branch:" it's default
141 [brady-air]/Users/brady/vcs/mercurial-repo: hg log -l 1
changeset: 26211:ea489d94e1dc
bookmark: @
tag: tip
user: Gregory Szorc <gregory.szorc@gmail.com>
date: Sat Aug 22 17:08:37 2015 -0700
summary: hgweb: assign ctype to requestcontext
142 [brady-air]/Users/brady/vcs/mercurial-repo: hg log -l 1 -r 3.5.1
changeset: 26120:1a45e49a6bed
branch: stable
tag: 3.5.1
user: Matt Mackall <mpm@selenic.com>
date: Tue Sep 01 16:08:07 2015 -0500
summary: hgweb: fix trust of templates path (BC)
143 [brady-air]/Users/brady/vcs/mercurial-repo: hg identify
ea489d94e1dc tip @
144 [brady-air]/Users/brady/vcs/mercurial-repo: hg identify -r 3.5.1
1a45e49a6bed (stable) 3.5.1
145 [brady-air]/Users/brady/vcs/mercurial-repo: hg identify -i -b -t -r 3.5.1
1a45e49a6bed stable 3.5.1
146 [brady-air]/Users/brady/vcs/mercurial-repo: hg identify -i -b -t
ea489d94e1dc default tip
147 [brady-air]/Users/brady/vcs/mercurial-repo:
*/
var result *Result
if specificRev != "" {
result, err = run(hgTool, "identify", "-r", specificRev)
} else {
result, err = run(hgTool, "identify")
}
results.add(result)
if err != nil {
return nil, results, err
}
parts := strings.SplitN(result.Output, " ", 2)
sha := strings.TrimSpace(parts[0])
rev.SetCore(Rev(sha))
revs = append(revs, rev)
}
return revs, results, err
}
// HgExists verifies the local repo or remote location is a Hg repo,
// returns where it was found ("" if not found), a resulter (cmds
// run and their output to accomplish task) and and any error. If
// it does not exist a wrapped ErrNoExist error is returned (use
// out.IsError() to check)
func HgExists(e Existence, l Location) (string, Resulter, error) {
results := newResults()
var err error
path := ""
if l == LocalPath {
if exists, err := dir.Exists(e.LocalRepoPath() + "/.hg"); exists && err == nil {
return e.LocalRepoPath(), nil, nil
}
err = out.WrapErrf(ErrNoExist, 4504, "Local hg location, \"%s\", does not exist, err: %s", e.LocalRepoPath(), err)
} else { // checking remote "URL" as well as possible for current VCS..
remote := e.Remote()
scheme := url.GetScheme(remote)
// if we have a scheme then just see if the repo exists...
if scheme != "" {
var result *Result
result, err = run(hgTool, "identify", remote)
results.add(result)
if err == nil {
path = remote
}
} else {
vcsSchemes := e.Schemes()
for _, scheme = range vcsSchemes {
var result *Result
result, err = run(hgTool, "identify", scheme+"://"+remote)
results.add(result)
if err == nil {
path = scheme + "://" + remote
break
}
}
}
if err == nil {
return path, results, nil
}
err = out.WrapErrf(ErrNoExist, 4503, "Remote hg location, \"%s\", does not exist, err: %s", e.Remote(), err)
}
return path, results, err
}
// HgCheckRemote attempts to take a remote string (URL) and validate
// it against any local repo and try and set it when it is empty. Returns:
// - string: this is the new remote (current remote returned if no new remote)
// - Resulter: cmd(s) run and output of the Hg commands
// - error: non-nil if an error occurred
func HgCheckRemote(e Existence, remote string) (string, Resulter, error) {
results := newResults()
// Make sure the local Hg repo is configured the same as the remote when
// A remote value was passed in.
var outStr string
if loc, existResults, err := e.Exists(LocalPath); err == nil && loc != "" {
if existResults != nil {
for _, existResult := range existResults.All() {
results.add(existResult)
}
}
// An Hg repo was found so test that the URL there matches
// the repo passed in here.
oldDir, err := os.Getwd()
if err != nil {
return remote, nil, err
}
err = os.Chdir(e.LocalRepoPath())
if err != nil {
return remote, nil, err
}
defer os.Chdir(oldDir)
result, err := run(hgTool, "paths")
results.add(result)
if err != nil {
return remote, results, err
}
outStr = result.Output
m := hgDetectURL.FindStringSubmatch(outStr)
//FIXME: added that remote != "", think it's needed, check
if remote != "" && m[1] != "" && m[1] != remote {
return remote, results, ErrWrongRemote
}
// If no remote was passed in but one is configured for the locally
// checked out Hg repo use that one.
if remote == "" && m[1] != "" {
return m[1], results, nil
}
} else if err != nil {
if existResults != nil {
for _, existResult := range existResults.All() {
results.add(existResult)
}
}
}
return remote, results, nil
}
// SetDefaultHgSchemes allows one to override the default ordering
// and set of hg remote URL schemes to try for any remote that has
// no scheme provided, defaults to Go core list for now.
func SetDefaultHgSchemes(schemes []string) {
if schemes == nil {
defaultHgSchemes = []string{"https", "http", "ssh"}
} else {
defaultHgSchemes = schemes
}
}