summaryrefslogtreecommitdiff
path: root/fastimport/helpers.py
diff options
context:
space:
mode:
Diffstat (limited to 'fastimport/helpers.py')
-rw-r--r--fastimport/helpers.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/fastimport/helpers.py b/fastimport/helpers.py
index 05cce6f..8e9a383 100644
--- a/fastimport/helpers.py
+++ b/fastimport/helpers.py
@@ -92,4 +92,33 @@ def binary_stream(stream):
return stream
+def common_directory(paths):
+ """Find the deepest common directory of a list of paths.
+
+ :return: if no paths are provided, None is returned;
+ if there is no common directory, '' is returned;
+ otherwise the common directory with a trailing / is returned.
+ """
+ from bzrlib import osutils
+ def get_dir_with_slash(path):
+ if path == '' or path.endswith('/'):
+ return path
+ else:
+ dirname, basename = osutils.split(path)
+ if dirname == '':
+ return dirname
+ else:
+ 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])
+ for path in paths[2:]:
+ common = common_path(common, path)
+ return get_dir_with_slash(common)
+
+