git ssb

30+

cel / git-ssb-web



Tree: 1b19c0c5740d31d5c88317bfa73673305ee7a6f3

Files: 1b19c0c5740d31d5c88317bfa73673305ee7a6f3 / lib / repos / index.js

31525 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)
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) {
549 var self = this
550 return u.readNext(function (cb) {
551 repo.getTagParsed(rev, function (err, tag) {
552 if (err) return cb(err)
553 var title = req._t('TagName', {
554 tag: u.escape(tag.tag)
555 }) + ' · %{author}/%{repo}'
556 var body = (tag.title + '\n\n' +
557 tag.body.replace(/-----BEGIN PGP SIGNATURE-----\n[^.]*?\n-----END PGP SIGNATURE-----\s*$/, '')).trim()
558 cb(null, self.renderRepoPage(req, repo, 'tags', tag.object, title,
559 pull.once(
560 '<section class="collapse">' +
561 '<h3>' + u.link([repo.id, 'tag', rev], tag.tag) + '</h3>' +
562 req._t('TaggedOn', {
563 name: u.escape(tag.tagger.name),
564 date: tag.tagger.date.toLocaleString(req._locale)
565 }) + '<br/>' +
566 u.link([repo.id, tag.type, tag.object]) +
567 u.linkify(u.pre(body)) +
568 '</section>')))
569 })
570 })
571}
572
573
574/* Diff stat */
575
576R.renderDiffStat = function (req, repos, treeIds) {
577 if (treeIds.length == 0) treeIds = [null]
578 var id = treeIds[0]
579 var lastI = treeIds.length - 1
580 var oldTree = treeIds[0]
581 var changedFiles = []
582 return cat([
583 pull(
584 GitRepo.diffTrees(repos, treeIds, true),
585 pull.map(function (item) {
586 var filename = u.escape(item.filename = item.path.join('/'))
587 var oldId = item.id && item.id[0]
588 var newId = item.id && item.id[lastI]
589 var oldMode = item.mode && item.mode[0]
590 var newMode = item.mode && item.mode[lastI]
591 var action =
592 !oldId && newId ? req._t('action.added') :
593 oldId && !newId ? req._t('action.deleted') :
594 oldMode != newMode ? req._t('action.changedMode', {
595 old: oldMode.toString(8),
596 new: newMode.toString(8)
597 }) : req._t('changed')
598 if (item.id)
599 changedFiles.push(item)
600 var blobsPath = item.id[1]
601 ? [repos[1].id, 'blob', treeIds[1]]
602 : [repos[0].id, 'blob', treeIds[0]]
603 var rawsPath = item.id[1]
604 ? [repos[1].id, 'raw', treeIds[1]]
605 : [repos[0].id, 'raw', treeIds[0]]
606 item.blobPath = blobsPath.concat(item.path)
607 item.rawPath = rawsPath.concat(item.path)
608 var fileHref = item.id ?
609 '#' + encodeURIComponent(item.path.join('/')) :
610 u.encodeLink(item.blobPath)
611 return ['<a href="' + fileHref + '">' + filename + '</a>', action]
612 }),
613 table()
614 ),
615 pull(
616 pull.values(changedFiles),
617 paramap(function (item, cb) {
618 var extension = u.getExtension(item.filename)
619 if (extension in u.imgMimes) {
620 var filename = u.escape(item.filename)
621 return cb(null,
622 '<pre><table class="code">' +
623 '<tr><th id="' + u.escape(item.filename) + '">' +
624 filename + '</th></tr>' +
625 '<tr><td><img src="' + u.encodeLink(item.rawPath) + '"' +
626 ' alt="' + filename + '"/></td></tr>' +
627 '</table></pre>')
628 }
629 var done = multicb({ pluck: 1, spread: true })
630 var mode0 = item.mode && item.mode[0]
631 var modeI = item.mode && item.mode[lastI]
632 var isSubmodule = (modeI == 0160000)
633 getRepoObjectString(repos[0], item.id[0], mode0, done())
634 getRepoObjectString(repos[1], item.id[lastI], modeI, done())
635 done(function (err, strOld, strNew) {
636 if (err) return cb(err)
637 cb(null, htmlLineDiff(req, item.filename, item.filename,
638 strOld, strNew,
639 u.encodeLink(item.blobPath), !isSubmodule))
640 })
641 }, 4)
642 )
643 ])
644}
645
646function htmlLineDiff(req, filename, anchor, oldStr, newStr, blobHref,
647 showViewLink) {
648 var diff = JsDiff.structuredPatch('', '', oldStr, newStr)
649 var groups = diff.hunks.map(function (hunk) {
650 var oldLine = hunk.oldStart
651 var newLine = hunk.newStart
652 var header = '<tr class="diff-hunk-header"><td colspan=2></td><td>' +
653 '@@ -' + oldLine + ',' + hunk.oldLines + ' ' +
654 '+' + newLine + ',' + hunk.newLines + ' @@' +
655 '</td></tr>'
656 return [header].concat(hunk.lines.map(function (line) {
657 var s = line[0]
658 if (s == '\\') return
659 var html = u.highlight(line, u.getExtension(filename))
660 var trClass = s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : ''
661 var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++]
662 var id = [filename].concat(lineNums).join('-')
663 return '<tr id="' + u.escape(id) + '" class="' + trClass + '">' +
664 lineNums.map(function (num) {
665 return '<td class="code-linenum">' +
666 (num ? '<a href="#' + encodeURIComponent(id) + '">' +
667 num + '</a>' : '') + '</td>'
668 }).join('') +
669 '<td class="code-text">' + html + '</td></tr>'
670 }))
671 })
672 return '<pre><table class="code">' +
673 '<tr><th colspan=3 id="' + u.escape(anchor) + '">' + filename +
674 (showViewLink === false ? '' :
675 '<span class="right-bar">' +
676 '<a href="' + blobHref + '">' + req._t('View') + '</a> ' +
677 '</span>') +
678 '</th></tr>' +
679 [].concat.apply([], groups).join('') +
680 '</table></pre>'
681}
682
683/* An unknown message linking to a repo */
684
685R.serveRepoSomething = function (req, repo, id, msg, path) {
686 return this.renderRepoPage(req, repo, null, null, null,
687 pull.once('<section><h3>' + u.link([id]) + '</h3>' +
688 u.json(msg) + '</section>'))
689}
690
691/* Repo update */
692
693function objsArr(objs) {
694 return Array.isArray(objs) ? objs :
695 Object.keys(objs).map(function (sha1) {
696 var obj = Object.create(objs[sha1])
697 obj.sha1 = sha1
698 return obj
699 })
700}
701
702R.serveRepoUpdate = function (req, repo, id, msg, path) {
703 var self = this
704 var raw = req._u.query.raw != null
705 var title = req._t('Update') + ' · %{author}/%{repo}'
706
707 if (raw)
708 return self.renderRepoPage(req, repo, 'activity', null, title, pull.once(
709 '<a href="?" class="raw-link header-align">' +
710 req._t('Info') + '</a>' +
711 '<h3>' + req._t('Update') + '</h3>' +
712 '<section class="collapse">' +
713 u.json({key: id, value: msg}) + '</section>'))
714
715 // convert packs to old single-object style
716 if (msg.content.indexes) {
717 for (var i = 0; i < msg.content.indexes.length; i++) {
718 msg.content.packs[i] = {
719 pack: {link: msg.content.packs[i].link},
720 idx: msg.content.indexes[i]
721 }
722 }
723 }
724
725 var commits = cat([
726 msg.content.objects && pull(
727 pull.values(msg.content.objects),
728 pull.filter(function (obj) { return obj.type == 'commit' }),
729 paramap(function (obj, cb) {
730 self.web.getBlob(req, obj.link || obj.key, function (err, readObject) {
731 if (err) return cb(err)
732 GitRepo.getCommitParsed({read: readObject}, cb)
733 })
734 }, 8)
735 ),
736 msg.content.packs && pull(
737 pull.values(msg.content.packs),
738 paramap(function (pack, cb) {
739 var done = multicb({ pluck: 1, spread: true })
740 self.web.getBlob(req, pack.pack.link, done())
741 self.web.getBlob(req, pack.idx.link, done())
742 done(function (err, readPack, readIdx) {
743 if (err) return cb(self.web.renderError(err))
744 cb(null, gitPack.decodeWithIndex(repo, readPack, readIdx))
745 })
746 }, 4),
747 pull.flatten(),
748 pull.asyncMap(function (obj, cb) {
749 if (obj.type == 'commit')
750 GitRepo.getCommitParsed(obj, cb)
751 else
752 pull(obj.read, pull.drain(null, cb))
753 }),
754 pull.filter()
755 )
756 ])
757
758 return self.renderRepoPage(req, repo, 'activity', null, title, cat([
759 pull.once('<a href="?raw" class="raw-link header-align">' +
760 req._t('Data') + '</a>' +
761 '<h3>' + req._t('Update') + '</h3>' +
762 renderRepoUpdate(req, repo, {key: id, value: msg}, true)),
763 (msg.content.objects || msg.content.packs) &&
764 pull.once('<h3>' + req._t('Commits') + '</h3>'),
765 pull(commits, pull.map(function (commit) {
766 return renderCommit(req, repo, commit)
767 }))
768 ]))
769}
770
771/* Blob */
772
773R.serveRepoBlob = function (req, repo, rev, path) {
774 var self = this
775 return u.readNext(function (cb) {
776 repo.getFile(rev, path, function (err, object) {
777 if (err) return cb(null, self.web.serveBlobNotFound(req, repo.id, err))
778 var type = repo.isCommitHash(rev) ? 'Tree' : 'Branch'
779 var pathLinks = path.length === 0 ? '' :
780 ': ' + linkPath([repo.id, 'tree'], [rev].concat(path))
781 var rawFilePath = [repo.id, 'raw', rev].concat(path)
782 var dirPath = path.slice(0, path.length-1)
783 var filename = path[path.length-1]
784 var extension = u.getExtension(filename)
785 var title = (path.length ? path.join('/') + ' · ' : '') +
786 '%{author}/%{repo}' +
787 (repo.head == 'refs/heads/' + rev ? '' : '@' + rev)
788 cb(null, self.renderRepoPage(req, repo, 'code', rev, title, cat([
789 pull.once('<section><form action="" method="get">' +
790 '<h3>' + req._t(type) + ': ' + rev + ' '),
791 self.revMenu(req, repo, rev),
792 pull.once('</h3></form>'),
793 type == 'Branch' && renderRepoLatest(req, repo, rev),
794 pull.once('</section><section class="collapse">' +
795 '<h3>' + req._t('Files') + pathLinks + '</h3>' +
796 '<div>' + object.length + ' bytes' +
797 '<span class="raw-link">' +
798 u.link(rawFilePath, req._t('Raw')) + '</span>' +
799 '</div></section>' +
800 '<section>'),
801 extension in u.imgMimes
802 ? pull.once('<img src="' + u.encodeLink(rawFilePath) +
803 '" alt="' + u.escape(filename) + '" />')
804 : self.web.renderObjectData(object, filename, repo, rev, dirPath),
805 pull.once('</section>')
806 ])))
807 })
808 })
809}
810
811/* Raw blob */
812
813R.serveRepoRaw = function (req, repo, branch, path) {
814 var self = this
815 return u.readNext(function (cb) {
816 repo.getFile(branch, path, function (err, object) {
817 if (err) return cb(null,
818 self.web.serveBuffer(404, req._t('error.BlobNotFound')))
819 var extension = u.getExtension(path[path.length-1])
820 var contentType = u.imgMimes[extension]
821 cb(null, pull(object.read, self.web.serveRaw(object.length, contentType)))
822 })
823 })
824}
825
826/* Digs */
827
828R.serveRepoDigs = function (req, repo) {
829 var self = this
830 return u.readNext(function (cb) {
831 var title = req._t('Digs') + ' · %{author}/%{repo}'
832 self.web.getVotes(repo.id, function (err, votes) {
833 cb(null, self.renderRepoPage(req, repo, null, null, title, cat([
834 pull.once('<section><h3>' + req._t('Digs') + '</h3>' +
835 '<div>' + req._t('Total') + ': ' + votes.upvotes + '</div>'),
836 pull(
837 pull.values(Object.keys(votes.upvoters)),
838 paramap(function (feedId, cb) {
839 self.web.about.getName(feedId, function (err, name) {
840 if (err) return cb(err)
841 cb(null, u.link([feedId], name))
842 })
843 }, 8),
844 ul()
845 ),
846 pull.once('</section>')
847 ])))
848 })
849 })
850}
851
852/* Forks */
853
854R.getForks = function (repo, includeSelf) {
855 var self = this
856 return pull(
857 cat([
858 includeSelf && u.readOnce(function (cb) {
859 self.web.getMsg(repo.id, function (err, value) {
860 cb(err, value && {key: repo.id, value: value})
861 })
862 }),
863 self.web.ssb.links({
864 dest: repo.id,
865 values: true,
866 rel: 'upstream'
867 })
868 ]),
869 pull.filter(function (msg) {
870 var c = msg && msg.value && msg.value.content
871 return c && c.type == 'git-repo'
872 }),
873 paramap(function (msg, cb) {
874 self.web.getRepoFullName(msg.value.author, msg.key,
875 function (err, repoName, authorName) {
876 if (err) return cb(err)
877 cb(null, {
878 key: msg.key,
879 value: msg.value,
880 repoName: repoName,
881 authorName: authorName
882 })
883 })
884 }, 8)
885 )
886}
887
888R.serveRepoForks = function (req, repo) {
889 var hasForks
890 var title = req._t('Forks') + ' · %{author}/%{repo}'
891 return this.renderRepoPage(req, repo, null, null, title, cat([
892 pull.once('<h3>' + req._t('Forks') + '</h3>'),
893 pull(
894 this.getForks(repo),
895 pull.map(function (msg) {
896 hasForks = true
897 return '<section class="collapse">' +
898 u.link([msg.value.author], msg.authorName) + ' / ' +
899 u.link([msg.key], msg.repoName) +
900 '<span class="right-bar">' +
901 u.timestamp(msg.value.timestamp, req) +
902 '</span></section>'
903 })
904 ),
905 u.readOnce(function (cb) {
906 cb(null, hasForks ? '' : req._t('NoForks'))
907 })
908 ]))
909}
910
911R.serveRepoForkPrompt = function (req, repo) {
912 var title = req._t('Fork') + ' · %{author}/%{repo}'
913 return this.renderRepoPage(req, repo, null, null, title, pull.once(
914 '<form action="" method="post" onreset="history.back()">' +
915 '<h3>' + req._t('ForkRepoPrompt') + '</h3>' +
916 '<p>' + u.hiddenInputs({ id: repo.id }) +
917 '<button class="btn open" type="submit" name="action" value="fork">' +
918 req._t('Fork') +
919 '</button>' +
920 ' <button class="btn" type="reset">' +
921 req._t('Cancel') + '</button>' +
922 '</p></form>'
923 ))
924}
925
926R.serveIssueOrPullRequest = function (req, repo, issue, path, id) {
927 return issue.msg.value.content.type == 'pull-request'
928 ? this.pulls.serveRepoPullReq(req, repo, issue, path, id)
929 : this.issues.serveRepoIssue(req, repo, issue, path, id)
930}
931

Built with git-ssb-web