6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-09 06:49:26 +00:00

Parse URL and create a repo dir inside CloneRepo function

This commit is contained in:
Grzegorz Dlugoszewski
2020-05-22 12:19:32 +02:00
parent 2bb6f06962
commit 2b622b99fe
4 changed files with 28 additions and 18 deletions

View File

@@ -14,7 +14,17 @@ type Repo struct {
Status *RepoStatus 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{ options := &git.CloneOptions{
CheckoutOpts: nil, CheckoutOpts: nil,
FetchOptions: nil, FetchOptions: nil,
@@ -23,11 +33,11 @@ func CloneRepo(url string, path string) error {
RemoteCreateCallback: nil, RemoteCreateCallback: nil,
} }
_, err := git.Clone(url, path, options) _, err = git.Clone(url, path, options)
if err != nil { 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) { func OpenRepo(path string) (*Repo, error) {

View File

@@ -13,12 +13,12 @@ func TestFetch(t *testing.T) {
createCommit(t, origin, "Initial commit") createCommit(t, origin, "Initial commit")
// Clone the origin repo // Clone the origin repo
dir := newTempDir(t) repoRoot := newTempDir(t)
err := CloneRepo(origin.Path(), dir) path, err := CloneRepo(origin.Path(), repoRoot)
checkFatal(t, err) checkFatal(t, err)
// Open cloned repo and load its status // Open cloned repo and load its status
repo, err := OpenRepo(dir) repo, err := OpenRepo(path)
checkFatal(t, err) checkFatal(t, err)
// Check cloned status. It should not be behind origin // Check cloned status. It should not be behind origin

View File

@@ -157,11 +157,11 @@ func TestStatusWithMultipleCommits(t *testing.T) {
func TestStatusCloned(t *testing.T) { func TestStatusCloned(t *testing.T) {
origin := newTestRepo(t) origin := newTestRepo(t)
dir := newTempDir(t) repoRoot := newTempDir(t)
err := CloneRepo(origin.Path(), dir) path, err := CloneRepo(origin.Path(), repoRoot)
checkFatal(t, err) checkFatal(t, err)
repo, err := OpenRepo(dir) repo, err := OpenRepo(path)
checkFatal(t, err) checkFatal(t, err)
status, err := loadStatus(repo.repo) status, err := loadStatus(repo.repo)
@@ -212,10 +212,10 @@ func TestBranchCloned(t *testing.T) {
createBranch(t, origin, "branch") createBranch(t, origin, "branch")
dir := newTempDir(t) repoRoot := newTempDir(t)
err := CloneRepo(origin.Path(), dir) path, err := CloneRepo(origin.Path(), repoRoot)
checkFatal(t, err) checkFatal(t, err)
repo, err := OpenRepo(dir) repo, err := OpenRepo(path)
checkFatal(t, err) checkFatal(t, err)
createBranch(t, repo.repo, "local") createBranch(t, repo.repo, "local")

View File

@@ -13,7 +13,7 @@ import (
// See: https://golang.org/src/cmd/go/internal/get/vcs.go // See: https://golang.org/src/cmd/go/internal/get/vcs.go
var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`) 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) url, err := parseURL(rawurl)
if err != nil { if err != nil {
return "", err return "", err
@@ -23,13 +23,13 @@ func URLToPath(rawurl string) (string, error) {
repoHost := strings.Split(url.Host, ":")[0] repoHost := strings.Split(url.Host, ":")[0]
// remove trailing ".git" from repo name // remove trailing ".git" from repo name
localPath := path.Join(repoHost, url.Path) repoPath = path.Join(repoHost, url.Path)
localPath = strings.TrimSuffix(localPath, ".git") repoPath = strings.TrimSuffix(repoPath, ".git")
// remove tilde (~) char from username // 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) { func parseURL(rawurl string) (url *urlpkg.URL, err error) {