Skip to content

Commit

Permalink
allow lib users to consume parsed git url
Browse files Browse the repository at this point in the history
  • Loading branch information
asiyani committed Jun 20, 2024
1 parent a32e393 commit 10bd34f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 51 deletions.
56 changes: 28 additions & 28 deletions pkg/mirror/git_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 'user@host.xz:path/to/repo.git','ssh://user@host.xz/path/to/repo.git' or 'https://host.xz/path/to/repo.git'",
Expand All @@ -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")
}

Expand All @@ -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
Expand Down
22 changes: 11 additions & 11 deletions pkg/mirror/git_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,55 @@ func TestParseGitURL(t *testing.T) {
}{
{"1",
"user@host.xz: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",
"git@github.com: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://user@host.xz: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://git@github.com/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,
},

Expand Down
4 changes: 2 additions & 2 deletions pkg/mirror/repo_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (rp *RepoPool) StartLoop() {
go repo.StartLoop(context.TODO())
continue
}
rp.log.Info("start loop is already running", "repo", repo.gitURL.repo)
rp.log.Info("start loop is already running", "repo", repo.gitURL.Repo)
}
}

Expand Down Expand Up @@ -129,7 +129,7 @@ func (rp *RepoPool) validateLinkPath(repo *Repository, link string) error {
for _, wl := range r.workTreeLinks {
if wl.link == newAbsLink {
return fmt.Errorf("repo with overlapping abs link path found repo:%s path:%s",
r.gitURL.repo, wl.link)
r.gitURL.Repo, wl.link)
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/mirror/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewRepository(repoConf RepositoryConfig, envs []string, log *slog.Logger) (
log = slog.Default()
}

log = log.With("repo", gURL.repo)
log = log.With("repo", gURL.Repo)

if !filepath.IsAbs(repoConf.Root) {
return nil, fmt.Errorf("repository root '%s' must be absolute", repoConf.Root)
Expand All @@ -98,7 +98,7 @@ 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 := gURL.repo
repoDir := gURL.Repo
if !strings.HasSuffix(repoDir, ".git") {
repoDir += ".git"
}
Expand Down Expand Up @@ -365,7 +365,7 @@ func (r *Repository) StartLoop(ctx context.Context) {
if err != nil {
r.log.Error("repository mirror failed", "err", err)
}
recordGitMirror(r.gitURL.repo, err == nil)
recordGitMirror(r.gitURL.Repo, err == nil)

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

defer updateMirrorLatency(r.gitURL.repo, time.Now())
defer updateMirrorLatency(r.gitURL.Repo, time.Now())

start := time.Now()

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

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

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

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

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

r.log.Info("mirror cycle complete", "time", time.Since(start), "fetch-time", fetchTime, "updated-refs", len(refs))
Expand Down
4 changes: 2 additions & 2 deletions pkg/mirror/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "user@host.xz:path/to/repo.git",
root: "/tmp",
dir: "/tmp/repo.git",
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 10bd34f

Please sign in to comment.