<commit_before>'use strict'

var assert = require('assert')
var ms = require('ms')
var Promise = require('promise')
var github = require('github').json

function delay(time, value) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      resolve(value)
    }, ms(time.toString()))
  })
}
function poll(condition, options) {
  options = options || {}
  return condition().then(function (done) {
    if (done) {
      return done
    } else if (options.timeout && options.start && Date.now() - options.start > ms(options.timeout.toString())) {
      var err = new Error('operation timed out')
      err.code = 'TIMEOUT'
      throw err
    } else {
      return delay(typeof options.delay === 'function' ? options.delay(options.attempt || 1) : options.delay || 0).then(function () {
        return poll(condition, {
          attempt: (options.attempt || 1) + 1
          delay: options.delay,
          timeout: options.timeout,
          start: options.start || Date.now()
        })
      })
    }
  })
}

exports.exists = exists
function exists(user, repo, options, callback) {
  options = JSON.stringify(JSON.parse(options || {}))
  options.host = 'github.com'
  return github('head', '/:user/:repo', {user: user, repo: repo}, options)
    .then(function () {
      return true
    }, function () {
      return false
    }).nodeify(callback)
}

exports.fork = fork
function fork(from, to, repo, options, callback) {
  options = options || {}
  return github('post', '/repos/:owner/:repo/forks', {owner: from, repo: repo, organization: to}, options)
    .then(function (res) {
      return poll(function () {
        return exists(to, repo, options)
      }, {timeout: options.timeout || '5 minutes', delay: function (attempt) { return (attempt * 5) + 'seconds' })
      .then(function () { return res })
    }).nodeify(callback)
}

exports.branch = branch
function branch(user, repo, from, to, options, callback) {
  github('get', '/repos/:owner/:repo/git/refs/:ref', {owner: user, repo: repo, ref: 'heads/' + from})
    .then(function (res) {
      return github('post', '/repos/:owner/:repo/git/refs', {owner: user, repo: repo, ref: 'refs/heads/' + to, sha: res.object.sha})
    })
}

exports.commit = commit //does 5 API requests
function commit(user, repo, commit, options, callback) {
  var branch = commit.branch || 'master'
  var message = commit.message
  var updates = commit.updates
  var shaLatestCommit, shaBaseTree, shaNewTree, shaNewCommit

  Promise.from(null).then(function () {

    //check for correct input
    assert(user && typeof user === 'string', '`user` must be a string')
    assert(repo && typeof repo === 'string', '`repo` must be a string')
    assert(branch && typeof branch === 'string', '`branch` must be a string')
    assert(message && typeof message === 'strin'g, '`message` must be a string')
    updates.forEach(function (file) { // {path: path, content: content}
      file.path = file.path.replace(/\\/g, '/').replace(/^\//, '')
      file.mode = file.mode || '100644'
      file.type = file.type || 'blob'
      file.content = file.content
    })


    return github('get', '/repos/:owner/:repo/git/refs/:ref', {owner: user, repo: repo, ref: 'heads/' + branch}, options)
  }).then(function (res) {
    shaLatestCommit = res.object.sha
    return github('get', '/repos/:owner/:repo/git/commits/:sha', {owner: user, repo: repo, sha: shaLatestCommit}, options)
  }).then(function (res) {
    shaBaseTree = res.tree.sha
    return github('post', '/repos/:owner/:repo/git/trees', {owner: user, repo: repo, tree: updates, base_tree: shaBaseTree}, options)
  }).then(function (res) {
    shaNewTree = res.sha
    return github('post', '/repos/:owner/:repo/git/commits', {owner: user, repo: repo, message: message, tree: shaNewTree, parents: [shaLatestCommit]}, options)
  }).then(function (res) {<commit_msg>Fix syntax errors (ooops)<commit_after>'use strict'

var assert = require('assert')
var ms = require('ms')
var Promise = require('promise')
var github = require('github-basic').json

function delay(time, value) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      resolve(value)
    }, ms(time.toString()))
  })
}
function poll(condition, options) {
  options = options || {}
  return condition().then(function (done) {
    if (done) {
      return done
    } else if (options.timeout && options.start && Date.now() - options.start > ms(options.timeout.toString())) {
      var err = new Error('operation timed out')
      err.code = 'TIMEOUT'
      throw err
    } else {
      return delay(typeof options.delay === 'function' ? options.delay(options.attempt || 1) : options.delay || 0).then(function () {
        return poll(condition, {
          attempt: (options.attempt || 1) + 1,
          delay: options.delay,
          timeout: options.timeout,
          start: options.start || Date.now()
        })
      })
    }
  })
}

exports.exists = exists
function exists(user, repo, options, callback) {
  options = JSON.stringify(JSON.parse(options || {}))
  options.host = 'github.com'
  return github('head', '/:user/:repo', {user: user, repo: repo}, options)
    .then(function () {
      return true
    }, function () {
      return false
    }).nodeify(callback)
}

exports.fork = fork
function fork(from, to, repo, options, callback) {
  options = options || {}
  return github('post', '/repos/:owner/:repo/forks', {owner: from, repo: repo, organization: to}, options)
    .then(function (res) {
      return poll(function () {
        return exists(to, repo, options)
      }, {timeout: options.timeout || '5 minutes', delay: function (attempt) { return (attempt * 5) + 'seconds' }})
      .then(function () { return res })
    }).nodeify(callback)
}

exports.branch = branch
function branch(user, repo, from, to, options, callback) {
  github('get', '/repos/:owner/:repo/git/refs/:ref', {owner: user, repo: repo, ref: 'heads/' + from})
    .then(function (res) {
      return github('post', '/repos/:owner/:repo/git/refs', {owner: user, repo: repo, ref: 'refs/heads/' + to, sha: res.object.sha})
    })
}

exports.commit = commit //does 5 API requests
function commit(user, repo, commit, options, callback) {
  var branch = commit.branch || 'master'
  var message = commit.message
  var updates = commit.updates
  var shaLatestCommit, shaBaseTree, shaNewTree, shaNewCommit

  Promise.from(null).then(function () {

    //check for correct input
    assert(user && typeof user === 'string', '`user` must be a string')
    assert(repo && typeof repo === 'string', '`repo` must be a string')
    assert(branch && typeof branch === 'string', '`branch` must be a string')
    assert(message && typeof message === 'string', '`message` must be a string')
    updates.forEach(function (file) { // {path: path, content: content}
      file.path = file.path.replace(/\\/g, '/').replace(/^\//, '')
      file.mode = file.mode || '100644'
      file.type = file.type || 'blob'
      file.content = file.content
    })


    return github('get', '/repos/:owner/:repo/git/refs/:ref', {owner: user, repo: repo, ref: 'heads/' + branch}, options)
  }).then(function (res) {
    shaLatestCommit = res.object.sha
    return github('get', '/repos/:owner/:repo/git/commits/:sha', {owner: user, repo: repo, sha: shaLatestCommit}, options)
  }).then(function (res) {
    shaBaseTree = res.tree.sha
    return github('post', '/repos/:owner/:repo/git/trees', {owner: user, repo: repo, tree: updates, base_tree: shaBaseTree}, options)
  }).then(function (res) {
    shaNewTree = res.sha
    return github('post', '/repos/:owner/:repo/git/commits', {owner: user, repo: repo, message: message, tree: shaNewTree, parents: [shaLatestCommit]}, options)
  }).then(function (res) {
{'fraction_non_alphanumeric': 0.1726850657725045, 'fraction_numerical': 0.0033531080732525148, 'mean_word_length': 3.3937677053824364, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6548988', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2390, 'n_words': 647}