I have a small application that manages several git repositories similar to Github/Gitorious. Github allows for inline file editing, and I'd like to know if anyone has any idea on how they manage this.
My initial thought was that it would do a complete clone of the repository, use your submission to replace the file, commit, and push, but this seems like an very expensive operation with large repositories like the linux kernel.
Any ideas on a more efficient way to add and edit files to a bare repository?
You can use plumbing commands.
Get your current HEAD, get the tree from there then your blobs.
Once you have the blob you can put the content in a textbox. When it's finished you just have to hash the new blob, create the new tree, the new commit and tadaam. It's "pushed".
PS: Remember you're in a bare repository, so check that every command you use don't need the index nor the working directory.
As it has been asked here is a step by step example.
First we get the current file content:
We do our little modification on that content and now have a new content ready to be pushed. To save that content we're going to hash it:
This will save it, and return a sha-1 of that object (blob), the very next step consist in creating a new folder
testwhich will contain ourtext.txtfile. But first let's look at what does the currenttestfolder look like:So what we want to do here, is replace the previous SHA-1 (
9daeafb...) with the new one (9764d22...) and generate a new tree based on that (pay attention to the\t).Great, so now we have the new file
text.txtand the parent foldertest, we now needvar.and now we need the parent of
var(which is the root of our repository):And it's done, our tree is ready. The only thing missing is the actual commit:
But it isn't ready yet, now we want the commit to be the HEAD, so the very last step is:
And now we're done.
Resources: