-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathendpoint.go
208 lines (169 loc) · 5.05 KB
/
endpoint.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
package grit
import (
"bytes"
"fmt"
"html/template"
"os"
"path"
"strings"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/client"
)
const slugSeparator = "/"
// EndpointTemplate is template for a Git repository URL.
type EndpointTemplate string
// Endpoint represents a Git clone endpoint, resolved from an EndpointTemplate.
type Endpoint struct {
// The actual URL used to clone the repository.
// This string will match the URL template from the configuration as closely
// as possible.
Actual string
// The substituted and normalized endpoint template. SCP-style Git URLs
// are converted to ssh:// URLs.
Normalized *transport.Endpoint
}
// Validate returns an error if the template is invalid.
func (t EndpointTemplate) Validate() error {
_, err := t.virtualEndpoint()
return err
}
// IsMatch returns true if may have been derived from the endpoint template.
func (t EndpointTemplate) IsMatch(e *transport.Endpoint) bool {
ep, err := t.virtualEndpoint()
if err != nil {
return false
}
// TODO: match slug heuristically
return e.Protocol == ep.Protocol && e.Host == ep.Host
}
// virtualEndpoint returns a Git endpoint from the template as though we had
// a slug to resolve.
func (t EndpointTemplate) virtualEndpoint() (*transport.Endpoint, error) {
ep, err := t.Resolve("__virtual/slug__")
return ep.Normalized, err
}
// Resolve returns a URL from the template.
func (t EndpointTemplate) Resolve(slug string) (ep Endpoint, err error) {
ep.Actual, err = t.replace(slug)
if err == nil {
ep.Normalized, err = transport.NewEndpoint(ep.Actual)
}
return
}
func (t EndpointTemplate) replace(slug string) (u string, err error) {
funcs := map[string]interface{}{
"slug": func() string { return slug },
"env": os.Getenv,
}
tmpl, err := template.
New("url").
Funcs(funcs).
Parse(string(t))
if err == nil {
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, nil)
u = buf.String()
}
return
}
// EndpointExists returns true if url is a Git repository.
func EndpointExists(ep Endpoint) (ok bool, err error) {
cli, err := client.NewClient(ep.Normalized)
if err != nil {
return
}
sess, err := cli.NewUploadPackSession(ep.Normalized, nil)
if err != nil {
return
}
defer sess.Close()
_, err = sess.AdvertisedReferences()
switch err {
case transport.ErrRepositoryNotFound:
err = nil
case transport.ErrEmptyRemoteRepository:
err = nil
ok = true
case nil:
ok = true
}
return
}
// EndpointToDir returns the absolute path for a clone of a repository.
func EndpointToDir(base string, ep *transport.Endpoint) string {
slug := EndpointToSlug(ep)
parts := strings.Split(slug, slugSeparator)
return path.Join(base, ep.Host, path.Join(parts...))
}
// EndpointToSlug returns the "slug" from ep.
func EndpointToSlug(ep *transport.Endpoint) string {
return strings.TrimSuffix(
strings.TrimPrefix(ep.Path, slugSeparator),
path.Ext(ep.Path),
)
}
// ReplaceSlug returns a copy of ep with the slug changed to s.
func ReplaceSlug(ep *transport.Endpoint, s string) *transport.Endpoint {
new, err := transport.NewEndpoint(
strings.Replace(
ep.String(),
slugSeparator+strings.TrimPrefix(ep.Path, slugSeparator),
slugSeparator+s+path.Ext(ep.Path),
1,
),
)
if err != nil {
panic(err)
}
return new
}
// MergeSlug returns a copy of ep with the slug changed to s. If s has less
// path atoms then the existing slug it is merged with the existing slug such
// that the original number of path atoms are retained.
func MergeSlug(ep *transport.Endpoint, s string) *transport.Endpoint {
a := strings.Split(s, slugSeparator)
slug := EndpointToSlug(ep)
atoms := strings.Split(slug, slugSeparator)
diff := len(atoms) - len(a)
if diff > 0 {
s = strings.Join(atoms[0:diff], slugSeparator) + slugSeparator + s
}
return ReplaceSlug(ep, s)
}
// EndpointIsSCP returns true if s is an SSH URL given in "SCP" style, that is
// [email protected]:jmalloc/grit.git, as opposed to ssh://[email protected]/jmalloc/grit.git.
func EndpointIsSCP(s string) bool {
ep, err := transport.NewEndpoint(s)
return err == nil &&
ep.Protocol == "ssh" &&
!strings.HasPrefix(s, "ssh://")
}
// EndpointToSCP converts a normalized ssh:// endpoint URL to an SCP-style URL.
func EndpointToSCP(ep *transport.Endpoint) (string, error) {
if ep.Protocol != "ssh" {
return "", fmt.Errorf("unexpected protocol: %s, expected ssh", ep.Protocol)
}
return fmt.Sprintf(
"%s@%s:%s",
ep.User,
ep.Host,
strings.TrimPrefix(ep.Path, slugSeparator),
), nil
}
// EndpointFromRemote returns the endpoint used to fetch from r.
func EndpointFromRemote(cfg *config.RemoteConfig) (ep *transport.Endpoint, url string, err error) {
url = cfg.URLs[0]
ep, err = transport.NewEndpoint(url)
return
}
// ParseEndpointOrSlug returns an endpoint if s contains a valid endpoint URL.
// If s is a "slug", isEndpoint is false.
func ParseEndpointOrSlug(s string) (ep *transport.Endpoint, isEndpoint bool, err error) {
ep, err = transport.NewEndpoint(s)
if err != nil {
return
}
isEndpoint = ep.Protocol != "file"
return
}