Unified git repository management

Signed-off-by: Alvise88 <vitalvise@gmail.com>
This commit is contained in:
Alvise88 2022-01-14 09:52:05 +00:00
parent e60b9dc268
commit f5da25870b
2 changed files with 11 additions and 36 deletions

View File

@ -25,28 +25,24 @@ type Require struct {
func newRequire(repoName, versionConstraint string) (*Require, error) {
switch {
case strings.HasPrefix(repoName, "github.com"):
return parseGithubRepoName(repoName, versionConstraint)
case strings.HasPrefix(repoName, pkg.AlphaModule):
return parseDaggerRepoName(repoName, versionConstraint)
case strings.Contains(repoName, ".git"):
return parseGitRepoName(repoName, versionConstraint)
default:
return nil, fmt.Errorf("repo name does not match suported providers")
return parseGitRepoName(repoName, versionConstraint)
}
}
var githubRepoNameRegex = regexp.MustCompile(`(github.com/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+)([a-zA-Z0-9/_.-]*)@?([0-9a-zA-Z.-]*)`)
var gitRepoNameRegex = regexp.MustCompile(`([a-zA-Z0-9_.-]+(?::\d*)?/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+)([a-zA-Z0-9/_.-]*)@?([0-9a-zA-Z.-]*)`)
func parseGithubRepoName(repoName, versionConstraint string) (*Require, error) {
repoMatches := githubRepoNameRegex.FindStringSubmatch(repoName)
func parseGitRepoName(repoName, versionConstraint string) (*Require, error) {
repoMatches := gitRepoNameRegex.FindStringSubmatch(repoName)
if len(repoMatches) < 4 {
return nil, fmt.Errorf("issue when parsing github repo")
}
return &Require{
repo: repoMatches[1],
repo: strings.TrimSuffix(repoMatches[1], ".git"),
path: repoMatches[2],
version: repoMatches[3],
versionConstraint: versionConstraint,
@ -76,27 +72,6 @@ func parseDaggerRepoName(repoName, versionConstraint string) (*Require, error) {
}, nil
}
// TODO: Get real URL regex
var gitRepoNameRegex = regexp.MustCompile(`^([a-zA-Z0-9_.-]+\.[a-zA-Z0-9]+(?::\d*)?/[a-zA-Z0-9_.-/]+?\.git)([a-zA-Z0-9/_.-]*)?@?([0-9a-zA-Z.-]*)`)
func parseGitRepoName(repoName, versionConstraint string) (*Require, error) {
repoMatches := gitRepoNameRegex.FindStringSubmatch(repoName)
if len(repoMatches) < 3 {
return nil, fmt.Errorf("issue when parsing git repo")
}
return &Require{
repo: repoMatches[1],
path: repoMatches[2],
version: repoMatches[3],
versionConstraint: versionConstraint,
cloneRepo: repoMatches[1],
clonePath: repoMatches[2],
}, nil
}
func (r *Require) String() string {
return fmt.Sprintf("%s@%s", r.fullPath(), r.version)
}

View File

@ -98,7 +98,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider without folder",
in: "git.blocklayer.com/dagger/test.git@main",
want: &Require{
repo: "git.blocklayer.com/dagger/test.git",
repo: "git.blocklayer.com/dagger/test",
path: "",
version: "main",
},
@ -107,7 +107,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider with folder and version",
in: "git.blocklayer.com/dagger/test.git/test@v1.1.0",
want: &Require{
repo: "git.blocklayer.com/dagger/test.git",
repo: "git.blocklayer.com/dagger/test",
path: "/test",
version: "v1.1.0",
},
@ -116,7 +116,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider with folder and version",
in: "git.blocklayer.com/dagger/test.git/test@v1.1.0",
want: &Require{
repo: "git.blocklayer.com/dagger/test.git",
repo: "git.blocklayer.com/dagger/test",
path: "/test",
version: "v1.1.0",
},
@ -125,7 +125,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider without folder",
in: "git.blocklayer.com/dagger/test.git",
want: &Require{
repo: "git.blocklayer.com/dagger/test.git",
repo: "git.blocklayer.com/dagger/test",
path: "",
version: "",
},
@ -134,7 +134,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider with folder, no version",
in: "git.blocklayer.com/dagger/test.git/test",
want: &Require{
repo: "git.blocklayer.com/dagger/test.git",
repo: "git.blocklayer.com/dagger/test",
path: "/test",
version: "",
},
@ -143,7 +143,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider with custom port, folder, and version",
in: "git.blocklayer.com:7999/ops/dagger.git/stuff/here@v5",
want: &Require{
repo: "git.blocklayer.com:7999/ops/dagger.git",
repo: "git.blocklayer.com:7999/ops/dagger",
path: "/stuff/here",
version: "v5",
},