使用非 ascii 字符复制 `git hash-object`

Replicating `git hash-object` with non-ascii characters

尝试复制散列对象时我发现它在使用非 ascii 字符时不起作用。

$ printf hola | git hash-object -w --stdin
b8b4a4e2a5db3ebed5f5e02beb3e2d27bca9fc9a
$ printf "blob 4[=11=]hola" | shasum
b8b4a4e2a5db3ebed5f5e02beb3e2d27bca9fc9a

但是如果我添加英镑符号

$ printf hola£ | git hash-object -w --stdin
8f9852933655612593d0bbd43c9f7c6f25d947a0
$ printf "blob 5[=12=]hola£" | shasum
54386ef126fcfc9e8242c6d6bade401b1f27999a

知道为什么会这样吗?

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

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

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

printf "blob 6[=11=]hola£" | shasum就是你想要的

符合预期returns 8f9852933655612593d0bbd43c9f7c6f25d947a0

检查自己:

解压刚刚写入的对象内容:

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

(或使用 pigz:Deflate command line tool