contrib/simplemerge
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 18 Jan 2022 12:02:50 +0100
changeset 48649 a746d13987a6
parent 48578 77e24ee8994b
child 48749 9ee70e175fed
permissions -rw-r--r--
stream-requirements: smoother matching in test-ssh-proto.t The value of `streamreqs` changes depending of the format used, but this is not relevant for this tests. So we doing a smarter matching of the line to avoid this test interfering with format changes in the future. Differential Revision: https://phab.mercurial-scm.org/D12080
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45830
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 45055
diff changeset
     1
#!/usr/bin/env python3
33895
aed91971d88c simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents: 30576
diff changeset
     2
from __future__ import absolute_import
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     3
30576
541949a10a68 fancyopts: switch from fancyopts.getopt.* to getopt.*
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30559
diff changeset
     4
import getopt
19378
9de689d20230 cleanup: drop unused variables and an unused import
Simon Heimberg <simohe@besonet.ch>
parents: 19022
diff changeset
     5
import sys
33895
aed91971d88c simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents: 30576
diff changeset
     6
aed91971d88c simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents: 30576
diff changeset
     7
import hgdemandimport
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
     8
33895
aed91971d88c simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents: 30576
diff changeset
     9
hgdemandimport.enable()
aed91971d88c simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents: 30576
diff changeset
    10
4363
2e3c54fb79a3 actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4362
diff changeset
    11
from mercurial.i18n import _
33895
aed91971d88c simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents: 30576
diff changeset
    12
from mercurial import (
34051
d2fc88426d21 context: add arbitraryfilectx, which can represent files outside the workdir
Phil Cohen <phillco@fb.com>
parents: 34050
diff changeset
    13
    context,
33895
aed91971d88c simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents: 30576
diff changeset
    14
    error,
aed91971d88c simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents: 30576
diff changeset
    15
    fancyopts,
39791
1dd82ecb869b py3: use pycompat.strkwargs() in contrib/simplemerge
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 39790
diff changeset
    16
    pycompat,
33895
aed91971d88c simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents: 30576
diff changeset
    17
    simplemerge,
aed91971d88c simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents: 30576
diff changeset
    18
    ui as uimod,
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34051
diff changeset
    19
)
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    20
from mercurial.utils import procutil, stringutil
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    21
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    22
options = [
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    23
    (b'L', b'label', [], _(b'labels to use on conflict markers')),
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    24
    (b'a', b'text', None, _(b'treat all files as text')),
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    25
    (b'p', b'print', None, _(b'print results instead of overwriting LOCAL')),
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    26
    (b'', b'no-minimal', None, _(b'no effect (DEPRECATED)')),
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    27
    (b'h', b'help', None, _(b'display help and exit')),
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    28
    (b'q', b'quiet', None, _(b'suppress output')),
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    29
]
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    30
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    31
usage = _(
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    32
    b'''simplemerge [OPTS] LOCAL BASE OTHER
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    33
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    34
    Simple three-way file merge utility with a minimal feature set.
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
    35
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    36
    Apply to LOCAL the changes necessary to go from BASE to OTHER.
5081
ea7b982b6c08 Remove trailing spaces
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4408
diff changeset
    37
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    38
    By default, LOCAL is overwritten with the results of this operation.
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    39
'''
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    40
)
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    41
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    42
6002
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
    43
class ParseError(Exception):
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
    44
    """Exception raised on errors in parsing the command line."""
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
    45
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    46
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    47
def showhelp():
45055
4c1b4805db57 pycompat: change users of pycompat.{stdin,stdout,stderr} to use procutil.std*
Manuel Jacob <me@manueljacob.de>
parents: 43659
diff changeset
    48
    procutil.stdout.write(usage)
4c1b4805db57 pycompat: change users of pycompat.{stdin,stdout,stderr} to use procutil.std*
Manuel Jacob <me@manueljacob.de>
parents: 43659
diff changeset
    49
    procutil.stdout.write(b'\noptions:\n')
4362
465b9ea02868 Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    50
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    51
    out_opts = []
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    52
    for shortopt, longopt, default, desc in options:
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    53
        out_opts.append(
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    54
            (
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    55
                b'%2s%s'
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    56
                % (
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    57
                    shortopt and b'-%s' % shortopt,
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    58
                    longopt and b' --%s' % longopt,
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    59
                ),
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    60
                b'%s' % desc,
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    61
            )
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    62
        )
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    63
    opts_len = max([len(opt[0]) for opt in out_opts])
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    64
    for first, second in out_opts:
45055
4c1b4805db57 pycompat: change users of pycompat.{stdin,stdout,stderr} to use procutil.std*
Manuel Jacob <me@manueljacob.de>
parents: 43659
diff changeset
    65
        procutil.stdout.write(b' %-*s  %s\n' % (opts_len, first, second))
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    66
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    67
6002
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
    68
