HACKER Q&A
📣 asimpletune

In Git, how do you “insert” a blob at an arbitrary position in a tree?


Note: let's just say that the normal way of doing things, I.e. through git porcelain, are not an option. FWIW, yes, I've read the git internals documentation, and they were helpful but very brief. They explained blobs, trees, and working directories, but I think for me to figure out on my own how to do what I want to do I would have to read some of the code for how git porcelain is implemented, which is fine.

My question:

Let's say I have a file that represents an array, where each element of the array is separated by a new line character. How would I append a new line to the end of that file, without actually editing the file directly? Just by creating a new blob, and associating that blob with a tree?

Then, imagine if the file began and ended with square brackets, so now to append to this "array" I need to insert at one line minus the end of the file (you can assume the brackets have a new line after/before, respectively)

The bonus round is taking the same logic from above, but applying it to just arbitrary lines within a file. This isn't so necessary for what I'm working on, but git obviously has a way of doing this, so I'd like to understand the general solution.

For context why I even want to do this:

I'd like to update a file that's in a GH repo, without actually pulling the file to do it. GH has a low level API that's basically an HTTP interface exposing many low-level GitHub operations. So, I'd like to just say "add this line, to this file", without having to clone the repo or do a bunch of back and fourth over the network.


  👤 saurik Accepted Answer ✓
AFAIK (but I don't work with the network protocol, only the repository layout; that said, I do have a passing understanding of the network protocol) you fundamentally have to pull the whole file, and git doesn't "obviously ha[ve] a way of doing this".

That said, at the end of your question you stop talking about git and start talking about GitHub and APIs offered by them, which is of course entirely unrelated to working with git itself and they could provide absolutely any random complex functionality.