mirror of
https://github.com/restic/restic.git
synced 2024-12-25 09:18:55 +00:00
filter: implement handling **
This commit is contained in:
parent
bbdb2ebfa0
commit
e9285539be
2 changed files with 47 additions and 1 deletions
|
@ -31,7 +31,39 @@ func Match(pattern, str string) (matched bool, err error) {
|
||||||
return match(patterns, strs)
|
return match(patterns, strs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasDoubleWildcard(list []string) (ok bool, pos int) {
|
||||||
|
for i, item := range list {
|
||||||
|
if item == "**" {
|
||||||
|
return true, i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, 0
|
||||||
|
}
|
||||||
|
|
||||||
func match(patterns, strs []string) (matched bool, err error) {
|
func match(patterns, strs []string) (matched bool, err error) {
|
||||||
|
if ok, pos := hasDoubleWildcard(patterns); ok {
|
||||||
|
// gradually expand '**' into separate wildcards
|
||||||
|
for i := 0; i <= len(strs)-len(patterns)+1; i++ {
|
||||||
|
newPat := patterns[:pos]
|
||||||
|
for k := 0; k < i; k++ {
|
||||||
|
newPat = append(newPat, "*")
|
||||||
|
}
|
||||||
|
newPat = append(newPat, patterns[pos+1:]...)
|
||||||
|
|
||||||
|
matched, err := match(newPat, strs)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if matched {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
if len(patterns) == 0 && len(strs) == 0 {
|
if len(patterns) == 0 && len(strs) == 0 {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,21 @@ var matchTests = []struct {
|
||||||
{"*.c", "bar/baz/test.go", false},
|
{"*.c", "bar/baz/test.go", false},
|
||||||
{"sdk", "/foo/bar/sdk", true},
|
{"sdk", "/foo/bar/sdk", true},
|
||||||
{"sdk", "/foo/bar/sdk/test/sdk_foo.go", true},
|
{"sdk", "/foo/bar/sdk/test/sdk_foo.go", true},
|
||||||
{"sdk/*/cpp/*/*vars*.html", "/usr/share/doc/libreoffice/sdk/docs/cpp/ref/a00517.html", false},
|
{
|
||||||
|
"sdk/*/cpp/*/*vars*.html",
|
||||||
|
"/usr/share/doc/libreoffice/sdk/docs/cpp/ref/a00517.html",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{"foo/**/bar/*.go", "/home/user/foo/work/special/project/bar/test.go", true},
|
||||||
|
{"foo/**/bar/*.go", "/home/user/foo/bar/test.go", true},
|
||||||
|
{"foo/**/bar/*.go", "x/foo/bar/test.go", true},
|
||||||
|
{"foo/**/bar/*.go", "foo/bar/test.go", true},
|
||||||
|
{"foo/**/bar/*.go", "/home/user/foo/test.c", false},
|
||||||
|
{"foo/**/bar/*.go", "bar/foo/main.go", false},
|
||||||
|
{"foo/**/bar/*.go", "/foo/bar/main.go", true},
|
||||||
|
{"foo/**/bar/*.go", "bar/main.go", false},
|
||||||
|
{"foo/**/bar", "/home/user/foo/x/y/bar", true},
|
||||||
|
{"foo/**/bar", "/home/user/foo/x/y/bar/main.go", true},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMatch(t *testing.T) {
|
func TestMatch(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue