Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
681 views
in Technique[技术] by (71.8m points)

git - Highlighting added/deleted lines, ignoring moves, in a patch file

I'm reviewing a patch that moved a lot of things around, added a few things, and removed a few things. I'm wondering if anyone's written a utility for picking out the unique adds/removes in a universal diff?

That is, an add and a remove of the same line should cancel themselves out.

Obviously this isn't useful all the time, but sometimes it's exactly what I want :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is what I ended up using.

Example usage:

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 = "33[0m"
COLOR_SEQ = "33[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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...