diff mercurial/scmutil.py @ 45882:8cc9e7f762d6

errors: move similarity_hint() to error module I want to be able to reuse it from `UnknownIdentifier`'s constructor. Moving it results in a new import of `difflib` in the `error` module. There was a comment at the top of `error.py` saying "Do not import anything but pycompat here, please", which was added (except for the "pycompat" bit) in 08cabecfa8a8 (errors: move revlog errors, 2009-01-11). I don't know the reason for the comment. I'm guessing the point was to not make the module depend on other Mercurial modules. If that was it, then importing `difflib` should be fine. Sorry about the churn (I moved this code from the `dispatch` module to the `scmutil` module very recently). Differential Revision: https://phab.mercurial-scm.org/D9345
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 19 Nov 2020 12:20:26 -0800
parents bb1b7a5bc96b
children 1817b66897ad
line wrap: on
line diff
--- a/mercurial/scmutil.py	Thu Nov 19 09:19:44 2020 -0800
+++ b/mercurial/scmutil.py	Thu Nov 19 12:20:26 2020 -0800
@@ -7,7 +7,6 @@
 
 from __future__ import absolute_import
 
-import difflib
 import errno
 import glob
 import os
@@ -143,23 +142,6 @@
         ui.status(_(b"no changes found\n"))
 
 
-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):
-    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
-
-
 def formatparse(write, inst):
     if inst.location is not None:
         write(
@@ -170,8 +152,8 @@
         write(_(b"hg: parse error: %s\n") % inst.message)
     if isinstance(inst, error.UnknownIdentifier):
         # make sure to check fileset first, as revset can invoke fileset
-        similar = getsimilar(inst.symbols, inst.function)
-        hint = similarity_hint(similar)
+        similar = error.getsimilar(inst.symbols, inst.function)
+        hint = error.similarity_hint(similar)
         if hint:
             write(b"(%s)\n" % hint)
     elif inst.hint: