mirror of
https://github.com/grdl/git-get.git
synced 2026-02-09 12:04:15 +00:00
Refactor Clone method to use go-git
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package new
|
package new
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -19,20 +21,20 @@ func checkFatal(t *testing.T, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//func newTempDir(t *testing.T) string {
|
func newTempDir(t *testing.T) string {
|
||||||
// dir, err := ioutil.TempDir("", "git-get-repo-")
|
dir, err := ioutil.TempDir("", "git-get-repo-")
|
||||||
// checkFatal(t, errors.Wrap(err, "Failed creating test repo directory"))
|
checkFatal(t, errors.Wrap(err, "Failed creating test repo directory"))
|
||||||
//
|
|
||||||
// // Automatically remove repo when test is over
|
// Automatically remove repo when test is over
|
||||||
// t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
// err := os.RemoveAll(dir)
|
err := os.RemoveAll(dir)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// t.Errorf("failed cleaning up repo")
|
t.Errorf("failed cleaning up repo")
|
||||||
// }
|
}
|
||||||
// })
|
})
|
||||||
//
|
|
||||||
// return dir
|
return dir
|
||||||
//}
|
}
|
||||||
|
|
||||||
func newTestRepo(t *testing.T) *git.Repository {
|
func newTestRepo(t *testing.T) *git.Repository {
|
||||||
fs := memfs.New()
|
fs := memfs.New()
|
||||||
|
|||||||
41
new/repo.go
41
new/repo.go
@@ -1,8 +1,47 @@
|
|||||||
package new
|
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 {
|
type Repo struct {
|
||||||
repo *git.Repository
|
repo *git.Repository
|
||||||
Status *RepoStatus
|
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
|
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) {
|
func TestRepoEmpty(t *testing.T) {
|
||||||
repo := newTestRepo(t)
|
repo := newTestRepo(t)
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func (r *Repo) LoadStatus() error {
|
|||||||
return nil
|
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 {
|
func hasUntracked(status git.Status) bool {
|
||||||
for _, fs := range status {
|
for _, fs := range status {
|
||||||
if fs.Worktree == git.Untracked {
|
if fs.Worktree == git.Untracked {
|
||||||
@@ -45,4 +45,5 @@ func hasUntracked(status git.Status) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ import (
|
|||||||
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 ParseURL(rawURL string) (url *urlpkg.URL, err error) {
|
func ParseURL(rawURL string) (url *urlpkg.URL, err error) {
|
||||||
// If rawURL matches SCP-like syntax, convert it into a URL.
|
// If rawURL matches the SCP-like syntax, convert it into a standard ssh URL.
|
||||||
// eg, "git@github.com:user/repo" becomes "ssh://git@github.com/user/repo".
|
// eg, git@github.com:user/repo => ssh://git@github.com/user/repo
|
||||||
if m := scpSyntax.FindStringSubmatch(rawURL); m != nil {
|
if m := scpSyntax.FindStringSubmatch(rawURL); m != nil {
|
||||||
url = &urlpkg.URL{
|
url = &urlpkg.URL{
|
||||||
Scheme: "ssh",
|
Scheme: "ssh",
|
||||||
|
|||||||
Reference in New Issue
Block a user