`git diff -w` (or `--ignore-all-space`) is broken

ⅰ亾dé卋堺 提交于 2021-02-19 02:34:06

问题


According to the official git docs:

git diff
    -w, --ignore-all-space
           Ignore whitespace when comparing lines. This ignores differences
           even if one line has whitespace where the other line has none.

However, a basic test shows that this is not really true.

Reproduction steps:

  1. Create somefile.txt with the following contents:
    • Line 1: Any text you want
    • Line 2: Any non-zero number of spaces
    • Line 3: Any text you want
  2. git add somefile.txt
  3. cp somefile.txt somefile.orig.txt
  4. Modify somefile.txt:
    • Line 1: Make any change you want
    • Line 2: Remove all the spaces (will now be an empty line)
    • Line 3: Do not change this line
  5. Create a so-called "whitespace-ignoring" diff with git diff: git diff -w somefile.txt > somefile.gitdiff
  6. Create a truly whitespace-ignoring diff with GNU diff: diff -uw somefile.orig.txt somefile.txt > somefile.diff
  7. Compare somefile.gitdiff vs somefile.diff. You should see that both diff and git diff have produced a diff that aims to modify only Line 1 (so far, so good). However...

Expected result:

Both diffs should be the same (except only for the filenames). Line 2 should appear as a context-only line, with its whitespace intact, unaltered from the original.

Actual result:

The diff generated by GNU diff is perfect; however, the diff generated by git diff -w has a critical flaw: the whitespace remains removed in Line 2.

Line 2 does appear as a context line and not as a line to be changed; however, because it is different from the original without being specified as such, it 1) is a whitespace change that was not actually ignored (contrary to what git diff -w says on the tin), and even worse, 2) it's not a correct/proper diff at all as it does not exactly/cleanly match the original file it was generated against (and thus it cannot be relied upon, something that's kind of a big deal for core tools like diff).

This problem is further complicated by an additional bug in git apply --ignore-whitespace also failing to do what it's supposed to (and so attempting to apply the given flawed patch will fail), as seen in the table below:

A pretty table to hopefully make this clearer

diff -w and patch --ignore-whitespace work perfectly. Not so with git's equivalents.

Note: I have tested and encountered the same results using both git v. 2.15.1 and git v. 2.25.1

Questions:

Given that git is such a well-established incredibly popular tool that's been around for well over a decade, I'm very surprised/skeptical that 1) it could still have not one but two rather conspicuous bugs at a rather fundamental level, and 2) I might possibly be the only/first person to notice/report this, after all this time.

Due to the points above, I thought I would ask the SO community first:

  • Have I missed something? Or am I really the first person to bother with raising this issue, in 2021?
  • Is there any clever/slick workaround for this?
  • Is this a legitimate bug that I should report to the git community?

来源:https://stackoverflow.com/questions/66221885/git-diff-w-or-ignore-all-space-is-broken

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!