mirror of
https://github.com/x-motemen/ghq.git
synced 2026-09-10 07:26:27 -04:00
82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"regexp"
|
|
"runtime"
|
|
"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, err = fillUsernameToPath(url.Path)
|
|
if err != nil {
|
|
return url, err
|
|
}
|
|
}
|
|
url.Scheme = "https"
|
|
url.Host = "github.com"
|
|
if url.Path[0] != '/' {
|
|
url.Path = "/" + url.Path
|
|
}
|
|
}
|
|
|
|
return url, nil
|
|
}
|
|
|
|
func ConvertGitURLHTTPToSSH(url *url.URL) (*url.URL, error) {
|
|
user := "git";
|
|
if url.User != nil {
|
|
user = url.User.Username()
|
|
}
|
|
sshURL := fmt.Sprintf("ssh://%s@%s%s", user, url.Host, url.Path)
|
|
return url.Parse(sshURL)
|
|
}
|
|
|
|
func fillUsernameToPath(path string) (string, error) {
|
|
user, err := GitConfigSingle("ghq.user")
|
|
if err != nil {
|
|
return path, err
|
|
}
|
|
if user == "" {
|
|
user = os.Getenv("GITHUB_USER")
|
|
}
|
|
if user == "" {
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
user = os.Getenv("USERNAME")
|
|
default:
|
|
user = os.Getenv("USER")
|
|
}
|
|
}
|
|
if user == "" {
|
|
// Make the error if it does not match any pattern
|
|
return path, fmt.Errorf("set ghq.user to your gitconfig")
|
|
}
|
|
path = user + "/" + path
|
|
return path, nil
|
|
}
|