Skip to content
peter-murach edited this page Apr 15, 2012 · 4 revisions

GithubApi: Committing File

This is a quick primer on how to commit files to your github repository using github_api gem. Using GitHub API v3 we have fain-grained access to manipulate git objects such as blobs, trees and commits. The process is as follows:

Instantiate Github::GitData part of github_api gem with desired authorization:

git_data = Github::GitData.new :basic_auth => 'login:password'

Send any type of content binary or string up to GitHub

blob = git_data.create_blob 'user', 'repo',
    "content": "Content of the blob",
    "encoding": "utf-8"

To confirm that all is fine do a simple status code check:

puts "Created" if blob.status == '201'

Next create tree like:

tree = git_data.create_tree 'user', 'repo', 'tree' => [
      {
        'path': 'file_path',
        'mode': '100644',
        'type': 'blob',
        'sha': blob.sha   # reference to the blob object
      }
    ]

Again quick sanity check:

puts "Tree created" if tree.status == '201'

Finally to create commit object in the root of your repository do:

commit = git_data.create_commit 'login', 'password',
    'message': 'example blob tree',
    'tree': tree.sha  # reference to the tree object

and confirmation

puts "Commit created" if commit.status == '201'
Clone this wiki locally