From bb45df49f36d3d06424e3467e6f7ff339beb94cf Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Wed, 2 Apr 2014 12:56:42 +0000 Subject: Escape special characters and handle whitespace in gitattributes Since git's parsing of .gitattributes can't deal with spaces, or even with quotes, replace any whitespace with [[:space:]]. Also, make sure to escape any glob metacharacters to avoid accidental pattern matches. Note: This approach could cause a file "foobar" to be treated as a binary if a file "foo bar" has already been added with morph add-binary. However this is as good of a workaround as I can find. Signed-off by: Adam Coldrick --- morphlib/plugins/add_binary_plugin.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'morphlib') diff --git a/morphlib/plugins/add_binary_plugin.py b/morphlib/plugins/add_binary_plugin.py index 1edae0e8..a192f792 100644 --- a/morphlib/plugins/add_binary_plugin.py +++ b/morphlib/plugins/add_binary_plugin.py @@ -17,6 +17,7 @@ import cliapp import logging import os +import re import urlparse import morphlib @@ -68,6 +69,13 @@ class AddBinaryPlugin(cliapp.Plugin): files = [gd.get_relpath(os.path.realpath(binary)) for binary in binaries] + # escape special characters and whitespace + escaped = [] + for path in files: + path = self.escape_glob(path) + path = self.escape_whitespace(path) + escaped.append(path) + # now add any files that aren't already mentioned in .gitattributes to # the file so that git fat knows what to do attr_path = gd.join_path('.gitattributes') @@ -76,7 +84,7 @@ class AddBinaryPlugin(cliapp.Plugin): current = set(f.split()[0] for f in attributes) else: current = set() - to_add = set(files) - current + to_add = set(escaped) - current # if we don't need to change .gitattributes then we can just do # `git add ` @@ -108,3 +116,15 @@ class AddBinaryPlugin(cliapp.Plugin): gitfat.write('[rsync]\n') gitfat.write('remote = %s' % fat_store) gd.get_index().add_files_from_working_tree([fat_path]) + + def escape_glob(self, path): + '''Escape glob metacharacters in a path and return the result.''' + metachars = re.compile('([*?[])') + path = metachars.sub(r'[\1]', path) + return path + + def escape_whitespace(self, path): + '''Substitute whitespace with [[:space:]] and return the result.''' + whitespace = re.compile('([ \n\r\t])') + path = whitespace.sub(r'[[:space:]]', path) + return path -- cgit v1.2.1