Skip to content

Commit 3b79a1d

Browse files
authored
Merge pull request #16 from utilitywarehouse/as-git-url
split URL package for easy import
2 parents 10bd34f + 2e11fa5 commit 3b79a1d

File tree

6 files changed

+51
-47
lines changed

6 files changed

+51
-47
lines changed

pkg/mirror/git_url.go renamed to pkg/giturl/git_url.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mirror
1+
package giturl
22

33
import (
44
"fmt"
@@ -23,8 +23,8 @@ var (
2323
localURLRgx = regexp.MustCompile(`^file:///(?P<path>([\w\-\.]+\/)*)(?P<repo>[\w\-\.]+(\.git)?)$`)
2424
)
2525

26-
// GitURL represents parsed git url
27-
type GitURL struct {
26+
// URL represents parsed git url
27+
type URL struct {
2828
Scheme string // value will be either 'scp', 'ssh', 'https' or 'local'
2929
User string // might be empty for http and local urls
3030
Host string // host or host:port
@@ -40,40 +40,40 @@ func NormaliseURL(rawURL string) string {
4040
return nURL
4141
}
4242

43-
// ParseGitURL parses a raw url into a GitURL structure.
43+
// Parse parses a raw url into a GitURL structure.
4444
// valid git urls are...
4545
// - [email protected]:path/to/repo.git
4646
// - ssh://[email protected][:port]/path/to/repo.git
4747
// - https://host.xz[:port]/path/to/repo.git
48-
func ParseGitURL(rawURL string) (*GitURL, error) {
49-
gURL := &GitURL{}
48+
func Parse(rawURL string) (*URL, error) {
49+
gURL := &URL{}
5050

5151
rawURL = NormaliseURL(rawURL)
5252

5353
var sections []string
5454

5555
switch {
56-
case isSCPURL(rawURL):
56+
case IsSCPURL(rawURL):
5757
sections = scpURLRgx.FindStringSubmatch(rawURL)
5858
gURL.Scheme = "scp"
5959
gURL.User = sections[scpURLRgx.SubexpIndex("user")]
6060
gURL.Host = sections[scpURLRgx.SubexpIndex("host")]
6161
gURL.Path = sections[scpURLRgx.SubexpIndex("path")]
6262
gURL.Repo = sections[scpURLRgx.SubexpIndex("repo")]
63-
case isSSHURL(rawURL):
63+
case IsSSHURL(rawURL):
6464
sections = sshURLRgx.FindStringSubmatch(rawURL)
6565
gURL.Scheme = "ssh"
6666
gURL.User = sections[sshURLRgx.SubexpIndex("user")]
6767
gURL.Host = sections[sshURLRgx.SubexpIndex("host")]
6868
gURL.Path = sections[sshURLRgx.SubexpIndex("path")]
6969
gURL.Repo = sections[sshURLRgx.SubexpIndex("repo")]
70-
case isHTTPSURL(rawURL):
70+
case IsHTTPSURL(rawURL):
7171
sections = httpsURLRgx.FindStringSubmatch(rawURL)
7272
gURL.Scheme = "https"
7373
gURL.Host = sections[httpsURLRgx.SubexpIndex("host")]
7474
gURL.Path = sections[httpsURLRgx.SubexpIndex("path")]
7575
gURL.Repo = sections[httpsURLRgx.SubexpIndex("repo")]
76-
case isLocalURL(rawURL):
76+
case IsLocalURL(rawURL):
7777
sections = localURLRgx.FindStringSubmatch(rawURL)
7878
gURL.Scheme = "local"
7979
gURL.Path = sections[localURLRgx.SubexpIndex("path")]
@@ -101,42 +101,42 @@ func ParseGitURL(rawURL string) (*GitURL, error) {
101101
// SameURL returns whether or not the two parsed git URLs are equivalent.
102102
// git URLs can be represented in multiple schemes so if host, path and repo name
103103
// of URLs are same then those URLs are for the same remote repository
104-
func SameURL(lURL, rURL *GitURL) bool {
104+
func SameURL(lURL, rURL *URL) bool {
105105
return lURL.Host == rURL.Host &&
106106
lURL.Path == rURL.Path &&
107107
lURL.Repo == rURL.Repo
108108
}
109109

110110
// SameRawURL returns whether or not the two remote URL strings are equivalent
111111
func SameRawURL(lRepo, rRepo string) (bool, error) {
112-
lURL, err := ParseGitURL(lRepo)
112+
lURL, err := Parse(lRepo)
113113
if err != nil {
114114
return false, err
115115
}
116-
rURL, err := ParseGitURL(rRepo)
116+
rURL, err := Parse(rRepo)
117117
if err != nil {
118118
return false, err
119119
}
120120

121121
return SameURL(lURL, rURL), nil
122122
}
123123

124-
// isSCPURL returns true if supplied URL is scp-like syntax
125-
func isSCPURL(rawURL string) bool {
124+
// IsSCPURL returns true if supplied URL is scp-like syntax
125+
func IsSCPURL(rawURL string) bool {
126126
return scpURLRgx.MatchString(rawURL)
127127
}
128128

129-
// isSSHURL returns true if supplied URL is SSH URL
130-
func isSSHURL(rawURL string) bool {
129+
// IsSSHURL returns true if supplied URL is SSH URL
130+
func IsSSHURL(rawURL string) bool {
131131
return sshURLRgx.MatchString(rawURL)
132132
}
133133

134-
// isHTTPSURL returns true if supplied URL is HTTPS URL
135-
func isHTTPSURL(rawURL string) bool {
134+
// IsHTTPSURL returns true if supplied URL is HTTPS URL
135+
func IsHTTPSURL(rawURL string) bool {
136136
return httpsURLRgx.MatchString(rawURL)
137137
}
138138

139-
// isLocalURL returns true if supplied URL is HTTPS URL
140-
func isLocalURL(rawURL string) bool {
139+
// IsLocalURL returns true if supplied URL is HTTPS URL
140+
func IsLocalURL(rawURL string) bool {
141141
return localURLRgx.MatchString(rawURL)
142142
}

pkg/mirror/git_url_test.go renamed to pkg/giturl/git_url_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mirror
1+
package giturl
22

33
import (
44
"testing"
@@ -11,60 +11,60 @@ func TestParseGitURL(t *testing.T) {
1111
tests := []struct {
1212
name string
1313
rawURL string
14-
want *GitURL
14+
want *URL
1515
wantErr bool
1616
}{
1717
{"1",
1818
"[email protected]:path/to/repo.git",
19-
&GitURL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"},
19+
&URL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"},
2020
false,
2121
},
2222
{"2",
2323
"[email protected]:org/repo",
24-
&GitURL{Scheme: "scp", User: "git", Host: "github.com", Path: "org", Repo: "repo"},
24+
&URL{Scheme: "scp", User: "git", Host: "github.com", Path: "org", Repo: "repo"},
2525
false},
2626
{"3",
2727
"ssh://[email protected]:123/path/to/repo.git",
28-
&GitURL{Scheme: "ssh", User: "user", Host: "host.xz:123", Path: "path/to", Repo: "repo.git"},
28+
&URL{Scheme: "ssh", User: "user", Host: "host.xz:123", Path: "path/to", Repo: "repo.git"},
2929
false},
3030
{"4",
3131
"ssh://[email protected]/org/repo",
32-
&GitURL{Scheme: "ssh", User: "git", Host: "github.com", Path: "org", Repo: "repo"},
32+
&URL{Scheme: "ssh", User: "git", Host: "github.com", Path: "org", Repo: "repo"},
3333
false},
3434
{"5",
3535
"https://host.xz:345/path/to/repo.git",
36-
&GitURL{Scheme: "https", Host: "host.xz:345", Path: "path/to", Repo: "repo.git"},
36+
&URL{Scheme: "https", Host: "host.xz:345", Path: "path/to", Repo: "repo.git"},
3737
false},
3838
{"6",
3939
"https://github.com/org/repo",
40-
&GitURL{Scheme: "https", Host: "github.com", Path: "org", Repo: "repo"},
40+
&URL{Scheme: "https", Host: "github.com", Path: "org", Repo: "repo"},
4141
false},
4242
{"7",
4343
"https://host.xz:123/path/to/repo.git",
44-
&GitURL{Scheme: "https", Host: "host.xz:123", Path: "path/to", Repo: "repo.git"},
44+
&URL{Scheme: "https", Host: "host.xz:123", Path: "path/to", Repo: "repo.git"},
4545
false},
4646
{
4747
"valid-special-char-scp",
4848
"user.name-with_@host-with_x.xz:123:path-with_.x/to/prr.test_test-repo0.git",
49-
&GitURL{Scheme: "scp", User: "user.name-with_", Host: "host-with_x.xz:123", Path: "path-with_.x/to", Repo: "prr.test_test-repo0.git"},
49+
&URL{Scheme: "scp", User: "user.name-with_", Host: "host-with_x.xz:123", Path: "path-with_.x/to", Repo: "prr.test_test-repo0.git"},
5050
false,
5151
},
5252
{
5353
"valid-special-char-ssh",
5454
"ssh://user.name-with_@host-with_x.xz:123/path-with_.x/to/prr.test_test-repo1.git",
55-
&GitURL{Scheme: "ssh", User: "user.name-with_", Host: "host-with_x.xz:123", Path: "path-with_.x/to", Repo: "prr.test_test-repo1.git"},
55+
&URL{Scheme: "ssh", User: "user.name-with_", Host: "host-with_x.xz:123", Path: "path-with_.x/to", Repo: "prr.test_test-repo1.git"},
5656
false,
5757
},
5858
{
5959
"valid-special-char-https",
6060
"https://host-with_x.xz:123/path-with_.x/to/prr.test_test-repo2.git",
61-
&GitURL{Scheme: "https", Host: "host-with_x.xz:123", Path: "path-with_.x/to", Repo: "prr.test_test-repo2.git"},
61+
&URL{Scheme: "https", Host: "host-with_x.xz:123", Path: "path-with_.x/to", Repo: "prr.test_test-repo2.git"},
6262
false,
6363
},
6464
{
6565
"valid-special-char-local",
6666
"file:///path-with_.x/to/prr.test_test-repo3.git",
67-
&GitURL{Scheme: "local", Path: "path-with_.x/to", Repo: "prr.test_test-repo3.git"},
67+
&URL{Scheme: "local", Path: "path-with_.x/to", Repo: "prr.test_test-repo3.git"},
6868
false,
6969
},
7070

@@ -97,12 +97,12 @@ func TestParseGitURL(t *testing.T) {
9797
}
9898
for _, tt := range tests {
9999
t.Run(tt.name, func(t *testing.T) {
100-
got, err := ParseGitURL(tt.rawURL)
100+
got, err := Parse(tt.rawURL)
101101
if (err != nil) != tt.wantErr {
102102
t.Errorf("ParseGitURL() error = %v, wantErr %v", err, tt.wantErr)
103103
return
104104
}
105-
if diff := cmp.Diff(tt.want, got, cmpopts.EquateComparable(GitURL{})); diff != "" {
105+
if diff := cmp.Diff(tt.want, got, cmpopts.EquateComparable(URL{})); diff != "" {
106106
t.Errorf("ParseGitURL() mismatch (-want +got):\n%s", diff)
107107
}
108108
})
Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/mirror/repo_pool.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"log/slog"
77
"time"
8+
9+
"github.com/utilitywarehouse/git-mirror/pkg/giturl"
810
)
911

1012
var (
@@ -97,13 +99,13 @@ func (rp *RepoPool) StartLoop() {
9799

98100
// Repository will return Repository object based on given remote URL
99101
func (rp *RepoPool) Repository(remote string) (*Repository, error) {
100-
gitURL, err := ParseGitURL(remote)
102+
gitURL, err := giturl.Parse(remote)
101103
if err != nil {
102104
return nil, err
103105
}
104106

105107
for _, repo := range rp.repos {
106-
if SameURL(repo.gitURL, gitURL) {
108+
if giturl.SameURL(repo.gitURL, gitURL) {
107109
return repo, nil
108110
}
109111
}

pkg/mirror/repository.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"strings"
1414
"sync"
1515
"time"
16+
17+
"github.com/utilitywarehouse/git-mirror/pkg/giturl"
1618
)
1719

1820
const (
@@ -47,7 +49,7 @@ const (
4749
// The implementation borrows heavily from https://github.com/kubernetes/git-sync.
4850
// A Repository is safe for concurrent use by multiple goroutines.
4951
type Repository struct {
50-
gitURL *GitURL // parsed remote git URL
52+
gitURL *giturl.URL // parsed remote git URL
5153
remote string // remote repo to mirror
5254
root string // absolute path to the root where repo directory createdabsolute path to the root where repo directory created
5355
dir string // absolute path to the repo directory
@@ -66,9 +68,9 @@ type Repository struct {
6668
// NewRepository creates new repository from the given config.
6769
// Remote repo will not be mirrored until either Mirror() or StartLoop() is called.
6870
func NewRepository(repoConf RepositoryConfig, envs []string, log *slog.Logger) (*Repository, error) {
69-
remoteURL := NormaliseURL(repoConf.Remote)
71+
remoteURL := giturl.NormaliseURL(repoConf.Remote)
7072

71-
gURL, err := ParseGitURL(remoteURL)
73+
gURL, err := giturl.Parse(remoteURL)
7274
if err != nil {
7375
return nil, err
7476
}
@@ -506,7 +508,7 @@ func (r *Repository) init(ctx context.Context) error {
506508
// and parse output to get default branch name
507509
func (r *Repository) getRemoteDefaultBranch(ctx context.Context) (string, error) {
508510
envs := []string{}
509-
if isSCPURL(r.remote) || isSSHURL(r.remote) {
511+
if giturl.IsSCPURL(r.remote) || giturl.IsSSHURL(r.remote) {
510512
envs = append(envs, r.auth.gitSSHCommand())
511513
}
512514

@@ -598,7 +600,7 @@ func (r *Repository) fetch(ctx context.Context) ([]string, error) {
598600
args := []string{"fetch", "origin", "--prune", "--no-progress", "--porcelain", "--no-auto-gc"}
599601

600602
envs := []string{}
601-
if isSCPURL(r.remote) || isSSHURL(r.remote) {
603+
if giturl.IsSCPURL(r.remote) || giturl.IsSSHURL(r.remote) {
602604
envs = append(envs, r.auth.gitSSHCommand())
603605
}
604606

pkg/mirror/repository_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/google/go-cmp/cmp"
99
"github.com/google/go-cmp/cmp/cmpopts"
10+
"github.com/utilitywarehouse/git-mirror/pkg/giturl"
1011
)
1112

1213
func TestNewRepo(t *testing.T) {
@@ -33,7 +34,7 @@ func TestNewRepo(t *testing.T) {
3334
gc: "always",
3435
},
3536
&Repository{
36-
gitURL: &GitURL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"},
37+
gitURL: &giturl.URL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"},
3738
remote: "[email protected]:path/to/repo.git",
3839
root: "/tmp",
3940
dir: "/tmp/repo.git",
@@ -110,7 +111,7 @@ func TestNewRepo(t *testing.T) {
110111
t.Errorf("NewRepository() error = %v, wantErr %v", err, tt.wantErr)
111112
return
112113
}
113-
if diff := cmp.Diff(tt.want, got, cmpopts.IgnoreFields(Repository{}, "log", "lock", "stop", "stopped"), cmp.AllowUnexported(Repository{}, GitURL{})); diff != "" {
114+
if diff := cmp.Diff(tt.want, got, cmpopts.IgnoreFields(Repository{}, "log", "lock", "stop", "stopped"), cmp.AllowUnexported(Repository{}, giturl.URL{})); diff != "" {
114115
t.Errorf("NewRepository() mismatch (-want +got):\n%s", diff)
115116
}
116117
})
@@ -119,7 +120,7 @@ func TestNewRepo(t *testing.T) {
119120

120121
func TestRepo_AddWorktreeLink(t *testing.T) {
121122
r := &Repository{
122-
gitURL: &GitURL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"},
123+
gitURL: &giturl.URL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"},
123124
root: "/tmp/root",
124125
interval: 10 * time.Second,
125126
auth: nil,

0 commit comments

Comments
 (0)