Подтвердить что ты не робот

Выделение добавленных/удаленных строк, игнорирование ходов, в файле патча

Я просматриваю патч, который перемещал много вещей, добавил несколько вещей и удалил несколько вещей. Мне интересно, если кто-нибудь написал утилиту для сбора уникальных добавлений/удалений в универсальном diff?

То есть, добавление и удаление одной и той же строки должны отменяться.

Очевидно, что это не всегда полезно, но иногда это именно то, что я хочу:)

4b9b3361

Ответ 1

Вот что я в итоге использовал.

Пример использования:

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

Ответ 2

Это помогло мне лучше разобраться с измененными файлами (исключая только перемещенные файлы).

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 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.

Ответ 4

Рабочее решение без дополнительных скриптов:

git diff --diff-filter=r origin/master..HEAD

Согласно man git-diff вы можете фильтровать все виды вещей (A - добавлено, C - скопировано, D - удалено, M - изменено). Примечание: строчная буква означает "исключить".