From 52170b13bd7c8d5b248761354e13846f204df04a Mon Sep 17 00:00:00 2001 From: cel Date: Tue, 9 Jan 2018 20:38:46 -1000 Subject: Support line comments on blobs as well as diffs --- lib/serve.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 3 deletions(-) (limited to 'lib/serve.js') diff --git a/lib/serve.js b/lib/serve.js index aaf558f..4923667 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -24,6 +24,8 @@ var jpeg = require('jpeg-autorotate') var unzip = require('unzip') var Catch = require('pull-catch') var Diff = require('diff') +var split = require('pull-split') +var utf8 = require('pull-utf8-decoder') module.exports = Serve @@ -2032,6 +2034,7 @@ Serve.prototype.gitBlob = function (rev) { missingBlobs ? self.askWantBlobsForm(missingBlobs) : pull( self.app.git.readObject(obj), self.wrapBinary({ + obj: obj, rawUrl: self.app.render.toUrl('/git/raw/' + rev + '?msg=' + encodeURIComponent(msg.key)), ext: self.query.ext @@ -2624,6 +2627,7 @@ Serve.prototype.zip = function (url) { Serve.prototype.wrapBinary = function (opts) { var self = this var ext = opts.ext + var hash = opts.obj.hash return function (read) { var readRendered, type read = ident(function (_ext) { @@ -2660,9 +2664,69 @@ Serve.prototype.wrapBinary = function (opts) { })(read) })) } - return ph('pre', pull.map(function (buf) { - return self.app.render.highlight(buf.toString('utf8'), ext) - })(read)) + var i = 0 + var updateMsg = opts.obj.msg + var commitId = self.query.commit + var filePath = self.query.path + var lineComments = opts.lineComments || {} + return u.readNext(function (cb) { + if (commitId && filePath) { + self.app.getLineComments({ + obj: opts.obj, + hash: hash, + }, gotLineComments) + } else { + gotLineComments(null, {}) + } + function gotLineComments(err, lineComments) { + if (err) return cb(err) + cb(null, ph('table', + pull( + read, + utf8(), + split(), + pull.map(function (line) { + var lineNum = i++ + var id = hash + '-' + lineNum + var idEnc = encodeURIComponent(id) + return [ + ph('tr', [ + ph('td', ph('a', { + name: id, + href: '?msg=' + encodeURIComponent(self.query.msg) + + '&commit=' + encodeURIComponent(self.query.commit) + + '&path=' + encodeURIComponent(self.query.path) + + '&comment=' + idEnc + + '#' + idEnc + }, String(lineNum))), + ph('td', ph('pre', self.app.render.highlight(line, ext))) + ]), + (lineComments[lineNum] ? + ph('tr', + ph('td', {colspan: 4}, + self.renderLineCommentThread(lineComments[lineNum], id) + ) + ) + : self.query.comment === id ? + ph('tr', + ph('td', {colspan: 4}, + self.renderLineCommentForm({ + id: id, + line: lineNum, + updateId: updateMsg.key, + repoId: updateMsg.value.content.repo, + commitId: commitId, + filePath: filePath, + }) + ) + ) + : '') + ] + }) + ) + )) + } + }) } } -- cgit v1.2.3