Skip to content

Commit 935beab

Browse files
committed
Start working on git integration.
1 parent 895aeab commit 935beab

File tree

8 files changed

+276
-1
lines changed

8 files changed

+276
-1
lines changed

coder-apps/common/vcs/app/app.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
exports.get_routes = [
3+
{ path:'/', handler:'index_handler' },
4+
];
5+
6+
exports.post_routes = [
7+
];
8+
9+
10+
exports.index_handler = function( app, req, res ) {
11+
res.render( app.view() );
12+
};
13+
14+
exports.on_destroy = function() {
15+
};
16+

coder-apps/common/vcs/app/git.js

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
"use strict";
2+
var spawn = require('child_process').spawn;
3+
var byline = require('byline');
4+
var async = require('async');
5+
var util = require('util');
6+
var path = require('path');
7+
8+
var Git = function(p)
9+
{
10+
this.path = path.resolve(p);
11+
};
12+
13+
Git.prototype.git = function(/* command, args, options, callback */)
14+
{
15+
var command, gitargs = [], options = {}, callback = null;
16+
var args = Array.prototype.slice.call(arguments);
17+
18+
command = args.shift();
19+
if (util.isArray(args[0]))
20+
gitargs = args.shift();
21+
if (typeof args[0] === "object")
22+
options = args.shift();
23+
if (typeof args[0] === "function")
24+
callback = args.shift();
25+
26+
options.encoding = options.encoding || 'utf8';
27+
options.env = options.env || {};
28+
options.cwd = options.cwd || this.path;
29+
30+
gitargs.unshift(command);
31+
var child = spawn("git", gitargs, { cwd: options.cwd, env: options.env});
32+
33+
child.stderr.setEncoding('utf8');
34+
child.stdout.setEncoding(options.encoding);
35+
36+
if (callback) {
37+
var called = false;
38+
var error = "Unknown error";
39+
40+
child.on('close', function (code) {
41+
if (code === 0) {
42+
callback(null);
43+
}
44+
else if (!called) {
45+
called = true;
46+
callback(error);
47+
}
48+
});
49+
50+
child.on('error', function (err) {
51+
if (!called) {
52+
called = true;
53+
callback(err);
54+
}
55+
});
56+
57+
var stderr = byline(child.stderr);
58+
59+
stderr.on('data', function (line) {
60+
if (line.indexOf("fatal: ") === 0)
61+
error = line;
62+
});
63+
}
64+
65+
return child;
66+
};
67+
68+
Git.prototype.status = function(callback) {
69+
var result = Object.create(null);
70+
71+
var child = this.git("status", ["--porcelain"], function(err) {
72+
if (err) callback(err, null);
73+
else callback(null, result);
74+
});
75+
76+
var stream = byline(child.stdout);
77+
78+
stream.on('data', function (line) {
79+
result[line.slice(3)] = line.slice(0, 2);
80+
});
81+
};
82+
83+
Git.prototype.addAll = function(callback) {
84+
this.git("add", ["--all", ":/"], callback);
85+
};
86+
87+
Git.prototype.update_ref = function(/* ref, value, oldvalue, callback */) {
88+
var ref, value, oldvalue, callback;
89+
var args = Array.prototype.slice.call(arguments);
90+
91+
ref = args.shift();
92+
value = args.shift();
93+
if (typeof args[0] === "string")
94+
oldvalue = args.shift();
95+
callback = args.shift();
96+
97+
if (oldvalue !== null)
98+
this.git("update-ref", [ref, value, oldvalue], callback);
99+
else
100+
this.git("update-ref", [ref, value], callback);
101+
};
102+
103+
104+
Git.prototype.write_tree = function(callback) {
105+
var result = null;
106+
107+
var child = this.git("write-tree", function(err) {
108+
if (err) callback(err, null);
109+
else callback(null, result);
110+
});
111+
112+
var stream = byline(child.stdout);
113+
114+
stream.on('data', function (line) {
115+
result = line;
116+
});
117+
};
118+
119+
Git.prototype.commit_tree = function(/* tree, message, parents, options, callback */) {
120+
var tree, message, parents = [], options = {}, callback;
121+
var args = Array.prototype.slice.call(arguments);
122+
123+
tree = args.shift();
124+
message = args.shift();
125+
if (args[0] == null)
126+
args.shift(); // Just no parents
127+
else if (util.isArray(args[0]))
128+
parents = args.shift();
129+
else if (typeof args[0] === "string")
130+
parents = [args.shift()];
131+
if (typeof args[0] === "object")
132+
options = args.shift();
133+
callback = args.shift();
134+
135+
var result = null;
136+
137+
var gitargs = [tree];
138+
for (var k in parents) {
139+
gitargs.push("-p");
140+
gitargs.push(parents[k]);
141+
}
142+
143+
var env = {};
144+
if (options.author) {
145+
env.GIT_AUTHOR_NAME = options.author.name;
146+
env.GIT_AUTHOR_EMAIL = options.author.email;
147+
}
148+
if (options.committer) {
149+
env.GIT_COMMITTER_NAME = options.committer.name;
150+
env.GIT_COMMITTER_EMAIL = options.committer.email;
151+
}
152+
153+
var child = this.git("commit-tree", gitargs, {env: env}, function(err) {
154+
if (err) callback(err, null);
155+
else callback(null, result);
156+
});
157+
158+
var stream = byline(child.stdout);
159+
160+
stream.on('data', function (line) {
161+
result = line;
162+
});
163+
164+
child.stdin.write(message);
165+
child.stdin.end();
166+
};
167+
168+
Git.prototype.rev_parse = function(rev, callback) {
169+
var result = null;
170+
171+
var child = this.git("rev-parse", [rev], function(err) {
172+
if (err) callback(err, null);
173+
else callback(null, result);
174+
});
175+
176+
var stream = byline(child.stdout);
177+
178+
stream.on('data', function (line) {
179+
result = line;
180+
});
181+
};
182+
183+
Git.prototype.commit = function(/* message, options, callback */) {
184+
var message, options = {}, callback;
185+
var args = Array.prototype.slice.call(arguments);
186+
187+
message = args.shift();
188+
if (typeof args[0] === "object")
189+
options = args.shift();
190+
callback = args.shift();
191+
192+
var parent;
193+
var self = this;
194+
195+
async.waterfall([
196+
function(callback) {
197+
self.rev_parse("HEAD", function(err, result) {
198+
parent = err ? null : result;
199+
callback(null);
200+
});
201+
},
202+
function(callback) {
203+
self.write_tree(callback);
204+
},
205+
function(tree, callback) {
206+
self.commit_tree(tree, message, parent, {author: options.author, committer: options.committer}, callback);
207+
},
208+
function(commit, callback) {
209+
self.update_ref("HEAD", commit, parent, function(err) {
210+
callback(err, commit);
211+
});
212+
}
213+
], callback);
214+
};
215+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
.pagecontent {
3+
padding: 24px;
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
$(document).ready( function() {
3+
4+
//This code will run after your page loads
5+
6+
});

coder-apps/common/vcs/static/media/.gitignore

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Coder</title>
5+
<meta charset="utf-8">
6+
<!-- Standard Coder Includes -->
7+
<script>
8+
var appname = "{{app_name}}"; //app name (id) of this app
9+
var appurl = "{{&app_url}}";
10+
var staticurl = "{{&static_url}}"; //base path to your static files /static/apps/yourapp
11+
</script>
12+
<link href="/static/apps/coderlib/css/index.css" media="screen" rel="stylesheet" type="text/css"/>
13+
<script src="/static/common/js/jquery.min.js"></script>
14+
<script src="/static/common/ace-min/ace.js" type="text/javascript" charset="utf-8"></script>
15+
<script src="/static/apps/coderlib/js/index.js"></script>
16+
<script>
17+
Coder.addBasicNav();
18+
</script>
19+
<!-- End Coder Includes -->
20+
21+
<!-- This app's includes -->
22+
<link href="{{&static_url}}/css/index.css" media="screen" rel="stylesheet" type="text/css"/>
23+
<script src="{{&static_url}}/js/index.js"></script>
24+
<!-- End apps includes -->
25+
</head>
26+
<body class="">
27+
<div class="pagecontent">
28+
<h1>Hello World</h1>
29+
<p>Your html goes here.</p>
30+
</div>
31+
</body>
32+
</html>

coder-apps/install_common.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ base=$1
2626
./install_app.sh game2d $base ./common/
2727
./install_app.sh hello_coder $base ./common/
2828
./install_app.sh space_rocks_ $base ./common/
29+
./install_app.sh vcs $base ./common/
2930

coder-base/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"async": "0.2.*",
1717
"ncp": "0.5.*",
1818
"tmp": "0.0.23",
19-
"rimraf": "~2.2.6"
19+
"rimraf": "~2.2.6",
20+
"byline": "~4.1.1"
2021
}
2122
}

0 commit comments

Comments
 (0)