# Problem creating commits/trees with commit-tree/mktree

4 messages from 2010-01-11 to 2010-01-14. Participants: Gavin Beatty, Shawn O. Pearce, Avery Pennarun.
Thread: https://gitlist.dev/t/22171

## Gavin Beatty, 2010-01-11 18:14

Subject: Problem creating commits/trees with commit-tree/mktree
Message-ID: <f6d77fed1001111014g73a06923na05cd14d37968b04@mail.gmail.com>
URL: https://gitlist.dev/e/f6d77fed1001111014g73a06923na05cd14d37968b04%40mail.gmail.com

```
Hello,

I want to write commits to a branch without touching the index or
having a checkout (for a git subcommand I'm writing).

I can create new blobs and trees but can't figure out how to commit a
new tree/blob _with_ the old tree.

Currently, I do something a lot like:

objsha=$(echo 'contents' | git hash-object -w --stdin)
objtreesha=$(printf "100644 blob $objsha\tfile.txt\000" | git mktree -z)
newtreesha=$(printf "040000 tree $objtreesha\ttreefileisin\000" | git mktree -z)
echo 'commit msg' | git commit-tree $newtreesha -p $(git rev-parse
refs/heads/new)

I get a commit with treefileisin/file.txt. I haven't included the
other trees/files so they are gone in this commit. How do I include
them? Is commit-tree the wrong tool?

Is there some way to use git ls-tree that I don't know about?

Gavin

-- 
Gavin Beatty

SEMPER UBI SUB UBI

```

## Shawn O. Pearce, 2010-01-11 18:17

Subject: Re: Problem creating commits/trees with commit-tree/mktree
Message-ID: <20100111181712.GQ32155@spearce.org>
URL: https://gitlist.dev/e/20100111181712.GQ32155%40spearce.org
In-Reply-To: <f6d77fed1001111014g73a06923na05cd14d37968b04@mail.gmail.com>

```
Gavin Beatty <gavinbeatty@gmail.com> wrote:
> I want to write commits to a branch without touching the index or
> having a checkout (for a git subcommand I'm writing).
> 
> I can create new blobs and trees but can't figure out how to commit a
> new tree/blob _with_ the old tree.
> 
> Currently, I do something a lot like:
> 
> objsha=$(echo 'contents' | git hash-object -w --stdin)
> objtreesha=$(printf "100644 blob $objsha\tfile.txt\000" | git mktree -z)
> newtreesha=$(printf "040000 tree $objtreesha\ttreefileisin\000" | git mktree -z)

You aren't feeding in the old tree contents as part of this command.

If you are really doing this via a script, you should look at
git-fast-import.  Its faster, and its language better supports
this notion of editing an existing tree.

-- 
Shawn.

```

## Avery Pennarun, 2010-01-11 18:38

Subject: Re: Problem creating commits/trees with commit-tree/mktree
Message-ID: <32541b131001111038m3dacaf72sc12f24aba8c60e@mail.gmail.com>
URL: https://gitlist.dev/e/32541b131001111038m3dacaf72sc12f24aba8c60e%40mail.gmail.com
In-Reply-To: <f6d77fed1001111014g73a06923na05cd14d37968b04@mail.gmail.com>

```
On Mon, Jan 11, 2010 at 1:14 PM, Gavin Beatty <gavinbeatty@gmail.com> wrote:
> I can create new blobs and trees but can't figure out how to commit a
> new tree/blob _with_ the old tree.
[...]
> I get a commit with treefileisin/file.txt. I haven't included the
> other trees/files so they are gone in this commit. How do I include
> them? Is commit-tree the wrong tool?

When I'm doing similar things, I often prefer just using a temporary
git index file to keep track of my intermediate trees.  Just set
GIT_INDEX_FILE to point at a temporary file; then you can use
git-read-tree to read in an old tree, and git-update-index
(particularly with the --stdin flag) to update it.  Then you can use
git-write-tree to convert the temporary index into a real tree object.

Have fun,

Avery

```

## Gavin Beatty, 2010-01-14 23:18

Subject: Re: Problem creating commits/trees with commit-tree/mktree
Message-ID: <f6d77fed1001141518l22987752x5a1c1e84cb68b677@mail.gmail.com>
URL: https://gitlist.dev/e/f6d77fed1001141518l22987752x5a1c1e84cb68b677%40mail.gmail.com
In-Reply-To: <32541b131001111038m3dacaf72sc12f24aba8c60e@mail.gmail.com>

```
On Mon, Jan 11, 2010 at 19:38, Avery Pennarun <apenwarr@gmail.com> wrote:
> When I'm doing similar things, I often prefer just using a temporary
> git index file to keep track of my intermediate trees.  Just set
> GIT_INDEX_FILE to point at a temporary file; then you can use
> git-read-tree to read in an old tree, and git-update-index
> (particularly with the --stdin flag) to update it.  Then you can use
> git-write-tree to convert the temporary index into a real tree object.

Your suggestion works well.  git update-index --info-only -z
--index-info takes ls-tree -r -z style format and pre-existing
objects: just as I wanted.

Thanks, git-fast-import was too much for the quite simple git-related
code I need.

-- 
Gavin Beatty

SEMPER UBI SUB UBI

```
