diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2017-05-02 23:05:09 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2017-05-02 23:05:09 +0200 |
| commit | c69e9f604c47469429a6a8a27ba7ef873134e0a1 (patch) | |
| tree | 9f1fe244cde4fde4b792b1136770af1e48f6fa6d /pkg/rtree/remote.go | |
| parent | 72733e8203654cd26464c8c5a6c955b025102cca (diff) | |
| download | gofu-c69e9f604c47469429a6a8a27ba7ef873134e0a1.tar.gz | |
initial implementation of "rtree get" command
Diffstat (limited to 'pkg/rtree/remote.go')
| -rw-r--r-- | pkg/rtree/remote.go | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/pkg/rtree/remote.go b/pkg/rtree/remote.go index 4170717..a2a2ab3 100644 --- a/pkg/rtree/remote.go +++ b/pkg/rtree/remote.go @@ -20,7 +20,9 @@ package rtree import ( "bytes" + "net/url" "os/exec" + "path/filepath" "regexp" "strings" @@ -64,17 +66,41 @@ func init() { // insteadOf = gh: // //and the input "gh:foo/bar", this function returns "git://github.com/foo/bar". -func ExpandRemoteURL(url string) string { +func ExpandRemoteURL(remoteURL string) string { var best *remoteAlias for _, current := range remoteAliases { - if strings.HasPrefix(url, current.Alias) { + if strings.HasPrefix(remoteURL, current.Alias) { if best == nil || len(best.Alias) < len(current.Alias) { best = current } } } if best == nil { - return url + return remoteURL } - return best.Replacement + strings.TrimPrefix(url, best.Alias) + return best.Replacement + strings.TrimPrefix(remoteURL, best.Alias) +} + +//This regex recognizes the scp-like syntax for git remotes +//(i.e. "[user@]example.org:path/to/repo") as specified by the "GIT URLS" +//section of man:git-clone(1). +var scpSyntaxRx = regexp.MustCompile(`^(?:[^/@:]+@)?([^/:]+\.[^/:]+):(.+)$`) + +//CheckoutPathForRemoteURL derives the checkout path for a remote URL that has +//already been expanded with ExpandRemoteURL() if necessary. +// +// "https://example.org/foo/bar" -> "example.org/foo/bar" +// "git@example.org:foo/bar" -> "example.org/foo/bar" +// +func CheckoutPathForRemoteURL(remoteURL string) string { + match := scpSyntaxRx.FindStringSubmatch(remoteURL) + if match != nil { + //match[1] is the hostname, match[2] is the path to the repo + return filepath.Join(match[1], match[2]) + } + + u, err := url.Parse(remoteURL) + util.FatalIfError(err) + + return filepath.Join(u.Hostname(), u.Path) } |
