Debian Codegraph @

contrib/simplemerge

raw
1 #!/usr/bin/env python3
2
3 import getopt
4 import sys
5
6 import hgdemandimport
7
8 hgdemandimport.enable()
9
10 from mercurial.i18n import _
11 from mercurial import (
12 context,
13 error,
14 fancyopts,
15 simplemerge,
16 ui as uimod,
17 util,
18 )
19 from mercurial.utils import procutil, stringutil
20
21 options = [
22 (b'L', b'label', [], _(b'labels to use on conflict markers')),
23 (b'a', b'text', None, _(b'treat all files as text')),
24 (b'p', b'print', None, _(b'print results instead of overwriting LOCAL')),
25 (b'', b'no-minimal', None, _(b'no effect (DEPRECATED)')),
26 (b'h', b'help', None, _(b'display help and exit')),
27 (b'q', b'quiet', None, _(b'suppress output')),
28 ]
29
30 usage = _(
31 b'''simplemerge [OPTS] LOCAL BASE OTHER
32
33 Simple three-way file merge utility with a minimal feature set.
34
35 Apply to LOCAL the changes necessary to go from BASE to OTHER.
36
37 By default, LOCAL is overwritten with the results of this operation.
38 '''
39 )
40
41
42 class ParseError(Exception):
43 """Exception raised on errors in parsing the command line."""
44
45
46 def showhelp():
47 procutil.stdout.write(usage)
48 procutil.stdout.write(b'\noptions:\n')
49
50 out_opts = []
51 for shortopt, longopt, default, desc in options:
52 out_opts.append(
53 (
54 b'%2s%s'
55 % (
56 shortopt and b'-%s' % shortopt,
57 longopt and b' --%s' % longopt,
58 ),
59 b'%s' % desc,
60 )
61 )
62 opts_len = max([len(opt[0]) for opt in out_opts])
63 for first, second in out_opts:
64 procutil.stdout.write(b' %-*s %s\n' % (opts_len, first, second))
65
66
67 def _verifytext(input, ui, quiet=False, allow_binary=False):
68 """verifies that text is non-binary (unless opts[text] is passed,
69 then we just warn)"""
70 if stringutil.binary(input.text()):
71 msg = _(b"%s looks like a binary file.") % input.fctx.path()
72 if not quiet:
73 ui.warn(_(b'warning: %s\n') % msg)
74 if not allow_binary:
75 sys.exit(1)
76
77
78 try:
79 for fp in (sys.stdin, procutil.stdout, sys.stderr):
80 procutil.setbinary(fp)
81
82 opts = {}
83 try:
84 bargv = [a.encode('utf8') for a in sys.argv[1:]]
85 args = fancyopts.fancyopts(bargv, options, opts)
86 except getopt.GetoptError as e:
87 raise ParseError(e)
88 if opts[b'help']:
89 showhelp()
90 sys.exit(0)
91 if len(args) != 3:
92 raise ParseError(_(b'wrong number of arguments').decode('utf8'))
93 mode = b'merge'
94 if len(opts[b'label']) > 2:
95 mode = b'merge3'
96 local, base, other = args
97 overrides = opts[b'label']
98 if len(overrides) > 3:
99 raise error.InputError(b'can only specify three labels.')
100 labels = [local, other, base]
101 labels[: len(overrides)] = overrides
102 local_input = simplemerge.MergeInput(
103 context.arbitraryfilectx(local), labels[0]
104 )
105 other_input = simplemerge.MergeInput(
106 context.arbitraryfilectx(other), labels[1]
107 )
108 base_input = simplemerge.MergeInput(
109 context.arbitraryfilectx(base), labels[2]
110 )
111
112 quiet = opts.get(b'quiet')
113 allow_binary = opts.get(b'text')
114 ui = uimod.ui.load()
115 _verifytext(local_input, ui, quiet=quiet, allow_binary=allow_binary)
116 _verifytext(base_input, ui, quiet=quiet, allow_binary=allow_binary)
117 _verifytext(other_input, ui, quiet=quiet, allow_binary=allow_binary)
118
119 merged_text, conflicts = simplemerge.simplemerge(
120 local_input,
121 base_input,
122 other_input,
123 mode,
124 allow_binary=allow_binary,
125 )
126 if opts.get(b'print'):
127 ui.fout.write(merged_text)
128 else:
129 util.writefile(local, merged_text)
130 sys.exit(1 if conflicts else 0)
131 except ParseError as e:
132 e = stringutil.forcebytestr(e)
133 procutil.stdout.write(b"%s: %s\n" % (sys.argv[0].encode('utf8'), e))
134 showhelp()
135 sys.exit(1)
136 except error.Abort as e:
137 procutil.stderr.write(b"abort: %s\n" % e)
138 sys.exit(255)
139 except KeyboardInterrupt:
140 sys.exit(255)