【问题标题】:Replicating `git hash-object` with non-ascii characters用非ASCII字符复制`git hash-object`
【发布时间】:2018-06-28 20:43:52
【问题描述】:

尝试复制哈希对象,我发现在使用非 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?

【问题讨论】:

    标签: git hash character-encoding sha


    【解决方案1】:

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

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 2012-10-19
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多