-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] split query param to ref|tag|sha (#98)
* split query param to ref|tag|sha
- Loading branch information
Showing
10 changed files
with
375 additions
and
474 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
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 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 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 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 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") | ||
|
@@ -113,10 +119,11 @@ func (o *CloneOptions) Parse() { | |
var ( | ||
host string | ||
orgRepo string | ||
suffix string | ||
) | ||
|
||
host, orgRepo, o.path, o.revision, _, _, _ = util.ParseGitUrl(o.Repo) | ||
o.url = host + orgRepo | ||
host, orgRepo, o.path, o.revision, suffix = parseGitUrl(o.Repo) | ||
o.url = host + orgRepo + suffix | ||
} | ||
|
||
func (o *CloneOptions) Clone(ctx context.Context) (Repository, fs.FS, error) { | ||
|
@@ -153,7 +160,7 @@ func (o *CloneOptions) URL() string { | |
} | ||
|
||
func (o *CloneOptions) Revision() string { | ||
return o.revision | ||
return plumbing.ReferenceName(o.revision).Short() | ||
} | ||
|
||
func (o *CloneOptions) Path() string { | ||
|
@@ -208,7 +215,7 @@ var clone = func(ctx context.Context, opts *CloneOptions) (*repo, error) { | |
} | ||
|
||
if opts.revision != "" { | ||
cloneOpts.ReferenceName = plumbing.NewBranchReferenceName(opts.revision) | ||
cloneOpts.ReferenceName = plumbing.ReferenceName(opts.revision) | ||
} | ||
|
||
log.G(ctx).WithFields(log.Fields{ | ||
|
@@ -280,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
Oops, something went wrong.