-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use fastwalk instead of filepath.Walk
- Loading branch information
1 parent
d929348
commit 29c592a
Showing
7 changed files
with
129 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package walker | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/get-woke/fastwalk" | ||
) | ||
|
||
// Walk is a helper function that will automatically skip the `.git` directory. | ||
// fastwalk is a fork of code that is a better, faster version of filepath.Walk. | ||
// tl;dr since filepath.Walk get a complete FileInfo for every file, | ||
// it's inherently slow. See https://github.com/golang/go/issues/16399 | ||
func Walk(root string, walkFn func(path string, typ os.FileMode) error) error { | ||
return fastwalk.Walk(root, func(path string, typ os.FileMode) error { | ||
path = filepath.Clean(path) | ||
|
||
if typ.IsDir() && isDotGit(path) { | ||
return filepath.SkipDir | ||
} | ||
|
||
return walkFn(path, typ) | ||
}) | ||
} | ||
|
||
func isDotGit(path string) bool { | ||
return filepath.Base(path) == ".git" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package walker | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWalker_Walk(t *testing.T) { | ||
dir, err := ioutil.TempDir(os.TempDir(), "") | ||
assert.NoError(t, err) | ||
assert.DirExists(t, dir) | ||
|
||
for i := 0; i < 10; i++ { | ||
var newDir string | ||
if i%2 == 0 { | ||
newDir = filepath.Join(dir, strconv.Itoa(i)) | ||
} else { | ||
newDir = filepath.Join(dir, ".git") | ||
} | ||
assert.NoError(t, os.MkdirAll(newDir, 0777)) | ||
|
||
filename := filepath.Join(newDir, ".foo") | ||
file, err := os.Create(filename) | ||
assert.NoError(t, err) | ||
assert.NoError(t, file.Close()) | ||
} | ||
|
||
defer os.RemoveAll(dir) | ||
err = Walk(dir, func(p string, typ os.FileMode) error { | ||
assert.False(t, isDotGit(p), "path should not be returned in walk: %s", p) | ||
return nil | ||
}) | ||
assert.NoError(t, err) | ||
|
||
} | ||
|
||
func TestInSlice(t *testing.T) { | ||
tests := []struct { | ||
path string | ||
assertion assert.BoolAssertionFunc | ||
}{ | ||
{".git", assert.True}, | ||
{".github", assert.False}, | ||
{"/foo/bar/.git", assert.True}, | ||
{"/foo/.git/bar", assert.False}, | ||
{"/foo/.github", assert.False}, | ||
{"foo/.git", assert.True}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.path, func(t *testing.T) { | ||
tt.assertion(t, isDotGit(tt.path)) | ||
}) | ||
} | ||
} |