mercurial/error.py
changeset 45897 8cc9e7f762d6
parent 45892 ac362d5a7893
child 45898 1817b66897ad
--- a/mercurial/error.py	Thu Nov 19 09:19:44 2020 -0800
+++ b/mercurial/error.py	Thu Nov 19 12:20:26 2020 -0800
@@ -13,6 +13,8 @@
 
 from __future__ import absolute_import
 
+import difflib
+
 # Do not import anything but pycompat here, please
 from . import pycompat
 
@@ -270,6 +272,25 @@
     __bytes__ = _tobytes
 
 
+def getsimilar(symbols, value):
+    sim = lambda x: difflib.SequenceMatcher(None, value, x).ratio()
+    # The cutoff for similarity here is pretty arbitrary. It should
+    # probably be investigated and tweaked.
+    return [s for s in symbols if sim(s) > 0.6]
+
+
+def similarity_hint(similar):
+    from .i18n import _
+
+    if len(similar) == 1:
+        return _(b"did you mean %s?") % similar[0]
+    elif similar:
+        ss = b", ".join(sorted(similar))
+        return _(b"did you mean one of %s?") % ss
+    else:
+        return None
+
+
 class UnknownIdentifier(ParseError):
     """Exception raised when a {rev,file}set references an unknown identifier"""