mirror of
https://github.com/x-motemen/ghq.git
synced 2026-09-10 07:26:27 -04:00
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// 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)
|
|
}
|
|
|
|
url, err := url.Parse(ref)
|
|
if err != nil {
|
|
return url, err
|
|
}
|
|
|
|
if !url.IsAbs() {
|
|
if !strings.Contains(url.Path, "/") {
|
|
url.Path = url.Path + "/" + url.Path
|
|
}
|
|
url.Scheme = "https"
|
|
url.Host = "github.com"
|
|
if url.Path[0] != '/' {
|
|
url.Path = "/" + url.Path
|
|
}
|
|
}
|
|
|
|
return url, nil
|
|
}
|
|
|
|
func ConvertGitHubURLHTTPToSSH(url *url.URL) (*url.URL, error) {
|
|
sshURL := fmt.Sprintf("ssh://git@github.com/%s", url.Path)
|
|
return url.Parse(sshURL)
|
|
}
|