Skip to content

Commit 10bd34f

Browse files
committed
allow lib users to consume parsed git url
1 parent a32e393 commit 10bd34f

File tree

5 files changed

+51
-51
lines changed

5 files changed

+51
-51
lines changed

pkg/mirror/git_url.go

+28-28
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ var (
2525

2626
// GitURL represents parsed git url
2727
type GitURL struct {
28-
scheme string // value will be either 'scp', 'ssh', 'https' or 'local'
29-
user string // might be empty for http and local urls
30-
host string // host or host:port
31-
path string // path to the repo
32-
repo string // repository name from the path includes .git
28+
Scheme string // value will be either 'scp', 'ssh', 'https' or 'local'
29+
User string // might be empty for http and local urls
30+
Host string // host or host:port
31+
Path string // path to the repo
32+
Repo string // repository name from the path includes .git
3333
}
3434

3535
// NormaliseURL will return normalised url
@@ -55,29 +55,29 @@ func ParseGitURL(rawURL string) (*GitURL, error) {
5555
switch {
5656
case isSCPURL(rawURL):
5757
sections = scpURLRgx.FindStringSubmatch(rawURL)
58-
gURL.scheme = "scp"
59-
gURL.user = sections[scpURLRgx.SubexpIndex("user")]
60-
gURL.host = sections[scpURLRgx.SubexpIndex("host")]
61-
gURL.path = sections[scpURLRgx.SubexpIndex("path")]
62-
gURL.repo = sections[scpURLRgx.SubexpIndex("repo")]
58+
gURL.Scheme = "scp"
59+
gURL.User = sections[scpURLRgx.SubexpIndex("user")]
60+
gURL.Host = sections[scpURLRgx.SubexpIndex("host")]
61+
gURL.Path = sections[scpURLRgx.SubexpIndex("path")]
62+
gURL.Repo = sections[scpURLRgx.SubexpIndex("repo")]
6363
case isSSHURL(rawURL):
6464
sections = sshURLRgx.FindStringSubmatch(rawURL)
65-
gURL.scheme = "ssh"
66-
gURL.user = sections[sshURLRgx.SubexpIndex("user")]
67-
gURL.host = sections[sshURLRgx.SubexpIndex("host")]
68-
gURL.path = sections[sshURLRgx.SubexpIndex("path")]
69-
gURL.repo = sections[sshURLRgx.SubexpIndex("repo")]
65+
gURL.Scheme = "ssh"
66+
gURL.User = sections[sshURLRgx.SubexpIndex("user")]
67+
gURL.Host = sections[sshURLRgx.SubexpIndex("host")]
68+
gURL.Path = sections[sshURLRgx.SubexpIndex("path")]
69+
gURL.Repo = sections[sshURLRgx.SubexpIndex("repo")]
7070
case isHTTPSURL(rawURL):
7171
sections = httpsURLRgx.FindStringSubmatch(rawURL)
72-
gURL.scheme = "https"
73-
gURL.host = sections[httpsURLRgx.SubexpIndex("host")]
74-
gURL.path = sections[httpsURLRgx.SubexpIndex("path")]
75-
gURL.repo = sections[httpsURLRgx.SubexpIndex("repo")]
72+
gURL.Scheme = "https"
73+
gURL.Host = sections[httpsURLRgx.SubexpIndex("host")]
74+
gURL.Path = sections[httpsURLRgx.SubexpIndex("path")]
75+
gURL.Repo = sections[httpsURLRgx.SubexpIndex("repo")]
7676
case isLocalURL(rawURL):
7777
sections = localURLRgx.FindStringSubmatch(rawURL)
78-
gURL.scheme = "local"
79-
gURL.path = sections[localURLRgx.SubexpIndex("path")]
80-
gURL.repo = sections[localURLRgx.SubexpIndex("repo")]
78+
gURL.Scheme = "local"
79+
gURL.Path = sections[localURLRgx.SubexpIndex("path")]
80+
gURL.Repo = sections[localURLRgx.SubexpIndex("repo")]
8181
default:
8282
return nil, fmt.Errorf(
8383
"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) {
8686

8787
// scp path doesn't have leading "/"
8888
// also removing training "/" for consistency
89-
gURL.path = strings.Trim(gURL.path, "/")
89+
gURL.Path = strings.Trim(gURL.Path, "/")
9090

91-
if gURL.path == "" {
91+
if gURL.Path == "" {
9292
return nil, fmt.Errorf("repo path (org) cannot be empty")
9393
}
94-
if gURL.repo == "" || gURL.repo == ".git" {
94+
if gURL.Repo == "" || gURL.Repo == ".git" {
9595
return nil, fmt.Errorf("repo name is invalid")
9696
}
9797

@@ -102,9 +102,9 @@ func ParseGitURL(rawURL string) (*GitURL, error) {
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
104104
func SameURL(lURL, rURL *GitURL) bool {
105-
return lURL.host == rURL.host &&
106-
lURL.path == rURL.path &&
107-
lURL.repo == rURL.repo
105+
return lURL.Host == rURL.Host &&
106+
lURL.Path == rURL.Path &&
107+
lURL.Repo == rURL.Repo
108108
}
109109

110110
// SameRawURL returns whether or not the two remote URL strings are equivalent

pkg/mirror/git_url_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,55 @@ func TestParseGitURL(t *testing.T) {
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+
&GitURL{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+
&GitURL{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+
&GitURL{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+
&GitURL{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+
&GitURL{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+
&GitURL{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+
&GitURL{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+
&GitURL{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+
&GitURL{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+
&GitURL{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+
&GitURL{Scheme: "local", Path: "path-with_.x/to", Repo: "prr.test_test-repo3.git"},
6868
false,
6969
},
7070

pkg/mirror/repo_pool.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (rp *RepoPool) StartLoop() {
9191
go repo.StartLoop(context.TODO())
9292
continue
9393
}
94-
rp.log.Info("start loop is already running", "repo", repo.gitURL.repo)
94+
rp.log.Info("start loop is already running", "repo", repo.gitURL.Repo)
9595
}
9696
}
9797

@@ -129,7 +129,7 @@ func (rp *RepoPool) validateLinkPath(repo *Repository, link string) error {
129129
for _, wl := range r.workTreeLinks {
130130
if wl.link == newAbsLink {
131131
return fmt.Errorf("repo with overlapping abs link path found repo:%s path:%s",
132-
r.gitURL.repo, wl.link)
132+
r.gitURL.Repo, wl.link)
133133
}
134134
}
135135
}

pkg/mirror/repository.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func NewRepository(repoConf RepositoryConfig, envs []string, log *slog.Logger) (
7777
log = slog.Default()
7878
}
7979

80-
log = log.With("repo", gURL.repo)
80+
log = log.With("repo", gURL.Repo)
8181

8282
if !filepath.IsAbs(repoConf.Root) {
8383
return nil, fmt.Errorf("repository root '%s' must be absolute", repoConf.Root)
@@ -98,7 +98,7 @@ func NewRepository(repoConf RepositoryConfig, envs []string, log *slog.Logger) (
9898
// hence we can add repo dir (with .git suffix to indicate bare repo) to the provided root.
9999
// this also makes it safe to delete this dir and re-create it if needed
100100
// also this root could have been shared with other mirror repository (repoPool)
101-
repoDir := gURL.repo
101+
repoDir := gURL.Repo
102102
if !strings.HasSuffix(repoDir, ".git") {
103103
repoDir += ".git"
104104
}
@@ -365,7 +365,7 @@ func (r *Repository) StartLoop(ctx context.Context) {
365365
if err != nil {
366366
r.log.Error("repository mirror failed", "err", err)
367367
}
368-
recordGitMirror(r.gitURL.repo, err == nil)
368+
recordGitMirror(r.gitURL.Repo, err == nil)
369369

370370
t := time.NewTimer(r.interval)
371371
select {
@@ -387,17 +387,17 @@ func (r *Repository) Mirror(ctx context.Context) error {
387387
r.lock.Lock()
388388
defer r.lock.Unlock()
389389

390-
defer updateMirrorLatency(r.gitURL.repo, time.Now())
390+
defer updateMirrorLatency(r.gitURL.Repo, time.Now())
391391

392392
start := time.Now()
393393

394394
if err := r.init(ctx); err != nil {
395-
return fmt.Errorf("unable to init repo:%s err:%w", r.gitURL.repo, err)
395+
return fmt.Errorf("unable to init repo:%s err:%w", r.gitURL.Repo, err)
396396
}
397397

398398
refs, err := r.fetch(ctx)
399399
if err != nil {
400-
return fmt.Errorf("unable to fetch repo:%s err:%w", r.gitURL.repo, err)
400+
return fmt.Errorf("unable to fetch repo:%s err:%w", r.gitURL.Repo, err)
401401
}
402402

403403
fetchTime := time.Since(start)
@@ -406,7 +406,7 @@ func (r *Repository) Mirror(ctx context.Context) error {
406406
// so always ensure worktree even if nothing fetched
407407
for _, wl := range r.workTreeLinks {
408408
if err := r.ensureWorktreeLink(ctx, wl); err != nil {
409-
return fmt.Errorf("unable to ensure worktree links repo:%s link:%s err:%w", r.gitURL.repo, wl.name, err)
409+
return fmt.Errorf("unable to ensure worktree links repo:%s link:%s err:%w", r.gitURL.Repo, wl.name, err)
410410
}
411411
}
412412

@@ -416,7 +416,7 @@ func (r *Repository) Mirror(ctx context.Context) error {
416416
}
417417

418418
if err := r.cleanup(ctx); err != nil {
419-
return fmt.Errorf("unable to cleanup repo:%s err:%w", r.gitURL.repo, err)
419+
return fmt.Errorf("unable to cleanup repo:%s err:%w", r.gitURL.Repo, err)
420420
}
421421

422422
r.log.Info("mirror cycle complete", "time", time.Since(start), "fetch-time", fetchTime, "updated-refs", len(refs))

pkg/mirror/repository_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestNewRepo(t *testing.T) {
3333
gc: "always",
3434
},
3535
&Repository{
36-
gitURL: &GitURL{scheme: "scp", user: "user", host: "host.xz", path: "path/to", repo: "repo.git"},
36+
gitURL: &GitURL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"},
3737
remote: "[email protected]:path/to/repo.git",
3838
root: "/tmp",
3939
dir: "/tmp/repo.git",
@@ -119,7 +119,7 @@ func TestNewRepo(t *testing.T) {
119119

120120
func TestRepo_AddWorktreeLink(t *testing.T) {
121121
r := &Repository{
122-
gitURL: &GitURL{scheme: "scp", user: "user", host: "host.xz", path: "path/to", repo: "repo.git"},
122+
gitURL: &GitURL{Scheme: "scp", User: "user", Host: "host.xz", Path: "path/to", Repo: "repo.git"},
123123
root: "/tmp/root",
124124
interval: 10 * time.Second,
125125
auth: nil,

0 commit comments

Comments
 (0)