forked from mhkeller/git-static-diffuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
247 lines (208 loc) · 7.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
var child = require("child_process"),
mime = require("mime"),
path = require("path");
var shaRe = /^[0-9a-f]{40}$/,
emailRe = /^<.*@.*>$/;
var repositories;
function readBlob(repositories, repository, revision, file, callback) {
var git = child.spawn("git", ["cat-file", "blob", revision + ":" + file], {cwd: repositories + '/' + repository}),
data = [],
exit;
git.stdout.on("data", function(chunk) {
data.push(chunk);
});
git.on("exit", function(code) {
exit = code;
});
git.on("close", function() {
if (exit > 0) return callback(error(exit));
callback(null, Buffer.concat(data));
});
git.stdin.end();
}
exports.readBlob = readBlob;
exports.getBranches = function(repository, callback) {
child.exec("git branch -l", {cwd: repositories + '/' + repository}, function(error, stdout) {
if (error) return callback(error);
callback(null, stdout.split(/\n/).slice(0, -1).map(function(s) { return s.slice(2); }));
});
};
exports.getSha = function(repository, revision, callback) {
child.exec("git rev-parse '" + revision.replace(/'/g, "'\''") + "'", {cwd: repositories + '/' + repository}, function(error, stdout) {
if (error) return callback(error);
callback(null, stdout.trim());
});
};
exports.getBranchCommits = function(repository, branch, callback) {
child.exec("git log " + branch, {cwd: repositories + '/' + repository}, function(error, stdout) {
if (error) return callback(error);
console.log(stdout)
// Split and filter for empty rows
var commits = stdout.trim().split("commit").filter(function(commit){ return commit });
commits = commits.map(function(commit){
var fields = commit.replace('\n\n','\n').split('\n').map(function(line){ return line.trim() }).filter(function(line){ return line });
return {
sha: fields[0],
author: fields[1].split(' ')[1],
date: fields[2].split('Date:')[1].trim(),
message: fields[3]
}
})
callback(null, commits);
});
};
exports.getCommit = function(repository, revision, callback) {
if (arguments.length < 3) callback = revision, revision = null;
child.exec(shaRe.test(revision)
? "git log -1 --date=iso " + revision + " --format='%H\n%ad'"
: "git for-each-ref --count 1 --sort=-authordate 'refs/heads/" + (revision ? revision.replace(/'/g, "'\''") : "") + "' --format='%(objectname)\n%(authordate:iso8601)'", {cwd: repositories + '/' + repository}, function(error, stdout) {
if (error) return callback(error);
var lines = stdout.split("\n"),
sha = lines[0],
date = new Date(lines[1]);
if (!shaRe.test(sha) || isNaN(date)) return void callback(new Error("unable to get commit"));
callback(null, {
sha: sha,
date: date
});
});
};
exports.getRelatedCommits = function(repository, branch, sha, callback) {
if (!shaRe.test(sha)) return callback(new Error("invalid SHA: " + sha));
child.exec("git log --format='%H' '" + branch.replace(/'/g, "'\''") + "' | grep -C1 " + sha, {cwd: repositories + '/' + repository}, function(error, stdout) {
if (error) return callback(error);
var shas = stdout.split(/\n/),
i = shas.indexOf(sha);
callback(null, {
previous: shas[i + 1],
next: shas[i - 1]
});
});
};
exports.listCommits = function(repository, sha1, sha2, callback) {
if (!shaRe.test(sha1)) return callback(new Error("invalid SHA: " + sha1));
if (!shaRe.test(sha2)) return callback(new Error("invalid SHA: " + sha2));
child.exec("git log --format='%H\t%ad' " + sha1 + ".." + sha2, {cwd: repositories + '/' + repository}, function(error, stdout) {
if (error) return callback(error);
callback(null, stdout.split(/\n/).slice(0, -1).map(function(commit) {
var fields = commit.split(/\t/);
return {
sha: fields[0],
date: new Date(fields[1])
};
}));
});
};
exports.listAllCommits = function(repository, callback) {
child.exec("git log --branches --format='%H\t%ad'", {cwd: repositories + '/' + repository}, function(error, stdout) {
if (error) return callback(error);
callback(null, stdout.split(/\n/).slice(0, -1).map(function(commit) {
var fields = commit.split(/\t/);
return {
sha: fields[0],
date: new Date(fields[1])
};
}));
});
};
exports.repositories = function(repositories_){
if (!arguments.length) return repositories;
repositories = repositories_;
return this;
}
exports.route = function() {
repositories = repositories || defaultRepositories();
var repository = defaultRepository,
revision = defaultRevision,
file = defaultFile,
type = defaultType;
function route(request, response) {
var repository_,
revision_,
file_;
if ( repositories == undefined
|| (repository_ = repository(request.url)) == undefined
|| (revision_ = revision(request.url)) == undefined
|| (file_ = file(request.url)) == undefined
) return serveNotFound();
readBlob(repositories, repository_, revision_, file_, function(error, data) {
if (error) return error.code === 128 ? serveNotFound() : serveError(error);
response.writeHead(200, {
"Content-Type": type(file_),
"Cache-Control": "public, max-age=300"
});
response.end(data);
});
function serveError(error) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.end(error + "");
}
function serveNotFound() {
if (!request.url.match("index.html$")) {
var possibleIndex = request.baseUrl + request.url.replace(/\/$/,'') + '/index.html';
response.redirect(possibleIndex);
}
else {
response.writeHead(404, {"Content-Type": "text/plain"});
response.end("File not found.");
}
}
}
route.repositories = function(_) {
if (!arguments.length) return repositories;
repositories = functor(_);
return route;
};
route.repository = function(_) {
if (!arguments.length) return repository;
repository = functor(_);
return route;
};
route.sha = // sha is deprecated; use revision instead
route.revision = function(_) {
if (!arguments.length) return revision;
revision = functor(_);
return route;
};
route.file = function(_) {
if (!arguments.length) return file;
file = functor(_);
return route;
};
route.type = function(_) {
if (!arguments.length) return type;
type = functor(_);
return route;
};
return route;
};
function functor(_) {
return typeof _ === "function" ? _ : function() { return _; };
}
function defaultRepositories() {
return path.join(__dirname, "repositories");
}
function defaultRepository(url) {
var dirs = url.substring(1).split('/');
return dirs[0] || undefined;
}
function defaultRevision(url) {
var dirs = url.substring(1).split('/');
return dirs[1] || undefined;
}
function defaultFile(url) {
var dirs = url.substring(1).split('/');
return dirs.slice(2, dirs.length).join('/') || undefined;
}
function defaultType(file) {
var type = mime.lookup(file, "text/plain");
return text(type) ? type + "; charset=utf-8" : type;
}
function text(type) {
return /^(text\/)|(application\/(javascript|json)|image\/svg$)/.test(type);
}
function error(code) {
var e = new Error;
e.code = code;
return e;
}