# Problem: `fatal: too-short tree object` when executing hash-object on tree

3 messages from 2023-08-31 to 2023-08-31. Participants: Gareth Hayes, Johannes Sixt, Junio C Hamano.
Thread: https://gitlist.dev/t/60178

## Gareth Hayes, 2023-08-31 10:58

Subject: Problem: `fatal: too-short tree object` when executing hash-object on tree
Message-ID: <0de414f8-f409-467f-a504-06a78f088981@app.fastmail.com>
URL: https://gitlist.dev/e/0de414f8-f409-467f-a504-06a78f088981%40app.fastmail.com

```
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`

This results in an error:
`fatal: too-short tree object`

To replicate: 
`git cat-file -p HEAD`
`git cat-file -p <tree object identifier from output of above> | git hash-object -t tree --stdin`

This works for other object types but not trees. What am I doing wrong?

```

## Johannes Sixt, 2023-08-31 17:10

Subject: Re: Problem: `fatal: too-short tree object` when executing hash-object on tree
Message-ID: <defa2a66-598e-4721-a644-680de0116dec@kdbg.org>
URL: https://gitlist.dev/e/defa2a66-598e-4721-a644-680de0116dec%40kdbg.org
In-Reply-To: <0de414f8-f409-467f-a504-06a78f088981@app.fastmail.com>

```
Am 31.08.23 um 12:58 schrieb Gareth Hayes:
> 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`

XY-problem alert!?

> This results in an error:
> `fatal: too-short tree object`
> 
> To replicate: 
> `git cat-file -p HEAD`
> `git cat-file -p <tree object identifier from output of above> | git hash-object -t tree --stdin`
> 
> This works for other object types but not trees. What am I doing wrong?

Recall that the -p in cat-file means "pretty" (-print). The data that
makes up a tree object is far from pretty, hence, you can't feed it into
git hash-object directly.

You can either turn the pretty-printed input into a tree object

   git cat-file -p $treeoid | git mktree

or hash the raw data

   git cat-file tree $treeoid | git hash-object -t tree --stdin


-- Hannes


```

## Junio C Hamano, 2023-08-31 17:25

Subject: Re: Problem: `fatal: too-short tree object` when executing hash-object on tree
Message-ID: <xmqqjztbw2cf.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqjztbw2cf.fsf%40gitster.g
In-Reply-To: <0de414f8-f409-467f-a504-06a78f088981@app.fastmail.com>

```
"Gareth Hayes" <gareth.hayes@bitcoin.org.hk> writes:

> 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`
>
> This results in an error:
> `fatal: too-short tree object`
>
> To replicate: 
> `git cat-file -p HEAD`
> `git cat-file -p <tree object identifier from output of above> | git hash-object -t tree --stdin`
>
> This works for other object types but not trees. What am I doing wrong?

It would work *ONLY* for blob, and not commit or tag object types,
no?

The "-p" option is to present the object in a human-friendly format,
as opposed to giving the raw stream of bytes that is suitable for
machine consumption and required by hash-object.  It so happens that
for "blob", the raw stream of bytes is just as human-friendly as the
tool can make, without having a deep knowledge of the content type
(e.g. it may be possible to make "cat-file -p <blob>" to somehow
apply an appropriate textconv filter if the path to <blob> is known
as a future enhancement, but such a code does not exist yet, and
when it happens, even "cat-file -p <blob> | hash-object --stdin"
would not round-trip).

So, that is what you are doing wrong, I think.

```
