Move top level packages to internal.
This commit is contained in:
148
internal/ignore/ignore.go
Normal file
148
internal/ignore/ignore.go
Normal file
@@ -0,0 +1,148 @@
|
||||
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
||||
// All rights reserved. Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package ignore
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/syncthing/syncthing/internal/fnmatch"
|
||||
)
|
||||
|
||||
type Pattern struct {
|
||||
match *regexp.Regexp
|
||||
include bool
|
||||
}
|
||||
|
||||
type Patterns []Pattern
|
||||
|
||||
func Load(file string) (Patterns, error) {
|
||||
seen := make(map[string]bool)
|
||||
return loadIgnoreFile(file, seen)
|
||||
}
|
||||
|
||||
func Parse(r io.Reader, file string) (Patterns, error) {
|
||||
seen := map[string]bool{
|
||||
file: true,
|
||||
}
|
||||
return parseIgnoreFile(r, file, seen)
|
||||
}
|
||||
|
||||
func (l Patterns) Match(file string) bool {
|
||||
for _, pattern := range l {
|
||||
if pattern.match.MatchString(file) {
|
||||
return pattern.include
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func loadIgnoreFile(file string, seen map[string]bool) (Patterns, error) {
|
||||
if seen[file] {
|
||||
return nil, fmt.Errorf("Multiple include of ignore file %q", file)
|
||||
}
|
||||
seen[file] = true
|
||||
|
||||
fd, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
return parseIgnoreFile(fd, file, seen)
|
||||
}
|
||||
|
||||
func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) (Patterns, error) {
|
||||
var exps Patterns
|
||||
|
||||
addPattern := func(line string) error {
|
||||
include := true
|
||||
if strings.HasPrefix(line, "!") {
|
||||
line = line[1:]
|
||||
include = false
|
||||
}
|
||||
|
||||
if strings.HasPrefix(line, "/") {
|
||||
// Pattern is rooted in the current dir only
|
||||
exp, err := fnmatch.Convert(line[1:], fnmatch.FNM_PATHNAME)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
||||
}
|
||||
exps = append(exps, Pattern{exp, include})
|
||||
} else if strings.HasPrefix(line, "**/") {
|
||||
// Add the pattern as is, and without **/ so it matches in current dir
|
||||
exp, err := fnmatch.Convert(line, fnmatch.FNM_PATHNAME)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
||||
}
|
||||
exps = append(exps, Pattern{exp, include})
|
||||
|
||||
exp, err = fnmatch.Convert(line[3:], fnmatch.FNM_PATHNAME)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
||||
}
|
||||
exps = append(exps, Pattern{exp, include})
|
||||
} else if strings.HasPrefix(line, "#include ") {
|
||||
includeFile := filepath.Join(filepath.Dir(currentFile), line[len("#include "):])
|
||||
includes, err := loadIgnoreFile(includeFile, seen)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
exps = append(exps, includes...)
|
||||
}
|
||||
} else {
|
||||
// Path name or pattern, add it so it matches files both in
|
||||
// current directory and subdirs.
|
||||
exp, err := fnmatch.Convert(line, fnmatch.FNM_PATHNAME)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
||||
}
|
||||
exps = append(exps, Pattern{exp, include})
|
||||
|
||||
exp, err = fnmatch.Convert("**/"+line, fnmatch.FNM_PATHNAME)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid pattern %q in ignore file", line)
|
||||
}
|
||||
exps = append(exps, Pattern{exp, include})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(fd)
|
||||
var err error
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
switch {
|
||||
case line == "":
|
||||
continue
|
||||
case strings.HasPrefix(line, "//"):
|
||||
continue
|
||||
case strings.HasPrefix(line, "#"):
|
||||
err = addPattern(line)
|
||||
case strings.HasSuffix(line, "/**"):
|
||||
err = addPattern(line)
|
||||
case strings.HasSuffix(line, "/"):
|
||||
err = addPattern(line)
|
||||
if err == nil {
|
||||
err = addPattern(line + "**")
|
||||
}
|
||||
default:
|
||||
err = addPattern(line)
|
||||
if err == nil {
|
||||
err = addPattern(line + "/**")
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return exps, nil
|
||||
}
|
||||
153
internal/ignore/ignore_test.go
Normal file
153
internal/ignore/ignore_test.go
Normal file
@@ -0,0 +1,153 @@
|
||||
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
||||
// All rights reserved. Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package ignore_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/syncthing/syncthing/internal/ignore"
|
||||
)
|
||||
|
||||
func TestIgnore(t *testing.T) {
|
||||
pats, err := ignore.Load("testdata/.stignore")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
f string
|
||||
r bool
|
||||
}{
|
||||
{"afile", false},
|
||||
{"bfile", true},
|
||||
{"cfile", false},
|
||||
{"dfile", false},
|
||||
{"efile", true},
|
||||
{"ffile", true},
|
||||
|
||||
{"dir1", false},
|
||||
{filepath.Join("dir1", "cfile"), true},
|
||||
{filepath.Join("dir1", "dfile"), false},
|
||||
{filepath.Join("dir1", "efile"), true},
|
||||
{filepath.Join("dir1", "ffile"), false},
|
||||
|
||||
{"dir2", false},
|
||||
{filepath.Join("dir2", "cfile"), false},
|
||||
{filepath.Join("dir2", "dfile"), true},
|
||||
{filepath.Join("dir2", "efile"), true},
|
||||
{filepath.Join("dir2", "ffile"), false},
|
||||
|
||||
{filepath.Join("dir3"), true},
|
||||
{filepath.Join("dir3", "afile"), true},
|
||||
}
|
||||
|
||||
for i, tc := range tests {
|
||||
if r := pats.Match(tc.f); r != tc.r {
|
||||
t.Errorf("Incorrect ignoreFile() #%d (%s); E: %v, A: %v", i, tc.f, tc.r, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExcludes(t *testing.T) {
|
||||
stignore := `
|
||||
!iex2
|
||||
!ign1/ex
|
||||
ign1
|
||||
i*2
|
||||
!ign2
|
||||
`
|
||||
pats, err := ignore.Parse(bytes.NewBufferString(stignore), ".stignore")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
f string
|
||||
r bool
|
||||
}{
|
||||
{"ign1", true},
|
||||
{"ign2", true},
|
||||
{"ibla2", true},
|
||||
{"iex2", false},
|
||||
{filepath.Join("ign1", "ign"), true},
|
||||
{filepath.Join("ign1", "ex"), false},
|
||||
{filepath.Join("ign1", "iex2"), false},
|
||||
{filepath.Join("iex2", "ign"), false},
|
||||
{filepath.Join("foo", "bar", "ign1"), true},
|
||||
{filepath.Join("foo", "bar", "ign2"), true},
|
||||
{filepath.Join("foo", "bar", "iex2"), false},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
if r := pats.Match(tc.f); r != tc.r {
|
||||
t.Errorf("Incorrect match for %s: %v != %v", tc.f, r, tc.r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadPatterns(t *testing.T) {
|
||||
var badPatterns = []string{
|
||||
"[",
|
||||
"/[",
|
||||
"**/[",
|
||||
"#include nonexistent",
|
||||
"#include .stignore",
|
||||
"!#include makesnosense",
|
||||
}
|
||||
|
||||
for _, pat := range badPatterns {
|
||||
parsed, err := ignore.Parse(bytes.NewBufferString(pat), ".stignore")
|
||||
if err == nil {
|
||||
t.Errorf("No error for pattern %q: %v", pat, parsed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaseSensitivity(t *testing.T) {
|
||||
ign, _ := ignore.Parse(bytes.NewBufferString("test"), ".stignore")
|
||||
|
||||
match := []string{"test"}
|
||||
dontMatch := []string{"foo"}
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "darwin", "windows":
|
||||
match = append(match, "TEST", "Test", "tESt")
|
||||
default:
|
||||
dontMatch = append(dontMatch, "TEST", "Test", "tESt")
|
||||
}
|
||||
|
||||
for _, tc := range match {
|
||||
if !ign.Match(tc) {
|
||||
t.Errorf("Incorrect match for %q: should be matched", tc)
|
||||
}
|
||||
}
|
||||
|
||||
for _, tc := range dontMatch {
|
||||
if ign.Match(tc) {
|
||||
t.Errorf("Incorrect match for %q: should not be matched", tc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommentsAndBlankLines(t *testing.T) {
|
||||
stignore := `
|
||||
// foo
|
||||
//bar
|
||||
|
||||
//!baz
|
||||
//#dex
|
||||
|
||||
// ips
|
||||
|
||||
|
||||
`
|
||||
pats, _ := ignore.Parse(bytes.NewBufferString(stignore), ".stignore")
|
||||
if len(pats) > 0 {
|
||||
t.Errorf("Expected no patterns")
|
||||
}
|
||||
}
|
||||
6
internal/ignore/testdata/.stignore
vendored
Normal file
6
internal/ignore/testdata/.stignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
#include excludes
|
||||
|
||||
bfile
|
||||
dir1/cfile
|
||||
**/efile
|
||||
/ffile
|
||||
1
internal/ignore/testdata/dir3/cfile
vendored
Normal file
1
internal/ignore/testdata/dir3/cfile
vendored
Normal file
@@ -0,0 +1 @@
|
||||
baz
|
||||
1
internal/ignore/testdata/dir3/dfile
vendored
Normal file
1
internal/ignore/testdata/dir3/dfile
vendored
Normal file
@@ -0,0 +1 @@
|
||||
quux
|
||||
2
internal/ignore/testdata/excludes
vendored
Normal file
2
internal/ignore/testdata/excludes
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
dir2/dfile
|
||||
#include further-excludes
|
||||
1
internal/ignore/testdata/further-excludes
vendored
Normal file
1
internal/ignore/testdata/further-excludes
vendored
Normal file
@@ -0,0 +1 @@
|
||||
dir3
|
||||
Reference in New Issue
Block a user