Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
git散列对象与shasum对象的比较';内容以数字开头时不匹配_Git_Hash_Sha1 - Fatal编程技术网

git散列对象与shasum对象的比较';内容以数字开头时不匹配

git散列对象与shasum对象的比较';内容以数字开头时不匹配,git,hash,sha1,Git,Hash,Sha1,我正在尝试一个自定义解决方案来连接git API,当我对以字母开头的内容进行shasum时,结果匹配: $ echo -e -n "blob 4\0test" | shasum 30d74d258442c7c65512eafab474568dd706c430 *- $ echo -e -n "test" | git hash-object --stdin 30d74d258442c7c65512eafab474568dd706c430 但内容以数字开头

我正在尝试一个自定义解决方案来连接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

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

当你使用
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)