[PATCH] cmdutil: add within-line color diff capacity

Anton Shestakov av6 at dwimlabs.net
Sat Nov 18 02:32:19 EST 2017


On Sat, 18 Nov 2017 15:38:54 +0900
matthieu.laneuville at octobus.net wrote:

> # HG changeset patch
> # User Matthieu Laneuville <matthieu.laneuville at octobus.net>
> # Date 1508944418 -32400
> #      Thu Oct 26 00:13:38 2017 +0900
> # Node ID 99b64b304324b89cd8e472faf82e5235a846ff36
> # Parent  75013952d8d9608f73cd45f68405fbd6ec112bf2
> # EXP-Topic hg248
> cmdutil: add within-line color diff capacity
> 
> The `diff' command usually writes deletion in red and insertions in green. This
> patch adds within-line colors, to highlight which part of the lines differ.

Is this what git calls "word diff"? If so, this is great.

> diff -r 75013952d8d9 -r 99b64b304324 mercurial/cmdutil.py
> --- a/mercurial/cmdutil.py	Fri Nov 10 19:14:06 2017 +0800
> +++ b/mercurial/cmdutil.py	Thu Oct 26 00:13:38 2017 +0900
> @@ -7,6 +7,7 @@
>  
>  from __future__ import absolute_import
>  
> +import difflib
>  import errno
>  import itertools
>  import os
> @@ -1513,6 +1514,11 @@ def diffordiffstat(ui, repo, diffopts, n
>                  ui.warn(_('warning: %s not inside relative root %s\n') % (
>                      match.uipath(matchroot), uirelroot))
>  
> +    store = {
> +        'diff.inserted': [],
> +        'diff.deleted':[]

Would be nice to have a space after ":".

> +def _inlinediff(s1, s2, direction):
> +    '''Perform string diff to highlight specific changes.'''
> +    direction_skip = '+?' if direction == 'deleted' else '-?'
> +    s = difflib.ndiff(s2.split(' '), s1.split(' '))
> +    # buffer required to remove last space, there may be smarter ways to do this
> +    buff = []
> +    for line in s:
> +        if line[0] in direction_skip:
> +            continue
> +        l = 'diff.'+direction+'.highlight'
> +        if line[0] == ' ':
> +            l = 'diff.'+direction
> +        buff.append((l, line[2:]+' '))

check-code.py complains if you write "1+2": "missing whitespace in
expression" (around the operator), but it doesn't complain when strings
are involved. It should, and this function also should have spaces
around "+".

To be fair, these are all stylistic nitpicks that can probably just be
fixed in-flight, but we do have a
https://www.mercurial-scm.org/wiki/CodingStyle


More information about the Mercurial-devel mailing list