Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/utils/diffutil.py @ 38563:c88d2c9b00dd
diffutil: extract diff options code into a dedicated util-module
We want to be able to create and use diffoptions in more places. Currently the
official function to instantiate diffoptions live into `mercurial.patch`. A
module too high level to be easily imported in some places.
So we extract the diff options related function in their own utility module.
author | Boris Feld <boris.feld@octobus.net> |
---|---|
date | Thu, 28 Jun 2018 16:11:13 +0200 |
parents | mercurial/patch.py@da2a7d8354b2 |
children | b62000a28812 |
rev | line source |
---|---|
38563
c88d2c9b00dd
diffutil: extract diff options code into a dedicated util-module
Boris Feld <boris.feld@octobus.net>
parents:
38497
diff
changeset
|
1 # diffutil.py - utility functions related to diff and patch |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
2 # |
2865
71e78f2ca5ae
merge git patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2863
diff
changeset
|
3 # Copyright 2006 Brendan Cully <brendan@kublai.com> |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
4 # Copyright 2007 Chris Mason <chris.mason@oracle.com> |
38563
c88d2c9b00dd
diffutil: extract diff options code into a dedicated util-module
Boris Feld <boris.feld@octobus.net>
parents:
38497
diff
changeset
|
5 # Copyright 2018 Octobus <octobus@octobus.net> |
2865
71e78f2ca5ae
merge git patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2863
diff
changeset
|
6 # |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8209
diff
changeset
|
7 # This software may be used and distributed according to the terms of the |
10263 | 8 # GNU General Public License version 2 or any later version. |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
9 |
38563
c88d2c9b00dd
diffutil: extract diff options code into a dedicated util-module
Boris Feld <boris.feld@octobus.net>
parents:
38497
diff
changeset
|
10 from __future__ import absolute_import |
14370
17cea10c343e
patch: add a workingbackend dirstate layer on top of fsbackend
Patrick Mezard <pmezard@gmail.com>
parents:
14369
diff
changeset
|
11 |
38563
c88d2c9b00dd
diffutil: extract diff options code into a dedicated util-module
Boris Feld <boris.feld@octobus.net>
parents:
38497
diff
changeset
|
12 from ..i18n import _ |
24269
9a745ced79a9
record: move filterpatch from record to patch
Laurent Charignon <lcharignon@fb.com>
parents:
24268
diff
changeset
|
13 |
38563
c88d2c9b00dd
diffutil: extract diff options code into a dedicated util-module
Boris Feld <boris.feld@octobus.net>
parents:
38497
diff
changeset
|
14 from .. import ( |
c88d2c9b00dd
diffutil: extract diff options code into a dedicated util-module
Boris Feld <boris.feld@octobus.net>
parents:
38497
diff
changeset
|
15 mdiff, |
c88d2c9b00dd
diffutil: extract diff options code into a dedicated util-module
Boris Feld <boris.feld@octobus.net>
parents:
38497
diff
changeset
|
16 pycompat, |
c88d2c9b00dd
diffutil: extract diff options code into a dedicated util-module
Boris Feld <boris.feld@octobus.net>
parents:
38497
diff
changeset
|
17 ) |
7198
df79ee9b6278
patch: extract local function addmodehdr
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7186
diff
changeset
|
18 |
23431
10223d2278f4
patch: rename diffopts to diffallopts
Siddharth Agarwal <sid0@fb.com>
parents:
23430
diff
changeset
|
19 def diffallopts(ui, opts=None, untrusted=False, section='diff'): |
23430
3821be85fd4d
patch: add a new function to initialize diffopts by feature
Siddharth Agarwal <sid0@fb.com>
parents:
23429
diff
changeset
|
20 '''return diffopts with all features supported and parsed''' |
23433
41dd76b3facb
patch.difffeatureopts: add a feature for whitespace diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23432
diff
changeset
|
21 return difffeatureopts(ui, opts=opts, untrusted=untrusted, section=section, |
23434
60300a4c0ae5
patch.difffeatureopts: add a feature for format-changing diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23433
diff
changeset
|
22 git=True, whitespace=True, formatchanging=True) |
23430
3821be85fd4d
patch: add a new function to initialize diffopts by feature
Siddharth Agarwal <sid0@fb.com>
parents:
23429
diff
changeset
|
23 |
23431
10223d2278f4
patch: rename diffopts to diffallopts
Siddharth Agarwal <sid0@fb.com>
parents:
23430
diff
changeset
|
24 diffopts = diffallopts |
10223d2278f4
patch: rename diffopts to diffallopts
Siddharth Agarwal <sid0@fb.com>
parents:
23430
diff
changeset
|
25 |
23433
41dd76b3facb
patch.difffeatureopts: add a feature for whitespace diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23432
diff
changeset
|
26 def difffeatureopts(ui, opts=None, untrusted=False, section='diff', git=False, |
23434
60300a4c0ae5
patch.difffeatureopts: add a feature for format-changing diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23433
diff
changeset
|
27 whitespace=False, formatchanging=False): |
23432
27af986a332b
patch.difffeatureopts: add a feature for diff.git
Siddharth Agarwal <sid0@fb.com>
parents:
23431
diff
changeset
|
28 '''return diffopts with only opted-in features parsed |
27af986a332b
patch.difffeatureopts: add a feature for diff.git
Siddharth Agarwal <sid0@fb.com>
parents:
23431
diff
changeset
|
29 |
27af986a332b
patch.difffeatureopts: add a feature for diff.git
Siddharth Agarwal <sid0@fb.com>
parents:
23431
diff
changeset
|
30 Features: |
27af986a332b
patch.difffeatureopts: add a feature for diff.git
Siddharth Agarwal <sid0@fb.com>
parents:
23431
diff
changeset
|
31 - git: git-style diffs |
23433
41dd76b3facb
patch.difffeatureopts: add a feature for whitespace diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23432
diff
changeset
|
32 - whitespace: whitespace options like ignoreblanklines and ignorews |
23434
60300a4c0ae5
patch.difffeatureopts: add a feature for format-changing diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23433
diff
changeset
|
33 - formatchanging: options that will likely break or cause correctness issues |
60300a4c0ae5
patch.difffeatureopts: add a feature for format-changing diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23433
diff
changeset
|
34 with most diff parsers |
23432
27af986a332b
patch.difffeatureopts: add a feature for diff.git
Siddharth Agarwal <sid0@fb.com>
parents:
23431
diff
changeset
|
35 ''' |
23295
ac072c79bd9d
patch.diffopts: break get function into if statements
Siddharth Agarwal <sid0@fb.com>
parents:
22460
diff
changeset
|
36 def get(key, name=None, getter=ui.configbool, forceplain=None): |
ac072c79bd9d
patch.diffopts: break get function into if statements
Siddharth Agarwal <sid0@fb.com>
parents:
22460
diff
changeset
|
37 if opts: |
ac072c79bd9d
patch.diffopts: break get function into if statements
Siddharth Agarwal <sid0@fb.com>
parents:
22460
diff
changeset
|
38 v = opts.get(key) |
29952
e40343ce9c4c
diffopts: notice a negated boolean flag in diffopts
Augie Fackler <augie@google.com>
parents:
29904
diff
changeset
|
39 # diffopts flags are either None-default (which is passed |
e40343ce9c4c
diffopts: notice a negated boolean flag in diffopts
Augie Fackler <augie@google.com>
parents:
29904
diff
changeset
|
40 # through unchanged, so we can identify unset values), or |
e40343ce9c4c
diffopts: notice a negated boolean flag in diffopts
Augie Fackler <augie@google.com>
parents:
29904
diff
changeset
|
41 # some other falsey default (eg --unified, which defaults |
e40343ce9c4c
diffopts: notice a negated boolean flag in diffopts
Augie Fackler <augie@google.com>
parents:
29904
diff
changeset
|
42 # to an empty string). We only want to override the config |
e40343ce9c4c
diffopts: notice a negated boolean flag in diffopts
Augie Fackler <augie@google.com>
parents:
29904
diff
changeset
|
43 # entries from hgrc with command line values if they |
e40343ce9c4c
diffopts: notice a negated boolean flag in diffopts
Augie Fackler <augie@google.com>
parents:
29904
diff
changeset
|
44 # appear to have been set, which is any truthy value, |
e40343ce9c4c
diffopts: notice a negated boolean flag in diffopts
Augie Fackler <augie@google.com>
parents:
29904
diff
changeset
|
45 # True, or False. |
e40343ce9c4c
diffopts: notice a negated boolean flag in diffopts
Augie Fackler <augie@google.com>
parents:
29904
diff
changeset
|
46 if v or isinstance(v, bool): |
23295
ac072c79bd9d
patch.diffopts: break get function into if statements
Siddharth Agarwal <sid0@fb.com>
parents:
22460
diff
changeset
|
47 return v |
23296
922fcfb02e77
patch.diffopts: allow a setting to be forced in plain mode
Siddharth Agarwal <sid0@fb.com>
parents:
23295
diff
changeset
|
48 if forceplain is not None and ui.plain(): |
922fcfb02e77
patch.diffopts: allow a setting to be forced in plain mode
Siddharth Agarwal <sid0@fb.com>
parents:
23295
diff
changeset
|
49 return forceplain |
34521
aacb17cc0ee4
configitems: register the 'diff.*' config
Boris Feld <boris.feld@octobus.net>
parents:
34381
diff
changeset
|
50 return getter(section, name or key, untrusted=untrusted) |
23295
ac072c79bd9d
patch.diffopts: break get function into if statements
Siddharth Agarwal <sid0@fb.com>
parents:
22460
diff
changeset
|
51 |
23434
60300a4c0ae5
patch.difffeatureopts: add a feature for format-changing diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23433
diff
changeset
|
52 # core options, expected to be understood by every diff parser |
23429
f35526b999f4
patch.diffopts: use a dict for initialization
Siddharth Agarwal <sid0@fb.com>
parents:
23300
diff
changeset
|
53 buildopts = { |
f35526b999f4
patch.diffopts: use a dict for initialization
Siddharth Agarwal <sid0@fb.com>
parents:
23300
diff
changeset
|
54 'nodates': get('nodates'), |
f35526b999f4
patch.diffopts: use a dict for initialization
Siddharth Agarwal <sid0@fb.com>
parents:
23300
diff
changeset
|
55 'showfunc': get('show_function', 'showfunc'), |
f35526b999f4
patch.diffopts: use a dict for initialization
Siddharth Agarwal <sid0@fb.com>
parents:
23300
diff
changeset
|
56 'context': get('unified', getter=ui.config), |
f35526b999f4
patch.diffopts: use a dict for initialization
Siddharth Agarwal <sid0@fb.com>
parents:
23300
diff
changeset
|
57 } |
35286
6ba79cf34f5e
patch: add within-line color diff capacity
Matthieu Laneuville <matthieu.laneuville@octobus.net>
parents:
35202
diff
changeset
|
58 buildopts['worddiff'] = ui.configbool('experimental', 'worddiff') |
36705
c6a61298ac32
mdiff: add a config option to use xdiff algorithm
Jun Wu <quark@fb.com>
parents:
36675
diff
changeset
|
59 buildopts['xdiff'] = ui.configbool('experimental', 'xdiff') |
23429
f35526b999f4
patch.diffopts: use a dict for initialization
Siddharth Agarwal <sid0@fb.com>
parents:
23300
diff
changeset
|
60 |
23432
27af986a332b
patch.difffeatureopts: add a feature for diff.git
Siddharth Agarwal <sid0@fb.com>
parents:
23431
diff
changeset
|
61 if git: |
27af986a332b
patch.difffeatureopts: add a feature for diff.git
Siddharth Agarwal <sid0@fb.com>
parents:
23431
diff
changeset
|
62 buildopts['git'] = get('git') |
30788
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
63 |
30806
e2796f193f06
patch: add similarity config knob in experimental section
Sean Farley <sean@farley.io>
parents:
30790
diff
changeset
|
64 # since this is in the experimental section, we need to call |
e2796f193f06
patch: add similarity config knob in experimental section
Sean Farley <sean@farley.io>
parents:
30790
diff
changeset
|
65 # ui.configbool directory |
e2796f193f06
patch: add similarity config knob in experimental section
Sean Farley <sean@farley.io>
parents:
30790
diff
changeset
|
66 buildopts['showsimilarity'] = ui.configbool('experimental', |
e2796f193f06
patch: add similarity config knob in experimental section
Sean Farley <sean@farley.io>
parents:
30790
diff
changeset
|
67 'extendedheader.similarity') |
e2796f193f06
patch: add similarity config knob in experimental section
Sean Farley <sean@farley.io>
parents:
30790
diff
changeset
|
68 |
30788
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
69 # need to inspect the ui object instead of using get() since we want to |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
70 # test for an int |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
71 hconf = ui.config('experimental', 'extendedheader.index') |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
72 if hconf is not None: |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
73 hlen = None |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
74 try: |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
75 # the hash config could be an integer (for length of hash) or a |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
76 # word (e.g. short, full, none) |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
77 hlen = int(hconf) |
30819
897726622877
patch: check length of git index header only if integer is specified
Yuya Nishihara <yuya@tcha.org>
parents:
30808
diff
changeset
|
78 if hlen < 0 or hlen > 40: |
897726622877
patch: check length of git index header only if integer is specified
Yuya Nishihara <yuya@tcha.org>
parents:
30808
diff
changeset
|
79 msg = _("invalid length for extendedheader.index: '%d'\n") |
897726622877
patch: check length of git index header only if integer is specified
Yuya Nishihara <yuya@tcha.org>
parents:
30808
diff
changeset
|
80 ui.warn(msg % hlen) |
30788
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
81 except ValueError: |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
82 # default value |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
83 if hconf == 'short' or hconf == '': |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
84 hlen = 12 |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
85 elif hconf == 'full': |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
86 hlen = 40 |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
87 elif hconf != 'none': |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
88 msg = _("invalid value for extendedheader.index: '%s'\n") |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
89 ui.warn(msg % hconf) |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
90 finally: |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
91 buildopts['index'] = hlen |
d1901c4c8ec0
patch: add config knob for displaying the index header
Sean Farley <sean@farley.io>
parents:
30417
diff
changeset
|
92 |
23433
41dd76b3facb
patch.difffeatureopts: add a feature for whitespace diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23432
diff
changeset
|
93 if whitespace: |
41dd76b3facb
patch.difffeatureopts: add a feature for whitespace diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23432
diff
changeset
|
94 buildopts['ignorews'] = get('ignore_all_space', 'ignorews') |
41dd76b3facb
patch.difffeatureopts: add a feature for whitespace diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23432
diff
changeset
|
95 buildopts['ignorewsamount'] = get('ignore_space_change', |
41dd76b3facb
patch.difffeatureopts: add a feature for whitespace diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23432
diff
changeset
|
96 'ignorewsamount') |
41dd76b3facb
patch.difffeatureopts: add a feature for whitespace diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23432
diff
changeset
|
97 buildopts['ignoreblanklines'] = get('ignore_blank_lines', |
41dd76b3facb
patch.difffeatureopts: add a feature for whitespace diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23432
diff
changeset
|
98 'ignoreblanklines') |
34031
da07367d683b
mdiff: add a --ignore-space-at-eol option
David Soria Parra <davidsp@fb.com>
parents:
33671
diff
changeset
|
99 buildopts['ignorewseol'] = get('ignore_space_at_eol', 'ignorewseol') |
23434
60300a4c0ae5
patch.difffeatureopts: add a feature for format-changing diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23433
diff
changeset
|
100 if formatchanging: |
60300a4c0ae5
patch.difffeatureopts: add a feature for format-changing diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23433
diff
changeset
|
101 buildopts['text'] = opts and opts.get('text') |
31822
fde4822b0102
diff: add --binary option for git mode diffs
Alexander Fomin <afomin@fb.com>
parents:
31821
diff
changeset
|
102 binary = None if opts is None else opts.get('binary') |
fde4822b0102
diff: add --binary option for git mode diffs
Alexander Fomin <afomin@fb.com>
parents:
31821
diff
changeset
|
103 buildopts['nobinary'] = (not binary if binary is not None |
fde4822b0102
diff: add --binary option for git mode diffs
Alexander Fomin <afomin@fb.com>
parents:
31821
diff
changeset
|
104 else get('nobinary', forceplain=False)) |
23434
60300a4c0ae5
patch.difffeatureopts: add a feature for format-changing diffopts
Siddharth Agarwal <sid0@fb.com>
parents:
23433
diff
changeset
|
105 buildopts['noprefix'] = get('noprefix', forceplain=False) |
23432
27af986a332b
patch.difffeatureopts: add a feature for diff.git
Siddharth Agarwal <sid0@fb.com>
parents:
23431
diff
changeset
|
106 |
31636
a7acda2de4b8
diff: use pycompat.{byteskwargs, strkwargs} to switch opts b/w bytes and str
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31635
diff
changeset
|
107 return mdiff.diffopts(**pycompat.strkwargs(buildopts)) |