<commit_before>//     Github.js 0.1.0
//     (c) 2012 <NAME>, Development Seed
//     Github.js is freely distributable under the MIT license.
//     For all details and documentation:
//     http://substance.io/michael/github

(function() {
  var Github;
  var API_URL = 'https://api.github.com';
  
  // Github API
  // -------

  Github = window.Github = function(options) {
    var username = options.username;
    var password = <PASSWORD>;

    // Util
    // -------

    function _request(method, route, data, cb) {
      $.ajax({
          type: method,
          url: API_URL + route,
          data: JSON.stringify(data),
          dataType: 'json',
          contentType: 'application/x-www-form-urlencoded',
          success: function(res) { cb(null, res); },
          error: function(err) { cb(err); },
          headers : { Authorization : 'Basic ' + Base64.encode(username + ':' + password) }
      });
    }

    // USER API
    // -------

    Github.User = function(options) {
      this.username = options.username;

      // TODO: implement
    };

    // Repository API
    // -------

    Github.Repository = function(options) {

      // Show repository information
      // -------

      var repoPath = "/repos/" + username + "/" + options.name;

      this.show = function(cb) {
        _request("GET", repoPath, null, function(err, res) {
          cb();
        });
      };


      // Read file at given path
      // -------

      this.read = function(path, cb) {

      };

      // Write file contents on a given path
      // -------

      this.write = function(path, content, cb) {

        // Get latest commit from master
        function getLatestCommit(cb) {
          _request("GET", repoPath + "/git/refs/heads/master", null, function(err, res) {
            if (err) return cb(err);
            cb(null, res.object.sha);
          });
        }

        // Retrieve the tree a commit points to

        function getTree(commit, cb) {
          _request("GET", repoPath + "/git/trees/"+commit, null, function(err, res) {
            if (err) return cb(err);
            cb(null, res.sha);
          });
        }

        // Post a new blob object, getting a blob SHA back

        function postBlob(content, cb) {
          var data = {
            "content": content,
            "encoding": "utf-8"
          };
          _request("POST", repoPath + "/git/blobs", data, function(err, res) {
            if (err) return cb(err);
            cb(null, res.sha);
          });
        }

        // Post a new tree object having a file path pointer replaced
        // with a new blob SHA getting a tree SHA back

        function postTree(baseTree, path, blob, cb) {
          var data = {
            "base_tree": baseTree,
            "tree": [
              {
                "path": path,
                "mode": "100644",
                "type": "blob",
                "sha": blob
              }
            ]
          };
          _request("POST", repoPath + "/git/trees", data, function(err, res) {
            if (err) return cb(err);
            cb(null, res.sha);
          });
        };

        // Create a new commit object with the current commit SHA as the parent
        // and the new tree SHA, getting a commit SHA back

        function createCommit(parent, tree, cb) {
          var data = {
            "message": "Spooky. Isn't it?",
            "author": {
              "name": "Ghost"
            },
            "parents": [
              parent
            ],
            "tree": tree
          };

          _request("POST", repoPath + "/git/commits", data, function(err, res) {
            if (err) return cb(err);
            cb(null, res.sha);
          });
        }

        // Update the reference of your head to point to the new commit SHA

        function updateHead(commit, cb) {
          _request("PATCH", repoPath + "/git/refs/heads/master", { "sha": commit }, function(err, res) {
            cb(err);
          });
        }

        // Write it.

        getLatestCommit(function(err, latestCommit) {
          getTree(latestCommit, function(err, tree) {
            postBlob(content, function(err, blob) {
              postTree(tree, path, blob, function(err, tree) {
                createCommit(latestCommit, tree, function(err, commit) {
                  updateHead(commit, function(err) {
                    cb(err);
                  });
                });
              });
            });
          });
        });
      };
    };

    // Top Level API
    // -------

    this.getRepo = function(repo) {
      return new Github.Repository({name: repo});
    };

    this.getUser = function() {
      return new Github.User({name: repo});
    };
  };
}).call(this);<commit_msg>First bit of the User API.<commit_after>//     Github.js 0.1.0
//     (c) 2012 <NAME>, Development Seed
//     Github.js is freely distributable under the MIT license.
//     For all details and documentation:
//     http://substance.io/michael/github

