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