git ssb

16+

cel / patchfoo



Commit 09cf04663fe2a772d5a06bbcf86b9014dc4f5228

Render line comments on diffs

cel committed on 1/9/2018, 8:03:51 AM
Parent: cbf9a1d87eb43eaea73e6c019e7b4a2dca3bbaa6

Files changed

lib/app.jschanged
lib/render-msg.jschanged
lib/serve.jschanged
lib/app.jsView
@@ -904,4 +904,79 @@
904904 links.forEach(addLink)
905905 }
906906 }
907907 }
908 +
909 +App.prototype.getLineComments = function (opts, cb) {
910 + // get line comments for a git-update message and git object id.
911 + // line comments include message id, commit id and path
912 + // but we have message id and git object hash.
913 + // look up the git object hash for each line-comment
914 + // to verify that it is for the git object file we want
915 + var updateId = opts.obj.msg.key
916 + var objId = opts.hash
917 + var self = this
918 + var lineComments = {}
919 + pull(
920 + self.sbot.backlinks ? self.sbot.backlinks.read({
921 + query: [
922 + {$filter: {
923 + dest: updateId,
924 + value: {
925 + content: {
926 + type: 'line-comment',
927 + updateId: updateId,
928 + }
929 + }
930 + }}
931 + ]
932 + }) : pull(
933 + self.sbot.links({
934 + dest: updateId,
935 + rel: 'updateId',
936 + values: true
937 + }),
938 + pull.filter(function (msg) {
939 + var c = msg && msg.value && msg.value.content
940 + return c && c.type === 'line-comment'
941 + && c.updateId === updateId
942 + })
943 + ),
944 + paramap(function (msg, cb) {
945 + var c = msg.value.content
946 + self.git.getObjectAtPath({
947 + msg: updateId,
948 + obj: c.commitId,
949 + path: c.filePath,
950 + }, function (err, info) {
951 + if (err) return cb(err)
952 + cb(null, {
953 + obj: info.obj,
954 + hash: info.hash,
955 + msg: msg,
956 + })
957 + })
958 + }, 4),
959 + pull.filter(function (info) {
960 + return info.hash === objId
961 + }),
962 + pull.drain(function (info) {
963 + lineComments[info.msg.value.content.line] = info
964 + }, function (err) {
965 + cb(err, lineComments)
966 + })
967 + )
968 +}
969 +
970 +App.prototype.getThread = function (msg) {
971 + return cat([
972 + pull.once(msg),
973 + this.sbot.backlinks ? this.sbot.backlinks.read({
974 + query: [
975 + {$filter: {dest: msg.key}}
976 + ]
977 + }) : this.sbot.links({
978 + dest: msg.key,
979 + values: true
980 + })
981 + ])
982 +}
lib/render-msg.jsView
@@ -1667,9 +1667,12 @@
16671667 ), ' ',
16681668 h('a', {
16691669 href: self.toUrl('/git/commit/' + self.c.commitId + '?msg=' + encodeURIComponent(self.c.updateId))
16701670 }, String(self.c.commitId).substr(0, 8)), ' ',
1671- h('code', self.c.filePath + ':' + self.c.line)
1671 + h('a', {
1672 + href: self.toUrl('/git/line-comment/' +
1673 + encodeURIComponent(self.msg.key))
1674 + }, h('code', self.c.filePath + ':' + self.c.line))
16721675 )),
16731676 self.c.text ?
16741677 h('div', {innerHTML: self.render.markdown(self.c.text)}) : ''), cb)
16751678 })
lib/serve.jsView
@@ -1650,8 +1650,9 @@
16501650 case 'tree': return this.gitTree(m[2])
16511651 case 'blob': return this.gitBlob(m[2])
16521652 case 'raw': return this.gitRaw(m[2])
16531653 case 'diff': return this.gitDiff(m[2])
1654 + case 'line-comment': return this.gitLineComment(m[2])
16541655 default: return this.respond(404, 'Not found')
16551656 }
16561657 }
16571658
@@ -2113,14 +2114,18 @@
21132114
21142115 var done = multicb({pluck: 1, spread: true})
21152116 pull.collect(done())(self.app.git.readObject(obj1))
21162117 pull.collect(done())(self.app.git.readObject(obj2))
2117- done(function (err, bufs1, bufs2) {
2118 + self.app.getLineComments({obj: obj2, hash: rev2}, done())
2119 + done(function (err, bufs1, bufs2, lineComments) {
21182120 if (err) return cb(err)
21192121 var str1 = Buffer.concat(bufs1, obj1.length).toString('utf8')
21202122 var str2 = Buffer.concat(bufs2, obj2.length).toString('utf8')
21212123 var diff = Diff.structuredPatch('', '', str1, str2)
2122- cb(null, self.gitDiffTable(diff))
2124 + cb(null, self.gitDiffTable(diff, lineComments, {
2125 + obj: obj2,
2126 + hash: rev2,
2127 + }))
21232128 })
21242129 })
21252130 })
21262131 ]),
@@ -2129,14 +2134,12 @@
21292134 )
21302135 })
21312136 }
21322137
2133-Serve.prototype.gitDiffTable = function (diff) {
2138 +Serve.prototype.gitDiffTable = function (diff, lineComments, lineCommentInfo) {
21342139 var self = this
21352140 return pull(
21362141 ph('table', [
2137- ph('tr', [
2138- ]),
21392142 pull(
21402143 pull.values(diff.hunks),
21412144 pull.map(function (hunk) {
21422145 var oldLine = hunk.oldStart
@@ -2156,41 +2159,44 @@
21562159 var html = self.app.render.highlight(line)
21572160 var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++]
21582161 // var id = [filename].concat(lineNums).join('-')
21592162 var newLineNum = lineNums[lineNums.length-1]
2160- return ph('tr', {
2161- class: s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : ''
2162- }, [
2163- lineNums.map(function (num, i) {
2164- return ph('td', String(num))
2165- // TODO: allow linking to comments
2166- /*
2167- var idEnc = encodeURIComponent(id)
2168- return '<td class="code-linenum">' +
2169- (num ? '<a href="#' + idEnc + '">' +
2170- num + '</a>' +
2171- (updateId && i === lineNums.length-1 && s !== '-' ?
2172- ' <a href="?comment=' + idEnc + '#' + idEnc + '">…</a>'
2173- : '')
2174- : '') + '</td>'
2175- }
2176- */
2177- }),
2178- ph('td', ph('pre', u.escapeHTML(html)))
2179- ])
2180-
2181- // TODO: line-comments
2182- /*
2183- (lineCommentThreads[newLineNum] ?
2184- '<tr><td colspan=4>' +
2185- lineCommentThreads[newLineNum] +
2186- '</td></tr>'
2187- : commit && query.comment === id ?
2188- '<tr><td colspan=4>' +
2189- forms.lineComment(req, repo, updateId, commit, filename, newLineNum) +
2190- '</td></tr>'
2191- : '')
2192- */
2163 + return [
2164 + ph('tr', {
2165 + class: s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : ''
2166 + }, [
2167 + lineNums.map(function (num, i) {
2168 + return ph('td', String(num))
2169 + // TODO: allow linking to comments
2170 + /*
2171 + var idEnc = encodeURIComponent(id)
2172 + return '<td class="code-linenum">' +
2173 + (num ? '<a href="#' + idEnc + '">' +
2174 + num + '</a>' +
2175 + (updateId && i === lineNums.length-1 && s !== '-' ?
2176 + ' <a href="?comment=' + idEnc + '#' + idEnc + '">…</a>'
2177 + : '')
2178 + : '') + '</td>'
2179 + }
2180 + */
2181 + }),
2182 + ph('td', ph('pre', u.escapeHTML(html)))
2183 + ]),
2184 + (lineComments[newLineNum] ?
2185 + ph('tr',
2186 + ph('td', {colspan: 4},
2187 + self.renderLineCommentThread(lineComments[newLineNum])
2188 + )
2189 + )
2190 + : /*commit && query.comment === id ?
2191 + ph('tr',
2192 + ph('td', {colspan: 4},
2193 + // self.renderLineCommentForm(req, repo, updateId, commit, filename, newLineNum)
2194 + self.renderLineCommentForm(lineCommentInfo)
2195 + )
2196 + )
2197 + :*/ '')
2198 + ]
21932199 })
21942200 )
21952201 ]
21962202 })
@@ -2198,8 +2204,59 @@
21982204 ])
21992205 )
22002206 }
22012207
2208 +Serve.prototype.renderLineCommentThread = function (lineComment) {
2209 + var self = this
2210 + return ph('table', {class: 'ssb-msgs'}, pull(
2211 + this.app.getThread(lineComment.msg),
2212 + self.renderThread()
2213 + ))
2214 +}
2215 +
2216 +Serve.prototype.renderLineCommentForm = function (info) {
2217 + var obj = info.obj
2218 + var hash = info.hash
2219 + return ph('pre', JSON.stringify(info, 0, 2))
2220 +}
2221 +
2222 +Serve.prototype.gitLineComment = function (path) {
2223 + var self = this
2224 + var id = decodeURIComponent(String(path))
2225 + self.getMsgDecryptedMaybeOoo(id, function (err, msg) {
2226 + if (err) return pull(
2227 + pull.once(u.renderError(err).outerHTML),
2228 + self.respondSink(400, {'Content-Type': ctype('html')})
2229 + )
2230 + var c = msg && msg.value && msg.value.content
2231 + if (!c) return pull(
2232 + pull.once('Missing message ' + id),
2233 + self.respondSink(500, {'Content-Type': ctype('html')})
2234 + )
2235 + self.app.git.getObjectAtPath({
2236 + msg: c.updateId,
2237 + obj: c.commitId,
2238 + path: c.filePath,
2239 + }, function (err, obj) {
2240 + if (err && err.name === 'BlobNotFoundError')
2241 + return self.askWantBlobs(err.links)
2242 + if (err) return pull(
2243 + pull.once(err.stack),
2244 + self.respondSink(400, {'Content-Type': 'text/plain'})
2245 + )
2246 +
2247 + self.redirect(self.app.render.toUrl(publishedMsg.key))
2248 +
2249 + /*
2250 + pull(
2251 + ph('pre', JSON.stringify(obj, 0, 2)),
2252 + self.respondSink(200)
2253 + )
2254 + */
2255 + })
2256 + })
2257 +}
2258 +
22022259 Serve.prototype.gitObjectLinks = function (headMsgId, type) {
22032260 var self = this
22042261 return paramap(function (id, cb) {
22052262 self.app.git.getObjectMsg({

Built with git-ssb-web