Mercurial > hg
changeset 45854:4b4160a83303
dispatch: move some helper functions down into scmutil
I plan to reuse `formatparse()` in the next patch.
Differential Revision: https://phab.mercurial-scm.org/D9331
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Mon, 16 Nov 2020 16:00:13 -0800 |
parents | b4694ef45db5 |
children | c10683da6889 |
files | mercurial/chgserver.py mercurial/dispatch.py mercurial/scmutil.py |
diffstat | 3 files changed, 39 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/chgserver.py Mon Nov 16 15:11:51 2020 -0800 +++ b/mercurial/chgserver.py Mon Nov 16 16:00:13 2020 -0800 @@ -62,6 +62,7 @@ extensions, node, pycompat, + scmutil, util, ) @@ -503,13 +504,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
--- a/mercurial/dispatch.py Mon Nov 16 15:11:51 2020 -0800 +++ b/mercurial/dispatch.py Mon Nov 16 16:00:13 2020 -0800 @@ -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)
--- a/mercurial/scmutil.py Mon Nov 16 15:11:51 2020 -0800 +++ b/mercurial/scmutil.py Mon Nov 16 16:00:13 2020 -0800 @@ -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