Files: bdbeb9c156a4c8b0e0ecd33e2613564c8491ee66 / lib / repos / index.js
45536 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 digsPath = [repo.id, 'digs'] |
282 | |
283 | var done = multicb({ pluck: 1, spread: true }) |
284 | self.web.getRepoName(repo.feed, repo.id, done()) |
285 | self.web.about.getName(repo.feed, done()) |
286 | self.web.getVotes(repo.id, done()) |
287 | |
288 | if (repo.upstream) { |
289 | self.web.getRepoName(repo.upstream.feed, repo.upstream.id, done()) |
290 | self.web.about.getName(repo.upstream.feed, done()) |
291 | } |
292 | |
293 | return u.readNext(function (cb) { |
294 | done(function (err, repoName, authorName, votes, upstreamName, upstreamAuthorName) { |
295 | if (err) return cb(null, self.web.serveError(req, err)) |
296 | var upvoted = votes.upvoters[self.web.myId] > 0 |
297 | var upstreamLink = !repo.upstream ? '' : |
298 | u.link([repo.upstream]) |
299 | var title = titleTemplate ? titleTemplate |
300 | .replace(/%\{repo\}/g, repoName) |
301 | .replace(/%\{author\}/g, authorName) |
302 | : (authorName ? authorName + '/' : '') + repoName |
303 | var isPublic = self.web.isPublic |
304 | var isLocal = !isPublic |
305 | cb(null, self.web.serveTemplate(req, title)(cat([ |
306 | h('div', {class: 'repo-title'}, [ |
307 | h('form', {class: 'right-bar', action: '', method: 'post'}, [ |
308 | h('strong', {class: 'ml2 mr1'}, u.link(digsPath, votes.upvotes)), |
309 | h('button', |
310 | extend( |
311 | {class: 'btn', name: 'action', value: 'vote'}, |
312 | isPublic ? {disabled: 'disabled'} : {type: 'submit'} |
313 | ), [ |
314 | h('i', '✌ '), |
315 | h('span', req._t(isLocal && upvoted ? 'Undig' : 'Dig')) |
316 | ] |
317 | ), |
318 | u.when(isLocal, () => cat([ |
319 | h('input', {type: 'hidden', name: 'value', value: (upvoted ? '0' : '1')}), |
320 | h('input', {type: 'hidden', name: 'id', value: u.escape(repo.id)}) |
321 | ])), |
322 | h('a', {href: u.encodeLink([repo.id, 'forks']), title: req._t('Forks'), class: 'ml2 mr1'}, '+'), |
323 | u.when(isLocal, () => |
324 | h('button', {class: 'btn', type: 'submit', name: 'action', value: 'fork-prompt'}, [ |
325 | h('i', '⑂ '), |
326 | once(req._t('Fork')) |
327 | ]) |
328 | ) |
329 | ]), |
330 | forms.name(req, isLocal, repo.id, repoName, 'repo-name', null, req._t('repo.Rename'), |
331 | h('h2', {class: 'bgslash'}, |
332 | (authorName ? u.link([repo.feed], authorName, false, 'class="repo-author"') + ' / ' : '') + |
333 | u.link([repo.id], repoName) + |
334 | (repo.private ? ' ' + u.privateIcon(req) : '')) |
335 | ), |
336 | ]), |
337 | u.when(repo.upstream, () => |
338 | h('small', {class: 'bgslash'}, req._t('ForkedFrom', { |
339 | repo: `${u.link([repo.upstream.feed], upstreamAuthorName)} / ${u.link([repo.upstream.id], upstreamName)}` |
340 | })) |
341 | ), |
342 | u.nav([ |
343 | [[repo.id], req._t('Code'), 'code'], |
344 | [[repo.id, 'activity'], req._t('Activity'), 'activity'], |
345 | [[repo.id, 'commits', branch||''], req._t('Commits'), 'commits'], |
346 | [[repo.id, 'issues'], self.web.indexCache ? req._t('IssuesN', { |
347 | count: self.web.indexCache.getIssuesCount(repo.id, '…') |
348 | }) : req._t('Issues'), 'issues'], |
349 | [[repo.id, 'pulls'], self.web.indexCache ? req._t('PullRequestsN', { |
350 | count: self.web.indexCache.getPRsCount(repo.id, '…') |
351 | }) : req._t('PullRequests'), 'pulls'] |
352 | ], page), |
353 | body |
354 | ]) |
355 | )) |
356 | }) |
357 | }) |
358 | } |
359 | |
360 | R.renderEmptyRepo = function (req, repo) { |
361 | if (repo.feed != this.web.myId) |
362 | return h('section', [ |
363 | h('h3', req._t('EmptyRepo')) |
364 | ]) |
365 | |
366 | var gitUrl = 'ssb://' + repo.id |
367 | return h('section', [ |
368 | h('h3', req._t('initRepo.GettingStarted')), |
369 | h('h4', req._t('initRepo.CreateNew')), |
370 | preInitRepo(req, gitUrl), |
371 | h('h4', req._t('initRepo.PushExisting')), |
372 | preRemote(gitUrl) |
373 | ]) |
374 | } |
375 | |
376 | var preInitRepo = (req, gitUrl) => h('pre', |
377 | `touch ${req._t('initRepo.README')}.md |
378 | git init |
379 | git add ${req._t('initRepo.README')}.md |
380 | git commit -m ${req._t('initRepo.InitialCommit')} |
381 | git remote add origin ${gitUrl} |
382 | git push -u origin master`) |
383 | |
384 | var preRemote = (gitUrl) => h('pre', |
385 | `git remote add origin ${gitUrl} |
386 | git push -u origin master`) |
387 | |
388 | |
389 | R.serveRepoTree = function (req, repo, rev, path) { |
390 | var type = repo.isCommitHash(rev) ? 'Tree' : 'Branch' |
391 | var title = |
392 | (path.length ? `${path.join('/')} · ` : '') + |
393 | '%{author}/%{repo}' + |
394 | (repo.head == `refs/heads/${rev}` ? '' : `@${rev}`) |
395 | var gitUrl = 'ssb://' + repo.id |
396 | var host = req.headers.host || '127.0.0.1:7718' |
397 | var targetpath = '/' + encodeURIComponent(repo.id) |
398 | var httpUrl = 'http://' + encodeURI(host) + targetpath |
399 | var cloneUrls = '<div class="clone-urls">' + |
400 | '<select class="custom-dropdown clone-url-protocol" ' + |
401 | 'onchange="with(this.nextSibling.firstChild) {' + |
402 | 'value = this.value; select() }">' + |
403 | '<option selected="selected" value="' + gitUrl + '">SSB</option>' + |
404 | '<option class="http-clone-url" value="' + httpUrl + '">HTTP</option>' + |
405 | '</select>' + |
406 | '<div class="clone-url-wrapper">' + |
407 | '<input class="clone-url" readonly="readonly" ' + |
408 | 'value="ssb://' + repo.id + '" size="45" ' + |
409 | 'onclick="this.select()"/>' + |
410 | '<script>' + |
411 | 'var httpOpt = document.querySelector(".http-clone-url")\n' + |
412 | 'if (location.protocol === "https:") httpOpt.text = "HTTPS"\n' + |
413 | 'httpOpt.value = location.origin + "' + targetpath + '"\n' + |
414 | '</script>' + |
415 | '</div>' + |
416 | '</div>' |
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('div', {class: 'rev-menu-line'}, [ |
427 | h('span', `${req._t(type)}: `), |
428 | this.revMenu(req, repo, rev) |
429 | ]), |
430 | cloneUrls |
431 | ]), |
432 | u.when(numSkipped > 0, () => |
433 | h('div', {class: 'missing-blobs-warning mt2'}, |
434 | h('em', req._t('missingBlobsWarning', numSkipped)) |
435 | ) |
436 | ) |
437 | ]), |
438 | h('section', {class: 'files'}, renderRepoTree(req, repo, revGot, path, type)), |
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 | '<span>' + |
624 | req._t(actionKey, { |
625 | name: u.escape(commit[actor].name), |
626 | commitName: u.link(commitPath, commit.title) |
627 | }) + |
628 | '</span>' + |
629 | '<span class="float-right">' + |
630 | req._t('LatestOn', { |
631 | commitId: commit.id.slice(0, 7), |
632 | date: commit[actor].date.toLocaleString(req._locale) |
633 | }) + |
634 | '</span>' |
635 | ) |
636 | }) |
637 | }) |
638 | } |
639 | |
640 | // breadcrumbs |
641 | function linkPath(basePath, path) { |
642 | path = path.slice() |
643 | var last = path.pop() |
644 | return path.map(function (dir, i) { |
645 | return u.link(basePath.concat(path.slice(0, i+1)), dir) |
646 | }).concat(last).join(' / ') |
647 | } |
648 | |
649 | function renderRepoTree(req, repo, rev, path, type) { |
650 | var source = repo.readDir(rev,path) |
651 | var pathLinks = path.length === 0 ? '' : |
652 | ': ' + linkPath([repo.id, 'tree'], [rev].concat(path)) |
653 | |
654 | var location = once('') |
655 | if (path.length !== 0) { |
656 | var link = linkPath([repo.id, 'tree'], [rev].concat(path)) |
657 | location = h('div', {class: 'fileLocation'}, `${req._t('Files')}: ${link}`) |
658 | } |
659 | |
660 | return cat([ |
661 | location, |
662 | h('table', {class: "files w-100", cellspacing: "0"}, cat([ |
663 | u.when(type === 'Branch', () => |
664 | h('thead', h('tr', h('td', {colspan: '2'}, [ |
665 | renderRepoLatest(req, repo, rev) |
666 | ]))) |
667 | ), |
668 | u.sourceMap(source, file => |
669 | h('tr', [ |
670 | h('td', [ |
671 | h('i', fileIcon(file)) |
672 | ]), |
673 | h('td', u.link(filePath(file), file.name)) |
674 | ]) |
675 | ) |
676 | ])) |
677 | ]) |
678 | |
679 | function fileIcon(file) { |
680 | return fileType(file) === 'tree' ? '📁' : '📄' |
681 | } |
682 | |
683 | function filePath(file) { |
684 | var type = fileType(file) |
685 | return [repo.id, type, rev].concat(path, file.name) |
686 | } |
687 | |
688 | function fileType(file) { |
689 | if (file.mode === 040000) return 'tree' |
690 | else if (file.mode === 0160000) return 'commit' |
691 | else return 'blob' |
692 | } |
693 | } |
694 | |
695 | /* Repo readme */ |
696 | |
697 | R.renderRepoReadme = function (req, repo, branch, path) { |
698 | var self = this |
699 | return u.readNext(function (cb) { |
700 | pull( |
701 | repo.readDir(branch, path), |
702 | pull.filter(function (file) { |
703 | return /readme(\.|$)/i.test(file.name) |
704 | }), |
705 | pull.take(1), |
706 | pull.collect(function (err, files) { |
707 | if (err) return cb(null, pull.empty()) |
708 | var file = files[0] |
709 | if (!file) |
710 | return cb(null, pull.once(path.length ? '' : |
711 | '<p>' + req._t('NoReadme') + '</p>')) |
712 | repo.getObjectFromAny(file.id, function (err, obj) { |
713 | if (err) return cb(err) |
714 | cb(null, cat([ |
715 | pull.once('<section class="readme">' + |
716 | '<div class="readme-filename">' + file.name + '</div>'), |
717 | self.web.renderObjectData(obj, file.name, repo, branch, path), |
718 | pull.once('</section>') |
719 | ])) |
720 | }) |
721 | }) |
722 | ) |
723 | }) |
724 | } |
725 | |
726 | /* Repo commit */ |
727 | |
728 | R.serveRepoCommit = function (req, repo, rev, filePath) { |
729 | // TODO: use filePath argument |
730 | var self = this |
731 | return u.readNext(function (cb) { |
732 | repo.getCommitParsed(rev, function (err, commit) { |
733 | if (err) return cb(null, |
734 | self.serveRepoTemplate(req, repo, null, rev, `%{author}/%{repo}@${rev}`, |
735 | pull.once(self.web.renderError(err)))) |
736 | getObjectMsgId(repo, commit.id, function (err, objMsgId) { |
737 | if (err) return cb(null, |
738 | self.serveRepoTemplate(req, repo, null, rev, `%{author}/%{repo}@${rev}`, |
739 | pull.once(self.web.renderError(err)))) |
740 | var commitPath = [repo.id, 'commit', commit.id] |
741 | var treePath = [repo.id, 'tree', commit.id] |
742 | var title = u.escape(commit.title) + ' · ' + |
743 | '%{author}/%{repo}@' + commit.id.substr(0, 8) |
744 | cb(null, self.serveRepoTemplate(req, repo, null, rev, title, cat([ |
745 | pull.once( |
746 | '<h3>' + u.link(commitPath, |
747 | req._t('CommitRev', {rev: rev})) + '</h3>' + |
748 | '<section class="collapse">' + |
749 | '<div class="right-bar">' + |
750 | u.link(treePath, req._t('BrowseFiles')) + |
751 | '</div>' + |
752 | '<h4>' + u.linkify(u.escape(commit.title)) + '</h4>' + |
753 | (commit.body ? u.linkify(u.pre(commit.body)) : '') + |
754 | (commit.separateAuthor ? req._t('AuthoredOn', { |
755 | name: u.escape(commit.author.name), |
756 | date: commit.author.date.toLocaleString(req._locale) |
757 | }) + '<br/>' : '') + |
758 | req._t('CommittedOn', { |
759 | name: u.escape(commit.committer.name), |
760 | date: commit.committer.date.toLocaleString(req._locale) |
761 | }) + '<br/>' + |
762 | commit.parents.map(function (id) { |
763 | return req._t('Parent') + ': ' + |
764 | u.link([repo.id, 'commit', id], id) |
765 | }).join('<br>') + |
766 | '</section>' + |
767 | '<section><h3>' + req._t('FilesChanged') + '</h3>'), |
768 | // TODO: show diff from all parents (merge commits) |
769 | self.renderDiffStat(req, [repo, repo], [commit.parents[0], commit.id], commit.id, objMsgId), |
770 | pull.once('</section>') |
771 | ]))) |
772 | }) |
773 | }) |
774 | }) |
775 | } |
776 | |
777 | /* Repo tag */ |
778 | |
779 | R.serveRepoTag = function (req, repo, rev, path) { |
780 | var self = this |
781 | return u.readNext(function (cb) { |
782 | repo.getTagParsed(rev, function (err, tag) { |
783 | if (err) { |
784 | if (/Expected tag, got commit/.test(err.message)) { |
785 | req._u.pathname = u.encodeLink([repo.id, 'commit', rev].concat(path)) |
786 | return cb(null, self.web.serveRedirect(req, url.format(req._u))) |
787 | } |
788 | return cb(null, self.web.serveError(req, err)) |
789 | } |
790 | |
791 | var title = req._t('TagName', { |
792 | tag: u.escape(tag.tag) |
793 | }) + ' · %{author}/%{repo}' |
794 | var body = (tag.title + '\n\n' + |
795 | tag.body.replace(/-----BEGIN PGP SIGNATURE-----\n[^.]*?\n-----END PGP SIGNATURE-----\s*$/, '')).trim() |
796 | var date = tag.tagger.date |
797 | cb(null, self.serveRepoTemplate(req, repo, 'tags', tag.object, title, |
798 | pull.once( |
799 | '<section class="collapse">' + |
800 | '<h3>' + u.link([repo.id, 'tag', rev], tag.tag) + '</h3>' + |
801 | req._t('TaggedOn', { |
802 | name: u.escape(tag.tagger.name), |
803 | date: date && date.toLocaleString(req._locale) |
804 | }) + '<br/>' + |
805 | u.link([repo.id, tag.type, tag.object]) + |
806 | u.linkify(u.pre(body)) + |
807 | '</section>'))) |
808 | }) |
809 | }) |
810 | } |
811 | |
812 | |
813 | /* Diff stat */ |
814 | |
815 | R.renderDiffStat = function (req, repos, treeIds, commit, updateId) { |
816 | var self = this |
817 | if (treeIds.length == 0) treeIds = [null] |
818 | var id = treeIds[0] |
819 | var lastI = treeIds.length - 1 |
820 | var oldTree = treeIds[0] |
821 | var changedFiles = [] |
822 | var source = GitRepo.diffTrees(repos, treeIds, true) |
823 | |
824 | return cat([ |
825 | h('table', u.sourceMap(source, item => { |
826 | var filename = u.escape(item.filename = item.path.join('/')) |
827 | var oldId = item.id && item.id[0] |
828 | var newId = item.id && item.id[lastI] |
829 | var oldMode = item.mode && item.mode[0] |
830 | var newMode = item.mode && item.mode[lastI] |
831 | var action = |
832 | !oldId && newId ? req._t('action.added') : |
833 | oldId && !newId ? req._t('action.deleted') : |
834 | oldMode != newMode ? req._t('action.changedMode', { |
835 | old: oldMode.toString(8), |
836 | new: newMode.toString(8) |
837 | }) : req._t('changed') |
838 | if (item.id) |
839 | changedFiles.push(item) |
840 | var blobsPath = item.id[1] |
841 | ? [repos[1].id, 'blob', treeIds[1]] |
842 | : [repos[0].id, 'blob', treeIds[0]] |
843 | var rawsPath = item.id[1] |
844 | ? [repos[1].id, 'raw', treeIds[1]] |
845 | : [repos[0].id, 'raw', treeIds[0]] |
846 | item.blobPath = blobsPath.concat(item.path) |
847 | item.rawPath = rawsPath.concat(item.path) |
848 | var fileHref = item.id ? |
849 | '#' + encodeURIComponent(item.path.join('/')) : |
850 | u.encodeLink(item.blobPath) |
851 | |
852 | return h('tr', [ |
853 | h('td', [ |
854 | h('a', {href: fileHref}, filename) |
855 | ]), |
856 | h('td', action) |
857 | ]) |
858 | })), |
859 | pull( |
860 | pull.values(changedFiles), |
861 | paramap(function (item, cb) { |
862 | var extension = u.getExtension(item.filename) |
863 | if (extension in u.imgMimes) { |
864 | var filename = u.escape(item.filename) |
865 | return cb(null, |
866 | '<pre><table class="code">' + |
867 | '<tr><th id="' + u.escape(item.filename) + '">' + |
868 | filename + '</th></tr>' + |
869 | '<tr><td><img src="' + u.encodeLink(item.rawPath) + '"' + |
870 | ' alt="' + filename + '"/></td></tr>' + |
871 | '</table></pre>') |
872 | } |
873 | var done = multicb({ pluck: 1, spread: true }) |
874 | var mode0 = item.mode && item.mode[0] |
875 | var modeI = item.mode && item.mode[lastI] |
876 | var isSubmodule = (modeI == 0160000) |
877 | var repo = repos[1] |
878 | getRepoObjectString(repos[0], item.id[0], mode0, done()) |
879 | getRepoObjectString(repos[1], item.id[lastI], modeI, done()) |
880 | self.getLineCommentThreads(req, repo, updateId, commit, item.filename, done()) |
881 | done(function (err, strOld, strNew, lineCommentThreads) { |
882 | if (err) return cb(err) |
883 | cb(null, htmlLineDiff(req, repo, updateId, commit, item.filename, item.filename, |
884 | strOld, strNew, |
885 | u.encodeLink(item.blobPath), !isSubmodule, lineCommentThreads)) |
886 | }) |
887 | }, 4) |
888 | ) |
889 | ]) |
890 | } |
891 | |
892 | function htmlLineDiff(req, repo, updateId, commit, filename, anchor, oldStr, newStr, blobHref, |
893 | showViewLink, lineCommentThreads) { |
894 | return '<div class="code-wrap"><table class="code">' + |
895 | '<tr><th colspan=3 id="' + u.escape(anchor) + '">' + filename + |
896 | (showViewLink === false ? '' : |
897 | '<span class="right-bar">' + |
898 | '<a href="' + blobHref + '">' + req._t('View') + '</a> ' + |
899 | '</span>') + |
900 | '</th></tr>' + |
901 | (oldStr.length + newStr.length > 200000 |
902 | ? '<tr><td class="diff-info" colspan=3>' + req._t('diff.TooLarge') + '<br>' + |
903 | req._t('diff.OldFileSize', {bytes: oldStr.length}) + '<br>' + |
904 | req._t('diff.NewFileSize', {bytes: newStr.length}) + '</td></tr>' |
905 | : tableDiff(req, repo, updateId, commit, oldStr, newStr, filename, lineCommentThreads)) + |
906 | '</table></div>' |
907 | } |
908 | |
909 | function tableDiff(req, repo, updateId, commit, oldStr, newStr, filename, lineCommentThreads) { |
910 | var query = req._u.query |
911 | var diff = JsDiff.structuredPatch('', '', oldStr, newStr) |
912 | var groups = diff.hunks.map(function (hunk) { |
913 | var oldLine = hunk.oldStart |
914 | var newLine = hunk.newStart |
915 | var header = '<tr class="diff-hunk-header"><td colspan=2></td><td>' + |
916 | '@@ -' + oldLine + ',' + hunk.oldLines + ' ' + |
917 | '+' + newLine + ',' + hunk.newLines + ' @@' + |
918 | '</td></tr>' |
919 | return [header].concat(hunk.lines.map(function (line) { |
920 | var s = line[0] |
921 | if (s == '\\') return |
922 | var html = u.highlight(line, u.getExtension(filename)) |
923 | var trClass = s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : '' |
924 | var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++] |
925 | var id = [filename].concat(lineNums).join('-') |
926 | var newLineNum = lineNums[lineNums.length-1] |
927 | return '<tr id="' + u.escape(id) + '" class="' + trClass + '">' + |
928 | lineNums.map(function (num, i) { |
929 | var idEnc = encodeURIComponent(id) |
930 | return '<td class="code-linenum">' + |
931 | (num ? '<a href="#' + idEnc + '">' + |
932 | num + '</a>' + |
933 | (updateId && i === lineNums.length-1 && s !== '-' ? |
934 | // TODO: use a more descriptive icon for the comment action |
935 | ' <a href="?comment=' + idEnc + '#' + idEnc + '">…</a>' |
936 | : '') |
937 | : '') + '</td>' |
938 | }).join('') + |
939 | '<td class="code-text">' + html + '</td></tr>' + |
940 | (lineCommentThreads[newLineNum] ? |
941 | '<tr><td colspan=4>' + |
942 | lineCommentThreads[newLineNum] + |
943 | '</td></tr>' |
944 | : commit && query.comment === id ? |
945 | '<tr><td colspan=4>' + |
946 | forms.lineComment(req, repo, updateId, commit, filename, newLineNum) + |
947 | '</td></tr>' |
948 | : '') |
949 | })) |
950 | }) |
951 | return [].concat.apply([], groups).join('') |
952 | } |
953 | |
954 | /* An unknown message linking to a repo */ |
955 | |
956 | R.serveRepoSomething = function (req, repo, id, msg, path) { |
957 | return this.serveRepoTemplate(req, repo, null, null, null, |
958 | pull.once('<section><h3>' + u.link([id]) + '</h3>' + |
959 | u.json(msg) + '</section>')) |
960 | } |
961 | |
962 | /* Repo update */ |
963 | |
964 | function objsArr(objs) { |
965 | return Array.isArray(objs) ? objs : |
966 | Object.keys(objs).map(function (sha1) { |
967 | var obj = Object.create(objs[sha1]) |
968 | obj.sha1 = sha1 |
969 | return obj |
970 | }) |
971 | } |
972 | |
973 | R.serveRepoUpdate = function (req, repo, msg, path) { |
974 | var self = this |
975 | var raw = req._u.query.raw != null |
976 | var title = req._t('Update') + ' · %{author}/%{repo}' |
977 | var c = msg.value.content |
978 | |
979 | if (raw) |
980 | return self.serveRepoTemplate(req, repo, 'activity', null, title, pull.once( |
981 | '<a href="?" class="raw-link header-align">' + |
982 | req._t('Info') + '</a>' + |
983 | '<h3>' + req._t('Update') + '</h3>' + |
984 | '<section class="collapse">' + |
985 | u.json(msg) + '</section>')) |
986 | |
987 | // convert packs to old single-object style |
988 | if (c.indexes) { |
989 | for (var i = 0; i < c.indexes.length; i++) { |
990 | c.packs[i] = { |
991 | pack: {link: c.packs[i].link}, |
992 | idx: c.indexes[i] |
993 | } |
994 | } |
995 | } |
996 | |
997 | var commits = cat([ |
998 | c.objects && pull( |
999 | pull.values(c.objects), |
1000 | pull.filter(function (obj) { return obj.type == 'commit' }), |
1001 | paramap(function (obj, cb) { |
1002 | self.web.getBlob(req, obj.link || obj.key, function (err, readObject) { |
1003 | if (err) return cb(err) |
1004 | GitRepo.getCommitParsed({read: readObject}, cb) |
1005 | }) |
1006 | }, 8) |
1007 | ), |
1008 | c.packs && pull( |
1009 | pull.values(c.packs), |
1010 | paramap(function (pack, cb) { |
1011 | var done = multicb({ pluck: 1, spread: true }) |
1012 | self.web.getBlob(req, pack.pack.link, done()) |
1013 | self.web.getBlob(req, pack.idx.link, done()) |
1014 | done(function (err, readPack, readIdx) { |
1015 | if (err) return cb(self.web.renderError(err)) |
1016 | cb(null, gitPack.decodeWithIndex(repo, readPack, readIdx)) |
1017 | }) |
1018 | }, 4), |
1019 | pull.flatten(), |
1020 | pull.asyncMap(function (obj, cb) { |
1021 | if (obj.type == 'commit') |
1022 | GitRepo.getCommitParsed(obj, cb) |
1023 | else |
1024 | pull(obj.read, pull.drain(null, cb)) |
1025 | }), |
1026 | pull.filter() |
1027 | ) |
1028 | ]) |
1029 | |
1030 | return self.serveRepoTemplate(req, repo, 'activity', null, title, cat([ |
1031 | pull.once('<a href="?raw" class="raw-link header-align">' + |
1032 | req._t('Data') + '</a>' + |
1033 | '<h3>' + req._t('Update') + '</h3>'), |
1034 | pull( |
1035 | pull.once(msg), |
1036 | pull.asyncMap(renderRepoUpdate.bind(self, req, repo, true)) |
1037 | ), |
1038 | (c.objects || c.packs) && |
1039 | pull.once('<h3>' + req._t('Commits') + '</h3>'), |
1040 | pull(commits, pull.map(function (commit) { |
1041 | return renderCommit(req, repo, commit) |
1042 | })) |
1043 | ])) |
1044 | } |
1045 | |
1046 | /* Blob */ |
1047 | |
1048 | R.serveRepoBlob = function (req, repo, rev, path) { |
1049 | var self = this |
1050 | return u.readNext(function (cb) { |
1051 | repo.getFile(rev, path, function (err, object) { |
1052 | if (err) return cb(null, self.web.serveBlobNotFound(req, repo.id, err)) |
1053 | var type = repo.isCommitHash(rev) ? 'Tree' : 'Branch' |
1054 | var pathLinks = path.length === 0 ? '' : |
1055 | ': ' + linkPath([repo.id, 'tree'], [rev].concat(path)) |
1056 | var rawFilePath = [repo.id, 'raw', rev].concat(path) |
1057 | var dirPath = path.slice(0, path.length-1) |
1058 | var filename = path[path.length-1] |
1059 | var extension = u.getExtension(filename) |
1060 | var title = (path.length ? path.join('/') + ' · ' : '') + |
1061 | '%{author}/%{repo}' + |
1062 | (repo.head == 'refs/heads/' + rev ? '' : '@' + rev) |
1063 | cb(null, self.serveRepoTemplate(req, repo, 'code', rev, title, cat([ |
1064 | pull.once('<section><form action="" method="get">' + |
1065 | '<h3>' + req._t(type) + ': ' + rev + ' '), |
1066 | self.revMenu(req, repo, rev), |
1067 | pull.once('</h3></form>'), |
1068 | type == 'Branch' && renderRepoLatest(req, repo, rev), |
1069 | pull.once('</section><section class="collapse">' + |
1070 | '<h3>' + req._t('Files') + pathLinks + '</h3>' + |
1071 | '<div>' + object.length + ' bytes' + |
1072 | '<span class="raw-link">' + |
1073 | u.link(rawFilePath, req._t('Raw')) + '</span>' + |
1074 | '</div></section>' + |
1075 | '<section>'), |
1076 | extension in u.imgMimes |
1077 | ? pull.once('<img src="' + u.encodeLink(rawFilePath) + |
1078 | '" alt="' + u.escape(filename) + '" />') |
1079 | : self.web.renderObjectData(object, filename, repo, rev, dirPath), |
1080 | pull.once('</section>') |
1081 | ]))) |
1082 | }) |
1083 | }) |
1084 | } |
1085 | |
1086 | /* Raw blob */ |
1087 | |
1088 | R.serveRepoRaw = function (req, repo, branch, path) { |
1089 | var self = this |
1090 | return u.readNext(function (cb) { |
1091 | repo.getFile(branch, path, function (err, object) { |
1092 | if (err) return cb(null, |
1093 | self.web.serveBuffer(404, req._t('error.BlobNotFound'))) |
1094 | var extension = u.getExtension(path[path.length-1]) |
1095 | var contentType = u.imgMimes[extension] |
1096 | cb(null, pull(object.read, self.web.serveRaw(object.length, contentType))) |
1097 | }) |
1098 | }) |
1099 | } |
1100 | |
1101 | /* Digs */ |
1102 | |
1103 | R.serveRepoDigs = function serveRepoDigs (req, repo) { |
1104 | var self = this |
1105 | return u.readNext(cb => { |
1106 | var title = req._t('Digs') + ' · %{author}/%{repo}' |
1107 | self.web.getVotes(repo.id, (err, votes) => { |
1108 | cb(null, self.serveRepoTemplate(req, repo, null, null, title, |
1109 | h('section', [ |
1110 | h('h3', req._t('Digs')), |
1111 | h('div', `${req._t('Total')}: ${votes.upvotes}`), |
1112 | h('ul', u.paraSourceMap(Object.keys(votes.upvoters), (feedId, cb) => { |
1113 | self.web.about.getName(feedId, (err, name) => { |
1114 | cb(null, h('li', u.link([feedId], name))) |
1115 | }) |
1116 | })) |
1117 | ]) |
1118 | )) |
1119 | }) |
1120 | }) |
1121 | } |
1122 | |
1123 | /* Forks */ |
1124 | |
1125 | R.getForks = function (repo, includeSelf) { |
1126 | var self = this |
1127 | return pull( |
1128 | cat([ |
1129 | includeSelf && pull.once(repo.id), |
1130 | // get downstream repos |
1131 | pull( |
1132 | self.web.ssb.backlinks ? self.web.ssb.backlinks.read({ |
1133 | query: [ |
1134 | {$filter: { |
1135 | dest: repo.id, |
1136 | value: { |
1137 | content: { |
1138 | type: 'git-repo', |
1139 | upstream: repo.id, |
1140 | } |
1141 | } |
1142 | }}, |
1143 | {$map: 'key'} |
1144 | ] |
1145 | }) : pull( |
1146 | self.web.ssb.links({ |
1147 | dest: repo.id, |
1148 | rel: 'upstream' |
1149 | }), |
1150 | pull.map('key') |
1151 | ) |
1152 | ), |
1153 | // look for other repos that previously had pull requests to this one |
1154 | pull( |
1155 | self.web.ssb.backlinks ? self.web.ssb.backlinks.read({ |
1156 | query: [ |
1157 | {$filter: { |
1158 | dest: repo.id, |
1159 | value: { |
1160 | content: { |
1161 | type: 'pull-request', |
1162 | project: repo.id, |
1163 | } |
1164 | } |
1165 | }} |
1166 | ] |
1167 | }) : pull( |
1168 | self.web.ssb.links({ |
1169 | dest: repo.id, |
1170 | values: true, |
1171 | rel: 'project' |
1172 | }), |
1173 | u.decryptMessages(self.web.ssb), |
1174 | pull.filter(function (msg) { |
1175 | var c = msg && msg.value && msg.value.content |
1176 | return c && c.type == 'pull-request' |
1177 | }) |
1178 | ), |
1179 | pull.map(function (msg) { return msg.value.content.head_repo }) |
1180 | ) |
1181 | ]), |
1182 | pull.unique(), |
1183 | paramap(function (key, cb) { |
1184 | if (key && key[0] === '#') return cb(null, {key: key, value: { |
1185 | content: { |
1186 | type: 'git-repo', |
1187 | } |
1188 | }}) |
1189 | self.web.getMsg(key, cb) |
1190 | }, 4), |
1191 | u.decryptMessages(self.web.ssb), |
1192 | pull.filter(function (msg) { |
1193 | var c = msg && msg.value && msg.value.content |
1194 | return c && c.type == 'git-repo' |
1195 | }), |
1196 | paramap(function (msg, cb) { |
1197 | self.web.getRepoFullName(msg.value.author, msg.key, |
1198 | function (err, repoName, authorName) { |
1199 | if (err) return cb(err) |
1200 | cb(null, { |
1201 | key: msg.key, |
1202 | value: msg.value, |
1203 | repoName: repoName, |
1204 | authorName: authorName |
1205 | }) |
1206 | }) |
1207 | }, 8) |
1208 | ) |
1209 | } |
1210 | |
1211 | R.serveRepoForks = function (req, repo) { |
1212 | var hasForks |
1213 | var title = req._t('Forks') + ' · %{author}/%{repo}' |
1214 | return this.serveRepoTemplate(req, repo, null, null, title, cat([ |
1215 | pull.once('<h3>' + req._t('Forks') + '</h3>'), |
1216 | pull( |
1217 | this.getForks(repo), |
1218 | pull.map(function (msg) { |
1219 | hasForks = true |
1220 | return '<section class="collapse">' + |
1221 | u.link([msg.value.author], msg.authorName) + ' / ' + |
1222 | u.link([msg.key], msg.repoName) + |
1223 | '<span class="right-bar">' + |
1224 | u.timestamp(msg.value.timestamp, req) + |
1225 | '</span></section>' |
1226 | }) |
1227 | ), |
1228 | u.readOnce(function (cb) { |
1229 | cb(null, hasForks ? '' : req._t('NoForks')) |
1230 | }) |
1231 | ])) |
1232 | } |
1233 | |
1234 | R.serveRepoForkPrompt = function (req, repo) { |
1235 | var title = req._t('Fork') + ' · %{author}/%{repo}' |
1236 | return this.serveRepoTemplate(req, repo, null, null, title, pull.once( |
1237 | '<form action="" method="post" onreset="history.back()">' + |
1238 | '<h3>' + req._t('ForkRepoPrompt') + '</h3>' + |
1239 | '<p>' + u.hiddenInputs({ id: repo.id }) + |
1240 | '<button class="btn open" type="submit" name="action" value="fork">' + |
1241 | req._t('Fork') + |
1242 | '</button>' + |
1243 | ' <button class="btn" type="reset">' + |
1244 | req._t('Cancel') + '</button>' + |
1245 | '</p></form>' |
1246 | )) |
1247 | } |
1248 | |
1249 | R.serveIssueOrPullRequest = function (req, repo, issue, path, id) { |
1250 | return issue.msg.value.content.type == 'pull-request' |
1251 | ? this.pulls.serveRepoPullReq(req, repo, issue, path, id) |
1252 | : this.issues.serveRepoIssue(req, repo, issue, path, id) |
1253 | } |
1254 | |
1255 | function getRepoLastMod(repo, cb) { |
1256 | repo.getState(function (err, state) { |
1257 | if (err) return cb(err) |
1258 | var lastMod = new Date(Math.max.apply(Math, state.refs.map(function (ref) { |
1259 | return ref.link.value.timestamp |
1260 | }))) || new Date() |
1261 | cb(null, lastMod) |
1262 | }) |
1263 | } |
1264 | |
1265 | R.serveRepoRefs = function (req, repo) { |
1266 | var self = this |
1267 | return u.readNext(function (cb) { |
1268 | getRepoLastMod(repo, function (err, lastMod) { |
1269 | if (err) return cb(null, self.web.serveError(req, err, 500)) |
1270 | if (u.ifModifiedSince(req, lastMod)) { |
1271 | return cb(null, pull.once([304])) |
1272 | } |
1273 | repo.getState(function (err, state) { |
1274 | if (err) return cb(null, self.web.serveError(req, err, 500)) |
1275 | var buf = state.refs.sort(function (a, b) { |
1276 | return a.name > b.name ? 1 : a.name < b.name ? -1 : 0 |
1277 | }).map(function (ref) { |
1278 | return ref.hash + '\t' + ref.name + '\n' |
1279 | }).join('') |
1280 | cb(null, pull.values([[200, { |
1281 | 'Content-Type': 'text/plain; charset=utf-8', |
1282 | 'Content-Length': Buffer.byteLength(buf), |
1283 | 'Last-Modified': lastMod.toGMTString() |
1284 | }], buf])) |
1285 | }) |
1286 | }) |
1287 | }) |
1288 | } |
1289 | |
1290 | R.serveRepoObject = function (req, repo, sha1) { |
1291 | var self = this |
1292 | if (!/[0-9a-f]{20}/.test(sha1)) return pull.once([401]) |
1293 | return u.readNext(function (cb) { |
1294 | repo.getObjectFromAny(sha1, function (err, obj) { |
1295 | if (err) return cb(null, pull.once([404])) |
1296 | cb(null, cat([ |
1297 | pull.once([200, { |
1298 | 'Content-Type': 'application/x-git-loose-object', |
1299 | 'Cache-Control': 'max-age=31536000' |
1300 | }]), |
1301 | pull( |
1302 | cat([ |
1303 | pull.values([obj.type, ' ', obj.length.toString(10), '\0']), |
1304 | obj.read |
1305 | ]), |
1306 | toPull(zlib.createDeflate()) |
1307 | ) |
1308 | ])) |
1309 | }) |
1310 | }) |
1311 | } |
1312 | |
1313 | R.serveRepoHead = function (req, repo) { |
1314 | var self = this |
1315 | return u.readNext(function (cb) { |
1316 | repo.getHead(function (err, name) { |
1317 | if (err) return cb(null, pull.once([500])) |
1318 | return cb(null, self.web.serveBuffer(200, 'ref: ' + name)) |
1319 | }) |
1320 | }) |
1321 | } |
1322 | |
1323 | R.serveRepoPacksInfo = function (req, repo) { |
1324 | var self = this |
1325 | return u.readNext(function (cb) { |
1326 | getRepoLastMod(repo, function (err, lastMod) { |
1327 | if (err) return cb(null, self.web.serveError(req, err, 500)) |
1328 | if (u.ifModifiedSince(req, lastMod)) { |
1329 | return cb(null, pull.once([304])) |
1330 | } |
1331 | cb(null, cat([ |
1332 | pull.once([200, { |
1333 | 'Content-Type': 'text/plain; charset=utf-8', |
1334 | 'Last-Modified': lastMod.toGMTString() |
1335 | }]), |
1336 | pull( |
1337 | repo.packs(), |
1338 | pull.map(function (pack) { |
1339 | var sha1 = pack.sha1 |
1340 | if (!sha1) { |
1341 | // make up a sha1 and hope git doesn't notice |
1342 | var packId = new Buffer(pack.packId.substr(1, 44), 'base64') |
1343 | sha1 = packId.slice(0, 20).toString('hex') |
1344 | } |
1345 | return 'P pack-' + sha1 + '.pack\n' |
1346 | }) |
1347 | ) |
1348 | ])) |
1349 | }) |
1350 | }) |
1351 | } |
1352 | |
1353 | R.serveRepoPack = function (req, repo, name) { |
1354 | var m = name.match(/^pack-(.*)\.(pack|idx)$/) |
1355 | if (!m) return pull.once([400]) |
1356 | var hex; |
1357 | try { |
1358 | hex = new Buffer(m[1], 'hex') |
1359 | } catch(e) { |
1360 | return pull.once([400]) |
1361 | } |
1362 | |
1363 | var self = this |
1364 | return u.readNext(function (cb) { |
1365 | pull( |
1366 | repo.packs(), |
1367 | pull.filter(function (pack) { |
1368 | var sha1 = pack.sha1 |
1369 | ? new Buffer(pack.sha1, 'hex') |
1370 | : new Buffer(pack.packId.substr(1, 44), 'base64').slice(0, 20) |
1371 | return sha1.equals(hex) |
1372 | }), |
1373 | pull.take(1), |
1374 | pull.collect(function (err, packs) { |
1375 | if (err) return console.error(err), cb(null, pull.once([500])) |
1376 | if (packs.length < 1) return cb(null, pull.once([404])) |
1377 | var pack = packs[0] |
1378 | |
1379 | if (m[2] === 'pack') { |
1380 | repo.getPackfile(pack.packId, function (err, read) { |
1381 | if (err) return cb(err) |
1382 | cb(null, pull(read, |
1383 | self.web.serveRaw(null, 'application/x-git-packed-objects') |
1384 | )) |
1385 | }) |
1386 | } |
1387 | |
1388 | if (m[2] === 'idx') { |
1389 | repo.getPackIndex(pack.idxId, function (err, read) { |
1390 | if (err) return cb(err) |
1391 | cb(null, pull(read, |
1392 | self.web.serveRaw(null, 'application/x-git-packed-objects-toc') |
1393 | )) |
1394 | }) |
1395 | } |
1396 | }) |
1397 | ) |
1398 | }) |
1399 | } |
1400 |
Built with git-ssb-web