简体   繁体   English

git diff --numstat 通过字符串而不是文件路径

[英]git diff --numstat by strings instead of file paths

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.我正在尝试在 Linux(Debian Docker 容器)上运行git diff --no-index --numstat <string> <string> ,但我找不到实现这一目标的方法。 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.目标是从--numstat标志中检索统计信息。

This command should be executable outside of a git repository/directory and on Linux.此命令应该在git存储库/目录之外和 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. git diff --no-index --numstat /dev/fd/3 /dev/fd/4 3<<<$(echo "<string>") 4<<<$(echo "<string>") :这个在 MacOS 上工作,但在 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 ) git diff --numstat $(echo <string> | git hash-object -w --stdin) $(echo <string> | git hash-object -w --stdin) :仅在git存储库中有效(得到了这个部分解决方案从这里

Certainly there must be a way to achieve this, either via some git command or other bash concepts I'm unaware of.当然,必须有一种方法来实现这一点,通过一些git命令或我不知道的其他bash概念。 Any help would be great.任何帮助都会很棒。

Thanks!谢谢!

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".解决方案 1. 不起作用的原因是/dev/fd/3/dev/fd/4是符号链接,而git diff不遵循符号链接,而是使用它们的链接目标字符串作为它们的“内容”。

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.将字符串直接而不是文件传递给git diff的唯一方法是作为标准输入 - 这显然只适用于其中一个文件。 So I see only two possible solutions to your problem:所以我认为您的问题只有两种可能的解决方案:

  1. write the strings to (temporary) files first, then pass them to git diff首先将字符串写入(临时)文件,然后将它们传递给git diff
  2. use another tool, as suggested by @B--rian in the comment使用另一种工具,如@B--rian在评论中所建议的

Another, shorter version of 1. using process substitution would be:另一个更短的 1. 使用进程替换的版本是:

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不幸的是,出于同样的原因,这也不起作用/因为git diff不支持进程替换,请参阅https://stackoverflow.com/a/49636553/11932806

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM