contrib/simplemerge
author Gregory Szorc <gregory.szorc@gmail.com>
Sat, 05 May 2018 18:35:16 -0700
changeset 37844 8fb9985382be
parent 37120 a8a902d7176e
child 39790 d3e940a32be0
permissions -rwxr-xr-x
pycompat: export queue module instead of symbols in module (API) Previously, pycompat and util re-exported individual symbols from the queue module. This had the side-effect of forcing the loading of the queue module whenever pycompat/util was imported. These symbols aren't used very often. So importing the module to get a handle on the symbols is wasteful. This commit changes pycompat so it no longer exports the individual symbols in the queue module. Instead, we make the imported module a "public" symbol. We drop the individual symbol aliases from the util module. All consumers are updated to use pycompat.queue.* instead. This change makes 300 invocations of `hg log -r. -T '{rev}\n'` a little faster: before: 18.44s after: 17.87s Differential Revision: https://phab.mercurial-scm.org/D3441

#!/usr/bin/env python
from __future__ import absolute_import

import getopt
import sys

import hgdemandimport
hgdemandimport.enable()

from mercurial.i18n import _
from mercurial import (
    context,
    error,
    fancyopts,
    simplemerge,
    ui as uimod,
)
from mercurial.utils import (
    procutil,
)

options = [('L', 'label', [], _('labels to use on conflict markers')),
           ('a', 'text', None, _('treat all files as text')),
           ('p', 'print', None,
            _('print results instead of overwriting LOCAL')),
           ('', 'no-minimal', None, _('no effect (DEPRECATED)')),
           ('h', 'help', None, _('display help and exit')),
           ('q', 'quiet', None, _('suppress output'))]

usage = _('''simplemerge [OPTS] LOCAL BASE OTHER

    Simple three-way file merge utility with a minimal feature set.

    Apply to LOCAL the changes necessary to go from BASE to OTHER.

    By default, LOCAL is overwritten with the results of this operation.
''')

class ParseError(Exception):
    """Exception raised on errors in parsing the command line."""

def showhelp():
    sys.stdout.write(usage)
    sys.stdout.write('\noptions:\n')

    out_opts = []
    for shortopt, longopt, default, desc in options:
        out_opts.append(('%2s%s' % (shortopt and '-%s' % shortopt,
                                    longopt and ' --%s' % longopt),
                         '%s' % desc))
    opts_len = max([len(opt[0]) for opt in out_opts])
    for first, second in out_opts:
        sys.stdout.write(' %-*s  %s\n' % (opts_len, first, second))

try:
    for fp in (sys.stdin, sys.stdout, sys.stderr):
        procutil.setbinary(fp)

    opts = {}
    try:
        args = fancyopts.fancyopts(sys.argv[1:], options, opts)
    except getopt.GetoptError as e:
        raise ParseError(e)
    if opts['help']:
        showhelp()
        sys.exit(0)
    if len(args) != 3:
            raise ParseError(_('wrong number of arguments'))
    local, base, other = args
    sys.exit(simplemerge.simplemerge(uimod.ui.load(),
                                     context.arbitraryfilectx(local),
                                     context.arbitraryfilectx(base),
                                     context.arbitraryfilectx(other),
                                     **opts))
except ParseError as e:
    sys.stdout.write("%s: %s\n" % (sys.argv[0], e))
    showhelp()
    sys.exit(1)
except error.Abort as e:
    sys.stderr.write("abort: %s\n" % e)
    sys.exit(255)
except KeyboardInterrupt:
    sys.exit(255)