Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git diff without changes of single quotes to double quotes

Tags:

git

One of my colleagues applied results of js-linting, so there is a huge part of diff now where all that's actually changed on a line is just single quotes ' to double quotes "

- var mod = require('mod');
+ var mod = require("mod");

is there any way to filter it out of git diff? If something else changed on a line, it should still be displayed:

- var mod = require('mod1');
+ var mod = require("mod2");
like image 233
xaxa Avatar asked Jan 29 '23 18:01

xaxa


1 Answers

There's not a great way to do this, especially when you consider that changes between " and ' could be bugs.

"hello" => 'hello'

may be fine, but

"what's up" => 'what's up'

would be concerning (level of concerning perhaps varying by language).

You could use --word-diff-regex to treat ' and " as "not part of words"; in that case all ' and " characters would be ignored in calculating the diff. However, this performs a "word" diff rather than the typical line-oriented diff. For example

$ git diff file1

diff --git a/file1 b/file1
index 64315fd..cd80abf 100644
--- a/file1
+++ b/file1
@@ -1,4 +1,4 @@
-"this is a test"
-"this is a changing test"
-"this couldn't cause a problem, could it?"
-"it's possible some edits could be trouble"
+'this is a test'
+'this is a changing line of test'
+'this couldn't cause a problem, could it?'
+'it"s possible some edits could be trouble'"'"''

Well, that's not much use; it just says everything changed. Ok, how about

$ git diff --word-diff-regex="[^'\"]" file1
diff --git a/file1 b/file1
index 64315fd..cd80abf 100644
--- a/file1
+++ b/file1
@@ -1,4 +1,4 @@
'this is a test'
'this is a changing {+line of +}test'
'this couldn't cause a problem, could it?'
'it"s possible some edits could be trouble'"'"''

Well, it detected and highlighted the change on the 2nd line, so that's good. (This doesn't come through in the copied text, but by default on the console the addition would be highlighted green, so it does call the changes out reasonably well.) It doesn't notice that I've changed line 3 in a potentially destructive way, and it didn't notice a bunch of spurious quote changes (and additions) on line 4.

So it's not perfect.

You could get slightly better results by pre-processing the file, forcing both versions to use a normalized quote notation; that would catch line 4 above, but still not line 3. And you'd no longer be directly diffing the source-controlled versions, so what you do isn't quite as auditable so to speak.

like image 102
Mark Adelsberger Avatar answered Feb 05 '23 15:02

Mark Adelsberger