-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow lib users to consume parsed git url
- Loading branch information
Showing
5 changed files
with
51 additions
and
51 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 |
---|---|---|
|
@@ -25,11 +25,11 @@ var ( | |
|
||
// GitURL represents parsed git url | ||
type GitURL struct { | ||
scheme string // value will be either 'scp', 'ssh', 'https' or 'local' | ||
user string // might be empty for http and local urls | ||
host string // host or host:port | ||
path string // path to the repo | ||
repo string // repository name from the path includes .git | ||
Scheme string // value will be either 'scp', 'ssh', 'https' or 'local' | ||
User string // might be empty for http and local urls | ||
Host string // host or host:port | ||
Path string // path to the repo | ||
Repo string // repository name from the path includes .git | ||
} | ||
|
||
// NormaliseURL will return normalised url | ||
|
@@ -55,29 +55,29 @@ func ParseGitURL(rawURL string) (*GitURL, error) { | |
switch { | ||
case isSCPURL(rawURL): | ||
sections = scpURLRgx.FindStringSubmatch(rawURL) | ||
gURL.scheme = "scp" | ||
gURL.user = sections[scpURLRgx.SubexpIndex("user")] | ||
gURL.host = sections[scpURLRgx.SubexpIndex("host")] | ||
gURL.path = sections[scpURLRgx.SubexpIndex("path")] | ||
gURL.repo = sections[scpURLRgx.SubexpIndex("repo")] | ||
gURL.Scheme = "scp" | ||
gURL.User = sections[scpURLRgx.SubexpIndex("user")] | ||
gURL.Host = sections[scpURLRgx.SubexpIndex("host")] | ||
gURL.Path = sections[scpURLRgx.SubexpIndex("path")] | ||
gURL.Repo = sections[scpURLRgx.SubexpIndex("repo")] | ||
case isSSHURL(rawURL): | ||
sections = sshURLRgx.FindStringSubmatch(rawURL) | ||
gURL.scheme = "ssh" | ||
gURL.user = sections[sshURLRgx.SubexpIndex("user")] | ||
gURL.host = sections[sshURLRgx.SubexpIndex("host")] | ||
gURL.path = sections[sshURLRgx.SubexpIndex("path")] | ||
gURL.repo = sections[sshURLRgx.SubexpIndex("repo")] | ||
gURL.Scheme = "ssh" | ||
gURL.User = sections[sshURLRgx.SubexpIndex("user")] | ||
gURL.Host = sections[sshURLRgx.SubexpIndex("host")] | ||
gURL.Path = sections[sshURLRgx.SubexpIndex("path")] | ||
gURL.Repo = sections[sshURLRgx.SubexpIndex("repo")] | ||
case isHTTPSURL(rawURL): | ||
sections = httpsURLRgx.FindStringSubmatch(rawURL) | ||
gURL.scheme = "https" | ||
gURL.host = sections[httpsURLRgx.SubexpIndex("host")] | ||
gURL.path = sections[httpsURLRgx.SubexpIndex("path")] | ||
gURL.repo = sections[httpsURLRgx.SubexpIndex("repo")] | ||
gURL.Scheme = "https" | ||
gURL.Host = sections[httpsURLRgx.SubexpIndex("host")] | ||
gURL.Path = sections[httpsURLRgx.SubexpIndex("path")] | ||
gURL.Repo = sections[httpsURLRgx.SubexpIndex("repo")] | ||
case isLocalURL(rawURL): | ||
sections = localURLRgx.FindStringSubmatch(rawURL) | ||
gURL.scheme = "local" | ||
gURL.path = sections[localURLRgx.SubexpIndex("path")] | ||
gURL.repo = sections[localURLRgx.SubexpIndex("repo")] | ||
gURL.Scheme = "local" | ||
gURL.Path = sections[localURLRgx.SubexpIndex("path")] | ||
gURL.Repo = sections[localURLRgx.SubexpIndex("repo")] | ||
default: | ||
return nil, fmt.Errorf( | ||
"provided '%s' remote url is invalid, supported urls are '[email protected]:path/to/repo.git','ssh://[email protected]/path/to/repo.git' or 'https://host.xz/path/to/repo.git'", | ||
|
@@ -86,12 +86,12 @@ func ParseGitURL(rawURL string) (*GitURL, error) { | |
|
||
// scp path doesn't have leading "/" | ||
// also removing training "/" for consistency | ||
gURL.path = strings.Trim(gURL.path, "/") | ||
gURL.Path = strings.Trim(gURL.Path, "/") | ||
|
||
if gURL.path == "" { | ||
if gURL.Path == "" { | ||
return nil, fmt.Errorf("repo path (org) cannot be empty") | ||
} | ||
if gURL.repo == "" || gURL.repo == ".git" { | ||
if gURL.Repo == "" || gURL.Repo == ".git" { | ||
return nil, fmt.Errorf("repo name is invalid") | ||
} | ||
|
||
|
@@ -102,9 +102,9 @@ func ParseGitURL(rawURL string) (*GitURL, error) { | |
// git URLs can be represented in multiple schemes so if host, path and repo name | ||
// of URLs are same then those URLs are for the same remote repository | ||
func SameURL(lURL, rURL *GitURL) bool { | ||
return lURL.host == rURL.host && | ||
lURL.path == rURL.path && | ||
lURL.repo == rURL.repo | ||
return lURL.Host == rURL.Host && | ||
lURL.Path == rURL.Path && | ||
lURL.Repo == rURL.Repo | ||
} | ||
|
||
// SameRawURL returns whether or not the two remote URL strings are equivalent | ||
|
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 |
---|---|---|
|
@@ -16,55 +16,55 @@ func TestParseGitURL(t *testing.T) { | |
}{ | ||
{"1", | ||
"[email protected]:path/to/repo.git", | ||
&GitURL{scheme: "scp", user: "user", host: "host.xz", path: "path/to", repo: "repo.git"}, | ||
&GitURL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"}, | ||
false, | ||
}, | ||
{"2", | ||
"[email protected]:org/repo", | ||
&GitURL{scheme: "scp", user: "git", host: "github.com", path: "org", repo: "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"}, | ||
&GitURL{Scheme: "ssh", User: "user", Host: "host.xz:123", Path: "path/to", Repo: "repo.git"}, | ||
false}, | ||
{"4", | ||
"ssh://[email protected]/org/repo", | ||
&GitURL{scheme: "ssh", user: "git", host: "github.com", path: "org", repo: "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"}, | ||
&GitURL{Scheme: "https", Host: "host.xz:345", Path: "path/to", Repo: "repo.git"}, | ||
false}, | ||
{"6", | ||
"https://github.com/org/repo", | ||
&GitURL{scheme: "https", host: "github.com", path: "org", repo: "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"}, | ||
&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"}, | ||
&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"}, | ||
&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"}, | ||
&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"}, | ||
&GitURL{Scheme: "local", Path: "path-with_.x/to", Repo: "prr.test_test-repo3.git"}, | ||
false, | ||
}, | ||
|
||
|
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 |
---|---|---|
|
@@ -33,7 +33,7 @@ func TestNewRepo(t *testing.T) { | |
gc: "always", | ||
}, | ||
&Repository{ | ||
gitURL: &GitURL{scheme: "scp", user: "user", host: "host.xz", path: "path/to", repo: "repo.git"}, | ||
gitURL: &GitURL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"}, | ||
remote: "[email protected]:path/to/repo.git", | ||
root: "/tmp", | ||
dir: "/tmp/repo.git", | ||
|
@@ -119,7 +119,7 @@ func TestNewRepo(t *testing.T) { | |
|
||
func TestRepo_AddWorktreeLink(t *testing.T) { | ||
r := &Repository{ | ||
gitURL: &GitURL{scheme: "scp", user: "user", host: "host.xz", path: "path/to", repo: "repo.git"}, | ||
gitURL: &GitURL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"}, | ||
root: "/tmp/root", | ||
interval: 10 * time.Second, | ||
auth: nil, | ||
|