mirror of
https://github.com/TECHNOFAB11/jsonnet-bundler.git
synced 2025-12-11 23:50:05 +01:00
feat: generic git https (#73)
Previously, only `github.com` was supported for HTTP cloning of packages. With this change, every git host using the http protocol (GitHub, Gitlab, Gitea) is supported :D
This commit is contained in:
parent
8f14d63f95
commit
0ba0ff5522
4 changed files with 127 additions and 19 deletions
|
|
@ -40,7 +40,17 @@ func TestParseDependency(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "Invalid",
|
||||
path: "github.com/foo",
|
||||
path: "example.com/foo",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "InvalidDomain",
|
||||
path: "example.c/foo/bar",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "InvalidDomain2",
|
||||
path: "examplec/foo/bar",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -33,11 +33,11 @@ type Git struct {
|
|||
|
||||
// Hostname the repo is located at
|
||||
Host string
|
||||
// User (github.com/<user>)
|
||||
// User (example.com/<user>)
|
||||
User string
|
||||
// Repo (github.com/<user>/<repo>)
|
||||
// Repo (example.com/<user>/<repo>)
|
||||
Repo string
|
||||
// Subdir (github.com/<user>/<repo>/<subdir>)
|
||||
// Subdir (example.com/<user>/<repo>/<subdir>)
|
||||
Subdir string
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +75,7 @@ func (gs *Git) UnmarshalJSON(data []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Name returns the repository in a go-like format (github.com/user/repo/subdir)
|
||||
// Name returns the repository in a go-like format (example.com/user/repo/subdir)
|
||||
func (gs *Git) Name() string {
|
||||
return fmt.Sprintf("%s/%s/%s%s", gs.Host, gs.User, gs.Repo, gs.Subdir)
|
||||
}
|
||||
|
|
@ -100,15 +100,13 @@ func (gs *Git) Remote() string {
|
|||
|
||||
// regular expressions for matching package uris
|
||||
const (
|
||||
gitSSHExp = `ssh://git@(?P<host>.+)/(?P<user>.+)/(?P<repo>.+).git`
|
||||
gitSCPExp = `^git@(?P<host>.+):(?P<user>.+)/(?P<repo>.+).git`
|
||||
githubSlugExp = `github.com/(?P<user>[-_a-zA-Z0-9]+)/(?P<repo>[-_a-zA-Z0-9]+)`
|
||||
gitSSHExp = `ssh://git@(?P<host>.+)/(?P<user>.+)/(?P<repo>.+).git`
|
||||
gitSCPExp = `^git@(?P<host>.+):(?P<user>.+)/(?P<repo>.+).git`
|
||||
// The long ugly pattern for ${host} here is a generic pattern for "valid URL with zero or more subdomains and a valid TLD"
|
||||
gitHTTPSExp = `(?P<host>[a-zA-Z0-9][a-zA-Z0-9-\.]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,})/(?P<user>[-_a-zA-Z0-9]+)/(?P<repo>[-_a-zA-Z0-9]+)`
|
||||
)
|
||||
|
||||
var (
|
||||
gitSSHRegex = regexp.MustCompile(gitSSHExp)
|
||||
githubSlugRegex = regexp.MustCompile(githubSlugExp)
|
||||
|
||||
VersionRegex = `@(?P<version>.*)`
|
||||
PathRegex = `/(?P<subdir>.*)`
|
||||
PathAndVersionRegex = `/(?P<subdir>.*)@(?P<version>.*)`
|
||||
|
|
@ -129,10 +127,9 @@ func parseGit(uri string) *Dependency {
|
|||
case reMatch(gitSCPExp, uri):
|
||||
gs, version = match(uri, gitSCPExp)
|
||||
gs.Scheme = GitSchemeSSH
|
||||
case reMatch(githubSlugExp, uri):
|
||||
gs, version = match(uri, githubSlugExp)
|
||||
case reMatch(gitHTTPSExp, uri):
|
||||
gs, version = match(uri, gitHTTPSExp)
|
||||
gs.Scheme = GitSchemeHTTPS
|
||||
gs.Host = "github.com"
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
|
@ -150,7 +147,6 @@ func parseGit(uri string) *Dependency {
|
|||
|
||||
func match(p string, exp string) (gs *Git, version string) {
|
||||
gs = &Git{}
|
||||
|
||||
exps := []*regexp.Regexp{
|
||||
regexp.MustCompile(exp + PathAndVersionRegex),
|
||||
regexp.MustCompile(exp + PathRegex),
|
||||
|
|
|
|||
|
|
@ -67,6 +67,108 @@ func TestParseGit(t *testing.T) {
|
|||
want: sshWant("my.host"),
|
||||
wantRemote: "ssh://git@my.host/user/repo.git", // want ssh format here
|
||||
},
|
||||
{
|
||||
name: "ValidGitHTTPS",
|
||||
uri: "https://example.com/foo/bar",
|
||||
want: &Dependency{
|
||||
Version: "master",
|
||||
Source: Source{
|
||||
GitSource: &Git{
|
||||
Scheme: GitSchemeHTTPS,
|
||||
Host: "example.com",
|
||||
User: "foo",
|
||||
Repo: "bar",
|
||||
Subdir: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
wantRemote: "https://example.com/foo/bar",
|
||||
},
|
||||
{
|
||||
name: "ValidGitNoScheme",
|
||||
uri: "example.com/foo/bar",
|
||||
want: &Dependency{
|
||||
Version: "master",
|
||||
Source: Source{
|
||||
GitSource: &Git{
|
||||
Scheme: GitSchemeHTTPS,
|
||||
Host: "example.com",
|
||||
User: "foo",
|
||||
Repo: "bar",
|
||||
Subdir: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
wantRemote: "https://example.com/foo/bar",
|
||||
},
|
||||
{
|
||||
name: "ValidGitPath",
|
||||
uri: "example.com/foo/bar/baz/bat",
|
||||
want: &Dependency{
|
||||
Version: "master",
|
||||
Source: Source{
|
||||
GitSource: &Git{
|
||||
Scheme: GitSchemeHTTPS,
|
||||
Host: "example.com",
|
||||
User: "foo",
|
||||
Repo: "bar",
|
||||
Subdir: "/baz/bat",
|
||||
},
|
||||
},
|
||||
},
|
||||
wantRemote: "https://example.com/foo/bar",
|
||||
},
|
||||
{
|
||||
name: "ValidGitVersion",
|
||||
uri: "example.com/foo/bar@baz",
|
||||
want: &Dependency{
|
||||
Version: "baz",
|
||||
Source: Source{
|
||||
GitSource: &Git{
|
||||
Scheme: GitSchemeHTTPS,
|
||||
Host: "example.com",
|
||||
User: "foo",
|
||||
Repo: "bar",
|
||||
Subdir: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
wantRemote: "https://example.com/foo/bar",
|
||||
},
|
||||
{
|
||||
name: "ValidGitPathVersion",
|
||||
uri: "example.com/foo/bar/baz@bat",
|
||||
want: &Dependency{
|
||||
Version: "bat",
|
||||
Source: Source{
|
||||
GitSource: &Git{
|
||||
Scheme: GitSchemeHTTPS,
|
||||
Host: "example.com",
|
||||
User: "foo",
|
||||
Repo: "bar",
|
||||
Subdir: "/baz",
|
||||
},
|
||||
},
|
||||
},
|
||||
wantRemote: "https://example.com/foo/bar",
|
||||
},
|
||||
{
|
||||
name: "ValidGitSubdomain",
|
||||
uri: "git.example.com/foo/bar",
|
||||
want: &Dependency{
|
||||
Version: "master",
|
||||
Source: Source{
|
||||
GitSource: &Git{
|
||||
Scheme: GitSchemeHTTPS,
|
||||
Host: "git.example.com",
|
||||
User: "foo",
|
||||
Repo: "bar",
|
||||
Subdir: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
wantRemote: "https://git.example.com/foo/bar",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range tests {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue