Files: 5dba96e0853a4d7f84670f78b8043ab9bd5adc08 / lib / repos / index.js
39297 bytesRaw
1 | var url = require('url') |
2 | var pull = require('pull-stream') |
3 | var once = pull.once |
4 | var cat = require('pull-cat') |
5 | var paramap = require('pull-paramap') |
6 | var multicb = require('multicb') |
7 | var JsDiff = require('diff') |
8 | var GitRepo = require('pull-git-repo') |
9 | var gitPack = require('pull-git-pack') |
10 | var u = require('../util') |
11 | var paginate = require('../paginate') |
12 | var markdown = require('../markdown') |
13 | var forms = require('../forms') |
14 | var ssbRef = require('ssb-ref') |
15 | var zlib = require('zlib') |
16 | var toPull = require('stream-to-pull-stream') |
17 | var h = require('pull-hyperscript') |
18 | |
19 | function extend(obj, props) { |
20 | for (var k in props) |
21 | obj[k] = props[k] |
22 | return obj |
23 | } |
24 | |
25 | module.exports = function (web) { |
26 | return new RepoRoutes(web) |
27 | } |
28 | |
29 | function RepoRoutes(web) { |
30 | this.web = web |
31 | this.issues = require('./issues')(this, web) |
32 | this.pulls = require('./pulls')(this, web) |
33 | } |
34 | |
35 | var R = RepoRoutes.prototype |
36 | |
37 | function getRepoObjectString(repo, id, mode, cb) { |
38 | if (!id) return cb(null, '') |
39 | if (mode == 0160000) return cb(null, |
40 | 'Subproject commit ' + id) |
41 | repo.getObjectFromAny(id, function (err, obj) { |
42 | if (err) return cb(err) |
43 | u.readObjectString(obj, cb) |
44 | }) |
45 | } |
46 | |
47 | /* Repo */ |
48 | |
49 | R.serveRepoPage = function (req, repo, path) { |
50 | var self = this |
51 | var defaultBranch = 'master' |
52 | var query = req._u.query |
53 | |
54 | if (query.rev != null) { |
55 | // Allow navigating revs using GET query param. |
56 | // Replace the branch in the path with the rev query value |
57 | path[0] = path[0] || 'tree' |
58 | path[1] = query.rev |
59 | req._u.pathname = u.encodeLink([repo.id].concat(path)) |
60 | delete req._u.query.rev |
61 | delete req._u.search |
62 | return self.web.serveRedirect(req, url.format(req._u)) |
63 | } |
64 | |
65 | // get branch |
66 | return path[1] ? |
67 | R_serveRepoPage2.call(self, req, repo, path) : |
68 | u.readNext(function (cb) { |
69 | // TODO: handle this in pull-git-repo or ssb-git-repo |
70 | repo.getSymRef('HEAD', true, function (err, ref) { |
71 | if (err) return cb(err) |
72 | repo.resolveRef(ref, function (err, rev) { |
73 | path[1] = rev ? ref : null |
74 | cb(null, R_serveRepoPage2.call(self, req, repo, path)) |
75 | }) |
76 | }) |
77 | }) |
78 | } |
79 | |
80 | function R_serveRepoPage2(req, repo, path) { |
81 | var branch = path[1] |
82 | var filePath = path.slice(2) |
83 | switch (path[0]) { |
84 | case undefined: |
85 | case '': |
86 | return this.serveRepoTree(req, repo, branch, []) |
87 | case 'activity': |
88 | return this.serveRepoActivity(req, repo, branch) |
89 | case 'commits': |
90 | return this.serveRepoCommits(req, repo, branch) |
91 | case 'commit': |
92 | return this.serveRepoCommit(req, repo, path[1]) |
93 | case 'tag': |
94 | return this.serveRepoTag(req, repo, branch, filePath) |
95 | case 'tree': |
96 | return this.serveRepoTree(req, repo, branch, filePath) |
97 | case 'blob': |
98 | return this.serveRepoBlob(req, repo, branch, filePath) |
99 | case 'raw': |
100 | return this.serveRepoRaw(req, repo, branch, filePath) |
101 | case 'digs': |
102 | return this.serveRepoDigs(req, repo) |
103 | case 'fork': |
104 | return this.serveRepoForkPrompt(req, repo) |
105 | case 'forks': |
106 | return this.serveRepoForks(req, repo) |
107 | case 'issues': |
108 | switch (path[1]) { |
109 | case 'new': |
110 | if (filePath.length == 0) |
111 | return this.issues.serveRepoNewIssue(req, repo) |
112 | break |
113 | default: |
114 | return this.issues.serveRepoIssues(req, repo, false) |
115 | } |
116 | case 'pulls': |
117 | return this.issues.serveRepoIssues(req, repo, true) |
118 | case 'compare': |
119 | return this.pulls.serveRepoCompare(req, repo) |
120 | case 'comparing': |
121 | return this.pulls.serveRepoComparing(req, repo) |
122 | case 'info': |
123 | switch (path[1]) { |
124 | case 'refs': |
125 | return this.serveRepoRefs(req, repo) |
126 | default: |
127 | return this.web.serve404(req) |
128 | } |
129 | case 'objects': |
130 | switch (path[1]) { |
131 | case 'info': |
132 | switch (path[2]) { |
133 | case 'packs': |
134 | return this.serveRepoPacksInfo(req, repo) |
135 | default: |
136 | return this.web.serve404(req) |
137 | } |
138 | case 'pack': |
139 | return this.serveRepoPack(req, repo, filePath.join('/')) |
140 | default: |
141 | var hash = path[1] + path[2] |
142 | if (hash.length === 40) { |
143 | return this.serveRepoObject(req, repo, hash) |
144 | } |
145 | return this.web.serve404(req) |
146 | } |
147 | case 'HEAD': |
148 | return this.serveRepoHead(req, repo) |
149 | default: |
150 | return this.web.serve404(req) |
151 | } |
152 | } |
153 | |
154 | R.serveRepoNotFound = function (req, id, err) { |
155 | return this.web.serveTemplate(req, req._t('error.RepoNotFound'), 404) |
156 | (pull.values([ |
157 | '<h2>' + req._t('error.RepoNotFound') + '</h2>', |
158 | '<p>' + req._t('error.RepoNameNotFound') + '</p>', |
159 | '<pre>' + u.escape(err.stack) + '</pre>' |
160 | ])) |
161 | } |
162 | |
163 | R.serveRepoTemplate = function (req, repo, page, branch, titleTemplate, body) { |
164 | var self = this |
165 | var gitUrl = 'ssb://' + repo.id |
166 | var host = req.headers.host || '127.0.0.1:7718' |
167 | var path = '/' + encodeURIComponent(repo.id) |
168 | var httpUrl = 'http://' + encodeURI(host) + path |
169 | var digsPath = [repo.id, 'digs'] |
170 | var cloneUrls = '<div class="clone-urls">' + |
171 | '<select class="custom-dropdown clone-url-protocol" ' + |
172 | 'onchange="with(this.nextSibling.firstChild) {' + |
173 | 'value = this.value; select() }">' + |
174 | '<option selected="selected" value="' + gitUrl + '">SSB</option>' + |
175 | '<option class="http-clone-url" value="' + httpUrl + '">HTTP</option>' + |
176 | '</select>' + |
177 | '<div class="clone-url-wrapper">' + |
178 | '<input class="clone-url" readonly="readonly" ' + |
179 | 'value="ssb://' + repo.id + '" size="45" ' + |
180 | 'onclick="this.select()"/>' + |
181 | '<script>' + |
182 | 'var httpOpt = document.querySelector(".http-clone-url")\n' + |
183 | 'if (location.protocol === "https:") httpOpt.text = "HTTPS"\n' + |
184 | 'httpOpt.value = location.origin + "' + path + '"\n' + |
185 | '</script>' + |
186 | '</div>' + |
187 | '</div>' |
188 | |
189 | var done = multicb({ pluck: 1, spread: true }) |
190 | self.web.getRepoName(repo.feed, repo.id, done()) |
191 | self.web.about.getName(repo.feed, done()) |
192 | self.web.getVotes(repo.id, done()) |
193 | |
194 | if (repo.upstream) { |
195 | self.web.getRepoName(repo.upstream.feed, repo.upstream.id, done()) |
196 | self.web.about.getName(repo.upstream.feed, done()) |
197 | } |
198 | |
199 | return u.readNext(function (cb) { |
200 | done(function (err, repoName, authorName, votes, upstreamName, upstreamAuthorName) { |
201 | if (err) return cb(null, self.web.serveError(req, err)) |
202 | var upvoted = votes.upvoters[self.web.myId] > 0 |
203 | var upstreamLink = !repo.upstream ? '' : |
204 | u.link([repo.upstream]) |
205 | var title = titleTemplate ? titleTemplate |
206 | .replace(/%\{repo\}/g, repoName) |
207 | .replace(/%\{author\}/g, authorName) |
208 | : authorName + '/' + repoName |
209 | var isPublic = self.web.isPublic |
210 | var isLocal = !isPublic |
211 | cb(null, self.web.serveTemplate(req, title)(cat([ |
212 | h('div', {class: 'repo-title'}, [ |
213 | h('form', {class: 'right-bar', action: '', method: 'post'}, [ |
214 | h('strong', {class: 'ml2 mr1'}, u.link(digsPath, votes.upvotes)), |
215 | h('button', |
216 | extend( |
217 | {class: 'btn', name: 'action', value: 'vote'}, |
218 | isPublic ? {disabled: 'disabled'} : {type: 'submit'} |
219 | ), [ |
220 | h('i', '✌ '), |
221 | h('span', req._t(isLocal && upvoted ? 'Undig' : 'Dig')) |
222 | ] |
223 | ), |
224 | u.when(isLocal, () => cat([ |
225 | h('input', {type: 'hidden', name: 'value', value: (upvoted ? '0' : '1')}), |
226 | h('input', {type: 'hidden', name: 'id', value: u.escape(repo.id)}) |
227 | ])), |
228 | h('a', {href: u.encodeLink([repo.id, 'forks']), title: req._t('Forks'), class: 'ml2 mr1'}, '+'), |
229 | u.when(isLocal, () => |
230 | h('button', {class: 'btn', type: 'submit', name: 'action', value: 'fork-prompt'}, [ |
231 | h('i', '⑂ '), |
232 | once(req._t('Fork')) |
233 | ]) |
234 | ) |
235 | ]), |
236 | forms.name(req, isLocal, repo.id, repoName, 'repo-name', null, req._t('repo.Rename'), |
237 | h('h2', {class: 'bgslash'}, `${u.link([repo.feed], authorName)} / ${u.link([repo.id], repoName)}`) |
238 | ), |
239 | ]), |
240 | u.when(repo.upstream, () => |
241 | h('small', {class: 'bgslash'}, req._t('ForkedFrom', { |
242 | repo: `${u.link([repo.upstream.feed], upstreamAuthorName)} / ${u.link([repo.upstream.id], upstreamName)}` |
243 | })) |
244 | ), |
245 | u.nav([ |
246 | [[repo.id], req._t('Code'), 'code'], |
247 | [[repo.id, 'activity'], req._t('Activity'), 'activity'], |
248 | [[repo.id, 'commits', branch||''], req._t('Commits'), 'commits'], |
249 | [[repo.id, 'issues'], req._t('Issues'), 'issues'], |
250 | [[repo.id, 'pulls'], req._t('PullRequests'), 'pulls'] |
251 | ], page, cloneUrls), |
252 | body |
253 | ]) |
254 | )) |
255 | }) |
256 | }) |
257 | } |
258 | |
259 | R.renderEmptyRepo = function (req, repo) { |
260 | if (repo.feed != this.web.myId) |
261 | return h('section', [ |
262 | h('h3', req._t('EmptyRepo')) |
263 | ]) |
264 | |
265 | var gitUrl = 'ssb://' + repo.id |
266 | return h('section', [ |
267 | h('h3', req._t('initRepo.GettingStarted')), |
268 | h('h4', req._t('initRepo.CreateNew')), |
269 | preInitRepo(req, gitUrl), |
270 | h('h4', req._t('initRepo.PushExisting')), |
271 | preRemote(gitUrl) |
272 | ]) |
273 | } |
274 | |
275 | var preInitRepo = (req, gitUrl) => h('pre', |
276 | `touch ${req._t('initRepo.README')}.md |
277 | git init |
278 | git add ${req._t('initRepo.README')}.md |
279 | git commit -m ${req._t('initRepo.InitialCommit')} |
280 | git remote add origin ${gitUrl} |
281 | git push -u origin master`) |
282 | |
283 | var preRemote = (gitUrl) => h('pre', |
284 | `git remote add origin ${gitUrl} |
285 | git push -u origin master`) |
286 | |
287 | |
288 | R.serveRepoTree = function (req, repo, rev, path) { |
289 | var type = repo.isCommitHash(rev) ? 'Tree' : 'Branch' |
290 | var title = |
291 | (path.length ? `${path.join('/')} · ` : '') + |
292 | '%{author}/%{repo}' + |
293 | (repo.head == `refs/heads/${rev}` ? '' : `@${rev}`) |
294 | |
295 | return this.serveRepoTemplate(req, repo, 'code', rev, title, |
296 | u.readNext((cb) => { |
297 | repo.getLatestAvailableRev(rev, 10e3, (err, revGot, numSkipped) => { |
298 | if (err) return cb(err) |
299 | cb(null, cat([ |
300 | h('section', {class: 'branch-info light-grey', method: 'get'}, [ |
301 | h('form', {action: '', method: 'get'}, |
302 | h('h3', {class: 'rev-menu-line'}, [ |
303 | h('span', `${req._t(type)}: `), |
304 | this.revMenu(req, repo, rev) |
305 | ]) |
306 | ), |
307 | u.when(numSkipped > 0, () => |
308 | h('div', {class: 'missing-blobs-warning mt2'}, |
309 | h('em', req._t('missingBlobsWarning', numSkipped)) |
310 | ) |
311 | ), |
312 | u.when(type === 'Branch', () => renderRepoLatest(req, repo, revGot)) |
313 | ]), |
314 | rev ? cat([ |
315 | h('section', {class: 'files'}, renderRepoTree(req, repo, revGot, path)), |
316 | this.renderRepoReadme(req, repo, revGot, path) |
317 | ]) : this.renderEmptyRepo(req, repo) |
318 | ])) |
319 | }) |
320 | })) |
321 | } |
322 | |
323 | /* Repo activity */ |
324 | |
325 | R.serveRepoActivity = function (req, repo, branch) { |
326 | var self = this |
327 | var title = req._t('Activity') + ' · %{author}/%{repo}' |
328 | return self.serveRepoTemplate(req, repo, 'activity', branch, title, cat([ |
329 | h('h3', req._t('Activity')), |
330 | pull( |
331 | self.web.ssb.links({ |
332 | dest: repo.id, |
333 | rel: 'repo', |
334 | values: true |
335 | }), |
336 | pull.unique('key'), |
337 | u.sortMsgs(true), |
338 | pull.asyncMap(renderRepoUpdate.bind(self, req, repo, false)) |
339 | ), |
340 | u.readOnce(function (cb) { |
341 | var done = multicb({ pluck: 1, spread: true }) |
342 | self.web.about.getName(repo.feed, done()) |
343 | self.web.getMsg(repo.id, done()) |
344 | done(function (err, authorName, msg) { |
345 | if (err) return cb(err) |
346 | self.web.renderFeedItem(req, { |
347 | key: repo.id, |
348 | value: msg, |
349 | authorName: authorName |
350 | }, cb) |
351 | }) |
352 | }) |
353 | ])) |
354 | } |
355 | |
356 | function renderRepoUpdate(req, repo, full, msg, cb) { |
357 | var c = msg.value.content |
358 | |
359 | if (c.type != 'git-update') { |
360 | return cb(null, '') |
361 | // return renderFeedItem(msg, cb) |
362 | // TODO: render post, issue, pull-request |
363 | } |
364 | |
365 | var branches = [] |
366 | var tags = [] |
367 | if (c.refs) for (var name in c.refs) { |
368 | var m = name.match(/^refs\/(heads|tags)\/(.*)$/) || [,, name] |
369 | ;(m[1] == 'tags' ? tags : branches) |
370 | .push({name: m[2], value: c.refs[name]}) |
371 | } |
372 | var numObjects = c.objects ? Object.keys(c.objects).length : 0 |
373 | |
374 | var dateStr = new Date(msg.value.timestamp).toLocaleString(req._locale) |
375 | |
376 | this.web.about.getName(msg.value.author, function (err, name) { |
377 | if (err) return cb(err) |
378 | cb(null, '<section class="collapse">' + |
379 | u.link([msg.key], dateStr) + '<br>' + |
380 | u.link([msg.value.author], name) + '<br>' + |
381 | |
382 | branches.map(function (update) { |
383 | if (!update.value) { |
384 | return '<s>' + u.escape(update.name) + '</s><br/>' |
385 | } else { |
386 | var commitLink = u.link([repo.id, 'commit', update.value]) |
387 | var branchLink = u.link([repo.id, 'tree', update.name]) |
388 | return branchLink + ' → <tt>' + commitLink + '</tt><br/>' |
389 | } |
390 | }).join('') + |
391 | tags.map(function (update) { |
392 | return update.value |
393 | ? u.link([repo.id, 'tag', update.value], update.name) |
394 | : '<s>' + u.escape(update.name) + '</s>' |
395 | }).join(', ') + |
396 | '</section>') |
397 | }) |
398 | } |
399 | |
400 | /* Repo commits */ |
401 | |
402 | R.serveRepoCommits = function (req, repo, branch) { |
403 | var query = req._u.query |
404 | var title = req._t('Commits') + ' · %{author}/%{repo}' |
405 | return this.serveRepoTemplate(req, repo, 'commits', branch, title, cat([ |
406 | pull.once('<h3>' + req._t('Commits') + '</h3>'), |
407 | pull( |
408 | repo.readLog(query.start || branch), |
409 | pull.take(20), |
410 | paramap(repo.getCommitParsed.bind(repo), 8), |
411 | paginate( |
412 | !query.start ? '' : function (first, cb) { |
413 | cb(null, '…') |
414 | }, |
415 | pull.map(renderCommit.bind(this, req, repo)), |
416 | function (commit, cb) { |
417 | cb(null, commit.parents && commit.parents[0] ? |
418 | '<a href="?start=' + commit.id + '">' + |
419 | req._t('Older') + '</a>' : '') |
420 | } |
421 | ) |
422 | ) |
423 | ])) |
424 | } |
425 | |
426 | function renderCommit(req, repo, commit) { |
427 | var commitPath = [repo.id, 'commit', commit.id] |
428 | var treePath = [repo.id, 'tree', commit.id] |
429 | return '<section class="collapse">' + |
430 | '<strong>' + u.link(commitPath, commit.title) + '</strong><br>' + |
431 | '<tt>' + commit.id + '</tt> ' + |
432 | u.link(treePath, req._t('Tree')) + '<br>' + |
433 | u.escape(commit.author.name) + ' · ' + |
434 | commit.author.date.toLocaleString(req._locale) + |
435 | (commit.separateAuthor ? '<br>' + req._t('CommittedOn', { |
436 | name: u.escape(commit.committer.name), |
437 | date: commit.committer.date.toLocaleString(req._locale) |
438 | }) : '') + |
439 | '</section>' |
440 | } |
441 | |
442 | /* Branch menu */ |
443 | |
444 | R.formatRevOptions = function (currentName) { |
445 | return function (name) { |
446 | var htmlName = u.escape(name) |
447 | return '<option value="' + htmlName + '"' + |
448 | (name == currentName ? ' selected="selected"' : '') + |
449 | '>' + htmlName + '</option>' |
450 | } |
451 | } |
452 | |
453 | R.formatRevType = function(req, type) { |
454 | return ( |
455 | type == 'heads' ? req._t('Branches') : |
456 | type == 'tags' ? req._t('Tags') : |
457 | type) |
458 | } |
459 | |
460 | R.revMenu = function (req, repo, currentName) { |
461 | var self = this |
462 | return u.readOnce(function (cb) { |
463 | repo.getRefNames(function (err, refs) { |
464 | if (err) return cb(err) |
465 | cb(null, '<select class="custom-dropdown" name="rev" onchange="this.form.submit()">' + |
466 | Object.keys(refs).map(function (group) { |
467 | return '<optgroup ' + |
468 | 'label="' + self.formatRevType(req, group) + '">' + |
469 | refs[group].map(self.formatRevOptions(currentName)).join('') + |
470 | '</optgroup>' |
471 | }).join('') + |
472 | '</select><noscript> ' + |
473 | '<input type="submit" value="' + req._t('Go') + '"/></noscript>') |
474 | }) |
475 | }) |
476 | } |
477 | |
478 | /* Repo tree */ |
479 | |
480 | function renderRepoLatest(req, repo, rev) { |
481 | if (!rev) return pull.empty() |
482 | return u.readOnce(function (cb) { |
483 | repo.getCommitParsed(rev, function (err, commit) { |
484 | if (err) return cb(err) |
485 | var commitPath = [repo.id, 'commit', commit.id] |
486 | var actor = commit.separateAuthor ? 'author' : 'committer' |
487 | var actionKey = actor.slice(0,1).toUpperCase() + actor.slice(1) + 'ReleasedCommit' |
488 | cb(null, |
489 | '<div class="mt2">' + |
490 | '<span>' + |
491 | req._t(actionKey, { |
492 | name: u.escape(commit[actor].name), |
493 | commitName: u.link(commitPath, commit.title) |
494 | }) + |
495 | '</span>' + |
496 | '<tt class="float-right">' + |
497 | req._t('LatestOn', { |
498 | commitId: commit.id.slice(0, 7), |
499 | date: commit[actor].date.toLocaleString(req._locale) |
500 | }) + |
501 | '</tt>' + |
502 | '</div>' |
503 | ) |
504 | }) |
505 | }) |
506 | } |
507 | |
508 | // breadcrumbs |
509 | function linkPath(basePath, path) { |
510 | path = path.slice() |
511 | var last = path.pop() |
512 | return path.map(function (dir, i) { |
513 | return u.link(basePath.concat(path.slice(0, i+1)), dir) |
514 | }).concat(last).join(' / ') |
515 | } |
516 | |
517 | function renderRepoTree(req, repo, rev, path) { |
518 | var source = repo.readDir(rev,path) |
519 | var pathLinks = path.length === 0 ? '' : |
520 | ': ' + linkPath([repo.id, 'tree'], [rev].concat(path)) |
521 | |
522 | var location = once('') |
523 | if (path.length !== 0) { |
524 | var link = linkPath([repo.id, 'tree'], [rev].concat(path)) |
525 | location = h('div', {class: 'fileLocation'}, `${req._t('Files')}: ${link}`) |
526 | } |
527 | |
528 | return cat([ |
529 | location, |
530 | h('table', {class: "files w-100"}, u.sourceMap(source, file => |
531 | h('tr', [ |
532 | h('td', [ |
533 | h('i', fileIcon(file)) |
534 | ]), |
535 | h('td', u.link(filePath(file), file.name)) |
536 | ]) |
537 | )) |
538 | ]) |
539 | |
540 | function fileIcon(file) { |
541 | return fileType(file) === 'tree' ? '📁' : '📄' |
542 | } |
543 | |
544 | function filePath(file) { |
545 | var type = fileType(file) |
546 | return [repo.id, type, rev].concat(path, file.name) |
547 | } |
548 | |
549 | function fileType(file) { |
550 | if (file.mode === 040000) return 'tree' |
551 | else if (file.mode === 0160000) return 'commit' |
552 | else return 'blob' |
553 | } |
554 | } |
555 | |
556 | /* Repo readme */ |
557 | |
558 | R.renderRepoReadme = function (req, repo, branch, path) { |
559 | var self = this |
560 | return u.readNext(function (cb) { |
561 | pull( |
562 | repo.readDir(branch, path), |
563 | pull.filter(function (file) { |
564 | return /readme(\.|$)/i.test(file.name) |
565 | }), |
566 | pull.take(1), |
567 | pull.collect(function (err, files) { |
568 | if (err) return cb(null, pull.empty()) |
569 | var file = files[0] |
570 | if (!file) |
571 | return cb(null, pull.once(path.length ? '' : |
572 | '<p>' + req._t('NoReadme') + '</p>')) |
573 | repo.getObjectFromAny(file.id, function (err, obj) { |
574 | if (err) return cb(err) |
575 | cb(null, cat([ |
576 | pull.once('<section class="readme">'), |
577 | self.web.renderObjectData(obj, file.name, repo, branch, path), |
578 | pull.once('</section>') |
579 | ])) |
580 | }) |
581 | }) |
582 | ) |
583 | }) |
584 | } |
585 | |
586 | /* Repo commit */ |
587 | |
588 | R.serveRepoCommit = function (req, repo, rev) { |
589 | var self = this |
590 | return u.readNext(function (cb) { |
591 | repo.getCommitParsed(rev, function (err, commit) { |
592 | if (err) return cb(null, |
593 | self.serveRepoTemplate(req, repo, null, rev, `%{author}/%{repo}@${rev}`, |
594 | pull.once(self.web.renderError(err)))) |
595 | var commitPath = [repo.id, 'commit', commit.id] |
596 | var treePath = [repo.id, 'tree', commit.id] |
597 | var title = u.escape(commit.title) + ' · ' + |
598 | '%{author}/%{repo}@' + commit.id.substr(0, 8) |
599 | cb(null, self.serveRepoTemplate(req, repo, null, rev, title, cat([ |
600 | pull.once( |
601 | '<h3>' + u.link(commitPath, |
602 | req._t('CommitRev', {rev: rev})) + '</h3>' + |
603 | '<section class="collapse">' + |
604 | '<div class="right-bar">' + |
605 | u.link(treePath, req._t('BrowseFiles')) + |
606 | '</div>' + |
607 | '<h4>' + u.linkify(u.escape(commit.title)) + '</h4>' + |
608 | (commit.body ? u.linkify(u.pre(commit.body)) : '') + |
609 | (commit.separateAuthor ? req._t('AuthoredOn', { |
610 | name: u.escape(commit.author.name), |
611 | date: commit.author.date.toLocaleString(req._locale) |
612 | }) + '<br/>' : '') + |
613 | req._t('CommittedOn', { |
614 | name: u.escape(commit.committer.name), |
615 | date: commit.committer.date.toLocaleString(req._locale) |
616 | }) + '<br/>' + |
617 | commit.parents.map(function (id) { |
618 | return req._t('Parent') + ': ' + |
619 | u.link([repo.id, 'commit', id], id) |
620 | }).join('<br>') + |
621 | '</section>' + |
622 | '<section><h3>' + req._t('FilesChanged') + '</h3>'), |
623 | // TODO: show diff from all parents (merge commits) |
624 | self.renderDiffStat(req, [repo, repo], [commit.parents[0], commit.id]), |
625 | pull.once('</section>') |
626 | ]))) |
627 | }) |
628 | }) |
629 | } |
630 | |
631 | /* Repo tag */ |
632 | |
633 | R.serveRepoTag = function (req, repo, rev, path) { |
634 | var self = this |
635 | return u.readNext(function (cb) { |
636 | repo.getTagParsed(rev, function (err, tag) { |
637 | if (err) { |
638 | if (/Expected tag, got commit/.test(err.message)) { |
639 | req._u.pathname = u.encodeLink([repo.id, 'commit', rev].concat(path)) |
640 | return cb(null, self.web.serveRedirect(req, url.format(req._u))) |
641 | } |
642 | return cb(null, self.web.serveError(req, err)) |
643 | } |
644 | |
645 | var title = req._t('TagName', { |
646 | tag: u.escape(tag.tag) |
647 | }) + ' · %{author}/%{repo}' |
648 | var body = (tag.title + '\n\n' + |
649 | tag.body.replace(/-----BEGIN PGP SIGNATURE-----\n[^.]*?\n-----END PGP SIGNATURE-----\s*$/, '')).trim() |
650 | var date = tag.tagger.date |
651 | cb(null, self.serveRepoTemplate(req, repo, 'tags', tag.object, title, |
652 | pull.once( |
653 | '<section class="collapse">' + |
654 | '<h3>' + u.link([repo.id, 'tag', rev], tag.tag) + '</h3>' + |
655 | req._t('TaggedOn', { |
656 | name: u.escape(tag.tagger.name), |
657 | date: date && date.toLocaleString(req._locale) |
658 | }) + '<br/>' + |
659 | u.link([repo.id, tag.type, tag.object]) + |
660 | u.linkify(u.pre(body)) + |
661 | '</section>'))) |
662 | }) |
663 | }) |
664 | } |
665 | |
666 | |
667 | /* Diff stat */ |
668 | |
669 | R.renderDiffStat = function (req, repos, treeIds) { |
670 | if (treeIds.length == 0) treeIds = [null] |
671 | var id = treeIds[0] |
672 | var lastI = treeIds.length - 1 |
673 | var oldTree = treeIds[0] |
674 | var changedFiles = [] |
675 | var source = GitRepo.diffTrees(repos, treeIds, true) |
676 | |
677 | return cat([ |
678 | h('table', u.sourceMap(source, item => { |
679 | var filename = u.escape(item.filename = item.path.join('/')) |
680 | var oldId = item.id && item.id[0] |
681 | var newId = item.id && item.id[lastI] |
682 | var oldMode = item.mode && item.mode[0] |
683 | var newMode = item.mode && item.mode[lastI] |
684 | var action = |
685 | !oldId && newId ? req._t('action.added') : |
686 | oldId && !newId ? req._t('action.deleted') : |
687 | oldMode != newMode ? req._t('action.changedMode', { |
688 | old: oldMode.toString(8), |
689 | new: newMode.toString(8) |
690 | }) : req._t('changed') |
691 | if (item.id) |
692 | changedFiles.push(item) |
693 | var blobsPath = item.id[1] |
694 | ? [repos[1].id, 'blob', treeIds[1]] |
695 | : [repos[0].id, 'blob', treeIds[0]] |
696 | var rawsPath = item.id[1] |
697 | ? [repos[1].id, 'raw', treeIds[1]] |
698 | : [repos[0].id, 'raw', treeIds[0]] |
699 | item.blobPath = blobsPath.concat(item.path) |
700 | item.rawPath = rawsPath.concat(item.path) |
701 | var fileHref = item.id ? |
702 | '#' + encodeURIComponent(item.path.join('/')) : |
703 | u.encodeLink(item.blobPath) |
704 | |
705 | return h('tr', [ |
706 | h('td', [ |
707 | h('a', {href: fileHref}, filename) |
708 | ]), |
709 | h('td', action) |
710 | ]) |
711 | })), |
712 | pull( |
713 | pull.values(changedFiles), |
714 | paramap(function (item, cb) { |
715 | var extension = u.getExtension(item.filename) |
716 | if (extension in u.imgMimes) { |
717 | var filename = u.escape(item.filename) |
718 | return cb(null, |
719 | '<pre><table class="code">' + |
720 | '<tr><th id="' + u.escape(item.filename) + '">' + |
721 | filename + '</th></tr>' + |
722 | '<tr><td><img src="' + u.encodeLink(item.rawPath) + '"' + |
723 | ' alt="' + filename + '"/></td></tr>' + |
724 | '</table></pre>') |
725 | } |
726 | var done = multicb({ pluck: 1, spread: true }) |
727 | var mode0 = item.mode && item.mode[0] |
728 | var modeI = item.mode && item.mode[lastI] |
729 | var isSubmodule = (modeI == 0160000) |
730 | getRepoObjectString(repos[0], item.id[0], mode0, done()) |
731 | getRepoObjectString(repos[1], item.id[lastI], modeI, done()) |
732 | done(function (err, strOld, strNew) { |
733 | if (err) return cb(err) |
734 | cb(null, htmlLineDiff(req, item.filename, item.filename, |
735 | strOld, strNew, |
736 | u.encodeLink(item.blobPath), !isSubmodule)) |
737 | }) |
738 | }, 4) |
739 | ) |
740 | ]) |
741 | } |
742 | |
743 | function htmlLineDiff(req, filename, anchor, oldStr, newStr, blobHref, |
744 | showViewLink) { |
745 | return '<pre><table class="code">' + |
746 | '<tr><th colspan=3 id="' + u.escape(anchor) + '">' + filename + |
747 | (showViewLink === false ? '' : |
748 | '<span class="right-bar">' + |
749 | '<a href="' + blobHref + '">' + req._t('View') + '</a> ' + |
750 | '</span>') + |
751 | '</th></tr>' + |
752 | (oldStr.length + newStr.length > 200000 |
753 | ? '<tr><td class="diff-info">' + req._t('diff.TooLarge') + '<br>' + |
754 | req._t('diff.OldFileSize', {bytes: oldStr.length}) + '<br>' + |
755 | req._t('diff.NewFileSize', {bytes: newStr.length}) + '</td></tr>' |
756 | : tableDiff(oldStr, newStr, filename)) + |
757 | '</table></pre>' |
758 | } |
759 | |
760 | function tableDiff(oldStr, newStr, filename) { |
761 | var diff = JsDiff.structuredPatch('', '', oldStr, newStr) |
762 | var groups = diff.hunks.map(function (hunk) { |
763 | var oldLine = hunk.oldStart |
764 | var newLine = hunk.newStart |
765 | var header = '<tr class="diff-hunk-header"><td colspan=2></td><td>' + |
766 | '@@ -' + oldLine + ',' + hunk.oldLines + ' ' + |
767 | '+' + newLine + ',' + hunk.newLines + ' @@' + |
768 | '</td></tr>' |
769 | return [header].concat(hunk.lines.map(function (line) { |
770 | var s = line[0] |
771 | if (s == '\\') return |
772 | var html = u.highlight(line, u.getExtension(filename)) |
773 | var trClass = s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : '' |
774 | var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++] |
775 | var id = [filename].concat(lineNums).join('-') |
776 | return '<tr id="' + u.escape(id) + '" class="' + trClass + '">' + |
777 | lineNums.map(function (num) { |
778 | return '<td class="code-linenum">' + |
779 | (num ? '<a href="#' + encodeURIComponent(id) + '">' + |
780 | num + '</a>' : '') + '</td>' |
781 | }).join('') + |
782 | '<td class="code-text">' + html + '</td></tr>' |
783 | })) |
784 | }) |
785 | return [].concat.apply([], groups).join('') |
786 | } |
787 | |
788 | /* An unknown message linking to a repo */ |
789 | |
790 | R.serveRepoSomething = function (req, repo, id, msg, path) { |
791 | return this.serveRepoTemplate(req, repo, null, null, null, |
792 | pull.once('<section><h3>' + u.link([id]) + '</h3>' + |
793 | u.json(msg) + '</section>')) |
794 | } |
795 | |
796 | /* Repo update */ |
797 | |
798 | function objsArr(objs) { |
799 | return Array.isArray(objs) ? objs : |
800 | Object.keys(objs).map(function (sha1) { |
801 | var obj = Object.create(objs[sha1]) |
802 | obj.sha1 = sha1 |
803 | return obj |
804 | }) |
805 | } |
806 | |
807 | R.serveRepoUpdate = function (req, repo, id, msg, path) { |
808 | var self = this |
809 | var raw = req._u.query.raw != null |
810 | var title = req._t('Update') + ' · %{author}/%{repo}' |
811 | |
812 | if (raw) |
813 | return self.serveRepoTemplate(req, repo, 'activity', null, title, pull.once( |
814 | '<a href="?" class="raw-link header-align">' + |
815 | req._t('Info') + '</a>' + |
816 | '<h3>' + req._t('Update') + '</h3>' + |
817 | '<section class="collapse">' + |
818 | u.json({key: id, value: msg}) + '</section>')) |
819 | |
820 | // convert packs to old single-object style |
821 | if (msg.content.indexes) { |
822 | for (var i = 0; i < msg.content.indexes.length; i++) { |
823 | msg.content.packs[i] = { |
824 | pack: {link: msg.content.packs[i].link}, |
825 | idx: msg.content.indexes[i] |
826 | } |
827 | } |
828 | } |
829 | |
830 | var commits = cat([ |
831 | msg.content.objects && pull( |
832 | pull.values(msg.content.objects), |
833 | pull.filter(function (obj) { return obj.type == 'commit' }), |
834 | paramap(function (obj, cb) { |
835 | self.web.getBlob(req, obj.link || obj.key, function (err, readObject) { |
836 | if (err) return cb(err) |
837 | GitRepo.getCommitParsed({read: readObject}, cb) |
838 | }) |
839 | }, 8) |
840 | ), |
841 | msg.content.packs && pull( |
842 | pull.values(msg.content.packs), |
843 | paramap(function (pack, cb) { |
844 | var done = multicb({ pluck: 1, spread: true }) |
845 | self.web.getBlob(req, pack.pack.link, done()) |
846 | self.web.getBlob(req, pack.idx.link, done()) |
847 | done(function (err, readPack, readIdx) { |
848 | if (err) return cb(self.web.renderError(err)) |
849 | cb(null, gitPack.decodeWithIndex(repo, readPack, readIdx)) |
850 | }) |
851 | }, 4), |
852 | pull.flatten(), |
853 | pull.asyncMap(function (obj, cb) { |
854 | if (obj.type == 'commit') |
855 | GitRepo.getCommitParsed(obj, cb) |
856 | else |
857 | pull(obj.read, pull.drain(null, cb)) |
858 | }), |
859 | pull.filter() |
860 | ) |
861 | ]) |
862 | |
863 | return self.serveRepoTemplate(req, repo, 'activity', null, title, cat([ |
864 | pull.once('<a href="?raw" class="raw-link header-align">' + |
865 | req._t('Data') + '</a>' + |
866 | '<h3>' + req._t('Update') + '</h3>'), |
867 | pull( |
868 | pull.once({key: id, value: msg}), |
869 | pull.asyncMap(renderRepoUpdate.bind(self, req, repo, true)) |
870 | ), |
871 | (msg.content.objects || msg.content.packs) && |
872 | pull.once('<h3>' + req._t('Commits') + '</h3>'), |
873 | pull(commits, pull.map(function (commit) { |
874 | return renderCommit(req, repo, commit) |
875 | })) |
876 | ])) |
877 | } |
878 | |
879 | /* Blob */ |
880 | |
881 | R.serveRepoBlob = function (req, repo, rev, path) { |
882 | var self = this |
883 | return u.readNext(function (cb) { |
884 | repo.getFile(rev, path, function (err, object) { |
885 | if (err) return cb(null, self.web.serveBlobNotFound(req, repo.id, err)) |
886 | var type = repo.isCommitHash(rev) ? 'Tree' : 'Branch' |
887 | var pathLinks = path.length === 0 ? '' : |
888 | ': ' + linkPath([repo.id, 'tree'], [rev].concat(path)) |
889 | var rawFilePath = [repo.id, 'raw', rev].concat(path) |
890 | var dirPath = path.slice(0, path.length-1) |
891 | var filename = path[path.length-1] |
892 | var extension = u.getExtension(filename) |
893 | var title = (path.length ? path.join('/') + ' · ' : '') + |
894 | '%{author}/%{repo}' + |
895 | (repo.head == 'refs/heads/' + rev ? '' : '@' + rev) |
896 | cb(null, self.serveRepoTemplate(req, repo, 'code', rev, title, cat([ |
897 | pull.once('<section><form action="" method="get">' + |
898 | '<h3>' + req._t(type) + ': ' + rev + ' '), |
899 | self.revMenu(req, repo, rev), |
900 | pull.once('</h3></form>'), |
901 | type == 'Branch' && renderRepoLatest(req, repo, rev), |
902 | pull.once('</section><section class="collapse">' + |
903 | '<h3>' + req._t('Files') + pathLinks + '</h3>' + |
904 | '<div>' + object.length + ' bytes' + |
905 | '<span class="raw-link">' + |
906 | u.link(rawFilePath, req._t('Raw')) + '</span>' + |
907 | '</div></section>' + |
908 | '<section>'), |
909 | extension in u.imgMimes |
910 | ? pull.once('<img src="' + u.encodeLink(rawFilePath) + |
911 | '" alt="' + u.escape(filename) + '" />') |
912 | : self.web.renderObjectData(object, filename, repo, rev, dirPath), |
913 | pull.once('</section>') |
914 | ]))) |
915 | }) |
916 | }) |
917 | } |
918 | |
919 | /* Raw blob */ |
920 | |
921 | R.serveRepoRaw = function (req, repo, branch, path) { |
922 | var self = this |
923 | return u.readNext(function (cb) { |
924 | repo.getFile(branch, path, function (err, object) { |
925 | if (err) return cb(null, |
926 | self.web.serveBuffer(404, req._t('error.BlobNotFound'))) |
927 | var extension = u.getExtension(path[path.length-1]) |
928 | var contentType = u.imgMimes[extension] |
929 | cb(null, pull(object.read, self.web.serveRaw(object.length, contentType))) |
930 | }) |
931 | }) |
932 | } |
933 | |
934 | /* Digs */ |
935 | |
936 | R.serveRepoDigs = function serveRepoDigs (req, repo) { |
937 | var self = this |
938 | return u.readNext(cb => { |
939 | var title = req._t('Digs') + ' · %{author}/%{repo}' |
940 | self.web.getVotes(repo.id, (err, votes) => { |
941 | cb(null, self.serveRepoTemplate(req, repo, null, null, title, |
942 | h('section', [ |
943 | h('h3', req._t('Digs')), |
944 | h('div', `${req._t('Total')}: ${votes.upvotes}`), |
945 | h('ul', u.paraSourceMap(Object.keys(votes.upvoters), (feedId, cb) => { |
946 | self.web.about.getName(feedId, (err, name) => { |
947 | cb(null, h('li', u.link([feedId], name))) |
948 | }) |
949 | })) |
950 | ]) |
951 | )) |
952 | }) |
953 | }) |
954 | } |
955 | |
956 | /* Forks */ |
957 | |
958 | R.getForks = function (repo, includeSelf) { |
959 | var self = this |
960 | return pull( |
961 | cat([ |
962 | includeSelf && pull.once(repo.id), |
963 | // get downstream repos |
964 | pull( |
965 | self.web.ssb.links({ |
966 | dest: repo.id, |
967 | rel: 'upstream' |
968 | }), |
969 | pull.map('key') |
970 | ), |
971 | // look for other repos that previously had pull requests to this one |
972 | pull( |
973 | self.web.ssb.links({ |
974 | dest: repo.id, |
975 | values: true, |
976 | rel: 'project' |
977 | }), |
978 | pull.filter(function (msg) { |
979 | var c = msg && msg.value && msg.value.content |
980 | return c && c.type == 'pull-request' |
981 | }), |
982 | pull.map(function (msg) { return msg.value.content.head_repo }) |
983 | ) |
984 | ]), |
985 | pull.unique(), |
986 | paramap(function (key, cb) { |
987 | self.web.ssb.get(key, function (err, value) { |
988 | if (err) cb(err) |
989 | else cb(null, {key: key, value: value}) |
990 | }) |
991 | }, 4), |
992 | pull.filter(function (msg) { |
993 | var c = msg && msg.value && msg.value.content |
994 | return c && c.type == 'git-repo' |
995 | }), |
996 | paramap(function (msg, cb) { |
997 | self.web.getRepoFullName(msg.value.author, msg.key, |
998 | function (err, repoName, authorName) { |
999 | if (err) return cb(err) |
1000 | cb(null, { |
1001 | key: msg.key, |
1002 | value: msg.value, |
1003 | repoName: repoName, |
1004 | authorName: authorName |
1005 | }) |
1006 | }) |
1007 | }, 8) |
1008 | ) |
1009 | } |
1010 | |
1011 | R.serveRepoForks = function (req, repo) { |
1012 | var hasForks |
1013 | var title = req._t('Forks') + ' · %{author}/%{repo}' |
1014 | return this.serveRepoTemplate(req, repo, null, null, title, cat([ |
1015 | pull.once('<h3>' + req._t('Forks') + '</h3>'), |
1016 | pull( |
1017 | this.getForks(repo), |
1018 | pull.map(function (msg) { |
1019 | hasForks = true |
1020 | return '<section class="collapse">' + |
1021 | u.link([msg.value.author], msg.authorName) + ' / ' + |
1022 | u.link([msg.key], msg.repoName) + |
1023 | '<span class="right-bar">' + |
1024 | u.timestamp(msg.value.timestamp, req) + |
1025 | '</span></section>' |
1026 | }) |
1027 | ), |
1028 | u.readOnce(function (cb) { |
1029 | cb(null, hasForks ? '' : req._t('NoForks')) |
1030 | }) |
1031 | ])) |
1032 | } |
1033 | |
1034 | R.serveRepoForkPrompt = function (req, repo) { |
1035 | var title = req._t('Fork') + ' · %{author}/%{repo}' |
1036 | return this.serveRepoTemplate(req, repo, null, null, title, pull.once( |
1037 | '<form action="" method="post" onreset="history.back()">' + |
1038 | '<h3>' + req._t('ForkRepoPrompt') + '</h3>' + |
1039 | '<p>' + u.hiddenInputs({ id: repo.id }) + |
1040 | '<button class="btn open" type="submit" name="action" value="fork">' + |
1041 | req._t('Fork') + |
1042 | '</button>' + |
1043 | ' <button class="btn" type="reset">' + |
1044 | req._t('Cancel') + '</button>' + |
1045 | '</p></form>' |
1046 | )) |
1047 | } |
1048 | |
1049 | R.serveIssueOrPullRequest = function (req, repo, issue, path, id) { |
1050 | return issue.msg.value.content.type == 'pull-request' |
1051 | ? this.pulls.serveRepoPullReq(req, repo, issue, path, id) |
1052 | : this.issues.serveRepoIssue(req, repo, issue, path, id) |
1053 | } |
1054 | |
1055 | function getRepoLastMod(repo, cb) { |
1056 | repo.getState(function (err, state) { |
1057 | if (err) return cb(err) |
1058 | var lastMod = new Date(Math.max.apply(Math, state.refs.map(function (ref) { |
1059 | return ref.link.value.timestamp |
1060 | }))) || new Date() |
1061 | cb(null, lastMod) |
1062 | }) |
1063 | } |
1064 | |
1065 | R.serveRepoRefs = function (req, repo) { |
1066 | var self = this |
1067 | return u.readNext(function (cb) { |
1068 | getRepoLastMod(repo, function (err, lastMod) { |
1069 | if (err) return cb(null, self.web.serveError(req, err, 500)) |
1070 | if (u.ifModifiedSince(req, lastMod)) { |
1071 | return cb(null, pull.once([304])) |
1072 | } |
1073 | repo.getState(function (err, state) { |
1074 | if (err) return cb(null, self.web.serveError(req, err, 500)) |
1075 | var buf = state.refs.sort(function (a, b) { |
1076 | return a.name > b.name ? 1 : a.name < b.name ? -1 : 0 |
1077 | }).map(function (ref) { |
1078 | return ref.hash + '\t' + ref.name + '\n' |
1079 | }).join('') |
1080 | cb(null, pull.values([[200, { |
1081 | 'Content-Type': 'text/plain; charset=utf-8', |
1082 | 'Content-Length': Buffer.byteLength(buf), |
1083 | 'Last-Modified': lastMod.toGMTString() |
1084 | }], buf])) |
1085 | }) |
1086 | }) |
1087 | }) |
1088 | } |
1089 | |
1090 | R.serveRepoObject = function (req, repo, sha1) { |
1091 | var self = this |
1092 | if (!/[0-9a-f]{20}/.test(sha1)) return pull.once([401]) |
1093 | return u.readNext(function (cb) { |
1094 | repo.getObjectFromAny(sha1, function (err, obj) { |
1095 | if (err) return cb(null, pull.once([404])) |
1096 | cb(null, cat([ |
1097 | pull.once([200, { |
1098 | 'Content-Type': 'application/x-git-loose-object', |
1099 | 'Cache-Control': 'max-age=31536000' |
1100 | }]), |
1101 | pull( |
1102 | cat([ |
1103 | pull.values([obj.type, ' ', obj.length.toString(10), '\0']), |
1104 | obj.read |
1105 | ]), |
1106 | toPull(zlib.createDeflate()) |
1107 | ) |
1108 | ])) |
1109 | }) |
1110 | }) |
1111 | } |
1112 | |
1113 | R.serveRepoHead = function (req, repo) { |
1114 | var self = this |
1115 | return u.readNext(function (cb) { |
1116 | repo.getHead(function (err, name) { |
1117 | if (err) return cb(null, pull.once([500])) |
1118 | return cb(null, self.web.serveBuffer(200, 'ref: ' + name)) |
1119 | }) |
1120 | }) |
1121 | } |
1122 | |
1123 | R.serveRepoPacksInfo = function (req, repo) { |
1124 | var self = this |
1125 | return u.readNext(function (cb) { |
1126 | getRepoLastMod(repo, function (err, lastMod) { |
1127 | if (err) return cb(null, self.web.serveError(req, err, 500)) |
1128 | if (u.ifModifiedSince(req, lastMod)) { |
1129 | return cb(null, pull.once([304])) |
1130 | } |
1131 | cb(null, cat([ |
1132 | pull.once([200, { |
1133 | 'Content-Type': 'text/plain; charset=utf-8', |
1134 | 'Last-Modified': lastMod.toGMTString() |
1135 | }]), |
1136 | pull( |
1137 | repo.packs(), |
1138 | pull.map(function (pack) { |
1139 | var sha1 = pack.sha1 |
1140 | if (!sha1) { |
1141 | // make up a sha1 and hope git doesn't notice |
1142 | var packId = new Buffer(pack.packId.substr(1, 44), 'base64') |
1143 | sha1 = packId.slice(0, 20).toString('hex') |
1144 | } |
1145 | return 'P pack-' + sha1 + '.pack\n' |
1146 | }) |
1147 | ) |
1148 | ])) |
1149 | }) |
1150 | }) |
1151 | } |
1152 | |
1153 | R.serveRepoPack = function (req, repo, name) { |
1154 | var m = name.match(/^pack-(.*)\.(pack|idx)$/) |
1155 | if (!m) return pull.once([400]) |
1156 | var hex; |
1157 | try { |
1158 | hex = new Buffer(m[1], 'hex') |
1159 | } catch(e) { |
1160 | return pull.once([400]) |
1161 | } |
1162 | |
1163 | var self = this |
1164 | return u.readNext(function (cb) { |
1165 | pull( |
1166 | repo.packs(), |
1167 | pull.filter(function (pack) { |
1168 | var sha1 = pack.sha1 |
1169 | ? new Buffer(pack.sha1, 'hex') |
1170 | : new Buffer(pack.packId.substr(1, 44), 'base64').slice(0, 20) |
1171 | return sha1.equals(hex) |
1172 | }), |
1173 | pull.take(1), |
1174 | pull.collect(function (err, packs) { |
1175 | if (err) return console.error(err), cb(null, pull.once([500])) |
1176 | if (packs.length < 1) return cb(null, pull.once([404])) |
1177 | var pack = packs[0] |
1178 | |
1179 | if (m[2] === 'pack') { |
1180 | repo.getPackfile(pack.packId, function (err, read) { |
1181 | if (err) return cb(err) |
1182 | cb(null, pull(read, |
1183 | self.web.serveRaw(null, 'application/x-git-packed-objects') |
1184 | )) |
1185 | }) |
1186 | } |
1187 | |
1188 | if (m[2] === 'idx') { |
1189 | repo.getPackIndex(pack.idxId, function (err, read) { |
1190 | if (err) return cb(err) |
1191 | cb(null, pull(read, |
1192 | self.web.serveRaw(null, 'application/x-git-packed-objects-toc') |
1193 | )) |
1194 | }) |
1195 | } |
1196 | }) |
1197 | ) |
1198 | }) |
1199 | } |
1200 |
Built with git-ssb-web