try:
45055
4c1b4805db57 pycompat: change users of pycompat.{stdin,stdout,stderr} to use procutil.std*
Manuel Jacob <me@manueljacob.de>
parents: 43659
diff changeset
    69
    for fp in (sys.stdin, procutil.stdout, sys.stderr):
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34051
diff changeset
    70
        procutil.setbinary(fp)
19022
cba222f01056 tests: run check-code on Python files without .py extension
Mads Kiilerich <madski@unity3d.com>
parents: 14233
diff changeset
    71
6002
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
    72
    opts = {}
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    73
    try:
40260
b54d93fc3ba8 simplemerge: port to Python 3
Augie Fackler <augie@google.com>
parents: 39791
diff changeset
    74
        bargv = [a.encode('utf8') for a in sys.argv[1:]]
b54d93fc3ba8 simplemerge: port to Python 3
Augie Fackler <augie@google.com>
parents: 39791
diff changeset
    75
        args = fancyopts.fancyopts(bargv, options, opts)
30576
541949a10a68 fancyopts: switch from fancyopts.getopt.* to getopt.*
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30559
diff changeset
    76
    except getopt.GetoptError as e:
6002
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
    77
        raise ParseError(e)
39790
d3e940a32be0 py3: add b'' prefixes in contrib/simplemerge
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 37120
diff changeset
    78
    if opts[b'help']:
4364
d5c3a70f8422 polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4363
diff changeset
    79
        showhelp()
6002
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
    80
        sys.exit(0)
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
    81
    if len(args) != 3:
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
    82
        raise ParseError(_(b'wrong number of arguments').decode('utf8'))
48555
c91418480cb0 simplemerge: use 3-way markers if mode=='merge3', ignoring number of labels
Martin von Zweigbergk <martinvonz@google.com>
parents: 45830
diff changeset
    83
    if len(opts[b'label']) > 2:
c91418480cb0 simplemerge: use 3-way markers if mode=='merge3', ignoring number of labels
Martin von Zweigbergk <martinvonz@google.com>
parents: 45830
diff changeset
    84
        opts[b'mode'] = b'merge3'
33903
ed6f64173121 contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents: 33895
diff changeset
    85
    local, base, other = args
48560
6ad70879d2bd simplemerge: move default labels to simplemerge extension
Martin von Zweigbergk <martinvonz@google.com>
parents: 48555
diff changeset
    86
    overrides = opts[b'label']
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    87
    if len(overrides) > 3:
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    88
        raise error.InputError(b'can only specify three labels.')
48560
6ad70879d2bd simplemerge: move default labels to simplemerge extension
Martin von Zweigbergk <martinvonz@google.com>
parents: 48555
diff changeset
    89
    labels = [local, other, base]
6ad70879d2bd simplemerge: move default labels to simplemerge extension
Martin von Zweigbergk <martinvonz@google.com>
parents: 48555
diff changeset
    90
    labels[: len(overrides)] = overrides
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    91
    local_input = simplemerge.MergeInput(
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    92
        context.arbitraryfilectx(local), labels[0]
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    93
    )
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    94
    other_input = simplemerge.MergeInput(
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    95
        context.arbitraryfilectx(other), labels[1]
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    96
    )
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    97
    base_input = simplemerge.MergeInput(
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    98
        context.arbitraryfilectx(base), labels[2]
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
    99
    )
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
   100
    sys.exit(
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
   101
        simplemerge.simplemerge(
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
   102
            uimod.ui.load(),
48578
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
   103
            local_input,
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
   104
            base_input,
77e24ee8994b simplemerge: take arguments as annotated context objects
Martin von Zweigbergk <martinvonz@google.com>
parents: 48560
diff changeset
   105
            other_input,
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
   106
            **pycompat.strkwargs(opts)
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
   107
        )
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43367
diff changeset
   108
    )
28047
863075fd4cd0 misc: use modern exception syntax
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26587
diff changeset
   109
except ParseError as e:
43367
3c2799cbace4 py3: fix exception display encoding in contrib/simplemerge.py
Emmanuel Leblond <emmanuel.leblond@gmail.com>
parents: 40265
diff changeset
   110
    e = stringutil.forcebytestr(e)
45055
4c1b4805db57 pycompat: change users of pycompat.{stdin,stdout,stderr} to use procutil.std*
Manuel Jacob <me@manueljacob.de>
parents: 43659
diff changeset
   111
    procutil.stdout.write(b"%s: %s\n" % (sys.argv[0].encode('utf8'), e))
6002
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
   112
    showhelp()
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
   113
    sys.exit(1)
28047
863075fd4cd0 misc: use modern exception syntax
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26587
diff changeset
   114
except error.Abort as e:
45055
4c1b4805db57 pycompat: change users of pycompat.{stdin,stdout,stderr} to use procutil.std*
Manuel Jacob <me@manueljacob.de>
parents: 43659
diff changeset
   115
    procutil.stderr.write(b"abort: %s\n" % e)
6002
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
   116
    sys.exit(255)
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
   117
except KeyboardInterrupt:
abd66eb0889e merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents: 5081
diff changeset
   118
    sys.exit(255)