I use git's word diffing to find changes between texts on a per-character basis:
git diff --word-diff=porcelain --word-diff-regex='\[[^]]*\]?|.' --no-index original.txt changed.txt
(If you're wondering, the custom regex I use ensures that characters within brackets are never broken up – credit to jthill.)
The resulting diff does not indicate deletions or additions of newlines (neither with nor without my custom regex). And when I replace a newline with, say, a space, it only indicates the addition of the space, not the deletion of the the newline.
Given the following original
foo
bar
baz
and the following changed text (I removed one line break in the top half and added one in the bottom half)
foo
bar
baz
I get this porcelain-style diff, where ~
represents newlines:
@@ -1,5 +1,5 @@
foo
~
~
bar
~
~
~
baz
~
But I want the following diff:
@@ <whatever> @@
foo
-\n
~
bar
~
~
+\n
baz
~
I have tried adding |\n
to my regex, to no avail. (Btw git uses POSIX "extended" regular expressions.) The docs say that "[a] match that contains a newline is silently truncated(!) at the newline." I don't fully understand what this means but I suspect it could be the cause of the issue.
Is there any way to get git to produce the desired diff?