##// END OF EJS Templates
templatefilters: add commonprefix...
Joerg Sonnenberger -
r38198:56dd1517 default
parent child Browse files
Show More
@@ -0,0 +1,23 b''
1 $ hg debugtemplate '{""|splitlines|commonprefix}\n'
2
3 $ hg debugtemplate '{"foo/bar\nfoo/baz\nfoo/foobar\n"|splitlines|commonprefix}\n'
4 foo
5 $ hg debugtemplate '{"foo/bar\nfoo/bar\n"|splitlines|commonprefix}\n'
6 foo
7 $ hg debugtemplate '{"/foo/bar\n/foo/bar\n"|splitlines|commonprefix}\n'
8 foo
9 $ hg debugtemplate '{"/foo\n/foo\n"|splitlines|commonprefix}\n'
10
11 $ hg debugtemplate '{"foo/bar\nbar/baz"|splitlines|commonprefix}\n'
12
13 $ hg debugtemplate '{"foo/bar\nbar/baz\nbar/foo\n"|splitlines|commonprefix}\n'
14
15 $ hg debugtemplate '{"foo/../bar\nfoo/bar"|splitlines|commonprefix}\n'
16 foo
17 $ hg debugtemplate '{"foo\n/foo"|splitlines|commonprefix}\n'
18
19 $ hg init
20 $ hg log -r null -T '{rev|commonprefix}'
21 hg: parse error: argument is not a list of text
22 (template filter 'commonprefix' is not compatible with keyword 'rev')
23 [255]
@@ -99,6 +99,45 b' def basename(path):'
99 """
99 """
100 return os.path.basename(path)
100 return os.path.basename(path)
101
101
102 @templatefilter('commonprefix')
103 def commonprefix(filelist):
104 """List of text. Treats each list item as file name with /
105 as path separator and returns the longest common directory
106 prefix shared by all list items.
107 Returns the empty string if no common prefix exists.
108
109 The list items are not normalized, i.e. "foo/../bar" is handled as
110 file "bar" in the directory "foo/..". Leading slashes are ignored.
111
112 For example, ["foo/bar/baz", "foo/baz/bar"] becomes "foo" and
113 ["foo/bar", "baz"] becomes "".
114 """
115 def common(a, b):
116 if len(a) > len(b):
117 a = b[:len(a)]
118 elif len(b) > len(a):
119 b = b[:len(a)]
120 if a == b:
121 return a
122 for i in xrange(len(a)):
123 if a[i] != b[i]:
124 return a[:i]
125 return a
126 try:
127 if not filelist:
128 return ""
129 dirlist = [f.lstrip('/').split('/')[:-1] for f in filelist]
130 if len(dirlist) == 1:
131 return '/'.join(dirlist[0])
132 a = min(dirlist)
133 b = max(dirlist)
134 # The common prefix of a and b is shared with all
135 # elements of the list since Python sorts lexicographical
136 # and [1, x] after [1].
137 return '/'.join(common(a, b))
138 except TypeError:
139 raise error.ParseError(_('argument is not a list of text'))
140
102 @templatefilter('count')
141 @templatefilter('count')
103 def count(i):
142 def count(i):
104 """List or text. Returns the length as an integer."""
143 """List or text. Returns the length as an integer."""
General Comments 0
You need to be logged in to leave comments. Login now