Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git diff - ignore reorder

Tags:

git

bash

shell

sh

diff

git diff numbers

diff --git a/numbers b/numbers
index 5f5fbe7..d184fef 100644
--- a/numbers
+++ b/numbers
@@ -1,3 +1,3 @@
-1
+4
+3
 2
-3

Number 3 is repeated but the order is changed. Any way to ignore the reorder in git or any grep solution? I want the result of only added and deleted numbers, not reordering of the same numbers Any help?

like image 298
rabin Avatar asked Feb 14 '14 05:02

rabin


2 Answers

The diffing tools are usually implemented in terms of the Myers' diff algorithm. There isn't much that you can do to control the behaviour of GNU/git/diff. (There are a couple of switches that you can pass to diff to affect the behaviour, but in your case they are irrelevant.)

You could simply post-process the output and remove the duplicate lines, for example you can pipe your diff through the following awk script that will remove (duplicate) -/+ reordering.

git diff | awk '{ seen[substr($0,2)]++; l[i++] = $0; } END { for (j = 0; j < i; ++j) if (seen[substr(l[j],2)] < 2) print l[j] }'

For your example, the output would be,

diff --git a/numbers b/numbers
index 5f5fbe7..d184fef 100644
--- a/numbers
+++ b/numbers
@@ -1,3 +1,3 @@
-1
+4
 2
like image 159
mockinterface Avatar answered Sep 22 '22 12:09

mockinterface


If it is a single file, it can be done that way:

diff -u <(sort ./numbers) <(git show HEAD^^:./numbers | sort)

Running this command will tell you if a sorted file will introduce or not any other difference (i.e. ignoring lines order). If there are any suppressed or new lines, you will see them, but note that the diff output may not be accurate (files have been sorted, so these lines may be printed in a different order).

like image 27
Pere Avatar answered Sep 21 '22 12:09

Pere