注意过滤器!
git
实际上是在计算 sha 值之前对文件进行过滤。通常情况下,\r\n 的换行符会被转换为 \n。这就是为什么你可能会在使用 git hash-object
和 git hash-object --no-filters
时得到不同的结果。一些其他的东西也可能被过滤掉,而 .gitattributes
可能会对结果产生影响。
以下是一个在 Windows 命令提示符中的小例子:
在一个新文件夹中创建测试文件:
$ echo this is a test $Id$ > test1.txt
$ echo this is a test $Id: ffbf88668784c14e809c8c449d799b654d7a5fc5 $ > test2.txt
现在使用 git hash-object
$ git hash-object test1.txt
0c3a75d8155d54c2367e290cf7f33434805410be
$ git hash-object test2.txt
60fff1b8ec47ed41254719681e32369d640d6a0f
$ git hash-object --no-filters test2.txt
2f68d9b80a38fb800f039ef9062c764d2a4d4352
不同的文件导致不同的哈希值:没问题,但是 `git` 在某种程度上会过滤文件,因为 `--no-filters` 有影响。
现在在文件夹中创建一个 git 存储库和 .gitattributes。
$ git init .
Initialized empty Git repository in ~/.git
$ echo *.txt ident > .gitattributes
$ git hash-object test1.txt
0c3a75d8155d54c2367e290cf7f33434805410be
$ git hash-object test2.txt
0c3a75d8155d54c2367e290cf7f33434805410be
$ git hash-object --no-filters test2.txt
2f68d9b80a38fb800f039ef9062c764d2a4d4352
现在test1和test2具有相同的哈希值!但是--no-filters
选项仍然给出相同的值。
结论:您可以使用git和openssl获得相同的哈希值,但需要确保您的文件不受git过滤器的影响。