Only convert iexclude & iinclude casing once

This commit is contained in:
Johannes Hertenstein 2019-01-19 10:59:48 +00:00
parent 879f6e0c81
commit deedc38129
3 changed files with 11 additions and 38 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/restic/restic/internal/filter"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/restorer"
"strings"
"github.com/spf13/cobra"
)
@ -62,6 +63,14 @@ func runRestore(opts RestoreOptions, gopts GlobalOptions, args []string) error {
hasExcludes := len(opts.Exclude) > 0 || len(opts.InsensitiveExclude) > 0
hasIncludes := len(opts.Include) > 0 || len(opts.InsensitiveInclude) > 0
for i, str := range opts.InsensitiveExclude {
opts.InsensitiveExclude[i] = strings.ToLower(str)
}
for i, str := range opts.InsensitiveInclude {
opts.InsensitiveInclude[i] = strings.ToLower(str)
}
switch {
case len(args) == 0:
return errors.Fatal("no snapshot ID specified")
@ -131,7 +140,7 @@ func runRestore(opts RestoreOptions, gopts GlobalOptions, args []string) error {
Warnf("error for exclude pattern: %v", err)
}
matchedInsensitive, _, err := filter.InsensitiveList(opts.InsensitiveExclude, item)
matchedInsensitive, _, err := filter.List(opts.InsensitiveExclude, strings.ToLower(item))
if err != nil {
Warnf("error for iexclude pattern: %v", err)
}
@ -152,7 +161,7 @@ func runRestore(opts RestoreOptions, gopts GlobalOptions, args []string) error {
Warnf("error for include pattern: %v", err)
}
matchedInsensitive, childMayMatchInsensitive, err := filter.InsensitiveList(opts.InsensitiveInclude, item)
matchedInsensitive, childMayMatchInsensitive, err := filter.List(opts.InsensitiveInclude, strings.ToLower(item))
if err != nil {
Warnf("error for iexclude pattern: %v", err)
}

View File

@ -187,12 +187,3 @@ func List(patterns []string, str string) (matched bool, childMayMatch bool, err
return matched, childMayMatch, nil
}
// InsensitiveList is the same as List but case insensitive.
func InsensitiveList(patterns []string, str string) (matched bool, childMayMatch bool, err error) {
str = strings.ToLower(str)
for index, path := range patterns {
patterns[index] = strings.ToLower(path)
}
return List(patterns, str)
}

View File

@ -279,33 +279,6 @@ func ExampleList() {
// match: true
}
var filterInsensitiveListTests = []struct {
patterns []string
path string
match bool
}{
{[]string{"*.go"}, "/foo/bar/test.go", true},
{[]string{"test.go"}, "/foo/bar/test.go", true},
{[]string{"test.go"}, "/foo/bar/TEST.go", true},
{[]string{"BAR"}, "/foo/BAR/TEST.go", true},
}
func TestInsensitiveList(t *testing.T) {
for i, test := range filterInsensitiveListTests {
match, _, err := filter.InsensitiveList(test.patterns, test.path)
if err != nil {
t.Errorf("test %d failed: expected no error for patterns %q, but error returned: %v",
i, test.patterns, err)
continue
}
if match != test.match {
t.Errorf("test %d: filter.InsensitiveList(%q, %q): expected %v, got %v",
i, test.patterns, test.path, test.match, match)
}
}
}
func extractTestLines(t testing.TB) (lines []string) {
f, err := os.Open("testdata/libreoffice.txt.bz2")
if err != nil {