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