diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py --- a/mercurial/templatefilters.py +++ b/mercurial/templatefilters.py @@ -7,6 +7,7 @@ from __future__ import absolute_import +import functools import os import re import time @@ -99,6 +100,38 @@ """ return os.path.basename(path) +@templatefilter('commonprefix') +def commonprefix(filelist): + """List of text. Treats each list item as file name with / + as path separator and returns the longest common directory + prefix shared by all list items. + Returns the empty string if no common prefix exists. + + The list items are not normalized, i.e. "foo/../bar" is handled as + file "bar" in the directory "foo/..". Leading slashes are ignored. + + For example, ["foo/bar/baz", "foo/baz/bar"] becomes "foo" and + ["foo/bar", "baz"] becomes "". + """ + seen = set() + def common(a, b): + if b in seen: + return a + seen.add(b) + if len(a) > len(b): + a, b = b, a + for i in xrange(len(a)): + if a[i] != b[i]: + return a[:i] + return a + try: + if not filelist: + return "" + dirlist = [tuple(f.lstrip('/').split('/')[:-1]) for f in filelist] + return '/'.join(functools.reduce(common, dirlist)) + except TypeError: + raise error.ParseError(_('argument is not a list of text')) + @templatefilter('count') def count(i): """List or text. Returns the length as an integer.""" diff --git a/tests/test-template-filters.t b/tests/test-template-filters.t new file mode 100644 --- /dev/null +++ b/tests/test-template-filters.t @@ -0,0 +1,21 @@ + $ hg debugtemplate '{""|splitlines|commonprefix}\n' + + $ hg debugtemplate '{"foo/bar\nfoo/baz\n"|splitlines|commonprefix}\n' + foo + $ hg debugtemplate '{"foo/bar\nfoo/bar\n"|splitlines|commonprefix}\n' + foo + $ hg debugtemplate '{"/foo/bar\n/foo/bar\n"|splitlines|commonprefix}\n' + foo + $ hg debugtemplate '{"/foo\n/foo\n"|splitlines|commonprefix}\n' + + $ hg debugtemplate '{"foo/bar\nbar/baz"|splitlines|commonprefix}\n' + + $ hg debugtemplate '{"foo/../bar\nfoo/bar"|splitlines|commonprefix}\n' + foo + $ hg debugtemplate '{"foo\n/foo"|splitlines|commonprefix}\n' + + $ hg init + $ hg log -r null -T '{rev|commonprefix}' + hg: parse error: argument is not a list of text + (template filter 'commonprefix' is not compatible with keyword 'rev') + [255]