'use strict';

var reponame = 'bloghub-test';
var Octokat = require('octokat');
var base64 = require('base-64');
var utf8 = require('utf8');

function base64encode(string) {
  return base64.encode(utf8.encode(string));
}

var user = JSON.parse(localStorage.getItem('github'));

var octo = new Octokat({
  token: user.token
});

var repo = octo.repos(user.username, reponame);
repo.contents('README.md').readBinary().then(function(contents) {
  console.log(contents);
});

repo.contents('test.md').add({
  message: 'Updating file',
  content: base64encode('New file contents'),
  sha: '123456789abcdef'
}).then(function(info) {
  console.log('File Updated. new sha is ', info.commit.sha);
});

@@ -215,16 +215,79 @@
 g));%0A%7D%0A%0A
+function log(thing) %7B%0A  console.log(thing);%0A  return thing;%0A%7D%0A%0A
 var user
@@ -383,17 +383,16 @@
 ken%0A%7D);%0A
-%0A
 var repo
@@ -435,104 +435,1436 @@
 e);%0A
-repo.contents('README.md').readBinary().then(function(contents) %7B%0A  console.log(contents);%0A%7D);%0A%0A
+%0Afunction fetchHead(branch) %7B%0A  return repo.git.refs.heads(branch %7C%7C 'master').fetch();%0A%7D%0A%0Avar head;%0Afunction fetchTree(branch) %7B%0A  return fetchHead(branch).then(function(commit) %7B%0A    head = commit;%0A    return repo.git.trees(commit.object.sha).fetch();%0A  %7D);%0A%7D%0A%0Afunction fetchReadme() %7B%0A  return repo.contents('README.md').readBinary().then(log);%0A%7D%0A%0Afunction commitTree() %7B%0A  return Promise.all(%5B%0A    repo.git.blobs.create(%7B%0A      content: 'Content of the blob' + new Date(),%0A      encoding: 'utf-8'%0A    %7D),%0A    repo.git.blobs.create(%7B%0A      content: 'Content of the blob 2' + new Date(),%0A      encoding: 'utf-8'%0A    %7D)%0A  %5D).then(function(blobs) %7B%0A    return fetchTree().then(function(tree) %7B%0A      return %7B%0A        blobs: blobs,%0A        tree: tree%0A      %7D;%0A    %7D);%0A  %7D).then(function(data) %7B%0A    return repo.git.trees.create(%7B%0A      tree: data.blobs.map(function(blob) %7B%0A        return %7B%0A          path: blob.sha + '.md',%0A          mode: '100644',%0A          type: 'blob',%0A          sha: blob.sha%0A        %7D;%0A      %7D),%0A      base_tree: data.tree.sha%0A    %7D);%0A  %7D).then(function(tree) %7B%0A    return repo.git.commits.create(%7B%0A      message: 'Test commit of multiple files',%0A      tree: tree.sha,%0A      parents: %5B%0A        head.object.sha%0A      %5D%0A    %7D);%0A  %7D).then(function(commit) %7B%0A    return repo.git.refs.heads('master').update(%7B%0A      sha: commit.sha%0A    %7D);%0A  %7D).then(log);%0A%7D%0A%0AcommitTree();%0A%0Afunction commitOneFile() %7B%0A  return 
 repo
@@ -1890,16 +1890,18 @@
 ).add(%7B%0A
+  
   messag
@@ -1920,16 +1920,18 @@
  file',%0A
+  
   conten
@@ -1970,16 +1970,18 @@
 ts'),%0A  
+ 
+ 
 sha: '12
@@ -1995,24 +1995,26 @@
 abcdef'%0A
+  
 %7D).then(
 function
@@ -2009,86 +2009,12 @@
 hen(
-function(info) %7B%0A  console.log('File Updated. new sha is ', info.commit.sha
+log
 );%0A%7D
-);
 %0A

Allow to commit multiple files