在补丁文件中突出显示添加/删除的行,忽略移动

Jef*_*ris 18 git diff

我正在审查一个能够解决很多问题的补丁,添加了一些东西,并删除了一些东西.我想知道是否有人编写了一个实用程序,用于在通用差异中挑选出独特的添加/删除功能?

也就是说,添加和删除相同的行应该取消它们自己.

显然这并不是一直有用,但有时它正是我想要的:)

Jef*_*ris 16

这就是我最终使用的.

用法示例:

git diff -w | /path/to/ignore_moves.py | less -R

ignore_moves.py

#!/usr/bin/python                                                                                                             

import sys
from itertools import *

RED = 31
GREEN = 32

RESET_SEQ = "\033[0m"
COLOR_SEQ = "\033[0;%dm"

stack = []

def inverse(line):
    return ('-' if line[0] == '+' else '+') + line[1:].strip()

def reverse_enumerate(l):
    for i, x in enumerate(reversed(l)):
        yield len(l)-1-i, x

def dumpchanges():
    for line in stack:
        SEQ = COLOR_SEQ % (GREEN if line.startswith('+') else RED)
        print SEQ + line.strip() + RESET_SEQ
    stack[:] = []

for line in sys.stdin.readlines():
    if not line[1:].strip():
        continue # ignore empty lines                                                                                         
    if line.startswith(('---', '+++')):
        dumpchanges()
        print line.strip()
    elif line.startswith(('+', '-')):
        inverted = inverse(line)
        line = line[0] + line[1:].strip()
        for i, match in reverse_enumerate(stack):
            if inverted == match:
                stack.pop(i)
                break
        else:
            stack.append(line)

# finished reading, still have state to be dumped                                                                             
dumpchanges()
Run Code Online (Sandbox Code Playgroud)

  • 现在另请参阅“git diff --color-moved”。 (3认同)
  • 我非常喜欢这个,以至于我决定添加一个选项以获得简单的输出(适合存储在文件中或在带有语法高亮的编辑器中打开,比如vim)并且我从pyflakes,pylint和pep8获得了完美的标记.https://gist.github.com/1202342 (2认同)

小智 10

这对我来说更好地获得修改过的文件的差异(省略仅被移动的文件).

git diff -M -C -D

从git diff文档:

-M[<n>], --find-renames[=<n>]
       Detect renames. If n is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file's size). For example, -M90% means git should consider a delete/add pair to be a rename if more than 90% of the file hasn't changed.

-C[<n>], --find-copies[=<n>]
       Detect copies as well as renames. See also --find-copies-harder. If n is specified, it has the same meaning as for -M<n>.

-D, --irreversible-delete
       Omit the preimage for deletes, i.e. print only the header but not the diff between the preimage and /dev/null. The resulting patch is not meant to be applied with patch nor git apply; this is solely for people who want to just concentrate on reviewing the text after the change. In addition, the output obviously
       lack enough information to apply such a patch in reverse, even manually, hence the name of the option.
Run Code Online (Sandbox Code Playgroud)

  • 这不能回答问题,也不能隐藏在同一文件中移动的行。 (2认同)

小智 5

就在这里,用 Bash

git diff | diff-ignore-moved-lines
Run Code Online (Sandbox Code Playgroud)

忽略移动的差异行

  • 这样做的问题是它删除了上下文行。 (2认同)