diff --git a/pkg/repo.go b/pkg/repo.go index 0575b14..1466488 100644 --- a/pkg/repo.go +++ b/pkg/repo.go @@ -14,7 +14,17 @@ type Repo struct { Status *RepoStatus } -func CloneRepo(url string, path string) error { +func CloneRepo(url string, repoRoot string) (path string, err error) { + repoPath, err := URLToPath(url) + if err != nil { + return path, err + } + + path, err = MakeDir(repoRoot, repoPath) + if err != nil { + return path, err + } + options := &git.CloneOptions{ CheckoutOpts: nil, FetchOptions: nil, @@ -23,11 +33,11 @@ func CloneRepo(url string, path string) error { RemoteCreateCallback: nil, } - _, err := git.Clone(url, path, options) + _, err = git.Clone(url, path, options) if err != nil { - return errors.Wrap(err, "Failed cloning repo") + return path, errors.Wrap(err, "Failed cloning repo") } - return nil + return path, nil } func OpenRepo(path string) (*Repo, error) { diff --git a/pkg/repo_test.go b/pkg/repo_test.go index f7374fb..0bc48da 100644 --- a/pkg/repo_test.go +++ b/pkg/repo_test.go @@ -13,12 +13,12 @@ func TestFetch(t *testing.T) { createCommit(t, origin, "Initial commit") // Clone the origin repo - dir := newTempDir(t) - err := CloneRepo(origin.Path(), dir) + repoRoot := newTempDir(t) + path, err := CloneRepo(origin.Path(), repoRoot) checkFatal(t, err) // Open cloned repo and load its status - repo, err := OpenRepo(dir) + repo, err := OpenRepo(path) checkFatal(t, err) // Check cloned status. It should not be behind origin diff --git a/pkg/status_test.go b/pkg/status_test.go index d09bd27..0875735 100644 --- a/pkg/status_test.go +++ b/pkg/status_test.go @@ -157,11 +157,11 @@ func TestStatusWithMultipleCommits(t *testing.T) { func TestStatusCloned(t *testing.T) { origin := newTestRepo(t) - dir := newTempDir(t) + repoRoot := newTempDir(t) - err := CloneRepo(origin.Path(), dir) + path, err := CloneRepo(origin.Path(), repoRoot) checkFatal(t, err) - repo, err := OpenRepo(dir) + repo, err := OpenRepo(path) checkFatal(t, err) status, err := loadStatus(repo.repo) @@ -212,10 +212,10 @@ func TestBranchCloned(t *testing.T) { createBranch(t, origin, "branch") - dir := newTempDir(t) - err := CloneRepo(origin.Path(), dir) + repoRoot := newTempDir(t) + path, err := CloneRepo(origin.Path(), repoRoot) checkFatal(t, err) - repo, err := OpenRepo(dir) + repo, err := OpenRepo(path) checkFatal(t, err) createBranch(t, repo.repo, "local") diff --git a/pkg/url.go b/pkg/url.go index f98824c..ffee682 100644 --- a/pkg/url.go +++ b/pkg/url.go @@ -13,7 +13,7 @@ import ( // See: https://golang.org/src/cmd/go/internal/get/vcs.go var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`) -func URLToPath(rawurl string) (string, error) { +func URLToPath(rawurl string) (repoPath string, err error) { url, err := parseURL(rawurl) if err != nil { return "", err @@ -23,13 +23,13 @@ func URLToPath(rawurl string) (string, error) { repoHost := strings.Split(url.Host, ":")[0] // remove trailing ".git" from repo name - localPath := path.Join(repoHost, url.Path) - localPath = strings.TrimSuffix(localPath, ".git") + repoPath = path.Join(repoHost, url.Path) + repoPath = strings.TrimSuffix(repoPath, ".git") // remove tilde (~) char from username - localPath = strings.ReplaceAll(localPath, "~", "") + repoPath = strings.ReplaceAll(repoPath, "~", "") - return localPath, nil + return repoPath, nil } func parseURL(rawurl string) (url *urlpkg.URL, err error) {