5

I have a repository where there are many files that only have EOL whitespace changes, but some have actual changes. I am trying to craft an alias to open diffs of only the files with real changes in vim tabs, and as part of that I am running a git diff --name-only ... command to get a list of files that have changed so I can open them in tabs.

My problem is that --ignore-space-at-eol, --ignore-space-change, and --ignore-all-space seem to do nothing when combined with --name-only.

When I run the following command nothing is printed out (my_file has a CR/LF change):

git diff --ignore-space-at-eol my_file

But when I run this I get my_file printed out:

git diff --ignore-space-at-eol --name-only my_file

Is there some way to get only the names of files that have no whitespace changes? This seems like a bug to me, but I haven't been able to find a similar issue.

I am on git version 2.25.0

DrEsperanto
  • 167
  • 9
  • 1
    `git diff --name-only` lists files without looking into the content. You cannot filter the list by the content of the files. – phd May 06 '20 at 16:30
  • @phd I see that that is what it does, but to me that violates my expectations (especially since it does not inform me that those other flags are invalid for `--name-only`). I'm open to suggestions for alternative approaches. – DrEsperanto May 06 '20 at 17:07
  • You might try reporting this as a bug with the Git project folks. They may ignore you, but if they get enough people griping about this—I know *why* Git does this internally, but I don't think Git *should* do this, but I personally don't care enough to fix it myself :-)—maybe someone will actually fix it. – torek May 06 '20 at 18:28
  • @torek And if the OP choses to open a bug, it would be better to do so with Git 2.27: it comes with **[bugreport](https://github.com/git/git/blob/238b439d69890980dafc5154895d425cb4cf4a5e/Documentation/git-bugreport.txt)**, that I mention at the end of https://stackoverflow.com/a/10733251/6309) – VonC May 06 '20 at 18:53

2 Answers2

6

I happened to stumble onto a similar problem recently and also found that using the --name-only option will not work with either --ignore-all-space or --ignore-blank-lines.

As someone in the comments pointed out this is due to --name-only only checking if a file has changed but not calculating the actual change. What I found is that you can use the --stat option instead, since this will calculate the actual changes to files.

So for getting a list of files that have changed while ignoring whitespace changes you might use something like the following:

git diff --stat --ignore-all-space --ignore-blank-lines --diff-filter=M <branchname>

RalfW
  • 61
  • 1
  • 4
1

You will need to:

  • get the list of files first: git diff -–name-only
  • then for each file do a git diff --ignore-space-at-eol my_file

But if you want to perform the second diff only if there are actual changes, wrap that second diff with a test (as in here), using the --quiet option:

if ! git diff --quiet --ignore-space-at-eol my_file; then
    git diff --ignore-space-at-eol my_file
fi
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the answer, and this works when using the native diffing capabilities in git. I think I will need a more sophisticated script to collect the names in an array so that I can pass them to vim, but that is my own application and somewhat outside the scope of this question. I believe the use of --quiet will allow me to do what I need. – DrEsperanto May 07 '20 at 14:51
  • @DrEsperanto Great! Still, I believe what you have mentioned with `git diff --ignore-space-at-eol --name-only my_file` is a bug. – VonC May 07 '20 at 15:11