【问题标题】:git hash-object vs shasum doesn't match when the content starts with a number当内容以数字开头时,git hash-object vs shasum 不匹配
【发布时间】:2020-06-21 18:54:32
【问题描述】:

我正在尝试使用自定义解决方案来连接到 git API,当我对以字母开头的内容进行 shasum 时,结果匹配:

$ echo -e -n "blob 4\0test" | shasum
30d74d258442c7c65512eafab474568dd706c430 *-

$ echo -e -n "test" | git hash-object --stdin
30d74d258442c7c65512eafab474568dd706c430

但内容以数字开头时不匹配:

$ echo -e -n "blob 5\0test" | shasum
315a7861230f24fade469e87c0c548f0cc4bc8c8 *-

$ echo -e -n "0test" | git hash-object --stdin
475a3cf5e5dadf80fe51cc8748c9bfdabae29f07

我很困惑为什么它会给出不同的结果,有人知道为什么会这样吗?

【问题讨论】:

    标签: git hash sha1


    【解决方案1】:

    当您使用echo -e 时,您在编写\0 时提供了八进制转义。之后添加其他数字会导致它们被解释为八进制转义中的八进制数字。

    由于 POSIX 没有定义 echo -e 并且它不可移植,因此最好使用 printf,因为您知道八进制转义将被一致地解释并且最多可以包含三个数字:

    $ printf "blob 5\0000test" | shasum
    475a3cf5e5dadf80fe51cc8748c9bfdabae29f07  -
    $ printf "0test" | git hash-object --stdin
    475a3cf5e5dadf80fe51cc8748c9bfdabae29f07
    

    在第一次调用中,\0000 是八进制转义 \000(一个 NUL 字节)加上字符 0(十进制 48)。

    【讨论】:

      猜你喜欢
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多