This repository was archived by the owner on Sep 11, 2020. It is now read-only.
File tree 3 files changed +50
-14
lines changed
3 files changed +50
-14
lines changed Original file line number Diff line number Diff line change 8
8
"sort"
9
9
"strconv"
10
10
11
+ "gopkg.in/src-d/go-git.v4/internal/url"
11
12
format "gopkg.in/src-d/go-git.v4/plumbing/format/config"
12
13
)
13
14
@@ -399,3 +400,7 @@ func (c *RemoteConfig) marshal() *format.Subsection {
399
400
400
401
return c .raw
401
402
}
403
+
404
+ func (c * RemoteConfig ) IsFirstURLLocal () bool {
405
+ return url .IsLocalEndpoint (c .URLs [0 ])
406
+ }
Original file line number Diff line number Diff line change
1
+ package url
2
+
3
+ import (
4
+ "regexp"
5
+ )
6
+
7
+ var (
8
+ isSchemeRegExp = regexp .MustCompile (`^[^:]+://` )
9
+ scpLikeUrlRegExp = regexp .MustCompile (`^(?:(?P<user>[^@]+)@)?(?P<host>[^:\s]+):(?:(?P<port>[0-9]{1,5})/)?(?P<path>[^\\].*)$` )
10
+ )
11
+
12
+ // MatchesScheme returns true if the given string matches a URL-like
13
+ // format scheme.
14
+ func MatchesScheme (url string ) bool {
15
+ return isSchemeRegExp .MatchString (url )
16
+ }
17
+
18
+ // MatchesScpLike returns true if the given string matches an SCP-like
19
+ // format scheme.
20
+ func MatchesScpLike (url string ) bool {
21
+ return scpLikeUrlRegExp .MatchString (url )
22
+ }
23
+
24
+ // FindScpLikeComponents returns the user, host, port and path of the
25
+ // given SCP-like URL.
26
+ func FindScpLikeComponents (url string ) (user , host , port , path string ) {
27
+ m := scpLikeUrlRegExp .FindStringSubmatch (url )
28
+ return m [1 ], m [2 ], m [3 ], m [4 ]
29
+ }
30
+
31
+ // IsLocalEndpoint returns true if the given URL string specifies a
32
+ // local file endpoint. For example, on a Linux machine,
33
+ // `/home/user/src/go-git` would match as a local endpoint, but
34
+ // `https://github.com/src-d/go-git` would not.
35
+ func IsLocalEndpoint (url string ) bool {
36
+ return ! MatchesScheme (url ) && ! MatchesScpLike (url )
37
+ }
Original file line number Diff line number Diff line change @@ -19,10 +19,10 @@ import (
19
19
"fmt"
20
20
"io"
21
21
"net/url"
22
- "regexp"
23
22
"strconv"
24
23
"strings"
25
24
25
+ giturl "gopkg.in/src-d/go-git.v4/internal/url"
26
26
"gopkg.in/src-d/go-git.v4/plumbing"
27
27
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp"
28
28
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability"
@@ -224,34 +224,28 @@ func getPath(u *url.URL) string {
224
224
return res
225
225
}
226
226
227
- var (
228
- isSchemeRegExp = regexp .MustCompile (`^[^:]+://` )
229
- scpLikeUrlRegExp = regexp .MustCompile (`^(?:(?P<user>[^@]+)@)?(?P<host>[^:\s]+):(?:(?P<port>[0-9]{1,5})/)?(?P<path>[^\\].*)$` )
230
- )
231
-
232
227
func parseSCPLike (endpoint string ) (* Endpoint , bool ) {
233
- if isSchemeRegExp . MatchString (endpoint ) || ! scpLikeUrlRegExp . MatchString (endpoint ) {
228
+ if giturl . MatchesScheme (endpoint ) || ! giturl . MatchesScpLike (endpoint ) {
234
229
return nil , false
235
230
}
236
231
237
- m := scpLikeUrlRegExp .FindStringSubmatch (endpoint )
238
-
239
- port , err := strconv .Atoi (m [3 ])
232
+ user , host , portStr , path := giturl .FindScpLikeComponents (endpoint )
233
+ port , err := strconv .Atoi (portStr )
240
234
if err != nil {
241
235
port = 22
242
236
}
243
237
244
238
return & Endpoint {
245
239
Protocol : "ssh" ,
246
- User : m [ 1 ] ,
247
- Host : m [ 2 ] ,
240
+ User : user ,
241
+ Host : host ,
248
242
Port : port ,
249
- Path : m [ 4 ] ,
243
+ Path : path ,
250
244
}, true
251
245
}
252
246
253
247
func parseFile (endpoint string ) (* Endpoint , bool ) {
254
- if isSchemeRegExp . MatchString (endpoint ) {
248
+ if giturl . MatchesScheme (endpoint ) {
255
249
return nil , false
256
250
}
257
251
You can’t perform that action at this time.
0 commit comments