mirror of
https://github.com/x-motemen/ghq.git
synced 2026-09-10 07:26:27 -04:00
32 lines
852 B
Go
32 lines
852 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"regexp"
|
|
)
|
|
|
|
// Convert SCP-like URL to SSH URL(e.g. [user@]host.xz:path/to/repo.git/)
|
|
// ref. http://git-scm.com/docs/git-fetch#_git_urls
|
|
// (golang hasn't supported Perl-like negative look-behind match)
|
|
var hasSchemePattern = regexp.MustCompile("^[^:]+://")
|
|
var scpLikeUrlPattern = regexp.MustCompile("^([^@]+@)?([^:]+):(.+)$")
|
|
|
|
func NewURL(ref string) (*url.URL, error) {
|
|
if !hasSchemePattern.MatchString(ref) && scpLikeUrlPattern.MatchString(ref) {
|
|
matched := scpLikeUrlPattern.FindStringSubmatch(ref)
|
|
user := matched[1]
|
|
host := matched[2]
|
|
path := matched[3]
|
|
|
|
ref = fmt.Sprintf("ssh://%s%s/%s", user, host, path)
|
|
}
|
|
|
|
return url.Parse(ref)
|
|
}
|
|
|
|
func ConvertGitHubURLHTTPToSSH(url *url.URL) (*url.URL, error) {
|
|
sshURL := fmt.Sprintf("ssh://git@github.com/%s", url.Path)
|
|
return url.Parse(sshURL)
|
|
}
|