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