2

According to the git diff manual you should be able to do the following

git diff [options] <commit> <commit> [--] [<path>...]

I want to compare only *.h,*.c,*,*.cpp files. So I tried something like the bellow after reading What option should be used restrict the git diff to a given set of file extensions?:

$ git diff --shortstat `git rev-list --since="jun 30 2014" --reverse origin/master | head -1`..`git rev-list --until="dec 31 2014" origin/master | head -1` -- `find -name '*.h' -print0`

I have tried the find -name '*.h' -print0 command and it prints out plenty of .h files. But when I run the above whole command I get no result.

The following command also works fine:

git diff --shortstat `git rev-list --since="jun 30 2014" --reverse origin/master | head -1`..`git rev-list --until="dec 31 2014" origin/master | head -1`

It prints out

372 files changed, 31650 insertions(+), 9580 deletions(-)

But this I assume are for all files stored in the repo and I only want to know the difference among .h .c .cpp files.

Perhaps you have a better idea or you see what might be the problem?

Community
  • 1
  • 1
user1825441
  • 507
  • 9
  • 13

3 Answers3

0

You can try this

  git diff --name-only SHA1 SHA2
Sameer
  • 3,124
  • 5
  • 30
  • 57
0

The <path> supports wild cards, so you could just add: -- *.h *.c *.cpp at the end of your command. Make sure to remember to cdto the root of the git repo first, because this syntax will look for .h, .c and .cpp in current directory (recursive).

So:

cd `git rev-parse --show-toplevel`
git diff --shortstat `git rev-list --since="jun 30 2014" --reverse origin/master | head -1`..`git rev-list --until="dec 31 2014" origin/master | head -1` -- *.h *.c *.cpp
Alderath
  • 3,761
  • 1
  • 25
  • 43
  • What? `*.h` does not look for `*.h` recursively. It is expanded by your shell, and most shells (including bash) only perform wildcard expansion in the current directory. – nneonneo Jul 16 '15 at 16:28
  • @nneonneo You are incorrect. I have tested that it works for files in nested subdirectories in both `tcsh` and `bash`. I can even put quotes around it: `-- "*.h*" "*.c" "*.cpp"`. It still works, and when it is quoted, it is definitely not expanded by the shell. Whether Git can handle the syntax might be dependent on which version of Git you have (I checked that it works in 2.1.2 but not in 1.7.1) – Alderath Jul 17 '15 at 07:46
0

Your problem is that -print0 uses nul-terminators to separate the filenames, but this winds up passing all your filenames as a single argument to git diff (which won't understand the nuls).

Instead, use xargs:

# variables for sanity
START=$(git rev-list --since="jun 30 2014" --reverse origin/master | head -1)
END=$(git rev-list --until="dec 31 2014" origin/master | head -1)

find . \( -name '*.h' -o -name '*.cpp' -o -name '*.c' \) -print0 | xargs -0 git diff --shortstat ${START}..${END} --

find outputs nul-terminated file paths, which are passed to xargs -0 which splits those paths into arguments to git diff.

nneonneo
  • 171,345
  • 36
  • 312
  • 383