-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af80b38
commit 1c4e41a
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package git | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
func IsURL(u string) bool { | ||
return strings.HasPrefix(u, "git@") || isSupportedProtocol(u) | ||
} | ||
|
||
func isSupportedProtocol(u string) bool { | ||
return strings.HasPrefix(u, "ssh:") || | ||
strings.HasPrefix(u, "git+ssh:") || | ||
strings.HasPrefix(u, "git:") || | ||
strings.HasPrefix(u, "http:") || | ||
strings.HasPrefix(u, "git+https:") || | ||
strings.HasPrefix(u, "https:") | ||
} | ||
|
||
func isPossibleProtocol(u string) bool { | ||
return isSupportedProtocol(u) || | ||
strings.HasPrefix(u, "ftp:") || | ||
strings.HasPrefix(u, "ftps:") || | ||
strings.HasPrefix(u, "file:") | ||
} | ||
|
||
// ParseURL normalizes git remote urls | ||
func ParseURL(rawURL string) (u *url.URL, err error) { | ||
if !isPossibleProtocol(rawURL) && | ||
strings.ContainsRune(rawURL, ':') && | ||
// not a Windows path | ||
!strings.ContainsRune(rawURL, '\\') { | ||
// support scp-like syntax for ssh protocol | ||
rawURL = "ssh://" + strings.Replace(rawURL, ":", "/", 1) | ||
} | ||
|
||
u, err = url.Parse(rawURL) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if u.Scheme == "git+ssh" { | ||
u.Scheme = "ssh" | ||
} | ||
|
||
if u.Scheme == "git+https" { | ||
u.Scheme = "https" | ||
} | ||
|
||
if u.Scheme != "ssh" { | ||
return | ||
} | ||
|
||
if strings.HasPrefix(u.Path, "//") { | ||
u.Path = strings.TrimPrefix(u.Path, "/") | ||
} | ||
|
||
if idx := strings.Index(u.Host, ":"); idx >= 0 { | ||
u.Host = u.Host[0:idx] | ||
} | ||
|
||
return | ||
} | ||
|