Mercurial > hg
annotate contrib/simplemerge @ 33903:ed6f64173121
contrib: make simplemerge script pass context-like objects
`simplemerge()` will soon require context-like objects to work. Create a simple
context-like object that wraps the requested files and can be passed to the new
API.
Differential Revision: https://phab.mercurial-scm.org/D378
author | Phil Cohen <phillco@fb.com> |
---|---|
date | Thu, 24 Aug 2017 21:30:37 -0700 |
parents | aed91971d88c |
children | fa6309c5761d |
rev | line source |
---|---|
4363
2e3c54fb79a3
actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4362
diff
changeset
|
1 #!/usr/bin/env python |
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 |
aed91971d88c
simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents:
30576
diff
changeset
|
8 hgdemandimport.enable() |
aed91971d88c
simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents:
30576
diff
changeset
|
9 |
4363
2e3c54fb79a3
actually port simplemerge to hg
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4362
diff
changeset
|
10 from mercurial.i18n import _ |
33895
aed91971d88c
simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents:
30576
diff
changeset
|
11 from mercurial import ( |
aed91971d88c
simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents:
30576
diff
changeset
|
12 error, |
aed91971d88c
simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents:
30576
diff
changeset
|
13 fancyopts, |
aed91971d88c
simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents:
30576
diff
changeset
|
14 simplemerge, |
aed91971d88c
simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents:
30576
diff
changeset
|
15 ui as uimod, |
aed91971d88c
simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents:
30576
diff
changeset
|
16 util, |
aed91971d88c
simplemerge: update to conform with modern import conventions
Augie Fackler <raf@durin42.com>
parents:
30576
diff
changeset
|
17 ) |
4362
465b9ea02868
Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
18 |
4364
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
19 options = [('L', 'label', [], _('labels to use on conflict markers')), |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
20 ('a', 'text', None, _('treat all files as text')), |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
21 ('p', 'print', None, |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
22 _('print results instead of overwriting LOCAL')), |
22023
f18830651811
simplemerge: burn "minimal" feature to the ground
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
19378
diff
changeset
|
23 ('', 'no-minimal', None, _('no effect (DEPRECATED)')), |
4364
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
24 ('h', 'help', None, _('display help and exit')), |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
25 ('q', 'quiet', None, _('suppress output'))] |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
26 |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
27 usage = _('''simplemerge [OPTS] LOCAL BASE OTHER |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
28 |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
29 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
|
30 |
4364
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
31 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
|
32 |
4364
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
33 By default, LOCAL is overwritten with the results of this operation. |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
34 ''') |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
35 |
6002
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
36 class ParseError(Exception): |
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
37 """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
|
38 |
4364
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
39 def showhelp(): |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
40 sys.stdout.write(usage) |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
41 sys.stdout.write('\noptions:\n') |
4362
465b9ea02868
Import 3-way merge code from bzr
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
42 |
4364
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
43 out_opts = [] |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
44 for shortopt, longopt, default, desc in options: |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
45 out_opts.append(('%2s%s' % (shortopt and '-%s' % shortopt, |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
46 longopt and ' --%s' % longopt), |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
47 '%s' % desc)) |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
48 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
|
49 for first, second in out_opts: |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
50 sys.stdout.write(' %-*s %s\n' % (opts_len, first, second)) |
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
51 |
33903
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
52 class filebackedctx(object): |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
53 """simplemerge requires context-like objects""" |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
54 def __init__(self, path): |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
55 self._path = path |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
56 |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
57 def decodeddata(self): |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
58 with open(self._path, "rb") as f: |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
59 return f.read() |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
60 |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
61 def flags(self): |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
62 return '' |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
63 |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
64 def path(self): |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
65 return self._path |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
66 |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
67 def write(self, data, flags): |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
68 assert not flags |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
69 with open(self._path, "w") as f: |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
70 f.write(data) |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
71 |
6002
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
72 try: |
7080
a6477aa893b8
tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents:
6002
diff
changeset
|
73 for fp in (sys.stdin, sys.stdout, sys.stderr): |
14233
659f34b833b9
rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents:
8269
diff
changeset
|
74 util.setbinary(fp) |
19022
cba222f01056
tests: run check-code on Python files without .py extension
Mads Kiilerich <madski@unity3d.com>
parents:
14233
diff
changeset
|
75 |
6002
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
76 opts = {} |
4364
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
77 try: |
6002
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
78 args = fancyopts.fancyopts(sys.argv[1:], options, opts) |
30576
541949a10a68
fancyopts: switch from fancyopts.getopt.* to getopt.*
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30559
diff
changeset
|
79 except getopt.GetoptError as e: |
6002
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
80 raise ParseError(e) |
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
81 if opts['help']: |
4364
d5c3a70f8422
polish the simplemerge command; add a test
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4363
diff
changeset
|
82 showhelp() |
6002
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
83 sys.exit(0) |
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
84 if len(args) != 3: |
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
85 raise ParseError(_('wrong number of arguments')) |
33903
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
86 local, base, other = args |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
87 sys.exit(simplemerge.simplemerge(uimod.ui.load(), |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
88 local, |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
89 base, |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
90 other, |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
91 filebackedctx(local), |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
92 filebackedctx(base), |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
93 filebackedctx(other), |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
94 filtereddata=True, |
ed6f64173121
contrib: make simplemerge script pass context-like objects
Phil Cohen <phillco@fb.com>
parents:
33895
diff
changeset
|
95 **opts)) |
28047
863075fd4cd0
misc: use modern exception syntax
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
26587
diff
changeset
|
96 except ParseError as e: |
6002
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
97 sys.stdout.write("%s: %s\n" % (sys.argv[0], e)) |
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
98 showhelp() |
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
99 sys.exit(1) |
28047
863075fd4cd0
misc: use modern exception syntax
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
26587
diff
changeset
|
100 except error.Abort as e: |
6002
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
101 sys.stderr.write("abort: %s\n" % e) |
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
102 sys.exit(255) |
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
103 except KeyboardInterrupt: |
abd66eb0889e
merge: move the bulk of simplemerge into core
Matt Mackall <mpm@selenic.com>
parents:
5081
diff
changeset
|
104 sys.exit(255) |