diff options
author | Ian Clatworthy <ian.clatworthy@canonical.com> | 2009-02-17 16:45:35 +1000 |
---|---|---|
committer | Ian Clatworthy <ian.clatworthy@canonical.com> | 2009-02-17 16:45:35 +1000 |
commit | 0afbc6ac4bbadc98c7031440279581dc332c9dd3 (patch) | |
tree | 90cab040e07f5964a3f372710f66358f8c58a7a9 | |
parent | d555bbf276a5a490ca5f19d37cba9253fe6cdbb5 (diff) | |
download | bzr-fastimport-0afbc6ac4bbadc98c7031440279581dc332c9dd3.tar.gz |
include subdir & multiple files tests
-rw-r--r-- | helpers.py | 35 | ||||
-rw-r--r-- | processors/filter_processor.py | 3 | ||||
-rw-r--r-- | tests/test_filter_processor.py | 67 |
3 files changed, 104 insertions, 1 deletions
@@ -48,6 +48,7 @@ def invert_dict(d): keys.append(k) return result + def invert_dictset(d): """Invert a dictionary with keys matching a set of values, turned into lists.""" # Based on recipe from ASPN @@ -57,3 +58,37 @@ def invert_dictset(d): keys = result.setdefault(v, []) keys.append(k) return result + + +def common_path(l1, l2, common=[]): + """Find the common bit of 2 paths.""" + # From http://code.activestate.com/recipes/208993/ + if len(l1) < 1: return (common, l1, l2) + if len(l2) < 1: return (common, l1, l2) + if l1[0] != l2[0]: return (common, l1, l2) + return common_path(l1[1:], l2[1:], common+[l1[0]]) + + +def common_directory(paths): + """Find the deepest common directory of a list of paths. + + :return: if no paths are provided, None is returned, + otherwise a directory with a trailing /. + """ + from bzrlib import osutils + def get_dir_with_slash(path): + if path.endswith('/'): + return path + else: + dirname, basename = osutils.split(path) + return dirname + '/' + + if not paths: + return None + elif len(paths) == 1: + return get_dir_with_slash(paths[0]) + else: + common = common_path(paths[0], paths[1])[0] + for path in paths[2:]: + common = common_path(common, path) + return get_dir_with_slash(''.join(common)) diff --git a/processors/filter_processor.py b/processors/filter_processor.py index d37585a..6d07088 100644 --- a/processors/filter_processor.py +++ b/processors/filter_processor.py @@ -23,6 +23,7 @@ from bzrlib.trace import ( ) from bzrlib.plugins.fastimport import ( commands, + helpers, processor, ) @@ -50,7 +51,7 @@ class FilterProcessor(processor.ImportProcessor): self.includes = self.params.get('include_paths') self.excludes = self.params.get('exclude_paths') # What's the new root, if any - self.new_root = self._find_new_root(self.includes) + self.new_root = helpers.common_directory(self.includes) # Buffer of blobs until we know we need them: mark -> cmd self.blobs = {} # These are the commits we've output so far diff --git a/tests/test_filter_processor.py b/tests/test_filter_processor.py index 0d0576e..fd7db59 100644 --- a/tests/test_filter_processor.py +++ b/tests/test_filter_processor.py @@ -197,3 +197,70 @@ ing from :100 M 644 :3 README.txt """) + + def test_subdir(self): + params = {'include_paths': ['doc/']} + self.assertFiltering(_SAMPLE_WITH_DIR, params, \ +"""blob +mark :1 +data 9 +Welcome! +commit refs/heads/master +mark :100 +committer a <b@c> 1234798653 +0000 +data 4 +test +M 644 :1 README.txt +blob +mark :3 +data 19 +Welcome! +my friend +blob +mark :4 +data 11 +== Docs == +commit refs/heads/master +mark :102 +committer d <b@c> 1234798653 +0000 +data 8 +test +ing +from :100 +M 644 :3 README.txt +M 644 :4 index.txt +""") + + def test_multiple_files_in_subdir(self): + # The new root should be the subdrectory + params = {'include_paths': ['doc/README.txt', 'doc/index.txt']} + self.assertFiltering(_SAMPLE_WITH_DIR, params, \ +"""blob +mark :1 +data 9 +Welcome! +commit refs/heads/master +mark :100 +committer a <b@c> 1234798653 +0000 +data 4 +test +M 644 :1 README.txt +blob +mark :3 +data 19 +Welcome! +my friend +blob +mark :4 +data 11 +== Docs == +commit refs/heads/master +mark :102 +committer d <b@c> 1234798653 +0000 +data 8 +test +ing +from :100 +M 644 :3 README.txt +M 644 :4 index.txt +""") |