6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-06 01:12:57 +00:00

Refactor repoFinder

- Remove io package and move finder to git package
- Move tempDir and writeFile into test package. They are only used for testing purposes anyway.
- Add a way to specify parent folder for tempDir. Useful for testing nested repos.
- Add new test repos with .git/config files
This commit is contained in:
Grzegorz Dlugoszewski
2020-07-08 13:37:32 +02:00
parent 5ab6031382
commit 8ab4681c26
8 changed files with 68 additions and 79 deletions

View File

@@ -1,27 +1,18 @@
package git
import (
"git-get/pkg/io"
"git-get/pkg/run"
"git-get/pkg/test"
"path/filepath"
"testing"
)
// cfgStub represents a gitconfig file but instead of using a global one, it creates a temporary git repo and uses its local gitconfig.
type cfgStub struct {
repo *test.Repo
}
func newCfgStub(t *testing.T) *cfgStub {
r := test.RepoEmpty(t)
return &cfgStub{
repo: r,
}
*test.Repo
}
func (c *cfgStub) Get(key string) string {
out, err := run.Git("config", "--local", key).OnRepo(c.repo.Path()).AndCaptureLine()
out, err := run.Git("config", "--local", key).OnRepo(c.Path()).AndCaptureLine()
if err != nil {
return ""
}
@@ -74,22 +65,13 @@ func TestGitConfig(t *testing.T) {
}
func makeConfigEmpty(t *testing.T) *cfgStub {
c := newCfgStub(t)
io.Write(filepath.Join(c.repo.Path(), dotgit, "config"), "")
return c
return &cfgStub{
Repo: test.RepoWithEmptyConfig(t),
}
}
func makeConfigValid(t *testing.T) *cfgStub {
c := newCfgStub(t)
gitconfig := `
[user]
name = grdl
[gitget]
host = github.com
`
io.Write(filepath.Join(c.repo.Path(), dotgit, "config"), gitconfig)
return c
return &cfgStub{
Repo: test.RepoWithValidConfig(t),
}
}

102
pkg/git/finder.go Normal file
View File

@@ -0,0 +1,102 @@
package git
import (
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/karrick/godirwalk"
"github.com/pkg/errors"
)
// errSkipNode is used as an error indicating that .git directory has been found.
// It's handled by ErrorsCallback to tell the WalkCallback to skip this dir.
var errSkipNode = errors.New(".git directory found, skipping this node")
// errDirectoryAccess indicates a directory doesn't exists or can't be accessed
var errDirectoryAccess = errors.New("directory doesn't exist or can't be accessed")
// Exists returns true if a directory exists. If it doesn't or the directory can't be accessed it returns an error.
func Exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if err != nil {
if os.IsNotExist(err) {
return false, errDirectoryAccess
}
}
// Directory exists but can't be accessed
return true, errDirectoryAccess
}
// RepoFinder finds paths to git repos inside given path.
type RepoFinder struct {
root string
repos []string
}
// NewRepoFinder returns a RepoFinder pointed at given root path.
func NewRepoFinder(root string) *RepoFinder {
return &RepoFinder{
root: root,
}
}
// Find returns a sorted list of paths to git repos found inside a given root path.
// Returns error if root repo path can't be found or accessed.
func (r *RepoFinder) Find() ([]string, error) {
if _, err := Exists(r.root); err != nil {
return nil, err
}
walkOpts := &godirwalk.Options{
ErrorCallback: r.errorCb,
Callback: r.walkCb,
// Use Unsorted to improve speed because repos will be processed by goroutines in a random order anyway.
Unsorted: true,
}
err := godirwalk.Walk(r.root, walkOpts)
if err != nil {
return nil, err
}
if len(r.repos) == 0 {
return nil, fmt.Errorf("no git repos found in root path %s", r.root)
}
return r.repos, nil
}
func (r *RepoFinder) walkCb(path string, ent *godirwalk.Dirent) error {
// Do not traverse .git directories
if ent.IsDir() && ent.Name() == ".git" {
r.repos = append(r.repos, strings.TrimSuffix(path, ".git"))
return errSkipNode
}
// Do not traverse directories containing a .git directory
if ent.IsDir() {
_, err := os.Stat(filepath.Join(path, ".git"))
if err == nil {
r.repos = append(r.repos, strings.TrimSuffix(path, ".git"))
return ErrSkipNode
}
}
return nil
}
func (r *RepoFinder) errorCb(_ string, err error) godirwalk.ErrorAction {
// Skip .git directory and directories we don't have permissions to access
// TODO: Will syscall.EACCES work on windows?
if errors.Is(err, errSkipNode) || errors.Is(err, syscall.EACCES) {
return godirwalk.SkipNode
}
return godirwalk.Halt
}

View File

@@ -2,7 +2,6 @@ package git
import (
"fmt"
"git-get/pkg/io"
"git-get/pkg/run"
"net/url"
"strconv"
@@ -43,8 +42,7 @@ type CloneOpts struct {
// Open checks if given path can be accessed and returns a Repo instance pointing to it.
func Open(path string) (Repo, error) {
_, err := io.Exists(path)
if err != nil {
if _, err := Exists(path); err != nil {
return nil, err
}

View File

@@ -1,7 +1,6 @@
package git
import (
"git-get/pkg/io"
"git-get/pkg/test"
"reflect"
"testing"
@@ -10,7 +9,7 @@ import (
func TestOpen(t *testing.T) {
_, err := Open("/paththatdoesnotexist/repo")
if err != io.ErrDirectoryAccess {
if err != errDirectoryAccess {
t.Errorf("Opening a repo in non existing path should throw an error")
}
}