Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repo name can contain ., -, and _ also .git not required #4

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions pkg/mirror/git_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import (
)

var (
// The repository name can contain
// ASCII letters, digits, and the characters ., -, and _.

// [email protected]:path/to/repo.git
scpURLRgx = regexp.MustCompile(`^(?P<user>[a-zA-Z]+)@(?P<host>(\w+\.?\w+)+(\:\d+)?):(?P<path>(\w+\/)*)(?P<repo>\w+.git)$`)
scpURLRgx = regexp.MustCompile(`^(?P<user>[\w\-\.]+)@(?P<host>([\w\-]+\.?[\w\-]+)+(\:\d+)?):(?P<path>([\w\-\.]+\/)*)(?P<repo>[\w\-\.]+(\.git)?)$`)

// ssh://[email protected][:port]/path/to/repo.git
sshURLRgx = regexp.MustCompile(`^ssh://(?P<user>[a-zA-Z]+)@(?P<host>(\w+\.?\w+)+(\:\d+)??)/(?P<path>(\w+\/)*)(?P<repo>\w+.git)$`)
sshURLRgx = regexp.MustCompile(`^ssh://(?P<user>[\w\-\.]+)@(?P<host>([\w\-]+\.?[\w\-]+)+(\:\d+)??)/(?P<path>([\w\-\.]+\/)*)(?P<repo>[\w\-\.]+(\.git)?)$`)

// https://host.xz[:port]/path/to/repo.git
httpsURLRgx = regexp.MustCompile(`^https://(?P<host>(\w+\.?\w+)+(\:\d+)?)/(?P<path>(\w+\/)*)(?P<repo>\w+.git)$`)
httpsURLRgx = regexp.MustCompile(`^https://(?P<host>([\w\-]+\.?[\w\-]+)+(\:\d+)?)/(?P<path>([\w\-\.]+\/)*)(?P<repo>[\w\-\.]+(\.git)?)$`)

// file:///path/to/repo.git
localURLRgx = regexp.MustCompile(`^file:///(?P<path>([\w-]+\/)*)(?P<repo>\w+.git)$`)
localURLRgx = regexp.MustCompile(`^file:///(?P<path>([\w\-\.]+\/)*)(?P<repo>[\w\-\.]+(\.git)?)$`)
)

