用非ASCII字符复制`git hash-object`

时间:2018-06-28 12:32:31

标签: git hash character-encoding sha

尝试复制哈希对象时,发现使用非ASCII字符时无法正常工作。

$ printf hola | git hash-object -w --stdin
b8b4a4e2a5db3ebed5f5e02beb3e2d27bca9fc9a
$ printf "blob 4\0hola" | shasum
b8b4a4e2a5db3ebed5f5e02beb3e2d27bca9fc9a

但是如果我添加磅符号

$ printf hola£ | git hash-object -w --stdin
8f9852933655612593d0bbd43c9f7c6f25d947a0
$ printf "blob 5\0hola£" | shasum
54386ef126fcfc9e8242c6d6bade401b1f27999a

知道为什么会这样吗?

相关:How to assign a Git SHA1's to a file without Git?

1 个答案:

答案 0 :(得分:4)

数字定义字节数,而不是字符数

UTF-8中的£为2字节宽(0xc2 0xa3),因此:

printf "blob 6\0hola£" | shasum是您想要的

将按预期返回8f9852933655612593d0bbd43c9f7c6f25d947a0

自我检查:

解压缩刚刚编写的对象的内容:

printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" | cat - .git/objects/8f/9852933655612593d0bbd43c9f7c6f25d947a0 | gzip -dc | xxd

(或使用Pigz:Deflate command line tool