/
index.js
209 lines (183 loc) · 6.38 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
var fs = require('fs');
var url = require('url');
var path = require('path');
var mime = require('mime');
var gitteh = require('gitteh');
var GIT_DIR = '.git';
function setup(repoPath, options) {
options = options || {};
options.__proto__ = setup.DEFAULTS;
// 'setup' can be synchronous...
var isClone;
try {
isClone = fs.statSync(path.join(repoPath, GIT_DIR)).isDirectory();
} catch(e) {
if (e.code === 'ENOENT') {
isClone = false;
} else {
throw e;
}
}
if (isClone && repoPath.substring(repoPath.length-4) !== GIT_DIR) repoPath = path.join(repoPath, GIT_DIR);
//console.log("Using repository:", repoPath, "isClone:", isClone);
// Synchronously open the git repo
var repo = gitteh.openRepository(repoPath);
return function gitProvider(req, res, next) {
// Ensure that 'next()' exists (in the case of a plain http.Server)
next = next || function(err) {
if (err) console.error(err);
res.writeHead(err ? 500 : 404);
res.end(err ? err.stack : "Not Found");
};
if (!req.hasOwnProperty("uri")) { req.uri = url.parse(req.url); }
if (req.uri.pathname.substring(0, options.mountPoint.length) !== options.mountPoint) return next();
var gitPath = req.uri.pathname.substring(options.mountPoint.length);
var parts = gitPath.split('/');
if (parts[parts.length-1].length === 0) {
parts[parts.length-1] = options.indexFile;
}
// The first part may be a commit reference, a git tag/branch name,
// or just simply the name of the file to serve from a top-level.
var firstPart = parts[0];
//console.log("First Part: " + firstPart);
if (firstPart.length === 40) {
// Check the commit id, then git tag, then serve file
checkCommit(firstPart);
} else {
// Check for a git tag, then serve file
checkTag(firstPart);
}
function checkCommit(commitRef) {
repo.getCommit(commitRef, function(err, commit) {
if (err) {
if (err.gitError === gitteh.error.GIT_ENOTFOUND) {
// first part isn't a git commit, next check for a git tag
return checkTag(commitRef);
} else {
// Some other kind of error, baaddd!
return next(err);
}
}
// If there was no error then we got a valid commit id. Serve the file from parts[1]
repo.getTree(commit.tree, function(err, tree) {
serveGitFile(repo, tree, parts.slice(1), res, next);
});
});
}
function checkTag(tag) {
listTags(repo, function(err, tags) {
if (err) return next(err);
//console.log("Tags: " + tags);
var tagIndex = tags.indexOf(firstPart);
if (tagIndex >= 0) {
//console.log("Found tag: " + tags[tagIndex]);
// Requested a file from a git tag. Resolve the git tag then serve the file from parts[1]
resolveTag(repo, tag, function(err, commitRef) {
if (err) return next(err);
serveCommit(commitRef, parts.slice(1));
});
} else {
//console.log("Tag not found");
// No git tag, all the parts are the path of the file to server.
// Serve from HEAD (bare repo) or a real file (cloned repo).
if (isClone) {
fs.readFile(path.join(repoPath, '..', parts.join('/')), function(err, buf) {
if (err) {
if (err.code === 'ENOENT') return next();
else return next(err);
}
serveBuffer(buf, res, gitPath);
});
} else {
// Is a bare repo, serve file from resolved HEAD
resolveHead(repo, function(err, commitRef) {
if (err) return next(err);
serveCommit(commitRef, parts);
});
}
}
});
}
function serveCommit(commitRef, parts) {
repo.getCommit(commitRef, function(err, commit) {
if (err) return next(err);
repo.getTree(commit.tree, function(err, tree) {
if (err) return next(err);
serveGitFile(repo, tree, parts, res, next);
});
});
}
}
}
module.exports = setup;
setup.DEFAULTS = {
// The base request URL to serve from
mountPoint: '/',
// The name of the index file to serve when
// a directory name is requested
indexFile: 'index.html'
};
// Returns a Buffer of the specified file from the specified repo and commit reference.
// Recursively gets the tree instances until the last part, which is gets the rawObject
// for and serves back over HTTP.
function serveGitFile(repo, tree, parts, res, next) {
//console.log("Serving git file: " + parts);
var thisPart = parts.shift();
var isLastPart = parts.length === 0;
var entryIndex = -1;
for (var i=0; i < tree.entries.length; i++) {
if (tree.entries[i].name === thisPart) {
entryIndex = i;
break;
}
}
if (entryIndex < 0) return next();
var entry = tree.entries[entryIndex];
if (isLastPart) {
repo.getBlob(entry.id, function(err, buf) {
if (err) return next(err);
if (!buf.data) return next();
serveBuffer(buf.data, res, thisPart);
});
} else {
repo.getTree(entry.id, function(err, entryTree) {
if (err) return next(err);
serveGitFile(repo, entryTree, parts, res, next);
});
}
}
function serveBuffer(buf, res, filename) {
var contentType = mime.lookup(filename);
res.setHeader('Content-Length', buf.length);
res.setHeader('Content-Type', contentType);
res.end(buf);
}
function resolveReference(repo, reference, callback) {
repo.getReference(reference, onRef);
function onRef(err, ref) {
if (err) return callback(err);
if (ref.type === gitteh.GIT_REF_OID) {
callback(null, ref.target);
} else if (ref.type === gitteh.GIT_REF_SYMBOLIC) {
ref.resolve(onRef);
} else {
callback(new Error('Got unknown reference type: ' + ref.type));
}
}
}
function resolveHead(repo, callback) {
resolveReference(repo, 'HEAD', callback);
}
var TAG_REF = 'refs/tags/';
function resolveTag(repo, tag, callback) {
resolveReference(repo, TAG_REF+tag, callback);
}
function listTags(repo, callback) {
repo.listReferences(gitteh.GIT_REF_LISTALL, onList);
function onList(err, refs) {
if (err) return callback(err);
refs = refs.filter(function(ref) { return /^refs\/tags\//.test(ref); });
refs = refs.map(function(ref) { return ref.substring(TAG_REF.length); });
callback(null, refs);
}
}