view contrib/simplemerge @ 48555:c91418480cb0

simplemerge: use 3-way markers if mode=='merge3', ignoring number of labels Before this patch, we use a combination of `mode` and `labels` to decide which marker style to use. That's mostly my own fault because I was the one who added the `mergediff` as a "mode" instead of adding a separate argument for marker style. Still, let's make these arguments more orthogonal by ignoring the number of labels when deciding which marker style to use. This refactoring makes it a little clearer in the simplemerge *extension* that passing more than 2 `--label` arguments changes the marker style. Differential Revision: https://phab.mercurial-scm.org/D11972
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 14 Dec 2021 13:12:56 -0800
parents c102b704edb5
children 6ad70879d2bd
line wrap: on
line source

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

import getopt
import sys

import hgdemandimport

hgdemandimport.enable()

from mercurial.i18n import _
from mercurial import (
    context,
    error,
    fancyopts,
    pycompat,
    simplemerge,
    ui as uimod,
)
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))


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'))
    if len(opts[b'label']) > 2:
        opts[b'mode'] = b'merge3'
    local, base, other = args
    sys.exit(
        simplemerge.simplemerge(
            uimod.ui.load(),
            context.arbitraryfilectx(local),
            context.arbitraryfilectx(base),
            context.arbitraryfilectx(other),
            **pycompat.strkwargs(opts)
        )
    )
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)