Emadpres
Emadpres

Reputation: 3747

git diff output wrong line breakings

I'm writing an scripts which relies on git diff output and the line numbers displayed there. However, I faced a case where git mysteriously showing the output in a wrong format imho.

The problematic case (GitHub diff on web)

The issue in question is at this chunk (GitHub diff) (see image below):

enter image description here

You can also see the code block at parent commit and the actual commit.

The problematic case (git diff)

However performing the git diff --word-diff <full list of options below> gives you the following output (I just show the chunk in question):

@@ -77,2 +80,4 @@ public class CofVGenRoeMetz {
                        phi[i] = [-Gaussian.Phi((u[0]-]{+gauss.cumulativeProbability((u[0]+} + x[i])
                                        / Math.sqrt(scale20))
                                        * [-Gaussian.Phi((u[1]-]{+gauss.cumulativeProbability((u[1]+} + x[i])
                                                        / Math.sqrt(scale21));

Problem

Based on the git diff, one can interpret the / Math.sqrt(scale20)) belongs to line 78(before)/81(after) which is only true for the "after" scenario. Why do I get the wrong output?

In another word, from git output, you will learn the / Math.sqrt(scale20)) belonges to line 78 before this commit happens (this is based on git diff syntax). However, in reality, if you look at the file in parent commit, / Math.sqrt(scale20)) belongs to line 77.



PS: I'm using MacOS Monterey

The full git diff command I used: git diff --no-color --unified=0 --word-diff --ignore-submodules --ignore-all-space 3424dae cfdd0dfd1b7724efbc786d0cfc070dc0696435b4 -- imrmc/mrmc_source/src/simroemetz/core/CofVGenRoeMetz.java

Upvotes: 1

Views: 236

Answers (1)

LeGEC
LeGEC

Reputation: 52176

Try setting --word-diff-regex, to something like : --word-diff-regex="[^ \t]*"

The default regexp ([^[:space:]]* if I'm correct) completely ignores \n in the diff computation.


Quoting git help diff (emphasis mine) :

--word-diff-regex=<regex>

[...]

Every non-overlapping match of the <regex> is considered a word. Anything between these matches is considered whitespace and ignored(!) for the purposes of finding differences.

You may want to append |[^[:space:]] to your regular expression to make sure that it matches all non-whitespace characters. A match that contains a newline is silently truncated(!) at the newline.

Upvotes: 1

Related Questions