From c796d84fca48feea91ca3e85fbf38e16f764a468 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 16 Apr 2017 20:46:52 +0200 Subject: [PATCH 1/3] Ignore empty lines in excludes file Closes #915 --- src/cmds/restic/cmd_backup.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/cmds/restic/cmd_backup.go b/src/cmds/restic/cmd_backup.go index 16d596a44..df36b6fd7 100644 --- a/src/cmds/restic/cmd_backup.go +++ b/src/cmds/restic/cmd_backup.go @@ -415,11 +415,20 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error { scanner := bufio.NewScanner(file) for scanner.Scan() { - line := scanner.Text() - if !strings.HasPrefix(line, "#") { - line = os.ExpandEnv(line) - opts.Excludes = append(opts.Excludes, line) + line := strings.TrimSpace(scanner.Text()) + + // ignore empty lines + if line == "" { + continue } + + // strip comments + if strings.HasPrefix(line, "#") { + continue + } + + line = os.ExpandEnv(line) + opts.Excludes = append(opts.Excludes, line) } } From dd65ac56efd168d19be577b241621ab6d5a8ebc2 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 16 Apr 2017 20:49:20 +0200 Subject: [PATCH 2/3] filter: Ignore empty patterns --- src/restic/filter/filter.go | 7 ++++++- src/restic/filter/filter_test.go | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/restic/filter/filter.go b/src/restic/filter/filter.go index bb483d31c..37b58f7ef 100644 --- a/src/restic/filter/filter.go +++ b/src/restic/filter/filter.go @@ -100,9 +100,14 @@ func match(patterns, strs []string) (matched bool, err error) { return false, nil } -// List returns true if str matches one of the patterns. +// List returns true if str matches one of the patterns. Empty patterns are +// ignored. func List(patterns []string, str string) (matched bool, err error) { for _, pat := range patterns { + if pat == "" { + continue + } + matched, err = Match(pat, str) if err != nil { return false, err diff --git a/src/restic/filter/filter_test.go b/src/restic/filter/filter_test.go index 2fb04cdf5..232e2c4d3 100644 --- a/src/restic/filter/filter_test.go +++ b/src/restic/filter/filter_test.go @@ -152,6 +152,7 @@ var filterListTests = []struct { {[]string{"?", "x"}, "/foo/bar/x", true}, {[]string{"/*/*/bar/test.*"}, "/foo/bar/test.go", false}, {[]string{"/*/*/bar/test.*", "*.go"}, "/foo/bar/test.go", true}, + {[]string{"", "*.c"}, "/foo/bar/test.go", false}, } func TestMatchList(t *testing.T) { From 69dd1d2544e088513d4e39fad9e9b2767f4c84e4 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 16 Apr 2017 20:52:41 +0200 Subject: [PATCH 3/3] backup: Allow specifying multiple exclude files --- src/cmds/restic/cmd_backup.go | 46 ++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/cmds/restic/cmd_backup.go b/src/cmds/restic/cmd_backup.go index df36b6fd7..077bba85a 100644 --- a/src/cmds/restic/cmd_backup.go +++ b/src/cmds/restic/cmd_backup.go @@ -44,7 +44,7 @@ type BackupOptions struct { Parent string Force bool Excludes []string - ExcludeFile string + ExcludeFiles []string ExcludeOtherFS bool Stdin bool StdinFilename string @@ -68,7 +68,7 @@ func init() { f.StringVar(&backupOptions.Parent, "parent", "", "use this parent snapshot (default: last snapshot in the repo that has the same target files/directories)") f.BoolVarP(&backupOptions.Force, "force", "f", false, `force re-reading the target files/directories (overrides the "parent" flag)`) f.StringSliceVarP(&backupOptions.Excludes, "exclude", "e", nil, "exclude a `pattern` (can be specified multiple times)") - f.StringVar(&backupOptions.ExcludeFile, "exclude-file", "", "read exclude patterns from a file") + f.StringSliceVar(&backupOptions.ExcludeFiles, "exclude-file", nil, "read exclude patterns from a `file` (can be specified multiple times)") f.BoolVarP(&backupOptions.ExcludeOtherFS, "one-file-system", "x", false, "exclude other file systems") f.BoolVar(&backupOptions.Stdin, "stdin", false, "read backup from stdin") f.StringVar(&backupOptions.StdinFilename, "stdin-filename", "stdin", "file name to use when reading from stdin") @@ -406,29 +406,31 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error { Verbosef("scan %v\n", target) // add patterns from file - if opts.ExcludeFile != "" { - file, err := fs.Open(opts.ExcludeFile) - if err != nil { - Warnf("error reading exclude patterns: %v", err) - return nil - } - - scanner := bufio.NewScanner(file) - for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) - - // ignore empty lines - if line == "" { - continue + if len(opts.ExcludeFiles) > 0 { + for _, filename := range opts.ExcludeFiles { + file, err := fs.Open(filename) + if err != nil { + Warnf("error reading exclude patterns: %v", err) + return nil } - // strip comments - if strings.HasPrefix(line, "#") { - continue - } + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) - line = os.ExpandEnv(line) - opts.Excludes = append(opts.Excludes, line) + // ignore empty lines + if line == "" { + continue + } + + // strip comments + if strings.HasPrefix(line, "#") { + continue + } + + line = os.ExpandEnv(line) + opts.Excludes = append(opts.Excludes, line) + } } }