1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-03-03 18:27:01 +00:00

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.
This commit is contained in:
Michael Hanselmann 2016-01-13 14:30:54 +01:00
parent 2bafece093
commit 2369b8a0f2
3 changed files with 15 additions and 11 deletions

View file

@ -632,9 +632,11 @@ def do_break_lock(self, args):
expansion.
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:

View file

@ -236,10 +236,10 @@ def parse_timestamp(timestamp):
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]

View file

@ -324,17 +324,16 @@ def testInvalidUnicode(self):
(["*"], []),
(["# Comment",
"*/something00.txt",
" whitespace\t",
"/whitespace/at/end of filename \t ",
" *whitespace* ",
# Whitespace before comment
" #/ws*",
# Empty line
"",
"# EOF"],
["/more/data", "/home"]),
["/more/data", "/home", " #/wsfoobar"]),
(["re:.*"], []),
(["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",
# Case-insensitive pattern
@ -343,12 +342,15 @@ def testInvalidUnicode(self):
"*whitespace*",
"fm:*/something00*"],
["/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):
files = [
'/data/something00.txt', '/more/data', '/home',
' #/wsfoobar',
'/whitespace/at/end of filename \t ',
'\tstart/whitespace',
'/whitespace/end\t',
]
def evaluate(filename):