contrib/simplemerge
author Pierre-Yves David <pierre-yves.david@octobus.net>
Sun, 14 Apr 2024 02:33:36 +0200
changeset 51587 8cd317c033b8
parent 48875 6000f5b25c9b
permissions -rw-r--r--
stream-clone: disable gc for the entry listing section for the v2 format This is similar to the change we did for the v3 format in 6e4c8366c5ce. The benchmark bellow show this gives us a notable gains, especially on larger repositories. ### benchmark.name = hg.perf.stream-locked-section # benchmark.name = hg.perf.stream-locked-section # bin-env-vars.hg.flavor = default # bin-env-vars.hg.py-re2-module = default # benchmark.variants.version = v2 ## data-env-vars.name = pypy-2018-08-01-zstd-sparse-revlog 5e931bf8707c: 0.503820 ~~~~~ 1106d1bf695e: 0.470078 (-6.70%, -0.03) ## data-env-vars.name = pypy-2024-03-22-zstd-sparse-revlog 5e931bf8707c: 0.535756 ~~~~~ 1106d1bf695e: 0.490249 (-8.49%, -0.05) ## data-env-vars.name = heptapod-public-2024-03-25-zstd-sparse-revlog 5e931bf8707c: 1.327041 ~~~~~ 1106d1bf695e: 1.174636 (-11.48%, -0.15) ## data-env-vars.name = netbeans-2018-08-01-zstd-sparse-revlog 5e931bf8707c: 2.439158 ~~~~~ 1106d1bf695e: 2.220515 (-8.96%, -0.22) ## data-env-vars.name = netbeans-2019-11-07-zstd-sparse-revlog 5e931bf8707c: 2.630794 ~~~~~ 1106d1bf695e: 2.261473 (-14.04%, -0.37) ## data-env-vars.name = mozilla-central-2018-08-01-zstd-sparse-revlog 5e931bf8707c: 5.769002 ~~~~~ 1106d1bf695e: 5.062000 (-12.26%, -0.71) ## data-env-vars.name = mozilla-try-2019-02-18-zstd-sparse-revlog 5e931bf8707c: 13.351750 ~~~~~ 1106d1bf695e: 12.346655 (-7.53%, -1.01) ## data-env-vars.name = mozilla-central-2024-03-22-zstd-sparse-revlog 5e931bf8707c: 10.772939 ~~~~~ 1106d1bf695e: 9.495407 (-11.86%, -1.28) ## data-env-vars.name = mozilla-unified-2024-03-22-zstd-sparse-revlog 5e931bf8707c: 10.864297 ~~~~~ 1106d1bf695e: 9.475597 (-12.78%, -1.39) ## data-env-vars.name = mozilla-try-2023-03-22-zstd-sparse-revlog 5e931bf8707c: 17.448335 ~~~~~ 1106d1bf695e: 16.027474 (-8.14%, -1.42)

#!/usr/bin/env python3

import getopt
import sys

import hgdemandimport

hgdemandimport.enable()

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

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

usage = _(
    b'''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():
    procutil.stdout.write(usage)
    procutil.stdout.write(b'\noptions:\n')

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


def _verifytext(input, ui, quiet=False, allow_binary=False):
    """verifies that text is non-binary (unless opts[text] is passed,
    then we just warn)"""
    if stringutil.binary(input.text()):
        msg = _(b"%s looks like a binary file.") % input.fctx.path()
        if not quiet:
            ui.warn(_(b'warning: %s\n') % msg)
        if not allow_binary:
            sys.exit(1)


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

    opts = {}
    try:
        bargv = [a.encode('utf8') for a in sys.argv[1:]]
        args = fancyopts.fancyopts(bargv, options, opts)
    except getopt.GetoptError as e:
        raise ParseError(e)
    if opts[b'help']:
        showhelp()
        sys.exit(0)
    if len(args) != 3:
        raise ParseError(_(b'wrong number of arguments').decode('utf8'))
    mode = b'merge'
    if len(opts[b'label']) > 2:
        mode = b'merge3'
    local, base, other = args
    overrides = opts[b'label']
    if len(overrides) > 3:
        raise error.InputError(b'can only specify three labels.')
    labels = [local, other, base]
    labels[: len(overrides)] = overrides
    local_input = simplemerge.MergeInput(
        context.arbitraryfilectx(local), labels[0]
    )
    other_input = simplemerge.MergeInput(
        context.arbitraryfilectx(other), labels[1]
    )
    base_input = simplemerge.MergeInput(
        context.arbitraryfilectx(base), labels[2]
    )

    quiet = opts.get(b'quiet')
    allow_binary = opts.get(b'text')
    ui = uimod.ui.load()
    _verifytext(local_input, ui, quiet=quiet, allow_binary=allow_binary)
    _verifytext(base_input, ui, quiet=quiet, allow_binary=allow_binary)
    _verifytext(other_input, ui, quiet=quiet, allow_binary=allow_binary)

    merged_text, conflicts = simplemerge.simplemerge(
        local_input,
        base_input,
        other_input,
        mode,
        allow_binary=allow_binary,
    )
    if opts.get(b'print'):
        ui.fout.write(merged_text)
    else:
        util.writefile(local, merged_text)
    sys.exit(1 if conflicts else 0)
except ParseError as e:
    e = stringutil.forcebytestr(e)
    procutil.stdout.write(b"%s: %s\n" % (sys.argv[0].encode('utf8'), e))
    showhelp()
    sys.exit(1)
except error.Abort as e:
    procutil.stderr.write(b"abort: %s\n" % e)
    sys.exit(255)
except KeyboardInterrupt:
    sys.exit(255)