(function() {
  var Github;
  var API_URL = 'https://api.github.com';
  
  // Github API
  // -------

  Github = window.Github = function(options) {
    var username = options.username;
    var password = <PASSWORD>;

    // Util
    // -------

    function _request(method, route, data, cb) {
      $.ajax({
          type: method,
          url: API_URL + route,
          data: JSON.stringify(data),
          dataType: 'json',
          contentType: 'application/x-www-form-urlencoded',
          success: function(res) { cb(null, res); },
          error: function(err) { cb(err); },
          headers : { Authorization : 'Basic ' + Base64.encode(username + ':' + password) }
      });
    }

    // USER API
    // -------

    Github.User = function(options) {
      this.username = options.username;
      var userPath = "/users/" + options.username;
      this.repos = function() {
        _request("GET", userPath + "/repos", null, function(err, res) {
          cb(err, res);
        });
      }
    };

    // Repository API
    // -------

    Github.Repository = function(options) {

      // Show repository information
      // -------

      var repoPath = "/repos/" + username + "/" + options.name;

      this.show = function(cb) {
        _request("GET", repoPath, null, function(err, res) {
          cb();
        });
      };


      // Read file at given path
      // -------

      this.read = function(path, cb) {

      };

      // Write file contents on a given path
      // -------

      this.write = function(path, content, cb) {

        // Get latest commit from master
        function getLatestCommit(cb) {
          _request("GET", repoPath + "/git/refs/heads/master", null, function(err, res) {
            if (err) return cb(err);
            cb(null, res.object.sha);
          });
        }

        // Retrieve the tree a commit points to

        function getTree(commit, cb) {
          _request("GET", repoPath + "/git/trees/"+commit, null, function(err, res) {
            if (err) return cb(err);
            cb(null, res.sha);
          });
        }

        // Post a new blob object, getting a blob SHA back

        function postBlob(content, cb) {
          var data = {
            "content": content,
            "encoding": "utf-8"
          };
          _request("POST", repoPath + "/git/blobs", data, function(err, res) {
            if (err) return cb(err);
            cb(null, res.sha);
          });
        }

        // Post a new tree object having a file path pointer replaced
        // with a new blob SHA getting a tree SHA back

        function postTree(baseTree, path, blob, cb) {
          var data = {
            "base_tree": baseTree,
            "tree": [
              {
                "path": path,
                "mode": "100644",
                "type": "blob",
                "sha": blob
              }
            ]
          };
          _request("POST", repoPath + "/git/trees", data, function(err, res) {
            if (err) return cb(err);
            cb(null, res.sha);
          });
        };

        // Create a new commit object with the current commit SHA as the parent
        // and the new tree SHA, getting a commit SHA back

        function createCommit(parent, tree, cb) {
          var data = {
            "message": "Spooky. Isn't it?",
            "author": {
              "name": "Ghost"
            },
            "parents": [
              parent
            ],
            "tree": tree
          };

          _request("POST", repoPath + "/git/commits", data, function(err, res) {
            if (err) return cb(err);
            cb(null, res.sha);
          });
        }

        // Update the reference of your head to point to the new commit SHA

        function updateHead(commit, cb) {
          _request("PATCH", repoPath + "/git/refs/heads/master", { "sha": commit }, function(err, res) {
            cb(err);
          });
        }

        // Write it.

        getLatestCommit(function(err, latestCommit) {
          getTree(latestCommit, function(err, tree) {
            postBlob(content, function(err, blob) {
              postTree(tree, path, blob, function(err, tree) {
                createCommit(latestCommit, tree, function(err, commit) {
                  updateHead(commit, function(err) {
                    cb(err);
                  });
                });
              });
            });
          });
        });
      };
    };

    // Top Level API
    // -------

    this.getRepo = function(repo) {
      return new Github.Repository({name: repo});
    };

    this.getUser = function(user) {
      return new Github.User({username: user});
    };
  };
}).call(this);
{'fraction_non_alphanumeric': 0.14500922320147572, 'fraction_numerical': 0.003279360524697684, 'mean_word_length': 1.4409704852426213, 'pattern_counts': {'":': 28, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6973236', 'n_tokens_mistral': 2822, 'n_tokens_neox': 2698, 'n_words': 722}