mirror of
https://github.com/grdl/git-get.git
synced 2026-02-11 16:58:59 +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:
@@ -3,7 +3,6 @@ package pkg
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git-get/pkg/git"
|
"git-get/pkg/git"
|
||||||
"git-get/pkg/io"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -69,7 +68,7 @@ func cloneDumpFile(c *GetCfg) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If target path already exists, skip cloning this repo
|
// If target path already exists, skip cloning this repo
|
||||||
if exists, _ := io.Exists(opts.Path); exists {
|
if exists, _ := git.Exists(opts.Path); exists {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,18 @@
|
|||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git-get/pkg/io"
|
|
||||||
"git-get/pkg/run"
|
"git-get/pkg/run"
|
||||||
"git-get/pkg/test"
|
"git-get/pkg/test"
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// cfgStub represents a gitconfig file but instead of using a global one, it creates a temporary git repo and uses its local gitconfig.
|
// 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 {
|
type cfgStub struct {
|
||||||
repo *test.Repo
|
*test.Repo
|
||||||
}
|
|
||||||
|
|
||||||
func newCfgStub(t *testing.T) *cfgStub {
|
|
||||||
r := test.RepoEmpty(t)
|
|
||||||
return &cfgStub{
|
|
||||||
repo: r,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cfgStub) Get(key string) string {
|
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 {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -74,22 +65,13 @@ func TestGitConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func makeConfigEmpty(t *testing.T) *cfgStub {
|
func makeConfigEmpty(t *testing.T) *cfgStub {
|
||||||
c := newCfgStub(t)
|
return &cfgStub{
|
||||||
io.Write(filepath.Join(c.repo.Path(), dotgit, "config"), "")
|
Repo: test.RepoWithEmptyConfig(t),
|
||||||
|
}
|
||||||
return c
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeConfigValid(t *testing.T) *cfgStub {
|
func makeConfigValid(t *testing.T) *cfgStub {
|
||||||
c := newCfgStub(t)
|
return &cfgStub{
|
||||||
|
Repo: test.RepoWithValidConfig(t),
|
||||||
gitconfig := `
|
}
|
||||||
[user]
|
|
||||||
name = grdl
|
|
||||||
[gitget]
|
|
||||||
host = github.com
|
|
||||||
`
|
|
||||||
io.Write(filepath.Join(c.repo.Path(), dotgit, "config"), gitconfig)
|
|
||||||
|
|
||||||
return c
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
// Package io provides functions to read, write and search files and directories.
|
package git
|
||||||
package io
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -13,36 +11,12 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrSkipNode is used as an error indicating that .git directory has been found.
|
// 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.
|
// It's handled by ErrorsCallback to tell the WalkCallback to skip this dir.
|
||||||
var ErrSkipNode = errors.New(".git directory found, skipping this node")
|
var errSkipNode = errors.New(".git directory found, skipping this node")
|
||||||
|
|
||||||
// ErrDirectoryAccess indicated a directory doesn't exists or can't be accessed
|
// 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")
|
var errDirectoryAccess = errors.New("directory doesn't exist or can't be accessed")
|
||||||
|
|
||||||
// TempDir creates a temporary directory for test repos.
|
|
||||||
func TempDir() (string, error) {
|
|
||||||
dir, err := ioutil.TempDir("", "git-get-repo-")
|
|
||||||
if err != nil {
|
|
||||||
return "", errors.Wrap(err, "failed creating test repo directory")
|
|
||||||
}
|
|
||||||
|
|
||||||
return dir, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write writes string content into a file. If file doesn't exists, it will create it.
|
|
||||||
func Write(path string, content string) error {
|
|
||||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "failed opening a file for writing %s", path)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = file.Write([]byte(content))
|
|
||||||
if err != nil {
|
|
||||||
errors.Wrapf(err, "Failed writing to a file %s", path)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exists returns true if a directory exists. If it doesn't or the directory can't be accessed it returns an error.
|
// 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) {
|
func Exists(path string) (bool, error) {
|
||||||
@@ -54,12 +28,12 @@ func Exists(path string) (bool, error) {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return false, ErrDirectoryAccess
|
return false, errDirectoryAccess
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Directory exists but can't be accessed
|
// Directory exists but can't be accessed
|
||||||
return true, ErrDirectoryAccess
|
return true, errDirectoryAccess
|
||||||
}
|
}
|
||||||
|
|
||||||
// RepoFinder finds paths to git repos inside given path.
|
// RepoFinder finds paths to git repos inside given path.
|
||||||
@@ -105,7 +79,7 @@ func (r *RepoFinder) walkCb(path string, ent *godirwalk.Dirent) error {
|
|||||||
// Do not traverse .git directories
|
// Do not traverse .git directories
|
||||||
if ent.IsDir() && ent.Name() == ".git" {
|
if ent.IsDir() && ent.Name() == ".git" {
|
||||||
r.repos = append(r.repos, strings.TrimSuffix(path, ".git"))
|
r.repos = append(r.repos, strings.TrimSuffix(path, ".git"))
|
||||||
return ErrSkipNode
|
return errSkipNode
|
||||||
}
|
}
|
||||||
// Do not traverse directories containing a .git directory
|
// Do not traverse directories containing a .git directory
|
||||||
if ent.IsDir() {
|
if ent.IsDir() {
|
||||||
@@ -121,7 +95,7 @@ func (r *RepoFinder) walkCb(path string, ent *godirwalk.Dirent) error {
|
|||||||
func (r *RepoFinder) errorCb(_ string, err error) godirwalk.ErrorAction {
|
func (r *RepoFinder) errorCb(_ string, err error) godirwalk.ErrorAction {
|
||||||
// Skip .git directory and directories we don't have permissions to access
|
// Skip .git directory and directories we don't have permissions to access
|
||||||
// TODO: Will syscall.EACCES work on windows?
|
// TODO: Will syscall.EACCES work on windows?
|
||||||
if errors.Is(err, ErrSkipNode) || errors.Is(err, syscall.EACCES) {
|
if errors.Is(err, errSkipNode) || errors.Is(err, syscall.EACCES) {
|
||||||
return godirwalk.SkipNode
|
return godirwalk.SkipNode
|
||||||
}
|
}
|
||||||
return godirwalk.Halt
|
return godirwalk.Halt
|
||||||
@@ -2,7 +2,6 @@ package git
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git-get/pkg/io"
|
|
||||||
"git-get/pkg/run"
|
"git-get/pkg/run"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -43,8 +42,7 @@ type CloneOpts struct {
|
|||||||
|
|
||||||
// Open checks if given path can be accessed and returns a Repo instance pointing to it.
|
// Open checks if given path can be accessed and returns a Repo instance pointing to it.
|
||||||
func Open(path string) (Repo, error) {
|
func Open(path string) (Repo, error) {
|
||||||
_, err := io.Exists(path)
|
if _, err := Exists(path); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git-get/pkg/io"
|
|
||||||
"git-get/pkg/test"
|
"git-get/pkg/test"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -10,7 +9,7 @@ import (
|
|||||||
func TestOpen(t *testing.T) {
|
func TestOpen(t *testing.T) {
|
||||||
_, err := Open("/paththatdoesnotexist/repo")
|
_, err := Open("/paththatdoesnotexist/repo")
|
||||||
|
|
||||||
if err != io.ErrDirectoryAccess {
|
if err != errDirectoryAccess {
|
||||||
t.Errorf("Opening a repo in non existing path should throw an error")
|
t.Errorf("Opening a repo in non existing path should throw an error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package pkg
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git-get/pkg/cfg"
|
"git-get/pkg/cfg"
|
||||||
"git-get/pkg/io"
|
"git-get/pkg/git"
|
||||||
"git-get/pkg/print"
|
"git-get/pkg/print"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -18,7 +18,7 @@ type ListCfg struct {
|
|||||||
|
|
||||||
// List executes the "git list" command.
|
// List executes the "git list" command.
|
||||||
func List(c *ListCfg) error {
|
func List(c *ListCfg) error {
|
||||||
paths, err := io.NewRepoFinder(c.Root).Find()
|
paths, err := git.NewRepoFinder(c.Root).Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,20 +2,26 @@ package test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git-get/pkg/io"
|
|
||||||
"git-get/pkg/run"
|
"git-get/pkg/run"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *Repo) writeFile(filename string, content string) {
|
func (r *Repo) init() {
|
||||||
path := filepath.Join(r.path, filename)
|
err := run.Git("init", "--quiet", r.path).AndShutUp()
|
||||||
err := io.Write(path, content)
|
|
||||||
checkFatal(r.t, err)
|
checkFatal(r.t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repo) init() {
|
// writeFile writes the content string into a file. If file doesn't exists, it will create it.
|
||||||
err := run.Git("init", "--quiet", r.path).AndShutUp()
|
func (r *Repo) writeFile(filename string, content string) {
|
||||||
|
path := filepath.Join(r.path, filename)
|
||||||
|
|
||||||
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
|
checkFatal(r.t, err)
|
||||||
|
|
||||||
|
_, err = file.Write([]byte(content))
|
||||||
checkFatal(r.t, err)
|
checkFatal(r.t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,7 +51,7 @@ func (r *Repo) checkout(name string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repo) clone() *Repo {
|
func (r *Repo) clone() *Repo {
|
||||||
dir, err := io.TempDir()
|
dir, err := tempDir("")
|
||||||
checkFatal(r.t, err)
|
checkFatal(r.t, err)
|
||||||
|
|
||||||
url := fmt.Sprintf("file://%s/.git", r.path)
|
url := fmt.Sprintf("file://%s/.git", r.path)
|
||||||
@@ -66,6 +72,14 @@ func (r *Repo) fetch() {
|
|||||||
checkFatal(r.t, err)
|
checkFatal(r.t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tempDir creates a temporary directory inside the parent dir.
|
||||||
|
// If parent is empty it will use a system default temp dir (usually /tmp).
|
||||||
|
func tempDir(parent string) (string, error) {
|
||||||
|
dir, err := ioutil.TempDir(parent, "git-get-repo-")
|
||||||
|
|
||||||
|
return dir, err
|
||||||
|
}
|
||||||
|
|
||||||
func checkFatal(t *testing.T, err error) {
|
func checkFatal(t *testing.T, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed making test repo: %+v", err)
|
t.Fatalf("failed making test repo: %+v", err)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git-get/pkg/io"
|
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ func (r *Repo) cleanup() {
|
|||||||
|
|
||||||
// RepoEmpty creates an empty git repo.
|
// RepoEmpty creates an empty git repo.
|
||||||
func RepoEmpty(t *testing.T) *Repo {
|
func RepoEmpty(t *testing.T) *Repo {
|
||||||
dir, err := io.TempDir()
|
dir, err := tempDir("")
|
||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
|
|
||||||
r := &Repo{
|
r := &Repo{
|
||||||
@@ -178,3 +178,26 @@ func RepoWithBranchAheadAndBehind(t *testing.T) *Repo {
|
|||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RepoWithEmptyConfig creates a git repo with empty .git/config file
|
||||||
|
func RepoWithEmptyConfig(t *testing.T) *Repo {
|
||||||
|
r := RepoEmpty(t)
|
||||||
|
r.writeFile(filepath.Join(".git", "config"), "")
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// RepoWithValidConfig creates a git repo with valid content in .git/config file
|
||||||
|
func RepoWithValidConfig(t *testing.T) *Repo {
|
||||||
|
r := RepoEmpty(t)
|
||||||
|
|
||||||
|
gitconfig := `
|
||||||
|
[user]
|
||||||
|
name = grdl
|
||||||
|
[gitget]
|
||||||
|
host = github.com
|
||||||
|
`
|
||||||
|
r.writeFile(filepath.Join(".git", "config"), gitconfig)
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user