git ssb

30+

cel / git-ssb-web



Tree: 09a90c8c8dd420350b85fbb6d781b88a9786512f

Files: 09a90c8c8dd420350b85fbb6d781b88a9786512f / lib / repos / index.js

32399 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 var diff = JsDiff.structuredPatch('', '', oldStr, newStr)
657 var groups = diff.hunks.map(function (hunk) {
658 var oldLine = hunk.oldStart
659 var newLine = hunk.newStart
660 var header = '<tr class="diff-hunk-header"><td colspan=2></td><td>' +
661 '@@ -' + oldLine + ',' + hunk.oldLines + ' ' +
662 '+' + newLine + ',' + hunk.newLines + ' @@' +
663 '</td></tr>'
664 return [header].concat(hunk.lines.map(function (line) {
665 var s = line[0]
666 if (s == '\\') return
667 var html = u.highlight(line, u.getExtension(filename))
668 var trClass = s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : ''
669 var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++]
670 var id = [filename].concat(lineNums).join('-')
671 return '<tr id="' + u.escape(id) + '" class="' + trClass + '">' +
672 lineNums.map(function (num) {
673 return '<td class="code-linenum">' +
674 (num ? '<a href="#' + encodeURIComponent(id) + '">' +
675 num + '</a>' : '') + '</td>'
676 }).join('') +
677 '<td class="code-text">' + html + '</td></tr>'
678 }))
679 })
680 return '<pre><table class="code">' +
681 '<tr><th colspan=3 id="' + u.escape(anchor) + '">' + filename +
682 (showViewLink === false ? '' :
683 '<span class="right-bar">' +
684 '<a href="' + blobHref + '">' + req._t('View') + '</a> ' +
685 '</span>') +
686 '</th></tr>' +
687 [].concat.apply([], groups).join('') +
688 '</table></pre>'
689}
690
691/* An unknown message linking to a repo */
692
693R.serveRepoSomething = function (req, repo, id, msg, path) {
694 return this.renderRepoPage(req, repo, null, null, null,
695 pull.once('<section><h3>' + u.link([id]) + '</h3>' +
696 u.json(msg) + '</section>'))
697}
698
699/* Repo update */
700
701function objsArr(objs) {
702 return Array.isArray(objs) ? objs :
703 Object.keys(objs).map(function (sha1) {
704 var obj = Object.create(objs[sha1])
705 obj.sha1 = sha1
706 return obj
707 })
708}
709
710R.serveRepoUpdate = function (req, repo, id, msg, path) {
711 var self = this
712 var raw = req._u.query.raw != null
713 var title = req._t('Update') + ' · %{author}/%{repo}'
714
715 if (raw)
716 return self.renderRepoPage(req, repo, 'activity', null, title, pull.once(
717 '<a href="?" class="raw-link header-align">' +
718 req._t('Info') + '</a>' +
719 '<h3>' + req._t('Update') + '</h3>' +
720 '<section class="collapse">' +
721 u.json({key: id, value: msg}) + '</section>'))
722
723 // convert packs to old single-object style
724 if (msg.content.indexes) {
725 for (var i = 0; i < msg.content.indexes.length; i++) {
726 msg.content.packs[i] = {
727 pack: {link: msg.content.packs[i].link},
728 idx: msg.content.indexes[i]
729 }
730 }
731 }
732
733 var commits = cat([
734 msg.content.objects && pull(
735 pull.values(msg.content.objects),
736 pull.filter(function (obj) { return obj.type == 'commit' }),
737 paramap(function (obj, cb) {
738 self.web.getBlob(req, obj.link || obj.key, function (err, readObject) {
739 if (err) return cb(err)
740 GitRepo.getCommitParsed({read: readObject}, cb)
741 })
742 }, 8)
743 ),
744 msg.content.packs && pull(
745 pull.values(msg.content.packs),
746 paramap(function (pack, cb) {
747 var done = multicb({ pluck: 1, spread: true })
748 self.web.getBlob(req, pack.pack.link, done())
749 self.web.getBlob(req, pack.idx.link, done())
750 done(function (err, readPack, readIdx) {
751 if (err) return cb(self.web.renderError(err))
752 cb(null, gitPack.decodeWithIndex(repo, readPack, readIdx))
753 })
754 }, 4),
755 pull.flatten(),
756 pull.asyncMap(function (obj, cb) {
757 if (obj.type == 'commit')
758 GitRepo.getCommitParsed(obj, cb)
759 else
760 pull(obj.read, pull.drain(null, cb))
761 }),
762 pull.filter()
763 )
764 ])
765
766 return self.renderRepoPage(req, repo, 'activity', null, title, cat([
767 pull.once('<a href="?raw" class="raw-link header-align">' +
768 req._t('Data') + '</a>' +
769 '<h3>' + req._t('Update') + '</h3>' +
770 renderRepoUpdate(req, repo, {key: id, value: msg}, true)),
771 (msg.content.objects || msg.content.packs) &&
772 pull.once('<h3>' + req._t('Commits') + '</h3>'),
773 pull(commits, pull.map(function (commit) {
774 return renderCommit(req, repo, commit)
775 }))
776 ]))
777}
778
779/* Blob */
780
781R.serveRepoBlob = function (req, repo, rev, path) {
782 var self = this
783 return u.readNext(function (cb) {
784 repo.getFile(rev, path, function (err, object) {
785 if (err) return cb(null, self.web.serveBlobNotFound(req, repo.id, err))
786 var type = repo.isCommitHash(rev) ? 'Tree' : 'Branch'
787 var pathLinks = path.length === 0 ? '' :
788 ': ' + linkPath([repo.id, 'tree'], [rev].concat(path))
789 var rawFilePath = [repo.id, 'raw', rev].concat(path)
790 var dirPath = path.slice(0, path.length-1)
791 var filename = path[path.length-1]
792 var extension = u.getExtension(filename)
793 var title = (path.length ? path.join('/') + ' · ' : '') +
794 '%{author}/%{repo}' +
795 (repo.head == 'refs/heads/' + rev ? '' : '@' + rev)
796 cb(null, self.renderRepoPage(req, repo, 'code', rev, title, cat([
797 pull.once('<section><form action="" method="get">' +
798 '<h3>' + req._t(type) + ': ' + rev + ' '),
799 self.revMenu(req, repo, rev),
800 pull.once('</h3></form>'),
801 type == 'Branch' && renderRepoLatest(req, repo, rev),
802 pull.once('</section><section class="collapse">' +
803 '<h3>' + req._t('Files') + pathLinks + '</h3>' +
804 '<div>' + object.length + ' bytes' +
805 '<span class="raw-link">' +
806 u.link(rawFilePath, req._t('Raw')) + '</span>' +
807 '</div></section>' +
808 '<section>'),
809 extension in u.imgMimes
810 ? pull.once('<img src="' + u.encodeLink(rawFilePath) +
811 '" alt="' + u.escape(filename) + '" />')
812 : self.web.renderObjectData(object, filename, repo, rev, dirPath),
813 pull.once('</section>')
814 ])))
815 })
816 })
817}
818
819/* Raw blob */
820
821R.serveRepoRaw = function (req, repo, branch, path) {
822 var self = this
823 return u.readNext(function (cb) {
824 repo.getFile(branch, path, function (err, object) {
825 if (err) return cb(null,
826 self.web.serveBuffer(404, req._t('error.BlobNotFound')))
827 var extension = u.getExtension(path[path.length-1])
828 var contentType = u.imgMimes[extension]
829 cb(null, pull(object.read, self.web.serveRaw(object.length, contentType)))
830 })
831 })
832}
833
834/* Digs */
835
836R.serveRepoDigs = function (req, repo) {
837 var self = this
838 return u.readNext(function (cb) {
839 var title = req._t('Digs') + ' · %{author}/%{repo}'
840 self.web.getVotes(repo.id, function (err, votes) {
841 cb(null, self.renderRepoPage(req, repo, null, null, title, cat([
842 pull.once('<section><h3>' + req._t('Digs') + '</h3>' +
843 '<div>' + req._t('Total') + ': ' + votes.upvotes + '</div>'),
844 pull(
845 pull.values(Object.keys(votes.upvoters)),
846 paramap(function (feedId, cb) {
847 self.web.about.getName(feedId, function (err, name) {
848 if (err) return cb(err)
849 cb(null, u.link([feedId], name))
850 })
851 }, 8),
852 ul()
853 ),
854 pull.once('</section>')
855 ])))
856 })
857 })
858}
859
860/* Forks */
861
862R.getForks = function (repo, includeSelf) {
863 var self = this
864 return pull(
865 cat([
866 includeSelf && pull.once(repo.id),
867 // get downstream repos
868 pull(
869 self.web.ssb.links({
870 dest: repo.id,
871 rel: 'upstream'
872 }),
873 pull.map('key')
874 ),
875 // look for other repos that previously had pull requests to this one
876 pull(
877 self.web.ssb.links({
878 dest: repo.id,
879 values: true,
880 rel: 'project'
881 }),
882 pull.filter(function (msg) {
883 var c = msg && msg.value && msg.value.content
884 return c && c.type == 'pull-request'
885 }),
886 pull.map(function (msg) { return msg.value.content.head_repo })
887 )
888 ]),
889 pull.unique(),
890 paramap(function (key, cb) {
891 self.web.ssb.get(key, function (err, value) {
892 if (err) cb(err)
893 else cb(null, {key: key, value: value})
894 })
895 }, 4),
896 pull.filter(function (msg) {
897 var c = msg && msg.value && msg.value.content
898 return c && c.type == 'git-repo'
899 }),
900 paramap(function (msg, cb) {
901 self.web.getRepoFullName(msg.value.author, msg.key,
902 function (err, repoName, authorName) {
903 if (err) return cb(err)
904 cb(null, {
905 key: msg.key,
906 value: msg.value,
907 repoName: repoName,
908 authorName: authorName
909 })
910 })
911 }, 8)
912 )
913}
914
915R.serveRepoForks = function (req, repo) {
916 var hasForks
917 var title = req._t('Forks') + ' · %{author}/%{repo}'
918 return this.renderRepoPage(req, repo, null, null, title, cat([
919 pull.once('<h3>' + req._t('Forks') + '</h3>'),
920 pull(
921 this.getForks(repo),
922 pull.map(function (msg) {
923 hasForks = true
924 return '<section class="collapse">' +
925 u.link([msg.value.author], msg.authorName) + ' / ' +
926 u.link([msg.key], msg.repoName) +
927 '<span class="right-bar">' +
928 u.timestamp(msg.value.timestamp, req) +
929 '</span></section>'
930 })
931 ),
932 u.readOnce(function (cb) {
933 cb(null, hasForks ? '' : req._t('NoForks'))
934 })
935 ]))
936}
937
938R.serveRepoForkPrompt = function (req, repo) {
939 var title = req._t('Fork') + ' · %{author}/%{repo}'
940 return this.renderRepoPage(req, repo, null, null, title, pull.once(
941 '<form action="" method="post" onreset="history.back()">' +
942 '<h3>' + req._t('ForkRepoPrompt') + '</h3>' +
943 '<p>' + u.hiddenInputs({ id: repo.id }) +
944 '<button class="btn open" type="submit" name="action" value="fork">' +
945 req._t('Fork') +
946 '</button>' +
947 ' <button class="btn" type="reset">' +
948 req._t('Cancel') + '</button>' +
949 '</p></form>'
950 ))
951}
952
953R.serveIssueOrPullRequest = function (req, repo, issue, path, id) {
954 return issue.msg.value.content.type == 'pull-request'
955 ? this.pulls.serveRepoPullReq(req, repo, issue, path, id)
956 : this.issues.serveRepoIssue(req, repo, issue, path, id)
957}
958

Built with git-ssb-web