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):
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
git diff --word-diff
first makes a standard (line by line) diff. Then Git applies the word diff regex to each word in the various changed lines and rewrites the output-to-be-displayed with the word-change markers. Depending on both the regexp and the options like--ignore-all-space
, this can sometimes remove the entire diff, so that you get a diff hunk with no change in it, or otherwise damage the diff hunk in ways that will make some sort of sense to a human but be useless for machine replication. Don't trust the word diff.