comparison mercurial/patch.py @ 38562: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 da2a7d8354b2
children b62000a28812
comparison
equal deleted inserted replaced
38561:600d8d9b8551 38562:c88d2c9b00dd
39 util, 39 util,
40 vfs as vfsmod, 40 vfs as vfsmod,
41 ) 41 )
42 from .utils import ( 42 from .utils import (
43 dateutil, 43 dateutil,
44 diffutil,
44 procutil, 45 procutil,
45 stringutil, 46 stringutil,
46 ) 47 )
47 48
48 stringio = util.stringio 49 stringio = util.stringio
2230 return changed 2231 return changed
2231 2232
2232 class GitDiffRequired(Exception): 2233 class GitDiffRequired(Exception):
2233 pass 2234 pass
2234 2235
2235 def diffallopts(ui, opts=None, untrusted=False, section='diff'): 2236 diffopts = diffutil.diffopts
2236 '''return diffopts with all features supported and parsed''' 2237 diffallopts = diffutil.diffallopts
2237 return difffeatureopts(ui, opts=opts, untrusted=untrusted, section=section, 2238 difffeatureopts = diffutil.difffeatureopts
2238 git=True, whitespace=True, formatchanging=True)
2239
2240 diffopts = diffallopts
2241
2242 def difffeatureopts(ui, opts=None, untrusted=False, section='diff', git=False,
2243 whitespace=False, formatchanging=False):
2244 '''return diffopts with only opted-in features parsed
2245
2246 Features:
2247 - git: git-style diffs
2248 - whitespace: whitespace options like ignoreblanklines and ignorews
2249 - formatchanging: options that will likely break or cause correctness issues
2250 with most diff parsers
2251 '''
2252 def get(key, name=None, getter=ui.configbool, forceplain=None):
2253 if opts:
2254 v = opts.get(key)
2255 # diffopts flags are either None-default (which is passed
2256 # through unchanged, so we can identify unset values), or
2257 # some other falsey default (eg --unified, which defaults
2258 # to an empty string). We only want to override the config
2259 # entries from hgrc with command line values if they
2260 # appear to have been set, which is any truthy value,
2261 # True, or False.
2262 if v or isinstance(v, bool):
2263 return v
2264 if forceplain is not None and ui.plain():
2265 return forceplain
2266 return getter(section, name or key, untrusted=untrusted)
2267
2268 # core options, expected to be understood by every diff parser
2269 buildopts = {
2270 'nodates': get('nodates'),
2271 'showfunc': get('show_function', 'showfunc'),
2272 'context': get('unified', getter=ui.config),
2273 }
2274 buildopts['worddiff'] = ui.configbool('experimental', 'worddiff')
2275 buildopts['xdiff'] = ui.configbool('experimental', 'xdiff')
2276
2277 if git:
2278 buildopts['git'] = get('git')
2279
2280 # since this is in the experimental section, we need to call
2281 # ui.configbool directory
2282 buildopts['showsimilarity'] = ui.configbool('experimental',
2283 'extendedheader.similarity')
2284
2285 # need to inspect the ui object instead of using get() since we want to
2286 # test for an int
2287 hconf = ui.config('experimental', 'extendedheader.index')
2288 if hconf is not None:
2289 hlen = None
2290 try:
2291 # the hash config could be an integer (for length of hash) or a
2292 # word (e.g. short, full, none)
2293 hlen = int(hconf)
2294 if hlen < 0 or hlen > 40:
2295 msg = _("invalid length for extendedheader.index: '%d'\n")
2296 ui.warn(msg % hlen)
2297 except ValueError:
2298 # default value
2299 if hconf == 'short' or hconf == '':
2300 hlen = 12
2301 elif hconf == 'full':
2302 hlen = 40
2303 elif hconf != 'none':
2304 msg = _("invalid value for extendedheader.index: '%s'\n")
2305 ui.warn(msg % hconf)
2306 finally:
2307 buildopts['index'] = hlen
2308
2309 if whitespace:
2310 buildopts['ignorews'] = get('ignore_all_space', 'ignorews')
2311 buildopts['ignorewsamount'] = get('ignore_space_change',
2312 'ignorewsamount')
2313 buildopts['ignoreblanklines'] = get('ignore_blank_lines',
2314 'ignoreblanklines')
2315 buildopts['ignorewseol'] = get('ignore_space_at_eol', 'ignorewseol')
2316 if formatchanging:
2317 buildopts['text'] = opts and opts.get('text')
2318 binary = None if opts is None else opts.get('binary')
2319 buildopts['nobinary'] = (not binary if binary is not None
2320 else get('nobinary', forceplain=False))
2321 buildopts['noprefix'] = get('noprefix', forceplain=False)
2322
2323 return mdiff.diffopts(**pycompat.strkwargs(buildopts))
2324 2239
2325 def diff(repo, node1=None, node2=None, match=None, changes=None, 2240 def diff(repo, node1=None, node2=None, match=None, changes=None,
2326 opts=None, losedatafn=None, prefix='', relroot='', copy=None, 2241 opts=None, losedatafn=None, prefix='', relroot='', copy=None,
2327 hunksfilterfn=None): 2242 hunksfilterfn=None):
2328 '''yields diff of changes to files between two nodes, or node and 2243 '''yields diff of changes to files between two nodes, or node and