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