Permalink
Browse files

committing

  • Loading branch information...
1 parent c4e9638 commit cb2dd018ee5443dc4083fd6f41d16b99af43db90 @schacon schacon committed Jun 2, 2011
Showing with 127 additions and 12 deletions.
  1. +2 −1 Gemfile
  2. +4 −3 app.rb
  3. +45 −2 public/js/github.js
  4. +76 −6 public/js/terminal.js
View
@@ -2,5 +2,6 @@ source "http://rubygems.org"
gem "sinatra"
gem "sinatra_auth_github"
-gem "rest-client"
+gem "nestful"
+gem "i18n"
View
@@ -1,6 +1,6 @@
require 'sinatra'
require 'sinatra_auth_github'
-require 'rest_client'
+require 'nestful'
require 'pp'
class TerminalApp < Sinatra::Base
@@ -39,9 +39,10 @@ class TerminalApp < Sinatra::Base
post '/proxy' do
host = 'http://api.github.dev'
url = host + '/' + params.delete('proxy_url')
- params['access_token'] = github_user.token
+ p = JSON.parse(Base64.decode64(params['datap']))
+ p['access_token'] = github_user.token
- resp = RestClient.post url, params, :content_type => :json, :accept => :json
+ resp = Nestful.post url, :params => p
content_type :json
resp
View
@@ -50,8 +50,10 @@
// post to sinatra proxy so this is do-able, freaking cross-site grr
postp = function (url, vals, callback) {
- vals['proxy_url'] = url
- $.post('/proxy', vals, callback, 'json')
+ v = {}
+ v['proxy_url'] = url
+ v['datap'] = Base64.encode(JSON.stringify(vals))
+ $.post('/proxy', v, callback, 'json')
},
// This helper function will throw a TypeError if the library user is not
@@ -226,6 +228,10 @@
return gh.blob(this.user, this.repo, sha)
};
+ gh.repo.prototype.ref = function (ref, sha) {
+ return gh.ref(this.user, this.repo, ref, sha)
+ };
+
gh.repo.prototype.branches = withV3Api(function (callback, context) {
jsonp("repos/" + this.user + "/" + this.repo + "/git/refs/heads", callback, context);
@@ -254,6 +260,12 @@
return this;
});
+ gh.commit.prototype.write = function (commitHash, callback) {
+ url = "repos/" + this.user + "/" + this.repo + "/git/commits"
+ postp(url, commitHash, callback)
+ return this;
+ };
+
// ### Trees
gh.tree = function (user, repo, sha) {
@@ -270,6 +282,12 @@
return this;
});
+ gh.tree.prototype.write = function (treeHash, callback) {
+ url = "repos/" + this.user + "/" + this.repo + "/git/trees"
+ postp(url, treeHash, callback)
+ return this;
+ };
+
// ### Blobs
gh.blob = function (user, repo, sha) {
@@ -298,6 +316,31 @@
return Base64.decode(data)
}
+ // ### Refs
+
+ gh.ref = function (user, repo, ref, sha) {
+ if ( !(this instanceof gh.ref) )
+ return new gh.ref(user, repo, ref, sha);
+ this.user = user;
+ this.repo = repo;
+ this.ref = ref;
+ this.sha = sha;
+ };
+
+ gh.ref.prototype.show = withV3Api(function (callback, context) {
+ jsonp("repos/" + this.user + "/" + this.repo + "/git/" + this.ref,
+ callback, context);
+ return this;
+ });
+
+ gh.ref.prototype.update = function (commitSha, callback) {
+ var vals = {'ref': this.ref, 'type': 'commit', 'sha': commitSha}
+ url = "repos/" + this.user + "/" + this.repo + "/git/" + this.ref
+ postp(url, vals, callback)
+ return this;
+ };
+
+
// ### Pull Requests
gh.pulls = function(user, repo) {
View
@@ -36,7 +36,7 @@ function termHandler() {
this.clear()
nextTerm(help)
} else if (command == 'ls') {
- listCurrent(this)
+ listCurrent()
} else if (command == 'cd') {
var newState = this.argv[this.argc++];
newState.split("/").forEach(function(dir) {
@@ -46,6 +46,8 @@ function termHandler() {
runLog(this.argv)
} else if (command == 'status') {
runStatus()
+ } else if (command == 'test') {
+ runTest()
} else if (command == 'commit') {
runCommit()
} else if ((command == 'edit') || (command == 'vim') || (command == 'emacs')) {
@@ -56,6 +58,36 @@ function termHandler() {
}
}
+// sets up env for feature dev
+var commandStack = []
+var commandTimer = 500
+function runTest() {
+ commandStack = []
+ commandStack.push("listCurrent()")
+ commandStack.push("changeState('github')")
+ commandStack.push("listCurrent()")
+ commandStack.push("changeState('master')")
+ commandStack.push("listCurrent()")
+ commandStack.push("startEditor('config.ru')")
+ commandStack.push("editor.getSession().setValue('hey new content')")
+ commandStack.push("stopEditor()")
+ commandStack.push("changeState('config')")
+ commandStack.push("listCurrent()")
+ commandStack.push("startEditor('shotgun.rb')")
+ commandStack.push("editor.getSession().setValue('hey more new content')")
+ commandStack.push("stopEditor()")
+ commandStack.push("changeState('..')")
+ setTimeout("runNext()", commandTimer)
+}
+function runNext() {
+ if(cmd = commandStack.shift()) {
+ eval(cmd)
+ setTimeout("runNext()", commandTimer)
+ }
+}
+// -- sets up env for feature dev --
+
+
function runStatus() {
if(ghStage.length > 0) {
term.write("Parent Commit: %c(@indianred)" + ghStageCommit + "%n")
@@ -73,8 +105,38 @@ function runStatus() {
}
function runCommit() {
- term.write("Commit")
- nextTerm()
+
+ if(ghStage.length <= 0) {
+ return nextTerm("Nothing staged for commit%n")
+ }
+ if(ghStageCommit != ghCommit.sha) {
+ return nextTerm("Stage commit is mismatched%n")
+ }
+
+ term.write("Base Tree: %c(@khaki)" + ghCommit.cache.tree + '%n')
+ tr = {}
+ tr.base_tree = ghCommit.cache.tree
+ tr.tree = ghStage
+ term.write("Writing the new tree...")
+ var tree = ghRepo.tree()
+ tree.write(tr, function(resp) {
+ cm = {}
+ cm.tree = resp.sha
+ cm.message = "test message"
+ cm.parents = [ghStageCommit]
+ term.write(" tree %c(@lightyellow)" + resp.sha + "%n")
+ term.write("Committing files... ")
+ var commit = ghRepo.commit()
+ commit.write(cm, function(resp) {
+ term.write(" commit %c(@lightyellow)" + resp.sha + "%n")
+ term.write("Updating branch...")
+ var ref = ghRepo.ref(ghBranch.ref, ghBranch.sha)
+ ref.update(resp.sha, function(resp) {
+ nextTerm(" %c(@lightblue)Branch Updated")
+ ghBranch.sha = resp.sha
+ })
+ })
+ })
}
function changeState(newState) {
@@ -202,6 +264,14 @@ function currentPath() {
return "/" + ghPath.join('/')
}
+function treePath() {
+ if(ghPath.length > 0) {
+ return ghPath.join('/') + '/'
+ } else {
+ return ''
+ }
+}
+
function showCommit() {
data = ghCommit.cache
term.write("commit : %c(@lightyellow)" + data.sha + '%n')
@@ -310,7 +380,7 @@ function resetPs(str) {
function startEditor(fileName, type) {
if(sha = findTreeSha(fileName, false)) {
- lastEditPath = currentPath() + fileName
+ lastEditPath = treePath() + fileName
var blob = ghRepo.blob(sha)
blob.show(function(resp) {
b = resp.data
@@ -344,9 +414,9 @@ function stopEditor() {
$("#termDiv").show()
content = editor.getSession().getValue()
- var blob = ghRepo.blob(sha)
+ var blob = ghRepo.blob()
blob.write(content, function(resp) {
- ghStage.push({'path': lastEditPath, 'type': 'blob', 'sha': resp.sha})
+ ghStage.push({'path': lastEditPath, 'type': 'blob', 'sha': resp.sha, 'mode': '100644'})
term.write("File '" + lastEditPath + "' saved %c(@lightyellow)(" + resp['sha'] + ")")
term.prompt()
})

0 comments on commit cb2dd01

Please sign in to comment.