1

I'm trying to run git diff --no-index --numstat <string> <string> on Linux (Debian Docker container), but I'm failing in finding a way to achieve this. In essence I want to pass the files' contents as strings instead of their file paths. The goal is to retrieve the stats from the --numstat flag.

This command should be executable outside of a git repository/directory and on Linux. So far, I've found two solutions which lack the former requirements:

  1. git diff --no-index --numstat /dev/fd/3 /dev/fd/4 3<<<$(echo "<string>") 4<<<$(echo "<string>"): This works on MacOS, but fails to work on Linux.
  2. git diff --numstat $(echo <string> | git hash-object -w --stdin) $(echo <string> | git hash-object -w --stdin): which only works inside git repositories (got this partial solution from here)

Certainly there must be a way to achieve this, either via some git command or other bash concepts I'm unaware of. Any help would be great.

Thanks!

Sasha Fonseca
  • 2,257
  • 23
  • 41
  • Do I understand it correctly that you want to use `git` as diff-tool? If so, you could just use `diff`or `diffstat`under Linux, see e.g. https://stackoverflow.com/questions/767198/how-to-get-diff-to-report-summary-of-new-changed-and-deleted-lines – B--rian Aug 16 '19 at 14:53

1 Answers1

2

The reason that solution 1. isn't working is that /dev/fd/3 and /dev/fd/4 are symlinks and git diff does not follow symlinks but instead uses their link target string as their "content".

The only way to pass a string to git diff directly instead of a file is as stdin - which obviously only works for one of the files. So I see only two possible solutions to your problem:

  1. write the strings to (temporary) files first, then pass them to git diff
  2. use another tool, as suggested by @B--rian in the comment

Another, shorter version of 1. using process substitution would be:

git diff --no-index --numstat <(echo "<string1>") <(echo "<string2>")

Which unfortunately doesn't work either for the same reason/because git diff does not support process substitution, see https://stackoverflow.com/a/49636553/11932806

acran
  • 7,070
  • 1
  • 18
  • 35