mirror of
https://github.com/grdl/git-get.git
synced 2026-02-04 23:14:43 +00:00
Refactor Clone method to use go-git
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package new
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -19,20 +21,20 @@ func checkFatal(t *testing.T, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
//func newTempDir(t *testing.T) string {
|
||||
// dir, err := ioutil.TempDir("", "git-get-repo-")
|
||||
// checkFatal(t, errors.Wrap(err, "Failed creating test repo directory"))
|
||||
//
|
||||
// // Automatically remove repo when test is over
|
||||
// t.Cleanup(func() {
|
||||
// err := os.RemoveAll(dir)
|
||||
// if err != nil {
|
||||
// t.Errorf("failed cleaning up repo")
|
||||
// }
|
||||
// })
|
||||
//
|
||||
// return dir
|
||||
//}
|
||||
func newTempDir(t *testing.T) string {
|
||||
dir, err := ioutil.TempDir("", "git-get-repo-")
|
||||
checkFatal(t, errors.Wrap(err, "Failed creating test repo directory"))
|
||||
|
||||
// Automatically remove repo when test is over
|
||||
t.Cleanup(func() {
|
||||
err := os.RemoveAll(dir)
|
||||
if err != nil {
|
||||
t.Errorf("failed cleaning up repo")
|
||||
}
|
||||
})
|
||||
|
||||
return dir
|
||||
}
|
||||
|
||||
func newTestRepo(t *testing.T) *git.Repository {
|
||||
fs := memfs.New()
|
||||
|
||||
41
new/repo.go
41
new/repo.go
@@ -1,8 +1,47 @@
|
||||
package new
|
||||
|
||||
import "github.com/go-git/go-git/v5"
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/go-git/go-git/v5/plumbing/cache"
|
||||
"github.com/go-git/go-git/v5/storage/filesystem"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/go-git/go-billy/v5"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
)
|
||||
|
||||
type Repo struct {
|
||||
repo *git.Repository
|
||||
Status *RepoStatus
|
||||
}
|
||||
|
||||
func CloneRepo(url string, path billy.Filesystem) (r *Repo, err error) {
|
||||
opts := &git.CloneOptions{
|
||||
URL: url,
|
||||
Auth: nil,
|
||||
RemoteName: git.DefaultRemoteName,
|
||||
ReferenceName: "",
|
||||
SingleBranch: false,
|
||||
NoCheckout: false,
|
||||
Depth: 0,
|
||||
RecurseSubmodules: git.NoRecurseSubmodules,
|
||||
Progress: os.Stdout,
|
||||
Tags: git.AllTags,
|
||||
}
|
||||
|
||||
dotgit, _ := path.Chroot(git.GitDirName)
|
||||
s := filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault())
|
||||
|
||||
repo, err := git.Clone(s, path, opts)
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Failed cloning repo")
|
||||
}
|
||||
|
||||
r = &Repo{
|
||||
repo: repo,
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,44 @@
|
||||
package new
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/go-git/go-billy/v5/osfs"
|
||||
|
||||
"github.com/go-git/go-billy/v5/memfs"
|
||||
)
|
||||
|
||||
func TestRepoCloneInMemory(t *testing.T) {
|
||||
path := memfs.New()
|
||||
repo, err := CloneRepo("https://github.com/grdl/dotfiles", path)
|
||||
checkFatal(t, err)
|
||||
|
||||
wt, err := repo.repo.Worktree()
|
||||
checkFatal(t, err)
|
||||
|
||||
files, err := wt.Filesystem.ReadDir("")
|
||||
checkFatal(t, err)
|
||||
|
||||
if len(files) == 0 {
|
||||
t.Errorf("Cloned repo should contain files")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoCloneOnDisk(t *testing.T) {
|
||||
path := osfs.New(newTempDir(t))
|
||||
repo, err := CloneRepo("https://github.com/grdl/dotfiles", path)
|
||||
checkFatal(t, err)
|
||||
|
||||
wt, err := repo.repo.Worktree()
|
||||
checkFatal(t, err)
|
||||
|
||||
files, err := wt.Filesystem.ReadDir("")
|
||||
checkFatal(t, err)
|
||||
|
||||
if len(files) == 0 {
|
||||
t.Errorf("Cloned repo should contain files")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoEmpty(t *testing.T) {
|
||||
repo := newTestRepo(t)
|
||||
|
||||
@@ -37,7 +37,7 @@ func (r *Repo) LoadStatus() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// hasUntracked returns true if there's any untracked file in the worktree
|
||||
// hasUntracked returns true if there are any untracked files in the worktree
|
||||
func hasUntracked(status git.Status) bool {
|
||||
for _, fs := range status {
|
||||
if fs.Worktree == git.Untracked {
|
||||
@@ -45,4 +45,5 @@ func hasUntracked(status git.Status) bool {
|
||||
}
|
||||
}
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
@@ -14,8 +14,8 @@ import (
|
||||
var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`)
|
||||
|
||||
func ParseURL(rawURL string) (url *urlpkg.URL, err error) {
|
||||
// If rawURL matches SCP-like syntax, convert it into a URL.
|
||||
// eg, "git@github.com:user/repo" becomes "ssh://git@github.com/user/repo".
|
||||
// If rawURL matches the SCP-like syntax, convert it into a standard ssh URL.
|
||||
// eg, git@github.com:user/repo => ssh://git@github.com/user/repo
|
||||
if m := scpSyntax.FindStringSubmatch(rawURL); m != nil {
|
||||
url = &urlpkg.URL{
|
||||
Scheme: "ssh",
|
||||
|
||||
Reference in New Issue
Block a user