type GitURL struct {
Expand All @@ -32,10 +35,6 @@ func NormaliseURL(rawURL string) string {
nURL := strings.ToLower(strings.TrimSpace(rawURL))
nURL = strings.TrimRight(nURL, "/")

// add .git suffix if needed
if !strings.HasSuffix(nURL, ".git") {
nURL = nURL + ".git"
}
return nURL
}

Expand Down Expand Up @@ -83,6 +82,13 @@ func ParseGitURL(rawURL string) (*GitURL, error) {
// also removing training "/" for consistency
gURL.path = strings.Trim(gURL.path, "/")

if gURL.path == "" {
return nil, fmt.Errorf("repo path (org) cannot be empty")
}
if gURL.repo == "" || gURL.repo == ".git" {
return nil, fmt.Errorf("repo name is invalid")
}

return gURL, nil
}

Expand Down
36 changes: 30 additions & 6 deletions pkg/mirror/git_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,53 @@ func TestParseGitURL(t *testing.T) {
false,
},
{"2",
"[email protected]:org/repo.git",
&GitURL{scheme: "scp", user: "git", host: "github.com", path: "org", repo: "repo.git"},
"[email protected]:org/repo",
&GitURL{scheme: "scp", user: "git", host: "github.com", path: "org", repo: "repo"},
false},
{"3",
"ssh://[email protected]:123/path/to/repo.git",
&GitURL{scheme: "ssh", user: "user", host: "host.xz:123", path: "path/to", repo: "repo.git"},
false},
{"4",
"ssh://[email protected]/org/repo.git",
&GitURL{scheme: "ssh", user: "git", host: "github.com", path: "org", repo: "repo.git"},
"ssh://[email protected]/org/repo",
&GitURL{scheme: "ssh", user: "git", host: "github.com", path: "org", repo: "repo"},
false},
{"5",
"https://host.xz:345/path/to/repo.git",
&GitURL{scheme: "https", host: "host.xz:345", path: "path/to", repo: "repo.git"},
false},
{"6",
"https://github.com/org/repo.git",
&GitURL{scheme: "https", host: "github.com", path: "org", repo: "repo.git"},
"https://github.com/org/repo",
&GitURL{scheme: "https", host: "github.com", path: "org", repo: "repo"},
false},
{"7",
"https://host.xz:123/path/to/repo.git",
&GitURL{scheme: "https", host: "host.xz:123", path: "path/to", repo: "repo.git"},
false},
{
"valid-special-char-scp",
"user.name-with_@host-with_x.xz:123:path-with_.x/to/prr.test_test-repo0.git",
&GitURL{scheme: "scp", user: "user.name-with_", host: "host-with_x.xz:123", path: "path-with_.x/to", repo: "prr.test_test-repo0.git"},
false,
},
{
"valid-special-char-ssh",
"ssh://user.name-with_@host-with_x.xz:123/path-with_.x/to/prr.test_test-repo1.git",
&GitURL{scheme: "ssh", user: "user.name-with_", host: "host-with_x.xz:123", path: "path-with_.x/to", repo: "prr.test_test-repo1.git"},
false,
},
{
"valid-special-char-https",
"https://host-with_x.xz:123/path-with_.x/to/prr.test_test-repo2.git",
&GitURL{scheme: "https", host: "host-with_x.xz:123", path: "path-with_.x/to", repo: "prr.test_test-repo2.git"},
false,
},
{
"valid-special-char-local",
"file:///path-with_.x/to/prr.test_test-repo3.git",
&GitURL{scheme: "local", path: "path-with_.x/to", repo: "prr.test_test-repo3.git"},
false,
},

{"invalid_ssh_hostname", "ssh://[email protected]:org/repo.git", nil, true},
{"invalid_scp_url", "[email protected]/org/repo.git", nil, true},
Expand Down
6 changes: 5 additions & 1 deletion pkg/mirror/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ func NewRepository(repoConf RepositoryConfig, envs []string, log *slog.Logger) (
// hence we can add repo dir (with .git suffix to indicate bare repo) to the provided root.
// this also makes it safe to delete this dir and re-create it if needed
// also this root could have been shared with other mirror repository (repoPool)
repoDir := filepath.Join(repoConf.Root, gURL.repo)
repoDir := gURL.repo
if !strings.HasSuffix(repoDir, ".git") {
repoDir += ".git"
}
repoDir = filepath.Join(repoConf.Root, repoDir)

repo := &Repository{
gitURL: gURL,
Expand Down
8 changes: 4 additions & 4 deletions pkg/mirror/z_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
testUpstreamRepo = "upstream.git"
testUpstreamRepo = "upstream1"
testRoot = "root"
testInterval = 1 * time.Second

Expand Down Expand Up @@ -949,7 +949,7 @@ func Test_RepoPool_Success(t *testing.T) {

upstream1 := filepath.Join(testTmpDir, testUpstreamRepo)
remote1 := "file://" + upstream1
upstream2 := filepath.Join(testTmpDir, "upstream2.git")
upstream2 := filepath.Join(testTmpDir, "upstream2")
remote2 := "file://" + upstream2
root := filepath.Join(testTmpDir, testRoot)

Expand Down Expand Up @@ -1136,7 +1136,7 @@ func Test_RepoPool_Error(t *testing.T) {

upstream1 := filepath.Join(testTmpDir, testUpstreamRepo)
remote1 := "file://" + upstream1
upstream2 := filepath.Join(testTmpDir, "upstream2.git")
upstream2 := filepath.Join(testTmpDir, "upstream2")
remote2 := "file://" + upstream2
root := filepath.Join(testTmpDir, testRoot)

Expand Down Expand Up @@ -1234,7 +1234,7 @@ func TestRepoPool_validateLinkPath(t *testing.T) {

upstream1 := filepath.Join(testTmpDir, testUpstreamRepo)
remote1 := "file://" + upstream1
upstream2 := filepath.Join(testTmpDir, "upstream2.git")
upstream2 := filepath.Join(testTmpDir, "upstream2")
remote2 := "file://" + upstream2
root := filepath.Join(testTmpDir, testRoot)

Expand Down
Loading