Pithub: How to commit a new file via the Github v3 API
In my last article I was demonstrating how to create a new download using Pithub. Today I will show you how to commit a new file to a repository. Github provides a low level API, which can be used to work directly with git objects like blobs, trees and commits. Committing a file needs several steps:
- create a new blob with the content of the file
- get the SHA the current master branch points to
- fetch the tree this SHA belongs to
- create a new tree object with the new blob, based on the old tree
- create a new commit object using the new tree and point its parent to the current master
- finally update the heads/master reference to point to the new commit
You can also read up the explanation in different words on the Github API documentation. If you want to read more about the git internals and how everything works together, please check the Pro Git book, Chapter 9: Git Internals.
Ready? One note before: I’m skipping some validation code here, just to make it easier to read. In the full example script the validation code is present. Let’s take a look at step 1: create a new blob with the content of the file:
That was easy. We just used File::Slurp to read the content of the current file and create a new blob with that content. The encoding is set to utf-8 because the whole content can be represented in utf-8 without loosing any information. If you want to create a binary file, you need to encode the content in base64 and set the encoding accordingly.
The next steps are to get the SHA the current master branch points to and the tree this SHA belongs to:
$base_commit represents the last commit in the master branch. It also has the information (SHA) to which tree this commit belongs to. This SHA is necessary to create a new tree object (see the base_tree key):
Once the new tree has been created, we can finally create a new commit and point the master branch (or better: the heads/master reference) to it: