git ssb

30+

cel / git-ssb-web



Tree: 2ad0196e8b9972306535f04ebe1f9821a913c758

Files: 2ad0196e8b9972306535f04ebe1f9821a913c758 / lib / repos / index.js

32761 bytesRaw
1var url = require('url')
2var pull = require('pull-stream')
3var cat = require('pull-cat')
4var paramap = require('pull-paramap')
5var multicb = require('multicb')
6var JsDiff = require('diff')
7var GitRepo = require('pull-git-repo')
8var gitPack = require('pull-git-pack')
9var u = require('../util')
10var paginate = require('../paginate')
11var markdown = require('../markdown')
12var forms = require('../forms')
13
14module.exports = function (web) {
15 return new RepoRoutes(web)
16}
17
18function RepoRoutes(web) {
19 this.web = web
20 this.issues = require('./issues')(this, web)
21 this.pulls = require('./pulls')(this, web)
22}
23
24var R = RepoRoutes.prototype
25
26function getRepoObjectString(repo, id, mode, cb) {
27 if (!id) return cb(null, '')
28 if (mode == 0160000) return cb(null,
29 'Subproject commit ' + id)
30 repo.getObjectFromAny(id, function (err, obj) {
31 if (err) return cb(err)
32 u.readObjectString(obj, cb)
33 })
34}
35
36function table(props) {
37 return function (read) {
38 return cat([
39 pull.once('<table' + (props ? ' ' + props : '') + '>'),
40 pull(
41 read,
42 pull.map(function (row) {
43 return row ? '<tr>' + row.map(function (cell) {
44 return '<td>' + cell + '</td>'
45 }).join('') + '</tr>' : ''
46 })
47 ),
48 pull.once('</table>')
49 ])
50 }
51}
52
53function ul(props) {
54 return function (read) {
55 return cat([
56 pull.once('<ul' + (props ? ' ' + props : '') + '>'),
57 pull(read, pull.map(function (li) { return '<li>' + li + '</li>' })),
58 pull.once('</ul>')
59 ])
60 }
61}
62
63/* Repo */
64
65R.serveRepoPage = function (req, repo, path) {
66 var self = this
67 var defaultBranch = 'master'
68 var query = req._u.query
69
70 if (query.rev != null) {
71 // Allow navigating revs using GET query param.
72 // Replace the branch in the path with the rev query value
73 path[0] = path[0] || 'tree'
74 path[1] = query.rev
75 req._u.pathname = u.encodeLink([repo.id].concat(path))
76 delete req._u.query.rev
77 delete req._u.search
78 return self.web.serveRedirect(req, url.format(req._u))
79 }
80
81 // get branch
82 return path[1] ?
83 R_serveRepoPage2.call(self, req, repo, path) :
84 u.readNext(function (cb) {
85 // TODO: handle this in pull-git-repo or ssb-git-repo
86 repo.getSymRef('HEAD', true, function (err, ref) {
87 if (err) return cb(err)
88 repo.resolveRef(ref, function (err, rev) {
89 path[1] = rev ? ref : null
90 cb(null, R_serveRepoPage2.call(self, req, repo, path))
91 })
92 })
93 })
94}
95
96function R_serveRepoPage2(req, repo, path) {
97 var branch = path[1]
98 var filePath = path.slice(2)
99 switch (path[0]) {
100 case undefined:
101 case '':
102 return this.serveRepoTree(req, repo, branch, [])
103 case 'activity':
104 return this.serveRepoActivity(req, repo, branch)
105 case 'commits':
106 return this.serveRepoCommits(req, repo, branch)
107 case 'commit':
108 return this.serveRepoCommit(req, repo, path[1])
109 case 'tag':
110 return this.serveRepoTag(req, repo, branch, filePath)
111 case 'tree':
112 return this.serveRepoTree(req, repo, branch, filePath)
113 case 'blob':
114 return this.serveRepoBlob(req, repo, branch, filePath)
115 case 'raw':
116 return this.serveRepoRaw(req, repo, branch, filePath)
117 case 'digs':
118 return this.serveRepoDigs(req, repo)
119 case 'fork':
120 return this.serveRepoForkPrompt(req, repo)
121 case 'forks':
122 return this.serveRepoForks(req, repo)
123 case 'issues':
124 switch (path[1]) {
125 case 'new':
126 if (filePath.length == 0)
127 return this.issues.serveRepoNewIssue(req, repo)
128 break
129 default:
130 return this.issues.serveRepoIssues(req, repo, false)
131 }
132 case 'pulls':
133 return this.issues.serveRepoIssues(req, repo, true)
134 case 'compare':
135 return this.pulls.serveRepoCompare(req, repo)
136 case 'comparing':
137 return this.pulls.serveRepoComparing(req, repo)
138 default:
139 return this.web.serve404(req)
140 }
141}
142
143R.serveRepoNotFound = function (req, id, err) {
144 return this.web.serveTemplate(req, req._t('error.RepoNotFound'), 404)
145 (pull.values([
146 '<h2>' + req._t('error.RepoNotFound') + '</h2>',
147 '<p>' + req._t('error.RepoNameNotFound') + '</p>',
148 '<pre>' + u.escape(err.stack) + '</pre>'
149 ]))
150}
151
152R.renderRepoPage = function (req, repo, page, branch, titleTemplate, body) {
153 var self = this
154 var gitUrl = 'ssb://' + repo.id
155 var gitLink = '<input class="clone-url" readonly="readonly" ' +
156 'value="' + gitUrl + '" size="45" ' +
157 'onclick="this.select()"/>'
158 var digsPath = [repo.id, 'digs']
159
160 var done = multicb({ pluck: 1, spread: true })
161 self.web.getRepoName(repo.feed, repo.id, done())
162 self.web.about.getName(repo.feed, done())
163 self.web.getVotes(repo.id, done())
164
165 if (repo.upstream) {
166 self.web.getRepoName(repo.upstream.feed, repo.upstream.id, done())
167 self.web.about.getName(repo.upstream.feed, done())
168 }
169
170 return u.readNext(function (cb) {
171 done(function (err, repoName, authorName, votes,
172 upstreamName, upstreamAuthorName) {
173 if (err) return cb(null, self.web.serveError(req, err))
174 var upvoted = votes.upvoters[self.web.myId] > 0
175 var upstreamLink = !repo.upstream ? '' :
176 u.link([repo.upstream])
177 var title = titleTemplate ? titleTemplate
178 .replace(/%\{repo\}/g, repoName)
179 .replace(/%\{author\}/g, authorName)
180 : authorName + '/' + repoName
181 var isPublic = self.web.isPublic
182 cb(null, self.web.serveTemplate(req, title)(cat([
183 pull.once(
184 '<div class="repo-title">' +
185 '<form class="right-bar" action="" method="post">' +
186 '<button class="btn" name="action" value="vote" ' +
187 (isPublic ? 'disabled="disabled"' : ' type="submit"') + '>' +
188 '<i>✌</i> ' + req._t(!isPublic && upvoted ? 'Undig' : 'Dig') +
189 '</button>' +
190 (isPublic ? '' : '<input type="hidden" name="value" value="' +
191 (upvoted ? '0' : '1') + '">' +
192 '<input type="hidden" name="id" value="' +
193 u.escape(repo.id) + '">') + ' ' +
194 '<strong>' + u.link(digsPath, votes.upvotes) + '</strong> ' +
195 (isPublic ? '' : '<button class="btn" type="submit" ' +
196 ' name="action" value="fork-prompt">' +
197 '<i>⑂</i> ' + req._t('Fork') +
198 '</button>') + ' ' +
199 u.link([repo.id, 'forks'], '+', false, ' title="' +
200 req._t('Forks') + '"') +
201 '</form>' +
202 forms.name(req, !isPublic, repo.id, repoName, 'repo-name',
203 null, req._t('repo.Rename'),
204 '<h2 class="bgslash">' + u.link([repo.feed], authorName) + ' / ' +
205 u.link([repo.id], repoName) + '</h2>') +
206 '</div>' +
207 (repo.upstream ? '<small class="bgslash">' + req._t('ForkedFrom', {
208 repo: u.link([repo.upstream.feed], upstreamAuthorName) + '/' +
209 u.link([repo.upstream.id], upstreamName)
210 }) + '</small>' : '') +
211 u.nav([
212 [[repo.id], req._t('Code'), 'code'],
213 [[repo.id, 'activity'], req._t('Activity'), 'activity'],
214 [[repo.id, 'commits', branch||''], req._t('Commits'), 'commits'],
215 [[repo.id, 'issues'], req._t('Issues'), 'issues'],
216 [[repo.id, 'pulls'], req._t('PullRequests'), 'pulls']
217 ], page, gitLink)),
218 body
219 ])))
220 })
221 })
222}
223
224R.serveEmptyRepo = function (req, repo) {
225 if (repo.feed != this.web.myId)
226 return this.renderRepoPage(req, repo, 'code', null, null, pull.once(
227 '<section>' +
228 '<h3>' + req._t('EmptyRepo') + '</h3>' +
229 '</section>'))
230
231 var gitUrl = 'ssb://' + repo.id
232 return this.renderRepoPage(req, repo, 'code', null, null, pull.once(
233 '<section>' +
234 '<h3>' + req._t('initRepo.GettingStarted') + '</h3>' +
235 '<h4>' + req._t('initRepo.CreateNew') + '</h4><pre>' +
236 'touch ' + req._t('initRepo.README') + '.md\n' +
237 'git init\n' +
238 'git add ' + req._t('initRepo.README') + '.md\n' +
239 'git commit -m "' + req._t('initRepo.InitialCommit') + '"\n' +
240 'git remote add origin ' + gitUrl + '\n' +
241 'git push -u origin master</pre>\n' +
242 '<h4>' + req._t('initRepo.PushExisting') + '</h4>\n' +
243 '<pre>git remote add origin ' + gitUrl + '\n' +
244 'git push -u origin master</pre>' +
245 '</section>'))
246}
247
248R.serveRepoTree = function (req, repo, rev, path) {
249 if (!rev) return this.serveEmptyRepo(req, repo)
250 var type = repo.isCommitHash(rev) ? 'Tree' : 'Branch'
251 var title = (path.length ? path.join('/') + ' · ' : '') +
252 '%{author}/%{repo}' +
253 (repo.head == 'refs/heads/' + rev ? '' : '@' + rev)
254 return this.renderRepoPage(req, repo, 'code', rev, title, cat([
255 pull.once('<section><form action="" method="get">' +
256 '<h3>' + req._t(type) + ': ' + rev + ' '),
257 this.revMenu(req, repo, rev),
258 pull.once('</h3></form>'),
259 type == 'Branch' && renderRepoLatest(req, repo, rev),
260 pull.once('</section><section>'),
261 renderRepoTree(req, repo, rev, path),
262 pull.once('</section>'),
263 this.renderRepoReadme(req, repo, rev, path)
264 ]))
265}
266
267/* Repo activity */
268
269R.serveRepoActivity = function (req, repo, branch) {
270 var self = this
271 var title = req._t('Activity') + ' · %{author}/%{repo}'
272 return self.renderRepoPage(req, repo, 'activity', branch, title, cat([
273 pull.once('<h3>' + req._t('Activity') + '</h3>'),
274 pull(
275 self.web.ssb.links({
276 dest: repo.id,
277 source: repo.feed,
278 rel: 'repo',
279 values: true,
280 reverse: true
281 }),
282 pull.map(renderRepoUpdate.bind(self, req, repo))
283 ),
284 u.readOnce(function (cb) {
285 var done = multicb({ pluck: 1, spread: true })
286 self.web.about.getName(repo.feed, done())
287 self.web.getMsg(repo.id, done())
288 done(function (err, authorName, msg) {
289 if (err) return cb(err)
290 self.web.renderFeedItem(req, {
291 key: repo.id,
292 value: msg,
293 authorName: authorName
294 }, cb)
295 })
296 })
297 ]))
298}
299
300function renderRepoUpdate(req, repo, msg, full) {
301 var c = msg.value.content
302
303 if (c.type != 'git-update') {
304 return ''
305 // return renderFeedItem(msg, cb)
306 // TODO: render post, issue, pull-request
307 }
308
309 var branches = []
310 var tags = []
311 if (c.refs) for (var name in c.refs) {
312 var m = name.match(/^refs\/(heads|tags)\/(.*)$/) || [,, name]
313 ;(m[1] == 'tags' ? tags : branches)
314 .push({name: m[2], value: c.refs[name]})
315 }
316 var numObjects = c.objects ? Object.keys(c.objects).length : 0
317
318 var dateStr = new Date(msg.value.timestamp).toLocaleString(req._locale)
319 return '<section class="collapse">' +
320 u.link([msg.key], dateStr) + '<br>' +
321 branches.map(function (update) {
322 if (!update.value) {
323 return '<s>' + u.escape(update.name) + '</s><br/>'
324 } else {
325 var commitLink = u.link([repo.id, 'commit', update.value])
326 var branchLink = u.link([repo.id, 'tree', update.name])
327 return branchLink + ' &rarr; <tt>' + commitLink + '</tt><br/>'
328 }
329 }).join('') +
330 tags.map(function (update) {
331 return update.value
332 ? u.link([repo.id, 'tag', update.value], update.name)
333 : '<s>' + u.escape(update.name) + '</s>'
334 }).join(', ') +
335 '</section>'
336}
337
338/* Repo commits */
339
340R.serveRepoCommits = function (req, repo, branch) {
341 var query = req._u.query
342 var title = req._t('Commits') + ' · %{author}/%{repo}'
343 return this.renderRepoPage(req, repo, 'commits', branch, title, cat([
344 pull.once('<h3>' + req._t('Commits') + '</h3>'),
345 pull(
346 repo.readLog(query.start || branch),
347 pull.take(20),
348 paramap(repo.getCommitParsed.bind(repo), 8),
349 paginate(
350 !query.start ? '' : function (first, cb) {
351 cb(null, '&hellip;')
352 },
353 pull.map(renderCommit.bind(this, req, repo)),
354 function (commit, cb) {
355 cb(null, commit.parents && commit.parents[0] ?
356 '<a href="?start=' + commit.id + '">' +
357 req._t('Older') + '</a>' : '')
358 }
359 )
360 )
361 ]))
362}
363
364function renderCommit(req, repo, commit) {
365 var commitPath = [repo.id, 'commit', commit.id]
366 var treePath = [repo.id, 'tree', commit.id]
367 return '<section class="collapse">' +
368 '<strong>' + u.link(commitPath, commit.title) + '</strong><br>' +
369 '<tt>' + commit.id + '</tt> ' +
370 u.link(treePath, req._t('Tree')) + '<br>' +
371 u.escape(commit.author.name) + ' &middot; ' +
372 commit.author.date.toLocaleString(req._locale) +
373 (commit.separateAuthor ? '<br>' + req._t('CommittedOn', {
374 name: u.escape(commit.committer.name),
375 date: commit.committer.date.toLocaleString(req._locale)
376 }) : '') +
377 '</section>'
378}
379
380/* Branch menu */
381
382R.formatRevOptions = function (currentName) {
383 return function (name) {
384 var htmlName = u.escape(name)
385 return '<option value="' + htmlName + '"' +
386 (name == currentName ? ' selected="selected"' : '') +
387 '>' + htmlName + '</option>'
388 }
389}
390
391R.formatRevType = function(req, type) {
392 return (
393 type == 'heads' ? req._t('Branches') :
394 type == 'tags' ? req._t('Tags') :
395 type)
396}
397
398R.revMenu = function (req, repo, currentName) {
399 var self = this
400 return u.readOnce(function (cb) {
401 repo.getRefNames(function (err, refs) {
402 if (err) return cb(err)
403 cb(null, '<select name="rev" onchange="this.form.submit()">' +
404 Object.keys(refs).map(function (group) {
405 return '<optgroup ' +
406 'label="' + self.formatRevType(req, group) + '">' +
407 refs[group].map(self.formatRevOptions(currentName)).join('') +
408 '</optgroup>'
409 }).join('') +
410 '</select><noscript> ' +
411 '<input type="submit" value="' + req._t('Go') + '"/></noscript>')
412 })
413 })
414}
415
416/* Repo tree */
417
418function renderRepoLatest(req, repo, rev) {
419 return u.readOnce(function (cb) {
420 repo.getCommitParsed(rev, function (err, commit) {
421 if (err) return cb(err)
422 var commitPath = [repo.id, 'commit', commit.id]
423 cb(null,
424 req._t('Latest') + ': ' +
425 '<strong>' + u.link(commitPath, commit.title) + '</strong><br/>' +
426 '<tt>' + commit.id + '</tt><br/> ' +
427 req._t('CommittedOn', {
428 name: u.escape(commit.committer.name),
429 date: commit.committer.date.toLocaleString(req._locale)
430 }) +
431 (commit.separateAuthor ? '<br/>' + req._t('AuthoredOn', {
432 name: u.escape(commit.author.name),
433 date: commit.author.date.toLocaleString(req._locale)
434 }) : ''))
435 })
436 })
437}
438
439// breadcrumbs
440function linkPath(basePath, path) {
441 path = path.slice()
442 var last = path.pop()
443 return path.map(function (dir, i) {
444 return u.link(basePath.concat(path.slice(0, i+1)), dir)
445 }).concat(last).join(' / ')
446}
447
448function renderRepoTree(req, repo, rev, path) {
449 var pathLinks = path.length === 0 ? '' :
450 ': ' + linkPath([repo.id, 'tree'], [rev].concat(path))
451 return cat([
452 pull.once('<h3>' + req._t('Files') + pathLinks + '</h3>'),
453 pull(
454 repo.readDir(rev, path),
455 pull.map(function (file) {
456 var type = (file.mode === 040000) ? 'tree' :
457 (file.mode === 0160000) ? 'commit' : 'blob'
458 if (type == 'commit')
459 return [
460 '<span title="' + req._t('gitCommitLink') + '">🖈</span>',
461 '<span title="' + u.escape(file.id) + '">' +
462 u.escape(file.name) + '</span>']
463 var filePath = [repo.id, type, rev].concat(path, file.name)
464 return ['<i>' + (type == 'tree' ? '📁' : '📄') + '</i>',
465 u.link(filePath, file.name)]
466 }),
467 table('class="files"')
468 )
469 ])
470}
471
472/* Repo readme */
473
474R.renderRepoReadme = function (req, repo, branch, path) {
475 var self = this
476 return u.readNext(function (cb) {
477 pull(
478 repo.readDir(branch, path),
479 pull.filter(function (file) {
480 return /readme(\.|$)/i.test(file.name)
481 }),
482 pull.take(1),
483 pull.collect(function (err, files) {
484 if (err) return cb(null, pull.empty())
485 var file = files[0]
486 if (!file)
487 return cb(null, pull.once(path.length ? '' :
488 '<p>' + req._t('NoReadme') + '</p>'))
489 repo.getObjectFromAny(file.id, function (err, obj) {
490 if (err) return cb(err)
491 cb(null, cat([
492 pull.once('<section><h4><a name="readme">' +
493 u.escape(file.name) + '</a></h4><hr/>'),
494 self.web.renderObjectData(obj, file.name, repo, branch, path),
495 pull.once('</section>')
496 ]))
497 })
498 })
499 )
500 })
501}
502
503/* Repo commit */
504
505R.serveRepoCommit = function (req, repo, rev) {
506 var self = this
507 return u.readNext(function (cb) {
508 repo.getCommitParsed(rev, function (err, commit) {
509 if (err) return cb(err)
510 var commitPath = [repo.id, 'commit', commit.id]
511 var treePath = [repo.id, 'tree', commit.id]
512 var title = u.escape(commit.title) + ' · ' +
513 '%{author}/%{repo}@' + commit.id.substr(0, 8)
514 cb(null, self.renderRepoPage(req, repo, null, rev, title, cat([
515 pull.once(
516 '<h3>' + u.link(commitPath,
517 req._t('CommitRev', {rev: rev})) + '</h3>' +
518 '<section class="collapse">' +
519 '<div class="right-bar">' +
520 u.link(treePath, req._t('BrowseFiles')) +
521 '</div>' +
522 '<h4>' + u.linkify(u.escape(commit.title)) + '</h4>' +
523 (commit.body ? u.linkify(u.pre(commit.body)) : '') +
524 (commit.separateAuthor ? req._t('AuthoredOn', {
525 name: u.escape(commit.author.name),
526 date: commit.author.date.toLocaleString(req._locale)
527 }) + '<br/>' : '') +
528 req._t('CommittedOn', {
529 name: u.escape(commit.committer.name),
530 date: commit.committer.date.toLocaleString(req._locale)
531 }) + '<br/>' +
532 commit.parents.map(function (id) {
533 return req._t('Parent') + ': ' +
534 u.link([repo.id, 'commit', id], id)
535 }).join('<br>') +
536 '</section>' +
537 '<section><h3>' + req._t('FilesChanged') + '</h3>'),
538 // TODO: show diff from all parents (merge commits)
539 self.renderDiffStat(req, [repo, repo], [commit.parents[0], commit.id]),
540 pull.once('</section>')
541 ])))
542 })
543 })
544}
545
546/* Repo tag */
547
548R.serveRepoTag = function (req, repo, rev, path) {
549 var self = this
550 return u.readNext(function (cb) {
551 repo.getTagParsed(rev, function (err, tag) {
552 if (err) {
553 if (/Expected tag, got commit/.test(err.message)) {
554 req._u.pathname = u.encodeLink([repo.id, 'commit', rev].concat(path))
555 return cb(null, self.web.serveRedirect(req, url.format(req._u)))
556 }
557 return cb(null, self.web.serveError(req, err))
558 }
559
560 var title = req._t('TagName', {
561 tag: u.escape(tag.tag)
562 }) + ' · %{author}/%{repo}'
563 var body = (tag.title + '\n\n' +
564 tag.body.replace(/-----BEGIN PGP SIGNATURE-----\n[^.]*?\n-----END PGP SIGNATURE-----\s*$/, '')).trim()
565 var date = tag.tagger.date
566 cb(null, self.renderRepoPage(req, repo, 'tags', tag.object, title,
567 pull.once(
568 '<section class="collapse">' +
569 '<h3>' + u.link([repo.id, 'tag', rev], tag.tag) + '</h3>' +
570 req._t('TaggedOn', {
571 name: u.escape(tag.tagger.name),
572 date: date && date.toLocaleString(req._locale)
573 }) + '<br/>' +
574 u.link([repo.id, tag.type, tag.object]) +
575 u.linkify(u.pre(body)) +
576 '</section>')))
577 })
578 })
579}
580
581
582/* Diff stat */
583
584R.renderDiffStat = function (req, repos, treeIds) {
585 if (treeIds.length == 0) treeIds = [null]
586 var id = treeIds[0]
587 var lastI = treeIds.length - 1
588 var oldTree = treeIds[0]
589 var changedFiles = []
590 return cat([
591 pull(
592 GitRepo.diffTrees(repos, treeIds, true),
593 pull.map(function (item) {
594 var filename = u.escape(item.filename = item.path.join('/'))
595 var oldId = item.id && item.id[0]
596 var newId = item.id && item.id[lastI]
597 var oldMode = item.mode && item.mode[0]
598 var newMode = item.mode && item.mode[lastI]
599 var action =
600 !oldId && newId ? req._t('action.added') :
601 oldId && !newId ? req._t('action.deleted') :
602 oldMode != newMode ? req._t('action.changedMode', {
603 old: oldMode.toString(8),
604 new: newMode.toString(8)
605 }) : req._t('changed')
606 if (item.id)
607 changedFiles.push(item)
608 var blobsPath = item.id[1]
609 ? [repos[1].id, 'blob', treeIds[1]]
610 : [repos[0].id, 'blob', treeIds[0]]
611 var rawsPath = item.id[1]
612 ? [repos[1].id, 'raw', treeIds[1]]
613 : [repos[0].id, 'raw', treeIds[0]]
614 item.blobPath = blobsPath.concat(item.path)
615 item.rawPath = rawsPath.concat(item.path)
616 var fileHref = item.id ?
617 '#' + encodeURIComponent(item.path.join('/')) :
618 u.encodeLink(item.blobPath)
619 return ['<a href="' + fileHref + '">' + filename + '</a>', action]
620 }),
621 table()
622 ),
623 pull(
624 pull.values(changedFiles),
625 paramap(function (item, cb) {
626 var extension = u.getExtension(item.filename)
627 if (extension in u.imgMimes) {
628 var filename = u.escape(item.filename)
629 return cb(null,
630 '<pre><table class="code">' +
631 '<tr><th id="' + u.escape(item.filename) + '">' +
632 filename + '</th></tr>' +
633 '<tr><td><img src="' + u.encodeLink(item.rawPath) + '"' +
634 ' alt="' + filename + '"/></td></tr>' +
635 '</table></pre>')
636 }
637 var done = multicb({ pluck: 1, spread: true })
638 var mode0 = item.mode && item.mode[0]
639 var modeI = item.mode && item.mode[lastI]
640 var isSubmodule = (modeI == 0160000)
641 getRepoObjectString(repos[0], item.id[0], mode0, done())
642 getRepoObjectString(repos[1], item.id[lastI], modeI, done())
643 done(function (err, strOld, strNew) {
644 if (err) return cb(err)
645 cb(null, htmlLineDiff(req, item.filename, item.filename,
646 strOld, strNew,
647 u.encodeLink(item.blobPath), !isSubmodule))
648 })
649 }, 4)
650 )
651 ])
652}
653
654function htmlLineDiff(req, filename, anchor, oldStr, newStr, blobHref,
655 showViewLink) {
656 return '<pre><table class="code">' +
657 '<tr><th colspan=3 id="' + u.escape(anchor) + '">' + filename +
658 (showViewLink === false ? '' :
659 '<span class="right-bar">' +
660 '<a href="' + blobHref + '">' + req._t('View') + '</a> ' +
661 '</span>') +
662 '</th></tr>' +
663 (oldStr.length + newStr.length > 200000
664 ? '<tr><td class="diff-info">' + req._t('diff.TooLarge') + '<br>' +
665 req._t('diff.OldFileSize', {bytes: oldStr.length}) + '<br>' +
666 req._t('diff.NewFileSize', {bytes: newStr.length}) + '</td></tr>'
667 : tableDiff(oldStr, newStr, filename)) +
668 '</table></pre>'
669}
670
671function tableDiff(oldStr, newStr, filename) {
672 var diff = JsDiff.structuredPatch('', '', oldStr, newStr)
673 var groups = diff.hunks.map(function (hunk) {
674 var oldLine = hunk.oldStart
675 var newLine = hunk.newStart
676 var header = '<tr class="diff-hunk-header"><td colspan=2></td><td>' +
677 '@@ -' + oldLine + ',' + hunk.oldLines + ' ' +
678 '+' + newLine + ',' + hunk.newLines + ' @@' +
679 '</td></tr>'
680 return [header].concat(hunk.lines.map(function (line) {
681 var s = line[0]
682 if (s == '\\') return
683 var html = u.highlight(line, u.getExtension(filename))
684 var trClass = s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : ''
685 var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++]
686 var id = [filename].concat(lineNums).join('-')
687 return '<tr id="' + u.escape(id) + '" class="' + trClass + '">' +
688 lineNums.map(function (num) {
689 return '<td class="code-linenum">' +
690 (num ? '<a href="#' + encodeURIComponent(id) + '">' +
691 num + '</a>' : '') + '</td>'
692 }).join('') +
693 '<td class="code-text">' + html + '</td></tr>'
694 }))
695 })
696 return [].concat.apply([], groups).join('')
697}
698
699/* An unknown message linking to a repo */
700
701R.serveRepoSomething = function (req, repo, id, msg, path) {
702 return this.renderRepoPage(req, repo, null, null, null,
703 pull.once('<section><h3>' + u.link([id]) + '</h3>' +
704 u.json(msg) + '</section>'))
705}
706
707/* Repo update */
708
709function objsArr(objs) {
710 return Array.isArray(objs) ? objs :
711 Object.keys(objs).map(function (sha1) {
712 var obj = Object.create(objs[sha1])
713 obj.sha1 = sha1
714 return obj
715 })
716}
717
718R.serveRepoUpdate = function (req, repo, id, msg, path) {
719 var self = this
720 var raw = req._u.query.raw != null
721 var title = req._t('Update') + ' · %{author}/%{repo}'
722
723 if (raw)
724 return self.renderRepoPage(req, repo, 'activity', null, title, pull.once(
725 '<a href="?" class="raw-link header-align">' +
726 req._t('Info') + '</a>' +
727 '<h3>' + req._t('Update') + '</h3>' +
728 '<section class="collapse">' +
729 u.json({key: id, value: msg}) + '</section>'))
730
731 // convert packs to old single-object style
732 if (msg.content.indexes) {
733 for (var i = 0; i < msg.content.indexes.length; i++) {
734 msg.content.packs[i] = {
735 pack: {link: msg.content.packs[i].link},
736 idx: msg.content.indexes[i]
737 }
738 }
739 }
740
741 var commits = cat([
742 msg.content.objects && pull(
743 pull.values(msg.content.objects),
744 pull.filter(function (obj) { return obj.type == 'commit' }),
745 paramap(function (obj, cb) {
746 self.web.getBlob(req, obj.link || obj.key, function (err, readObject) {
747 if (err) return cb(err)
748 GitRepo.getCommitParsed({read: readObject}, cb)
749 })
750 }, 8)
751 ),
752 msg.content.packs && pull(
753 pull.values(msg.content.packs),
754 paramap(function (pack, cb) {
755 var done = multicb({ pluck: 1, spread: true })
756 self.web.getBlob(req, pack.pack.link, done())
757 self.web.getBlob(req, pack.idx.link, done())
758 done(function (err, readPack, readIdx) {
759 if (err) return cb(self.web.renderError(err))
760 cb(null, gitPack.decodeWithIndex(repo, readPack, readIdx))
761 })
762 }, 4),
763 pull.flatten(),
764 pull.asyncMap(function (obj, cb) {
765 if (obj.type == 'commit')
766 GitRepo.getCommitParsed(obj, cb)
767 else
768 pull(obj.read, pull.drain(null, cb))
769 }),
770 pull.filter()
771 )
772 ])
773
774 return self.renderRepoPage(req, repo, 'activity', null, title, cat([
775 pull.once('<a href="?raw" class="raw-link header-align">' +
776 req._t('Data') + '</a>' +
777 '<h3>' + req._t('Update') + '</h3>' +
778 renderRepoUpdate(req, repo, {key: id, value: msg}, true)),
779 (msg.content.objects || msg.content.packs) &&
780 pull.once('<h3>' + req._t('Commits') + '</h3>'),
781 pull(commits, pull.map(function (commit) {
782 return renderCommit(req, repo, commit)
783 }))
784 ]))
785}
786
787/* Blob */
788
789R.serveRepoBlob = function (req, repo, rev, path) {
790 var self = this
791 return u.readNext(function (cb) {
792 repo.getFile(rev, path, function (err, object) {
793 if (err) return cb(null, self.web.serveBlobNotFound(req, repo.id, err))
794 var type = repo.isCommitHash(rev) ? 'Tree' : 'Branch'
795 var pathLinks = path.length === 0 ? '' :
796 ': ' + linkPath([repo.id, 'tree'], [rev].concat(path))
797 var rawFilePath = [repo.id, 'raw', rev].concat(path)
798 var dirPath = path.slice(0, path.length-1)
799 var filename = path[path.length-1]
800 var extension = u.getExtension(filename)
801 var title = (path.length ? path.join('/') + ' · ' : '') +
802 '%{author}/%{repo}' +
803 (repo.head == 'refs/heads/' + rev ? '' : '@' + rev)
804 cb(null, self.renderRepoPage(req, repo, 'code', rev, title, cat([
805 pull.once('<section><form action="" method="get">' +
806 '<h3>' + req._t(type) + ': ' + rev + ' '),
807 self.revMenu(req, repo, rev),
808 pull.once('</h3></form>'),
809 type == 'Branch' && renderRepoLatest(req, repo, rev),
810 pull.once('</section><section class="collapse">' +
811 '<h3>' + req._t('Files') + pathLinks + '</h3>' +
812 '<div>' + object.length + ' bytes' +
813 '<span class="raw-link">' +
814 u.link(rawFilePath, req._t('Raw')) + '</span>' +
815 '</div></section>' +
816 '<section>'),
817 extension in u.imgMimes
818 ? pull.once('<img src="' + u.encodeLink(rawFilePath) +
819 '" alt="' + u.escape(filename) + '" />')
820 : self.web.renderObjectData(object, filename, repo, rev, dirPath),
821 pull.once('</section>')
822 ])))
823 })
824 })
825}
826
827/* Raw blob */
828
829R.serveRepoRaw = function (req, repo, branch, path) {
830 var self = this
831 return u.readNext(function (cb) {
832 repo.getFile(branch, path, function (err, object) {
833 if (err) return cb(null,
834 self.web.serveBuffer(404, req._t('error.BlobNotFound')))
835 var extension = u.getExtension(path[path.length-1])
836 var contentType = u.imgMimes[extension]
837 cb(null, pull(object.read, self.web.serveRaw(object.length, contentType)))
838 })
839 })
840}
841
842/* Digs */
843
844R.serveRepoDigs = function (req, repo) {
845 var self = this
846 return u.readNext(function (cb) {
847 var title = req._t('Digs') + ' · %{author}/%{repo}'
848 self.web.getVotes(repo.id, function (err, votes) {
849 cb(null, self.renderRepoPage(req, repo, null, null, title, cat([
850 pull.once('<section><h3>' + req._t('Digs') + '</h3>' +
851 '<div>' + req._t('Total') + ': ' + votes.upvotes + '</div>'),
852 pull(
853 pull.values(Object.keys(votes.upvoters)),
854 paramap(function (feedId, cb) {
855 self.web.about.getName(feedId, function (err, name) {
856 if (err) return cb(err)
857 cb(null, u.link([feedId], name))
858 })
859 }, 8),
860 ul()
861 ),
862 pull.once('</section>')
863 ])))
864 })
865 })
866}
867
868/* Forks */
869
870R.getForks = function (repo, includeSelf) {
871 var self = this
872 return pull(
873 cat([
874 includeSelf && pull.once(repo.id),
875 // get downstream repos
876 pull(
877 self.web.ssb.links({
878 dest: repo.id,
879 rel: 'upstream'
880 }),
881 pull.map('key')
882 ),
883 // look for other repos that previously had pull requests to this one
884 pull(
885 self.web.ssb.links({
886 dest: repo.id,
887 values: true,
888 rel: 'project'
889 }),
890 pull.filter(function (msg) {
891 var c = msg && msg.value && msg.value.content
892 return c && c.type == 'pull-request'
893 }),
894 pull.map(function (msg) { return msg.value.content.head_repo })
895 )
896 ]),
897 pull.unique(),
898 paramap(function (key, cb) {
899 self.web.ssb.get(key, function (err, value) {
900 if (err) cb(err)
901 else cb(null, {key: key, value: value})
902 })
903 }, 4),
904 pull.filter(function (msg) {
905 var c = msg && msg.value && msg.value.content
906 return c && c.type == 'git-repo'
907 }),
908 paramap(function (msg, cb) {
909 self.web.getRepoFullName(msg.value.author, msg.key,
910 function (err, repoName, authorName) {
911 if (err) return cb(err)
912 cb(null, {
913 key: msg.key,
914 value: msg.value,
915 repoName: repoName,
916 authorName: authorName
917 })
918 })
919 }, 8)
920 )
921}
922
923R.serveRepoForks = function (req, repo) {
924 var hasForks
925 var title = req._t('Forks') + ' · %{author}/%{repo}'
926 return this.renderRepoPage(req, repo, null, null, title, cat([
927 pull.once('<h3>' + req._t('Forks') + '</h3>'),
928 pull(
929 this.getForks(repo),
930 pull.map(function (msg) {
931 hasForks = true
932 return '<section class="collapse">' +
933 u.link([msg.value.author], msg.authorName) + ' / ' +
934 u.link([msg.key], msg.repoName) +
935 '<span class="right-bar">' +
936 u.timestamp(msg.value.timestamp, req) +
937 '</span></section>'
938 })
939 ),
940 u.readOnce(function (cb) {
941 cb(null, hasForks ? '' : req._t('NoForks'))
942 })
943 ]))
944}
945
946R.serveRepoForkPrompt = function (req, repo) {
947 var title = req._t('Fork') + ' · %{author}/%{repo}'
948 return this.renderRepoPage(req, repo, null, null, title, pull.once(
949 '<form action="" method="post" onreset="history.back()">' +
950 '<h3>' + req._t('ForkRepoPrompt') + '</h3>' +
951 '<p>' + u.hiddenInputs({ id: repo.id }) +
952 '<button class="btn open" type="submit" name="action" value="fork">' +
953 req._t('Fork') +
954 '</button>' +
955 ' <button class="btn" type="reset">' +
956 req._t('Cancel') + '</button>' +
957 '</p></form>'
958 ))
959}
960
961R.serveIssueOrPullRequest = function (req, repo, issue, path, id) {
962 return issue.msg.value.content.type == 'pull-request'
963 ? this.pulls.serveRepoPullReq(req, repo, issue, path, id)
964 : this.issues.serveRepoIssue(req, repo, issue, path, id)
965}
966

Built with git-ssb-web