【问题标题】:Git hash-object is yielding different SHA1 in Powershell, CMD and Bash?Git hash-object 在 Powershell、CMD 和 Bash 中产生不同的 SHA1?
【发布时间】:2018-02-17 00:05:15
【问题描述】:

我认为无论平台如何,SHA1 值都是相同的。我今天遇到了这个,我希望我能在这里得到一些澄清。

我的测试字符串是:'Apple Pie'

在 Bash 中:

echo 'Apple Pie' | git hash-object --stdin
23991897e13e47ed0adb91a0082c31c82fe0cbe5

在 CMD 中(Windows 10):

echo 'Apple Pie' | git hash-object --stdin
f554ff1fdde0e3c2ca9f67849791456302b5c12b

在 Powershell 5.0 (Windows 10) 中:

echo 'Apple Pie' | git hash-object --stdin
157cb7be4778a9cfad23b6fb514e364522167053

我现在很困惑 git 在这里是如何工作的,因为文件内容的 sha1 密钥在不同的环境中非常不同,我不确定如果我将一个项目克隆到我在 Powershell 中构建的 linux 机器中是否可以工作?这种行为在 git 或 SHA1 中是预期的吗?

【问题讨论】:

    标签: bash git powershell hash sha1


    【解决方案1】:

    这三个值无疑都是正确的。你看到的是echo在三个命令解释器中不是同一个命令!

    $ printf 'Apple Pie\n' | git hash-object --stdin
    23991897e13e47ed0adb91a0082c31c82fe0cbe5
    $ printf 'Apple Pie\r\n' | git hash-object --stdin
    157cb7be4778a9cfad23b6fb514e364522167053
    

    编辑:可以通过以下方式在 bash 中模拟 Windows 10 CMD(以获得相同的哈希):

    $ printf "'Apple Pie' \r\n" | git hash-object --stdin
    f554ff1fdde0e3c2ca9f67849791456302b5c12b
    

    感谢that other guy 的提示。

    【讨论】:

    • 感谢您的精彩解释,所以现在我知道问题出在 echo 命令中,但与实际文件内容无关,谢谢。
    • Windows 10 CMD 使用当前代码页的任何内容。与之前的迭代相同。
    • CMD 校验和为printf "'Apple Pie' \r\n" | git hash-object --stdin。在 DOS 和现在的 CMD 中,解析引号和空格由单个程序决定。
    • @thatotherguy:啊哈!是的,我忘记了(因为我避免使用 Windows)Windows 命令解析是如此临时和反复无常。 :-)
    • @thatotherguy DOS 自 2001 年以来未使用过。您错误地将 CMD 和 DOS 等同起来。
    【解决方案2】:

    我在做 CMake 项目时遇到了类似的问题。我发现您可以通过添加 --path / 选项来创建一致性。根据Git docs--path 对对象进行哈希处理,就好像它位于给定路径中一样,如果文件确实存在,则应用任何合适的过滤器。

    由于奇怪的引号解析,它不适用于 CMD 的 echo 问题,它仍然不适用于 CMD 中的单引号,但它适用于大多数程序的输出。

    在我的例子中,我使用cmake -E echo 命令代替echo,然后通过管道将其传送到git hash-object --path / --stdin,并且能够获得一致的哈希值。

    在 Bash 中:

    cmake -E echo "Apple Pie" | git hash-object --path / --stdin
    23991897e13e47ed0adb91a0082c31c82fe0cbe5
    

    在 CMD 中:

    cmake -E echo "Apple Pie" | git hash-object --path / --stdin
    23991897e13e47ed0adb91a0082c31c82fe0cbe5
    

    在 PowerShell 中:

    cmake -E echo "Apple Pie" | git hash-object --path / --stdin
    23991897e13e47ed0adb91a0082c31c82fe0cbe5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-14
      • 2017-03-27
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 2011-03-13
      相关资源
      最近更新 更多