mirror of
https://github.com/grdl/git-get.git
synced 2026-02-04 14:31:49 +00:00
Parse URL and create a repo dir inside CloneRepo function
This commit is contained in:
18
pkg/repo.go
18
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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
10
pkg/url.go
10
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) {
|
||||
|
||||
Reference in New Issue
Block a user