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