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

问题描述 投票:0回答:6

我正在审查一个补丁,它移动了很多的东西,添加了一些东西,并删除了一些东西。我想知道是否有人编写了一个实用程序来挑选通用差异中唯一的添加/删除?

也就是说,同一行的添加和删除应该自行抵消。

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

git diff
6个回答
21
投票

这就是我最终使用的。

使用示例:

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()

8
投票

这对我来说可以更好地获取修改后的文件的差异(忽略仅移动的文件)。

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.

5
投票

就是这样,用 Bash

git diff | diff-ignore-moved-lines

忽略移动的差异线


3
投票

要仅“查看”未移动的行,请在黑色背景的控制台中尝试此操作

git -c color.diff.newMoved=black -c color.diff.oldMoved=black diff --color-moved=plain --unified=0

所有移动的线条都会在视觉上消失。您只会看到实际更改的行。请注意,上下文也未显示。

参考:https://groups.google.com/g/git-users/c/kycBnxmmCcU?pli=1


1
投票

这是上面@Zombo提供的答案的变体https://stackoverflow.com/a/23462451/722087

当很多文件的更改仅是移动的行,然后还有一些文件实际上具有除移动的行之外的更改时,那么您可能希望找到那些已更改行的文件。以下内容将允许您做到这一点。

将文件 diff-ignore-moved-lines.sh 复制到您的路径中,以便您可以从任何地方执行它。然后在git文件夹中运行以下命令

for file in `git diff --name-only`; do  echo $file; git --no-pager diff -- "$file" | diff-ignore-moved-lines; done

0
投票

我有这个:

comm -3 <(git show HEAD:FILENAME | sort) <(cat FILENAME | sort)

但它没有显示更改的线路来自哪一侧。

此外,它需要重复文件名,这是不优雅的。

© www.soinside.com 2019 - 2024. All rights reserved.