{"thread":{"id":"20693","subject":"Raw commands to add object to existing tree","startedAt":"2009-08-21T22:31:19Z","lastAt":"2009-08-21T22:56:23Z","messageCount":4,"participants":["D Sundstrom","Avery Pennarun","Linus Torvalds"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"121473","messageId":"ef72456d0908211531m5e261805p2f67d3a8a16a0b5@mail.gmail.com","threadId":"20693","inReplyTo":null,"subject":"Raw commands to add object to existing tree","fromName":"D Sundstrom","fromEmail":"sunds@peapod.net","sentAt":"2009-08-21T22:31:19Z","receivedAt":"2009-08-21T22:31:19Z","isPatch":false,"sender":{"key":"sunds@peapod.net","avatar":null},"body":"Both the community git manual:\nhttp://book.git-scm.com/7_raw_git.html\n\nand Pro Git book:\nhttp://progit.org/book/ch9-2.html\n\nwalk you though low level details of creating objects and trees in git, and\nthen committing these new trees.\n\nHowever, they do not show you how to add and commit into existing trees.\n\nSpeaking in terms of files, I have a web application that needs to replace\na file in a known directory in a known branch of a bare repository.  I know the\nSHA1s of the directory tree and file.  I want to commit directly into a bare\nrepository to avoid having to manage working directories for different\nbranches from the web application.\n\nThanks!\nDavid\n"},{"id":"121474","messageId":"32541b130908211539ke64bd2bif0aa7cff6bc6ae8f@mail.gmail.com","threadId":"20693","inReplyTo":"ef72456d0908211531m5e261805p2f67d3a8a16a0b5@mail.gmail.com","subject":"Re: Raw commands to add object to existing tree","fromName":"Avery Pennarun","fromEmail":"apenwarr@gmail.com","sentAt":"2009-08-21T22:39:49Z","receivedAt":"2009-08-21T22:39:49Z","isPatch":false,"sender":{"key":"apenwarr@gmail.com","avatar":"https://avatars.githubusercontent.com/u/20592?v=4"},"body":"On Fri, Aug 21, 2009 at 10:31 PM, D Sundstrom<sunds@peapod.net> wrote:\n> Both the community git manual:\n> http://book.git-scm.com/7_raw_git.html\n>\n> and Pro Git book:\n> http://progit.org/book/ch9-2.html\n>\n> walk you though low level details of creating objects and trees in git, and\n> then committing these new trees.\n>\n> However, they do not show you how to add and commit into existing trees.\n\nNaturally, you can't change an \"existing\" tree, because any given tree\n(identified by its sha-1 hash) can never change.  You presumably want\nto create a *new* tree and commit that.\n\nThe easiest way I know to do this is to create a new indexfile, fill\nit with what you want, and create the tree for that.\n\n(Warning: untested code follows)\n\nexport GIT_INDEX_FILE=/tmp/my-temp-index\ngit read-tree PREVIOUS_COMMIT_OR_TREE_ID\nhash=$(git hash-object NEW_FILE)\ngit update-index --cacheinfo \"0644 $hash path/of/file/in/tree\"\ncommitid=$(echo COMMIT MESSAGE | git commit-tree $(git write-tree))\ngit update-ref refs/heads/BRANCHNAME $commitid PREVIOUS_COMMIT_OR_TREE_ID\n\nHave fun,\n\nAvery\n"},{"id":"121476","messageId":"alpine.LFD.2.01.0908211546250.3158@localhost.localdomain","threadId":"20693","inReplyTo":"32541b130908211539ke64bd2bif0aa7cff6bc6ae8f@mail.gmail.com","subject":"Re: Raw commands to add object to existing tree","fromName":"Linus Torvalds","fromEmail":"torvalds@linux-foundation.org","sentAt":"2009-08-21T22:49:52Z","receivedAt":"2009-08-21T22:49:52Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Fri, 21 Aug 2009, Avery Pennarun wrote:\n\n> (Warning: untested code follows)\n> \n> export GIT_INDEX_FILE=/tmp/my-temp-index\n> git read-tree PREVIOUS_COMMIT_OR_TREE_ID\n> hash=$(git hash-object NEW_FILE)\n> git update-index --cacheinfo \"0644 $hash path/of/file/in/tree\"\n> commitid=$(echo COMMIT MESSAGE | git commit-tree $(git write-tree))\n> git update-ref refs/heads/BRANCHNAME $commitid PREVIOUS_COMMIT_OR_TREE_ID\n\nLooks roughly correct, but you need to add a a \"-p $PREVIOUS_COMMIT\" to \nthat git commit-tree thing. Otherwise the new commit will be a root \ncommit. Which _might_ be what you want, of course, but I kind of expect it \nwasn't.\n\nAnd no, I didn't test that either, so there may be other things that need \nfixing too.\n\nLooking at that thing, I have ugly flashbacks to my git scripts in 2005 ;)\n\n\t\t\tLinus\n"},{"id":"121477","messageId":"32541b130908211556w314bca57hb6c8902d78528c95@mail.gmail.com","threadId":"20693","inReplyTo":"alpine.LFD.2.01.0908211546250.3158@localhost.localdomain","subject":"Re: Raw commands to add object to existing tree","fromName":"Avery Pennarun","fromEmail":"apenwarr@gmail.com","sentAt":"2009-08-21T22:56:23Z","receivedAt":"2009-08-21T22:56:23Z","isPatch":false,"sender":{"key":"apenwarr@gmail.com","avatar":"https://avatars.githubusercontent.com/u/20592?v=4"},"body":"On Fri, Aug 21, 2009 at 10:49 PM, Linus\nTorvalds<torvalds@linux-foundation.org> wrote:\n> Looking at that thing, I have ugly flashbacks to my git scripts in 2005 ;)\n\nIt rather resembles my git scripts in 2009, in which I'm using a web\nservice to let people upload Microsoft Access databases and version\ncontrol and branch them.  Long story, but I'm sure glad this plumbing\nstuff exists :)\n\nAnd yes, my scripts use the -p option.  Oops.\n\nHave fun,\n\nAvery\n"}]}