{"thread":{"id":"60178","subject":"Problem: `fatal: too-short tree object` when executing hash-object on tree","startedAt":"2023-08-31T10:58:59Z","lastAt":"2023-08-31T17:26:00Z","messageCount":3,"participants":["Gareth Hayes","Johannes Sixt","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"481232","messageId":"0de414f8-f409-467f-a504-06a78f088981@app.fastmail.com","threadId":"60178","inReplyTo":null,"subject":"Problem: `fatal: too-short tree object` when executing hash-object on tree","fromName":"Gareth Hayes","fromEmail":"gareth.hayes@bitcoin.org.hk","sentAt":"2023-08-31T10:58:30Z","receivedAt":"2023-08-31T10:58:59Z","isPatch":false,"sender":{"key":"gareth.hayes@bitcoin.org.hk","avatar":null},"body":"Problem: I'm trying to reproduce the identifier of a tree object using `git cat-file -p <tree object identifier> | git hash-object -t tree --stdin`\n\nThis results in an error:\n`fatal: too-short tree object`\n\nTo replicate: \n`git cat-file -p HEAD`\n`git cat-file -p <tree object identifier from output of above> | git hash-object -t tree --stdin`\n\nThis works for other object types but not trees. What am I doing wrong?\n"},{"id":"481247","messageId":"defa2a66-598e-4721-a644-680de0116dec@kdbg.org","threadId":"60178","inReplyTo":"0de414f8-f409-467f-a504-06a78f088981@app.fastmail.com","subject":"Re: Problem: `fatal: too-short tree object` when executing hash-object on tree","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2023-08-31T17:10:28Z","receivedAt":"2023-08-31T17:10:33Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 31.08.23 um 12:58 schrieb Gareth Hayes:\n> Problem: I'm trying to reproduce the identifier of a tree object using `git cat-file -p <tree object identifier> | git hash-object -t tree --stdin`\n\nXY-problem alert!?\n\n> This results in an error:\n> `fatal: too-short tree object`\n> \n> To replicate: \n> `git cat-file -p HEAD`\n> `git cat-file -p <tree object identifier from output of above> | git hash-object -t tree --stdin`\n> \n> This works for other object types but not trees. What am I doing wrong?\n\nRecall that the -p in cat-file means \"pretty\" (-print). The data that\nmakes up a tree object is far from pretty, hence, you can't feed it into\ngit hash-object directly.\n\nYou can either turn the pretty-printed input into a tree object\n\n   git cat-file -p $treeoid | git mktree\n\nor hash the raw data\n\n   git cat-file tree $treeoid | git hash-object -t tree --stdin\n\n\n-- Hannes\n\n"},{"id":"481249","messageId":"xmqqjztbw2cf.fsf@gitster.g","threadId":"60178","inReplyTo":"0de414f8-f409-467f-a504-06a78f088981@app.fastmail.com","subject":"Re: Problem: `fatal: too-short tree object` when executing hash-object on tree","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-08-31T17:25:52Z","receivedAt":"2023-08-31T17:26:00Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Gareth Hayes\" <gareth.hayes@bitcoin.org.hk> writes:\n\n> Problem: I'm trying to reproduce the identifier of a tree object using `git cat-file -p <tree object identifier> | git hash-object -t tree --stdin`\n>\n> This results in an error:\n> `fatal: too-short tree object`\n>\n> To replicate: \n> `git cat-file -p HEAD`\n> `git cat-file -p <tree object identifier from output of above> | git hash-object -t tree --stdin`\n>\n> This works for other object types but not trees. What am I doing wrong?\n\nIt would work *ONLY* for blob, and not commit or tag object types,\nno?\n\nThe \"-p\" option is to present the object in a human-friendly format,\nas opposed to giving the raw stream of bytes that is suitable for\nmachine consumption and required by hash-object.  It so happens that\nfor \"blob\", the raw stream of bytes is just as human-friendly as the\ntool can make, without having a deep knowledge of the content type\n(e.g. it may be possible to make \"cat-file -p <blob>\" to somehow\napply an appropriate textconv filter if the path to <blob> is known\nas a future enhancement, but such a code does not exist yet, and\nwhen it happens, even \"cat-file -p <blob> | hash-object --stdin\"\nwould not round-trip).\n\nSo, that is what you are doing wrong, I think.\n"}]}