<commit_before>'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));
}

function log(thing) {
  console.log(thing);
  return thing;
}

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

var octo = new Octokat({
  token: user.token
});
var repo = octo.repos(user.username, reponame);

function fetchHead(branch) {
  return repo.git.refs.heads(branch || 'master').fetch();
}

var head;
function fetchTree(branch) {
  return fetchHead(branch).then(function(commit) {
    head = commit;
    return repo.git.trees(commit.object.sha).fetch();
  });
}

function fetchReadme() {
  return repo.contents('README.md').readBinary().then(log);
}

function commitTree() {
  return Promise.all([
    repo.git.blobs.create({
      content: 'Content of the blob' + new Date(),
      encoding: 'utf-8'
    }),
    repo.git.blobs.create({
      content: 'Content of the blob 2' + new Date(),
      encoding: 'utf-8'
    })
  ]).then(function(blobs) {
    return fetchTree().then(function(tree) {
      return {
        blobs: blobs,
        tree: tree
      };
    });
  }).then(function(data) {
    return repo.git.trees.create({
      tree: data.blobs.map(function(blob) {
        return {
          path: blob.sha + '.md',
          mode: '100644',
          type: 'blob',
          sha: blob.sha
        };
      }),
      base_tree: data.tree.sha
    });
  }).then(function(tree) {
    return repo.git.commits.create({
      message: 'Test commit of multiple files',
      tree: tree.sha,
      parents: [
        head.object.sha
      ]
    });
  }).then(function(commit) {
    return repo.git.refs.heads('master').update({
      sha: commit.sha
    });
  }).then(log);
}

commitTree();

function commitOneFile() {
  return repo.contents('test.md').add({
    message: 'Updating file',
    content: base64encode('New file contents'),
    sha: '123456789abcdef'
  }).then(log);
}<commit_msg>Add commit method<commit_after>'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));
}

function log(thing) {
  console.log(thing);
  return thing;
}

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

var octo = new Octokat({
  token: user.token
});
var repo = octo.repos(user.username, reponame);

function fetchHead(branch) {
  return repo.git.refs.heads(branch || 'master').fetch();
}

var head;
function fetchTree(branch) {
  return fetchHead(branch).then(function(commit) {
    head = commit;
    return repo.git.trees(commit.object.sha).fetch();
  });
}

function fetchReadme() {
  return repo.contents('README.md').readBinary().then(log);
}

function commit(files, message) {
  return Promise.all(files.map(function(file) {
    return repo.git.blobs.create({
      content: file.content,
      encoding: 'utf-8'
    });
  })).then(function(blobs) {
    return fetchTree().then(function(tree) {
      return repo.git.trees.create({
        tree: files.map(function(file, index) {
          return {
            path: file.path,
            mode: '100644',
            type: 'blob',
            sha: blobs[index].sha
          };
        }),
        base_tree: tree.sha
      });
    });
  }).then(function(tree) {
    return repo.git.commits.create({
      message: message,
      tree: tree.sha,
      parents: [
        head.object.sha
      ]
    });
  }).then(function(commit) {
    return repo.git.refs.heads('master').update({
      sha: commit.sha
    });
  });
}

commit([{
  path: 'test/file1.md',
  content: '# File1'
}, {
  path: 'test/file2.md',
  content: '# File2'
}], 'test commit via commit function').then(log);

function commitOneFile() {
  return repo.contents('test.md').add({
    message: 'Updating file',
    content: base64encode('New file contents'),
    sha: '123456789abcdef'
  }).then(log);
}
{'fraction_non_alphanumeric': 0.16773874319643742, 'fraction_numerical': 0.01583374567046017, 'mean_word_length': 2.967615309126595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '292838', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1334, 'n_words': 268}