Patchwork D9331: dispatch: move some helper functions down into scmutil

login
register
mail settings
Submitter phabricator
Date Nov. 17, 2020, 12:28 a.m.
Message ID <differential-rev-PHID-DREV-m55seu5xvl436h4mxore-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47605/
State Superseded
Headers show

Comments

phabricator - Nov. 17, 2020, 12:28 a.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  I plan to reuse `formatparse()` in the next patch.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/chgserver.py
  mercurial/dispatch.py
  mercurial/scmutil.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -7,6 +7,7 @@ 
 
 from __future__ import absolute_import
 
+import difflib
 import errno
 import glob
 import os
@@ -142,6 +143,38 @@ 
         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 reportsimilar(write, similar):
+    if len(similar) == 1:
+        write(_(b"(did you mean %s?)\n") % similar[0])
+    elif similar:
+        ss = b", ".join(sorted(similar))
+        write(_(b"(did you mean one of %s?)\n") % ss)
+
+
+def formatparse(write, inst):
+    similar = []
+    if isinstance(inst, error.UnknownIdentifier):
+        # make sure to check fileset first, as revset can invoke fileset
+        similar = getsimilar(inst.symbols, inst.function)
+    if inst.location is not None:
+        write(
+            _(b"hg: parse error at %s: %s\n")
+            % (pycompat.bytestr(inst.location), inst.message)
+        )
+    else:
+        write(_(b"hg: parse error: %s\n") % inst.message)
+        reportsimilar(write, similar)
+    if inst.hint:
+        write(_(b"(%s)\n") % inst.hint)
+
+
 def callcatch(ui, func):
     """call func() with global exception handling
 
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -7,7 +7,6 @@ 
 
 from __future__ import absolute_import, print_function
 
-import difflib
 import errno
 import getopt
 import io
@@ -226,38 +225,6 @@ 
         pass
 
 
-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 _reportsimilar(write, similar):
-    if len(similar) == 1:
-        write(_(b"(did you mean %s?)\n") % similar[0])
-    elif similar:
-        ss = b", ".join(sorted(similar))
-        write(_(b"(did you mean one of %s?)\n") % ss)
-
-
-def _formatparse(write, inst):
-    similar = []
-    if isinstance(inst, error.UnknownIdentifier):
-        # make sure to check fileset first, as revset can invoke fileset
-        similar = _getsimilar(inst.symbols, inst.function)
-    if inst.location is not None:
-        write(
-            _(b"hg: parse error at %s: %s\n")
-            % (pycompat.bytestr(inst.location), inst.message)
-        )
-    else:
-        write(_(b"hg: parse error: %s\n") % inst.message)
-        _reportsimilar(write, similar)
-    if inst.hint:
-        write(_(b"(%s)\n") % inst.hint)
-
-
 def _formatargs(args):
     return b' '.join(procutil.shellquote(a) for a in args)
 
@@ -294,7 +261,7 @@ 
                 ferr.write(_(b"(%s)\n") % inst.hint)
             return -1
         except error.ParseError as inst:
-            _formatparse(ferr.write, inst)
+            scmutil.formatparse(ferr.write, inst)
             return -1
 
         msg = _formatargs(req.args)
@@ -502,7 +469,7 @@ 
             ui.warn(_(b"hg: %s\n") % inst.message)
             ui.warn(_(b"(use 'hg help -v' for a list of global options)\n"))
     except error.ParseError as inst:
-        _formatparse(ui.warn, inst)
+        scmutil.formatparse(ui.warn, inst)
         return -1
     except error.UnknownCommand as inst:
         nocmdmsg = _(b"hg: unknown command '%s'\n") % inst.command
@@ -517,10 +484,10 @@ 
         except (error.UnknownCommand, error.Abort):
             suggested = False
             if inst.all_commands:
-                sim = _getsimilar(inst.all_commands, inst.command)
+                sim = scmutil.getsimilar(inst.all_commands, inst.command)
                 if sim:
                     ui.warn(nocmdmsg)
-                    _reportsimilar(ui.warn, sim)
+                    scmutil.reportsimilar(ui.warn, sim)
                     suggested = True
             if not suggested:
                 ui.warn(nocmdmsg)
diff --git a/mercurial/chgserver.py b/mercurial/chgserver.py
--- a/mercurial/chgserver.py
+++ b/mercurial/chgserver.py
@@ -62,6 +62,7 @@ 
     extensions,
     node,
     pycompat,
+    scmutil,
     util,
 )
 
@@ -491,13 +492,11 @@ 
         list, the client can continue with this server after completing all
         the instructions.
         """
-        from . import dispatch  # avoid cycle
-
         args = self._readlist()
         try:
             self.ui, lui = _loadnewui(self.ui, args, self.cdebug)
         except error.ParseError as inst:
-            dispatch._formatparse(self.ui.warn, inst)
+            scmutil.formatparse(self.ui.warn, inst)
             self.ui.flush()
             self.cresult.write(b'exit 255')
             return