summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Coldrick <adam.coldrick@codethink.co.uk>2014-04-02 12:56:42 +0000
committerAdam Coldrick <adam.coldrick@codethink.co.uk>2014-04-02 13:55:25 +0000
commitbb45df49f36d3d06424e3467e6f7ff339beb94cf (patch)
tree51716d05a5423355295e3d98a4910472d30090a5
parentf56259c2ce62c301f0e0f9dd7c85001b45fc8f5c (diff)
downloadmorph-baserock/adamcoldrick/git-fat-handle-whitespace-rebase.tar.gz
Escape special characters and handle whitespace in gitattributesbaserock/adamcoldrick/git-fat-handle-whitespace-rebase
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 "foo<tab>bar" 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
-rw-r--r--morphlib/plugins/add_binary_plugin.py22
1 files changed, 21 insertions, 1 deletions
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 <binaries>`
@@ -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