comparison mercurial/diffutil.py @ 38589:1c93e0237a24

diffutil: move the module out of utils package mercurial.utils modules inherit the property of the mercurial.util, which is no dependency on ui, repo, ctx, etc. As the diffutil module seems to reside in the scmutil layer, it's probably better to not put it under the utils package.
author Yuya Nishihara <yuya@tcha.org>
date Fri, 06 Jul 2018 21:49:25 +0900
parents mercurial/utils/diffutil.py@b62000a28812
children be441eb65f09
comparison
equal deleted inserted replaced
38588:b62000a28812 38589:1c93e0237a24
1 # diffutil.py - utility functions related to diff and patch
2 #
3 # Copyright 2006 Brendan Cully <brendan@kublai.com>
4 # Copyright 2007 Chris Mason <chris.mason@oracle.com>
5 # Copyright 2018 Octobus <octobus@octobus.net>
6 #
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
9
10 from __future__ import absolute_import
11
12 from .i18n import _
13
14 from . import (
15 mdiff,
16 pycompat,
17 )
18
19 def diffallopts(ui, opts=None, untrusted=False, section='diff'):
20 '''return diffopts with all features supported and parsed'''
21 return difffeatureopts(ui, opts=opts, untrusted=untrusted, section=section,
22 git=True, whitespace=True, formatchanging=True)
23
24 def difffeatureopts(ui, opts=None, untrusted=False, section='diff', git=False,
25 whitespace=False, formatchanging=False):
26 '''return diffopts with only opted-in features parsed
27
28 Features:
29 - git: git-style diffs
30 - whitespace: whitespace options like ignoreblanklines and ignorews
31 - formatchanging: options that will likely break or cause correctness issues
32 with most diff parsers
33 '''
34 def get(key, name=None, getter=ui.configbool, forceplain=None):
35 if opts:
36 v = opts.get(key)
37 # diffopts flags are either None-default (which is passed
38 # through unchanged, so we can identify unset values), or
39 # some other falsey default (eg --unified, which defaults
40 # to an empty string). We only want to override the config
41 # entries from hgrc with command line values if they
42 # appear to have been set, which is any truthy value,
43 # True, or False.
44 if v or isinstance(v, bool):
45 return v
46 if forceplain is not None and ui.plain():
47 return forceplain
48 return getter(section, name or key, untrusted=untrusted)
49
50 # core options, expected to be understood by every diff parser
51 buildopts = {
52 'nodates': get('nodates'),
53 'showfunc': get('show_function', 'showfunc'),
54 'context': get('unified', getter=ui.config),
55 }
56 buildopts['worddiff'] = ui.configbool('experimental', 'worddiff')
57 buildopts['xdiff'] = ui.configbool('experimental', 'xdiff')
58
59 if git:
60 buildopts['git'] = get('git')
61
62 # since this is in the experimental section, we need to call
63 # ui.configbool directory
64 buildopts['showsimilarity'] = ui.configbool('experimental',
65 'extendedheader.similarity')
66
67 # need to inspect the ui object instead of using get() since we want to
68 # test for an int
69 hconf = ui.config('experimental', 'extendedheader.index')
70 if hconf is not None:
71 hlen = None
72 try:
73 # the hash config could be an integer (for length of hash) or a
74 # word (e.g. short, full, none)
75 hlen = int(hconf)
76 if hlen < 0 or hlen > 40:
77 msg = _("invalid length for extendedheader.index: '%d'\n")
78 ui.warn(msg % hlen)
79 except ValueError:
80 # default value
81 if hconf == 'short' or hconf == '':
82 hlen = 12
83 elif hconf == 'full':
84 hlen = 40
85 elif hconf != 'none':
86 msg = _("invalid value for extendedheader.index: '%s'\n")
87 ui.warn(msg % hconf)
88 finally:
89 buildopts['index'] = hlen
90
91 if whitespace:
92 buildopts['ignorews'] = get('ignore_all_space', 'ignorews')
93 buildopts['ignorewsamount'] = get('ignore_space_change',
94 'ignorewsamount')
95 buildopts['ignoreblanklines'] = get('ignore_blank_lines',
96 'ignoreblanklines')
97 buildopts['ignorewseol'] = get('ignore_space_at_eol', 'ignorewseol')
98 if formatchanging:
99 buildopts['text'] = opts and opts.get('text')
100 binary = None if opts is None else opts.get('binary')
101 buildopts['nobinary'] = (not binary if binary is not None
102 else get('nobinary', forceplain=False))
103 buildopts['noprefix'] = get('noprefix', forceplain=False)
104
105 return mdiff.diffopts(**pycompat.strkwargs(buildopts))