From 848e6f833be2a790cce09ee3b1de8d65a859a512 Mon Sep 17 00:00:00 2001 From: albfan Date: Tue, 10 Jun 2014 00:08:48 +0200 Subject: [PATCH] parse word-diff hidding whitespace or newline changes --- README.md | 20 ++++++++++++++++++++ filter-word-diff.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 README.md create mode 100755 filter-word-diff.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..c7f01ae --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# word-diff-filter + +Dealing with diff is a hard thing. You want to focus on big picture, what changes, what matters, but there's a lot to remove from a simple diff to make it usable (at least for a review) + +Alghorithms like patience try to reorder diffs but thats not enough. + +One of the most disrupting noise inside diffs is about indentation changes. They generate a lot of attention in a diff but in almost all code they are just a step further to ignore-whitespace, I want to take that step! + +By now this is a filter for **git diff --word-diff=porcelain** which works on **--color** or plain output, I will work to provide an ecosystem to ease it use. + +#Test + +```bash +$ git clone albfan:word-diff-filter +$ cd word-diff-filter +$ ln -s $(pwd)/word-diff-filter.py ~/bin/word-diff-filter +$ cd +$ git diff --word-diff=porcelain --color master feature | word-diff-filter +``` + diff --git a/filter-word-diff.py b/filter-word-diff.py new file mode 100755 index 0000000..22095ef --- /dev/null +++ b/filter-word-diff.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python + +import sys +import re + +header = "" +chunk = "" +add = 0 +change = False + +COLOR="(\x1b\[\d*m)?" +def printChunk(): + global header, chunk, change + if change: + if header: + print(header) + header = "" + print(chunk) + chunk = "" + change = False + +for line in sys.stdin: + if re.match("^"+COLOR+"diff --git", line): + printChunk() + header = "" + add = 5 + + if (add > 0): + header += line + add -= 1 + continue + + if re.match("^"+COLOR+"~", line): + continue + + if re.match("^"+COLOR+"@@", line): + #new chunk. does the actual contains a change + printChunk() + + chunk += line + + if not change: + change = re.match("^"+COLOR+"\+", line) or re.match("^"+COLOR+"-", line) + +printChunk() +