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!

1
  • Do I understand it correctly that you want to use git as diff-tool? If so, you could just use diffor diffstatunder Linux, see e.g. stackoverflow.com/questions/767198/… Commented Aug 16, 2019 at 14:53

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.