Accept the "HOST/OWNER/REPO" syntax or passing a full URL for both the
`--repo` flag and the GH_REPO environment variable and allow setting
GH_HOST environment variable to override just the hostname for
operations that assume "github.com" by default.
Examples:
$ gh repo clone example.org/owner/repo
$ GH_HOST=example.org gh repo clone repo
$ GH_HOST=example.org gh api user
$ GH_HOST=example.org gh gist create myfile.txt
$ gh issue list -R example.org/owner/repo
$ gh issue list -R https://example.org/owner/repo.git
$ GH_REPO=example.org/owner/repo gh issue list
48 lines
827 B
Go
48 lines
827 B
Go
package git
|
|
|
|
import (
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
protocolRe = regexp.MustCompile("^[a-zA-Z_+-]+://")
|
|
)
|
|
|
|
func IsURL(u string) bool {
|
|
return strings.HasPrefix(u, "git@") || protocolRe.MatchString(u)
|
|
}
|
|
|
|
// ParseURL normalizes git remote urls
|
|
func ParseURL(rawURL string) (u *url.URL, err error) {
|
|
if !protocolRe.MatchString(rawURL) &&
|
|
strings.Contains(rawURL, ":") &&
|
|
// not a Windows path
|
|
!strings.Contains(rawURL, "\\") {
|
|
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 != "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
|
|
}
|