-
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.
Add functionality (disabled) for all .gitignores in directory
I added support for finding all .gitignore files within a directory and adding those to the ignore list, but it's costly in large directories, so it's disabled
- Loading branch information
1 parent
de427db
commit 51416cf
Showing
8 changed files
with
155 additions
and
41 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 |
---|---|---|
@@ -1,23 +1,95 @@ | ||
package ignore | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func init() { | ||
zerolog.SetGlobalLevel(zerolog.NoLevel) | ||
} | ||
|
||
func TestIgnoreMatch(t *testing.T) { | ||
i, err := NewIgnore("my/files/*") | ||
assert.NoError(t, err) | ||
i := NewIgnore([]string{"my/files/*"}, []string{}) | ||
assert.NotNil(t, i) | ||
|
||
assert.False(t, i.Match("not/foo")) | ||
assert.True(t, i.Match("my/files/file1")) | ||
assert.False(t, i.Match("my/files")) | ||
} | ||
|
||
func TestReadIgnoreFIle(t *testing.T) { | ||
lines := readIgnoreFile("testdata/.gitignore") | ||
func TestIgnore_AddIgnoreFiles(t *testing.T) { | ||
i := NewIgnore([]string{"my/files/*"}, []string{"."}) | ||
i.AddIgnoreFiles(".gitignore", []string{"testdata"}) | ||
|
||
assert.True(t, i.Match("testdata/.gitignore")) | ||
assert.True(t, i.Match("testdata/.DS_Store")) | ||
assert.False(t, i.Match(".DS_Store")) | ||
assert.True(t, i.Match("my/files/file.txt")) | ||
assert.False(t, i.Match("my/files")) | ||
} | ||
|
||
func TestAddRecursiveGitIgnores(t *testing.T) { | ||
dir, err := ioutil.TempDir(os.TempDir(), "") | ||
assert.NoError(t, err) | ||
assert.DirExists(t, dir) | ||
expected := make([]string, 0) | ||
lastDir := dir | ||
for i := 0; i < 5; i++ { | ||
lastDir = filepath.Join(lastDir, strconv.Itoa(i)) | ||
assert.NoError(t, os.MkdirAll(lastDir, 0777)) | ||
|
||
ignoreFilename := filepath.Join(lastDir, ".gitignore") | ||
file, err := os.Create(ignoreFilename) | ||
assert.NoError(t, err) | ||
|
||
content := fmt.Sprintf("%d.txt", i) | ||
_, _ = file.WriteString(content) | ||
assert.NoError(t, file.Close()) | ||
|
||
expected = append(expected, filepath.Join(lastDir, content)) | ||
expected = append(expected, ignoreFilename) | ||
} | ||
defer os.RemoveAll(dir) | ||
lines := addRecursiveGitIgnores(".gitignore", []string{dir}) | ||
|
||
assert.EqualValues(t, expected, lines) | ||
} | ||
|
||
func BenchmarkIgnoreAddIgnoreFiles(b *testing.B) { | ||
dir, err := ioutil.TempDir(os.TempDir(), "") | ||
assert.NoError(b, err) | ||
assert.DirExists(b, dir) | ||
expected := []string{} | ||
|
||
for i := 0; i < b.N; i++ { | ||
newDir := filepath.Join(dir, strconv.Itoa(i)) | ||
assert.NoError(b, os.MkdirAll(newDir, 0777)) | ||
|
||
ignoreFilename := filepath.Join(newDir, ".gitignore") | ||
file, err := os.Create(ignoreFilename) | ||
assert.NoError(b, err) | ||
|
||
content := fmt.Sprintf("%d.txt", i) | ||
_, _ = file.WriteString(content) | ||
assert.NoError(b, file.Close()) | ||
|
||
expected = append(expected, ignoreFilename) | ||
expected = append(expected, filepath.Join(newDir, content)) | ||
} | ||
|
||
defer os.RemoveAll(dir) | ||
lines := addRecursiveGitIgnores(".gitignore", []string{dir}) | ||
sort.Strings(lines) | ||
sort.Strings(expected) | ||
|
||
assert.Equal(t, []string{"*.DS_Store"}, lines) | ||
assert.EqualValues(b, expected, lines) | ||
} |
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