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