Patchwork D3439: templatefilters: add commonprefix

login
register
mail settings
Submitter phabricator
Date May 8, 2018, 9:14 p.m.
Message ID <7bcc6b31b0d372b96f48da9f425dbe97@localhost.localdomain>
Download mbox | patch
Permalink /patch/31422/
State Not Applicable
Headers show

Comments

phabricator - May 8, 2018, 9:14 p.m.
joerg.sonnenberger updated this revision to Diff 8590.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D3439?vs=8549&id=8590

REVISION DETAIL
  https://phab.mercurial-scm.org/D3439

AFFECTED FILES
  mercurial/templatefilters.py
  tests/test-template-filters.t

CHANGE DETAILS




To: joerg.sonnenberger, #hg-reviewers
Cc: yuja, mercurial-devel
Yuya Nishihara - May 9, 2018, 12:45 p.m.
Please add tests of edge cases.

 - `str|commonprefix`
 - infinite loop (e.g. `["/foo", "bar"]`)
 - exact match (e.g. `["/foo"]` and `["/foo", "/foo"]`)
 - empty list

`os.path.commonprefix()` will provide some hints to avoid comparison of
all list elements by using min()/max().
phabricator - May 9, 2018, 12:46 p.m.
yuja added a comment.


  Please add tests of edge cases.
  
  - `str|commonprefix`
  - infinite loop (e.g. `["/foo", "bar"]`)
  - exact match (e.g. `["/foo"]` and `["/foo", "/foo"]`)
  - empty list
  
  `os.path.commonprefix()` will provide some hints to avoid comparison of
  all list elements by using min()/max().

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3439

To: joerg.sonnenberger, #hg-reviewers
Cc: yuja, mercurial-devel

Patch

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,11 @@ 
+  $ hg debugtemplate '{"foo/bar\nfoo/baz\n"|splitlines|commonprefix}\n'
+  foo
+  $ hg debugtemplate '{"foo/bar\nbar/baz"|splitlines|commonprefix}\n'
+  
+  $ hg debugtemplate '{"foo/../bar\nfoo/bar"|splitlines|commonprefix}\n'
+  foo
+  $ 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]
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,33 @@ 
     """
     return os.path.basename(path)
 
+@templatefilter('commonprefix')
+def commonprefix(filelist):
+    """List of text. Treats each list item as file name, 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/..".
+
+    For example, ["foo/bar/baz", "foo/baz/bar"] becomes "foo" and
+    ["foo/bar", "baz"] becomes "".
+    """
+    def common(a, b):
+        while a != b:
+            if len(a) > len(b):
+                a = os.path.dirname(a)
+            else:
+                b = os.path.dirname(b)
+        return a
+    try:
+        if not filelist:
+            return ""
+        dirlist = [os.path.dirname(f) for f in filelist]
+        return 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."""