-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
143 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"errors" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"os" | ||
"strings" | ||
|
||
|
@@ -57,6 +58,11 @@ type ( | |
} | ||
) | ||
|
||
const ( | ||
gitSuffix = ".git" | ||
gitDelimiter = "_git/" | ||
) | ||
|
||
// Errors | ||
var ( | ||
ErrNilOpts = errors.New("options cannot be nil") | ||
|
@@ -116,7 +122,7 @@ func (o *CloneOptions) Parse() { | |
suffix string | ||
) | ||
|
||
host, orgRepo, o.path, o.revision, suffix = util.ParseGitUrl(o.Repo) | ||
host, orgRepo, o.path, o.revision, suffix = parseGitUrl(o.Repo) | ||
o.url = host + orgRepo + suffix | ||
} | ||
|
||
|
@@ -281,3 +287,132 @@ func getAuth(auth Auth) transport.AuthMethod { | |
Password: auth.Password, | ||
} | ||
} | ||
|
||
// From strings like [email protected]:someOrg/someRepo.git or | ||
// https://github.com/someOrg/someRepo?ref=someHash, extract | ||
// the parts. | ||
func parseGitUrl(n string) (host, orgRepo, path, ref, gitSuff string) { | ||
if strings.Contains(n, gitDelimiter) { | ||
index := strings.Index(n, gitDelimiter) | ||
// Adding _git/ to host | ||
host = normalizeGitHostSpec(n[:index+len(gitDelimiter)]) | ||
orgRepo = strings.Split(strings.Split(n[index+len(gitDelimiter):], "/")[0], "?")[0] | ||
path, ref = peelQuery(n[index+len(gitDelimiter)+len(orgRepo):]) | ||
return | ||
} | ||
|
||
host, n = parseHostSpec(n) | ||
gitSuff = gitSuffix | ||
if strings.Contains(n, gitSuffix) { | ||
index := strings.Index(n, gitSuffix) | ||
orgRepo = n[0:index] | ||
n = n[index+len(gitSuffix):] | ||
path, ref = peelQuery(n) | ||
return | ||
} | ||
|
||
i := strings.Index(n, "/") | ||
if i < 1 { | ||
path, ref = peelQuery(n) | ||
return | ||
} | ||
|
||
j := strings.Index(n[i+1:], "/") | ||
if j >= 0 { | ||
j += i + 1 | ||
orgRepo = n[:j] | ||
path, ref = peelQuery(n[j+1:]) | ||
return | ||
} | ||
|
||
path = "" | ||
orgRepo, ref = peelQuery(n) | ||
return | ||
} | ||
|
||
func peelQuery(arg string) (path, ref string) { | ||
parsed, err := url.Parse(arg) | ||
if err != nil { | ||
return path, "" | ||
} | ||
|
||
path = parsed.Path | ||
values := parsed.Query() | ||
branch := values.Get("ref") | ||
tag := values.Get("tag") | ||
sha := values.Get("sha") | ||
if sha != "" { | ||
ref = sha | ||
return | ||
} | ||
|
||
if tag != "" { | ||
ref = "refs/tags/" + tag | ||
return | ||
} | ||
|
||
if branch != "" { | ||
ref = "refs/heads/" + branch | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func parseHostSpec(n string) (string, string) { | ||
var host string | ||
// Start accumulating the host part. | ||
for _, p := range [...]string{ | ||
// Order matters here. | ||
"git::", "gh:", "ssh://", "https://", "http://", | ||
"git@", "github.com:", "github.com/"} { | ||
if len(p) < len(n) && strings.ToLower(n[:len(p)]) == p { | ||
n = n[len(p):] | ||
host += p | ||
} | ||
} | ||
if host == "git@" { | ||
i := strings.Index(n, "/") | ||
if i > -1 { | ||
host += n[:i+1] | ||
n = n[i+1:] | ||
} else { | ||
i = strings.Index(n, ":") | ||
if i > -1 { | ||
host += n[:i+1] | ||
n = n[i+1:] | ||
} | ||
} | ||
return host, n | ||
} | ||
|
||
// If host is a http(s) or ssh URL, grab the domain part. | ||
for _, p := range [...]string{ | ||
"ssh://", "https://", "http://"} { | ||
if strings.HasSuffix(host, p) { | ||
i := strings.Index(n, "/") | ||
if i > -1 { | ||
host = host + n[0:i+1] | ||
n = n[i+1:] | ||
} | ||
break | ||
} | ||
} | ||
|
||
return normalizeGitHostSpec(host), n | ||
} | ||
|
||
func normalizeGitHostSpec(host string) string { | ||
s := strings.ToLower(host) | ||
if strings.Contains(s, "github.com") { | ||
if strings.Contains(s, "git@") || strings.Contains(s, "ssh:") { | ||
host = "[email protected]:" | ||
} else { | ||
host = "https://github.com/" | ||
} | ||
} | ||
if strings.HasPrefix(s, "git::") { | ||
host = strings.TrimPrefix(s, "git::") | ||
} | ||
return host | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.