-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathget_hg.go
206 lines (174 loc) · 5.02 KB
/
get_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
package getter
import (
"context"
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
urlhelper "github.com/hashicorp/go-getter/v2/helper/url"
safetemp "github.com/hashicorp/go-safetemp"
)
// HgGetter is a Getter implementation that will download a module from
// a Mercurial repository.
type HgGetter struct {
// Timeout sets a deadline which all hg CLI operations should
// complete within. Defaults to zero which means no timeout.
Timeout time.Duration
}
func (g *HgGetter) Mode(ctx context.Context, _ *url.URL) (Mode, error) {
return ModeDir, nil
}
func (g *HgGetter) Get(ctx context.Context, req *Request) error {
if _, err := exec.LookPath("hg"); err != nil {
return fmt.Errorf("hg must be available and on the PATH")
}
newURL, err := urlhelper.Parse(req.u.String())
if err != nil {
return err
}
if fixWindowsDrivePath(newURL) {
// See valid file path form on http://www.selenic.com/hg/help/urls
newURL.Path = fmt.Sprintf("/%s", newURL.Path)
}
// Extract some query parameters we use
var rev string
q := newURL.Query()
if len(q) > 0 {
rev = q.Get("rev")
q.Del("rev")
newURL.RawQuery = q.Encode()
}
_, err = os.Stat(req.Dst)
if err != nil && !os.IsNotExist(err) {
return err
}
if g.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, g.Timeout)
defer cancel()
}
if err != nil {
if err := g.clone(ctx, req.Dst, newURL); err != nil {
return err
}
}
if err := g.pull(ctx, req.Dst, newURL); err != nil {
return err
}
return g.update(ctx, req.Dst, newURL, rev)
}
// GetFile for Hg doesn't support updating at this time. It will download
// the file every time.
func (g *HgGetter) GetFile(ctx context.Context, req *Request) error {
// Create a temporary directory to store the full source. This has to be
// a non-existent directory.
td, tdcloser, err := safetemp.Dir("", "getter")
if err != nil {
return err
}
defer tdcloser.Close()
// Get the filename, and strip the filename from the URL so we can
// just get the repository directly.
filename := filepath.Base(req.u.Path)
req.u.Path = filepath.Dir(req.u.Path)
dst := req.Dst
req.Dst = td
// If we're on Windows, we need to set the host to "localhost" for hg
if runtime.GOOS == "windows" {
req.u.Host = "localhost"
}
// Get the full repository
if err := g.Get(ctx, req); err != nil {
return err
}
// Copy the single file
req.u, err = urlhelper.Parse(fmtFileURL(filepath.Join(td, filename)))
if err != nil {
return err
}
fg := &FileGetter{}
req.Copy = true
req.Dst = dst
return fg.GetFile(ctx, req)
}
func (g *HgGetter) clone(ctx context.Context, dst string, u *url.URL) error {
cmd := exec.CommandContext(ctx, "hg", "clone", "-U", "--", u.String(), dst)
return getRunCommand(cmd)
}
func (g *HgGetter) pull(ctx context.Context, dst string, u *url.URL) error {
cmd := exec.CommandContext(ctx, "hg", "pull")
cmd.Dir = dst
return getRunCommand(cmd)
}
func (g *HgGetter) update(ctx context.Context, dst string, u *url.URL, rev string) error {
args := []string{"update"}
if rev != "" {
args = append(args, "--", rev)
}
cmd := exec.CommandContext(ctx, "hg", args...)
cmd.Dir = dst
return getRunCommand(cmd)
}
func (g *HgGetter) Detect(req *Request) (bool, error) {
src := req.Src
if len(src) == 0 {
return false, nil
}
if req.Forced != "" {
// There's a getter being Forced
if !g.validScheme(req.Forced) {
// Current getter is not the Forced one
// Don't use it to try to download the artifact
return false, nil
}
}
isForcedGetter := req.Forced != "" && g.validScheme(req.Forced)
u, err := url.Parse(src)
if err == nil && u.Scheme != "" {
if isForcedGetter {
// Is the Forced getter and source is a valid url
return true, nil
}
if g.validScheme(u.Scheme) {
return true, nil
}
// Valid url with a scheme that is not valid for current getter
return false, nil
}
src, ok, err := new(BitBucketDetector).Detect(src, req.Pwd)
if err != nil {
return ok, err
}
forced, src := getForcedGetter(src)
if ok && g.validScheme(forced) {
req.Src = src
return ok, nil
}
if isForcedGetter {
// Is the Forced getter and should be used to download the artifact
if req.Pwd != "" && !filepath.IsAbs(src) {
// Make sure to add pwd to relative paths
src = filepath.Join(req.Pwd, src)
}
// Make sure we're using "/" on Windows. URLs are "/"-based.
req.Src = filepath.ToSlash(src)
return true, nil
}
return false, nil
}
func (g *HgGetter) validScheme(scheme string) bool {
return scheme == "hg"
}
func fixWindowsDrivePath(u *url.URL) bool {
// hg assumes a file:/// prefix for Windows drive letter file paths.
// (e.g. file:///c:/foo/bar)
// If the URL Path does not begin with a '/' character, the resulting URL
// path will have a file:// prefix. (e.g. file://c:/foo/bar)
// See http://www.selenic.com/hg/help/urls and the examples listed in
// http://selenic.com/repo/hg-stable/file/1265a3a71d75/mercurial/util.py#l1936
return runtime.GOOS == "windows" && u.Scheme == "file" &&
len(u.Path) > 1 && u.Path[0] != '/' && u.Path[1] == ':'
}