浏览代码

Strip whitespace when loading exclusions from file

Patterns to exclude files can be loaded from a text file using the
“--exclude-from” option. Whitespace at the beginning or end of lines was
not stripped. Indented comments would be interpreted as a pattern and
a misplaced space at the end of a line--some text editors don't strip
them--could cause an exclusion pattern to not match as desired. With the
recent addition of regular expression support for exclusions the spaces
can be matched if necessary (“^\s” or “\s$”), though it's highly
unlikely that there are many paths deliberately starting or ending with
whitespace.
Michael Hanselmann 9 年之前
父节点
当前提交
2369b8a0f2
共有 3 个文件被更改,包括 15 次插入11 次删除
  1. 5 3
      borg/archiver.py
  2. 3 3
      borg/helpers.py
  3. 7 5
      borg/testsuite/helpers.py

+ 5 - 3
borg/archiver.py

@@ -632,9 +632,11 @@ class Archiver:
         expansion.
         expansion.
 
 
         The `--exclude-from` option permits loading exclusion patterns from a text
         The `--exclude-from` option permits loading exclusion patterns from a text
-        file with one pattern per line. Empty lines as well as lines starting with
-        the number sign ('#') are ignored. The optional style selector prefix is
-        also supported for patterns loaded from a file.
+        file with one pattern per line. Lines empty or starting with the number sign
+        ('#') after removing whitespace on both ends are ignored. The optional style
+        selector prefix is also supported for patterns loaded from a file. Due to
+        whitespace removal paths with whitespace at the beginning or end can only be
+        excluded using regular expressions.
 
 
         Examples:
         Examples:
 
 

+ 3 - 3
borg/helpers.py

@@ -236,10 +236,10 @@ def parse_timestamp(timestamp):
 
 
 
 
 def load_excludes(fh):
 def load_excludes(fh):
-    """Load and parse exclude patterns from file object. Empty lines and lines starting with '#' are ignored, but
-    whitespace is not stripped.
+    """Load and parse exclude patterns from file object. Lines empty or starting with '#' after stripping whitespace on
+    both line ends are ignored.
     """
     """
-    patterns = (line.rstrip('\r\n') for line in fh if not line.startswith('#'))
+    patterns = (line for line in (i.strip() for i in fh) if not line.startswith('#'))
     return [parse_pattern(pattern) for pattern in patterns if pattern]
     return [parse_pattern(pattern) for pattern in patterns if pattern]
 
 
 
 

+ 7 - 5
borg/testsuite/helpers.py

@@ -324,17 +324,16 @@ class OSXPatternNormalizationTestCase(BaseTestCase):
     (["*"], []),
     (["*"], []),
     (["# Comment",
     (["# Comment",
       "*/something00.txt",
       "*/something00.txt",
-      "  whitespace\t",
-      "/whitespace/at/end of filename \t ",
+      "  *whitespace*  ",
       # Whitespace before comment
       # Whitespace before comment
       " #/ws*",
       " #/ws*",
       # Empty line
       # Empty line
       "",
       "",
       "# EOF"],
       "# EOF"],
-     ["/more/data", "/home"]),
+     ["/more/data", "/home", " #/wsfoobar"]),
     (["re:.*"], []),
     (["re:.*"], []),
     (["re:\s"], ["/data/something00.txt", "/more/data", "/home"]),
     (["re:\s"], ["/data/something00.txt", "/more/data", "/home"]),
-    ([r"re:(.)(\1)"], ["/more/data", "/home", "/whitespace/at/end of filename \t "]),
+    ([r"re:(.)(\1)"], ["/more/data", "/home", "\tstart/whitespace", "/whitespace/end\t"]),
     (["", "", "",
     (["", "", "",
       "# This is a test with mixed pattern styles",
       "# This is a test with mixed pattern styles",
       # Case-insensitive pattern
       # Case-insensitive pattern
@@ -343,12 +342,15 @@ class OSXPatternNormalizationTestCase(BaseTestCase):
       "*whitespace*",
       "*whitespace*",
       "fm:*/something00*"],
       "fm:*/something00*"],
      ["/more/data"]),
      ["/more/data"]),
+    ([r"  re:^\s  "], ["/data/something00.txt", "/more/data", "/home", "/whitespace/end\t"]),
+    ([r"  re:\s$  "], ["/data/something00.txt", "/more/data", "/home", " #/wsfoobar", "\tstart/whitespace"]),
     ])
     ])
 def test_patterns_from_file(tmpdir, lines, expected):
 def test_patterns_from_file(tmpdir, lines, expected):
     files = [
     files = [
         '/data/something00.txt', '/more/data', '/home',
         '/data/something00.txt', '/more/data', '/home',
         ' #/wsfoobar',
         ' #/wsfoobar',
-        '/whitespace/at/end of filename \t ',
+        '\tstart/whitespace',
+        '/whitespace/end\t',
     ]
     ]
 
 
     def evaluate(filename):
     def evaluate(filename):