threads / discuss / 20693

Raw commands to add object to existing tree

Subject: Raw commands to add object to existing tree

## tl;dr

4 messages between Aug 21, 2009 and Aug 21, 2009.

replies: 3people: 3as markdown or json

D Sundstrom· Aug 21, 2009, 22:31 UTC · lore

Both the community git manual: http://book.git-scm.com/7_raw_git.html

and Pro Git book: http://progit.org/book/ch9-2.html

walk you though low level details of creating objects and trees in git, and then committing these new trees.

However, they do not show you how to add and commit into existing trees.

Speaking in terms of files, I have a web application that needs to replace a file in a known directory in a known branch of a bare repository. I know the SHA1s of the directory tree and file. I want to commit directly into a bare repository to avoid having to manage working directories for different branches from the web application.

Thanks! David

Avery Pennarun· Aug 21, 2009, 22:39 UTC · re: D Sundstrom · lore

Re: Raw commands to add object to existing tree

On Fri, Aug 21, 2009 at 10:31 PM, D Sundstrom<sunds@peapod.net> wrote:
Show 10 quoted lines
> Both the community git manual:
> http://book.git-scm.com/7_raw_git.html
>
> and Pro Git book:
> http://progit.org/book/ch9-2.html
>
> walk you though low level details of creating objects and trees in git, and
> then committing these new trees.
>
> However, they do not show you how to add and commit into existing trees.

Naturally, you can't change an "existing" tree, because any given tree (identified by its sha-1 hash) can never change. You presumably want to create a *new* tree and commit that.

The easiest way I know to do this is to create a new indexfile, fill it with what you want, and create the tree for that.

(Warning: untested code follows)

export GIT_INDEX_FILE=/tmp/my-temp-index git read-tree PREVIOUS_COMMIT_OR_TREE_ID hash=$(git hash-object NEW_FILE) git update-index --cacheinfo "0644 $hash path/of/file/in/tree" commitid=$(echo COMMIT MESSAGE | git commit-tree $(git write-tree)) git update-ref refs/heads/BRANCHNAME $commitid PREVIOUS_COMMIT_OR_TREE_ID

Have fun,
Avery
Linus Torvalds· Aug 21, 2009, 22:49 UTC · re: Avery Pennarun · lore

Re: Raw commands to add object to existing tree

On Fri, 21 Aug 2009, Avery Pennarun wrote:
Show 8 quoted lines
> (Warning: untested code follows)
> 
> export GIT_INDEX_FILE=/tmp/my-temp-index
> git read-tree PREVIOUS_COMMIT_OR_TREE_ID
> hash=$(git hash-object NEW_FILE)
> git update-index --cacheinfo "0644 $hash path/of/file/in/tree"
> commitid=$(echo COMMIT MESSAGE | git commit-tree $(git write-tree))
> git update-ref refs/heads/BRANCHNAME $commitid PREVIOUS_COMMIT_OR_TREE_ID

Looks roughly correct, but you need to add a a "-p $PREVIOUS_COMMIT" to that git commit-tree thing. Otherwise the new commit will be a root commit. Which _might_ be what you want, of course, but I kind of expect it wasn't.

And no, I didn't test that either, so there may be other things that need fixing too.

Looking at that thing, I have ugly flashbacks to my git scripts in 2005 ;)
			Linus
Avery Pennarun· Aug 21, 2009, 22:56 UTC · re: Linus Torvalds · lore

Re: Raw commands to add object to existing tree

On Fri, Aug 21, 2009 at 10:49 PM, Linus Torvalds<torvalds@linux-foundation.org> wrote:

> Looking at that thing, I have ugly flashbacks to my git scripts in 2005 ;)

It rather resembles my git scripts in 2009, in which I'm using a web service to let people upload Microsoft Access databases and version control and branch them. Long story, but I'm sure glad this plumbing stuff exists :)

And yes, my scripts use the -p option.  Oops.
Have fun,
Avery

← back to recent threads