mercurial/utils/diffutil.py
changeset 38562 c88d2c9b00dd
parent 38493 da2a7d8354b2
child 38587 b62000a28812
equal deleted inserted replaced
38561:600d8d9b8551 38562:c88d2c9b00dd
       
     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 diffopts = diffallopts
       
    25 
       
    26 def difffeatureopts(ui, opts=None, untrusted=False, section='diff', git=False,
       
    27                     whitespace=False, formatchanging=False):
       
    28     '''return diffopts with only opted-in features parsed
       
    29 
       
    30     Features:
       
    31     - git: git-style diffs
       
    32     - whitespace: whitespace options like ignoreblanklines and ignorews
       
    33     - formatchanging: options that will likely break or cause correctness issues
       
    34       with most diff parsers
       
    35     '''
       
    36     def get(key, name=None, getter=ui.configbool, forceplain=None):
       
    37         if opts:
       
    38             v = opts.get(key)
       
    39             # diffopts flags are either None-default (which is passed
       
    40             # through unchanged, so we can identify unset values), or
       
    41             # some other falsey default (eg --unified, which defaults
       
    42             # to an empty string). We only want to override the config
       
    43             # entries from hgrc with command line values if they
       
    44             # appear to have been set, which is any truthy value,
       
    45             # True, or False.
       
    46             if v or isinstance(v, bool):
       
    47                 return v
       
    48         if forceplain is not None and ui.plain():
       
    49             return forceplain
       
    50         return getter(section, name or key, untrusted=untrusted)
       
    51 
       
    52     # core options, expected to be understood by every diff parser
       
    53     buildopts = {
       
    54         'nodates': get('nodates'),
       
    55         'showfunc': get('show_function', 'showfunc'),
       
    56         'context': get('unified', getter=ui.config),
       
    57     }
       
    58     buildopts['worddiff'] = ui.configbool('experimental', 'worddiff')
       
    59     buildopts['xdiff'] = ui.configbool('experimental', 'xdiff')
       
    60 
       
    61     if git:
       
    62         buildopts['git'] = get('git')
       
    63 
       
    64         # since this is in the experimental section, we need to call
       
    65         # ui.configbool directory
       
    66         buildopts['showsimilarity'] = ui.configbool('experimental',
       
    67                                                     'extendedheader.similarity')
       
    68 
       
    69         # need to inspect the ui object instead of using get() since we want to
       
    70         # test for an int
       
    71         hconf = ui.config('experimental', 'extendedheader.index')
       
    72         if hconf is not None:
       
    73             hlen = None
       
    74             try:
       
    75                 # the hash config could be an integer (for length of hash) or a
       
    76                 # word (e.g. short, full, none)
       
    77                 hlen = int(hconf)
       
    78                 if hlen < 0 or hlen > 40:
       
    79                     msg = _("invalid length for extendedheader.index: '%d'\n")
       
    80                     ui.warn(msg % hlen)
       
    81             except ValueError:
       
    82                 # default value
       
    83                 if hconf == 'short' or hconf == '':
       
    84                     hlen = 12
       
    85                 elif hconf == 'full':
       
    86                     hlen = 40
       
    87                 elif hconf != 'none':
       
    88                     msg = _("invalid value for extendedheader.index: '%s'\n")
       
    89                     ui.warn(msg % hconf)
       
    90             finally:
       
    91                 buildopts['index'] = hlen
       
    92 
       
    93     if whitespace:
       
    94         buildopts['ignorews'] = get('ignore_all_space', 'ignorews')
       
    95         buildopts['ignorewsamount'] = get('ignore_space_change',
       
    96                                           'ignorewsamount')
       
    97         buildopts['ignoreblanklines'] = get('ignore_blank_lines',
       
    98                                             'ignoreblanklines')
       
    99         buildopts['ignorewseol'] = get('ignore_space_at_eol', 'ignorewseol')
       
   100     if formatchanging:
       
   101         buildopts['text'] = opts and opts.get('text')
       
   102         binary = None if opts is None else opts.get('binary')
       
   103         buildopts['nobinary'] = (not binary if binary is not None
       
   104                                  else get('nobinary', forceplain=False))
       
   105         buildopts['noprefix'] = get('noprefix', forceplain=False)
       
   106 
       
   107     return mdiff.diffopts(**pycompat.strkwargs(buildopts))