git ssb

30+

cel / git-ssb-web



Commit 63bcff079120449a73ea2cceec00484d3378041c

Start adding line comments

cel committed on 11/21/2017, 9:40:11 PM
Parent: 47dad06f4a54a8840baed0e2b5ad39a6ba75cf16

Files changed

index.jschanged
lib/forms.jschanged
lib/repos/index.jschanged
lib/repos/pulls.jschanged
locale/en.jsonchanged
locale/eo.jsonchanged
schemas.mdadded
index.jsView
@@ -327,8 +327,46 @@
327327 if (err) return cb(null, self.serveError(req, err))
328328 cb(null, self.serveRedirect(req, req.url))
329329 })
330330
331 + case 'line-comment':
332 + if (!data.repo)
333 + return cb(null, self.serveError(req,
334 + new ParamError('missing repo id'), 400))
335 + if (!data.commitId)
336 + return cb(null, self.serveError(req,
337 + new ParamError('missing commit id'), 400))
338 + if (!data.filePath)
339 + return cb(null, self.serveError(req,
340 + new ParamError('missing file path'), 400))
341 + if (!data.line)
342 + return cb(null, self.serveError(req,
343 + new ParamError('missing line number'), 400))
344 + var lineNumber = Number(data.line)
345 + if (isNaN(lineNumber))
346 + return cb(null, self.serveError(req,
347 + new ParamError('bad line number'), 400))
348 + var repoBranches = data.repoBranch
349 + ? data.repoBranch.split(',') : ''
350 + var msg = {
351 + type: 'line-comment',
352 + text: data.text,
353 + repo: data.repo,
354 + repoBranch: repoBranches,
355 + commitId: data.commitId,
356 + filePath: data.filePath,
357 + line: lineNumber,
358 + }
359 + msg.issue = data.issue
360 + var mentions = Mentions(data.text)
361 + if (mentions.length)
362 + msg.mentions = mentions
363 + return cb(null, self.serveBuffer(200, JSON.stringify(msg, 0, 2)))
364 + return self.ssb.publish(msg, function (err) {
365 + if (err) return cb(null, self.serveError(req, err))
366 + cb(null, self.serveRedirect(req, req.url))
367 + })
368 +
331369 case 'new-issue':
332370 var msg = Issues.schemas.new(dir, data.text)
333371 var mentions = Mentions(data.text)
334372 if (mentions.length)
lib/forms.jsView
@@ -10,9 +10,11 @@
1010 req._t('post.Write') + '</label>' +
1111 '<label for="tab2" id="preview-tab-link" class="tab2-link">' +
1212 req._t('post.Preview') + '</label>' +
1313 '</div>' +
14- '<input type="hidden" id="repo-id" value="' + repo.id + '"/>' +
14 + (repo ?
15 + '<input type="hidden" id="repo-id" value="' + repo.id + '"/>'
16 + : '') +
1517 '<div id="write-tab" class="tab1">' +
1618 '<textarea id="post-text" name="text" class="wide-input"' +
1719 ' rows="' + (rows||4) + '" cols="77"' +
1820 (placeholder ? ' placeholder="' + placeholder + '"' : '') +
@@ -99,4 +101,18 @@
99101 '/>' +
100102 '<script>' + issueCommentButtonScript + '</script>' +
101103 '</form></section>'
102104 }
105 +
106 +forms.lineComment = function (req, repo, repoBranch, commitId, filePath, line) {
107 + return '<section><form action="" method="post">' +
108 + '<input type="hidden" name="action" value="line-comment">' +
109 + '<input type="hidden" name="repoBranch" value="' + repoBranch.join(',') + '">' +
110 + '<input type="hidden" name="repo" value="' + repo.id + '">' +
111 + '<input type="hidden" name="commitId" value="' + commitId + '">' +
112 + '<input type="hidden" name="filePath" value="' + filePath + '">' +
113 + '<input type="hidden" name="line" value="' + line + '">' +
114 + forms.post(req, repo) +
115 + '<input type="submit" class="btn open" value="' +
116 + req._t('issue.LineComment') + '" />' +
117 + '</form></section>'
118 +}
lib/repos/index.jsView
@@ -593,8 +593,9 @@
593593 /* Repo commit */
594594
595595 R.serveRepoCommit = function (req, repo, rev) {
596596 var self = this
597 + var repoBranch = []
597598 return u.readNext(function (cb) {
598599 repo.getCommitParsed(rev, function (err, commit) {
599600 if (err) return cb(null,
600601 self.serveRepoTemplate(req, repo, null, rev, `%{author}/%{repo}@${rev}`,
@@ -627,9 +628,9 @@
627628 }).join('<br>') +
628629 '</section>' +
629630 '<section><h3>' + req._t('FilesChanged') + '</h3>'),
630631 // TODO: show diff from all parents (merge commits)
631- self.renderDiffStat(req, [repo, repo], [commit.parents[0], commit.id]),
632 + self.renderDiffStat(req, [repo, repo], [commit.parents[0], commit.id], commit.id, repoBranch),
632633 pull.once('</section>')
633634 ])))
634635 })
635636 })
@@ -672,9 +673,9 @@
672673
673674
674675 /* Diff stat */
675676
676-R.renderDiffStat = function (req, repos, treeIds) {
677 +R.renderDiffStat = function (req, repos, treeIds, commit, repoBranch) {
677678 if (treeIds.length == 0) treeIds = [null]
678679 var id = treeIds[0]
679680 var lastI = treeIds.length - 1
680681 var oldTree = treeIds[0]
@@ -737,18 +738,19 @@
737738 getRepoObjectString(repos[0], item.id[0], mode0, done())
738739 getRepoObjectString(repos[1], item.id[lastI], modeI, done())
739740 done(function (err, strOld, strNew) {
740741 if (err) return cb(err)
741- cb(null, htmlLineDiff(req, item.filename, item.filename,
742 + var repo = repos[1]
743 + cb(null, htmlLineDiff(req, repo, repoBranch, commit, item.filename, item.filename,
742744 strOld, strNew,
743745 u.encodeLink(item.blobPath), !isSubmodule))
744746 })
745747 }, 4)
746748 )
747749 ])
748750 }
749751
750-function htmlLineDiff(req, filename, anchor, oldStr, newStr, blobHref,
752 +function htmlLineDiff(req, repo, repoBranch, commit, filename, anchor, oldStr, newStr, blobHref,
751753 showViewLink) {
752754 return '<pre><table class="code">' +
753755 '<tr><th colspan=3 id="' + u.escape(anchor) + '">' + filename +
754756 (showViewLink === false ? '' :
@@ -756,16 +758,17 @@
756758 '<a href="' + blobHref + '">' + req._t('View') + '</a> ' +
757759 '</span>') +
758760 '</th></tr>' +
759761 (oldStr.length + newStr.length > 200000
760- ? '<tr><td class="diff-info">' + req._t('diff.TooLarge') + '<br>' +
762 + ? '<tr><td class="diff-info" colspan=3>' + req._t('diff.TooLarge') + '<br>' +
761763 req._t('diff.OldFileSize', {bytes: oldStr.length}) + '<br>' +
762764 req._t('diff.NewFileSize', {bytes: newStr.length}) + '</td></tr>'
763- : tableDiff(oldStr, newStr, filename)) +
765 + : tableDiff(req, repo, repoBranch, commit, oldStr, newStr, filename)) +
764766 '</table></pre>'
765767 }
766768
767-function tableDiff(oldStr, newStr, filename) {
769 +function tableDiff(req, repo, repoBranch, commit, oldStr, newStr, filename) {
770 + var query = req._u.query
768771 var diff = JsDiff.structuredPatch('', '', oldStr, newStr)
769772 var groups = diff.hunks.map(function (hunk) {
770773 var oldLine = hunk.oldStart
771774 var newLine = hunk.newStart
@@ -780,14 +783,25 @@
780783 var trClass = s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : ''
781784 var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++]
782785 var id = [filename].concat(lineNums).join('-')
783786 return '<tr id="' + u.escape(id) + '" class="' + trClass + '">' +
784- lineNums.map(function (num) {
787 + lineNums.map(function (num, i) {
788 + var idEnc = encodeURIComponent(id)
785789 return '<td class="code-linenum">' +
786- (num ? '<a href="#' + encodeURIComponent(id) + '">' +
787- num + '</a>' : '') + '</td>'
790 + (num ? '<a href="#' + idEnc + '">' +
791 + num + '</a>' +
792 + (i === lineNums.length-1 && s !== '-' ?
793 + // TODO: use a more descriptive icon for the comment action
794 + ' <a href="?comment=' + idEnc + '#' + idEnc + '">…</a>'
795 + : '')
796 + : '') + '</td>'
788797 }).join('') +
789- '<td class="code-text">' + html + '</td></tr>'
798 + '<td class="code-text">' + html + '</td></tr>' +
799 + (commit && query.comment === id ?
800 + '<tr><td colspan=4>' +
801 + forms.lineComment(req, repo, repoBranch, commit, filename, lineNums[lineNums.length-1]) +
802 + '</td></tr>'
803 + : '')
790804 }))
791805 })
792806 return [].concat.apply([], groups).join('')
793807 }
lib/repos/pulls.jsView
@@ -112,13 +112,14 @@
112112 if (!revs.head) return cb(null, pull.once('<section>' +
113113 req._t('pullRequest.NoChanges') + '</section>'))
114114 GitRepo.getMergeBase(baseRepo, revs.base, headRepo, revs.head,
115115 function (err, mergeBase) {
116 + var repoBranch = []
116117 if (err) return cb(err)
117118 cb(null, cat([
118119 pull.once('<section>'),
119120 self.repo.renderDiffStat(req,
120- [baseRepo, headRepo], [mergeBase, revs.head]),
121 + [baseRepo, headRepo], [mergeBase, revs.head], revs.head, repoBranch),
121122 pull.once('</section>')
122123 ]))
123124 }
124125 )
locale/en.jsonView
@@ -130,8 +130,9 @@
130130 "Mentioned": "%{name} mentioned this %{type}",
131131 "MentionedIn": "%{name} mentioned this %{type} in %{post}",
132132 "Renamed": "%{author} renamed this %{type} to %{name}",
133133 "Comment": "Comment",
134 + "LineComment": "Line Comment",
134135 "Close": "Close %{type}",
135136 "CommentAndClose": "Comment and Close",
136137 "Reopen": "Reopen %{type}",
137138 "CommentAndReopen": "Comment and Reopen"
locale/eo.jsonView
@@ -130,8 +130,9 @@
130130 "Mentioned": "%{name} menciis tiun %{type}",
131131 "MentionedIn": "%{name} menciis tiun %{type} en %{post}",
132132 "Renamed": "%{author} renomiĝis tiun %{type} al %{name}",
133133 "Comment": "Komenti",
134 + "LineComment": "Linio-Komenti",
134135 "Close": "Fermi %{type}",
135136 "CommentAndClose": "Komenti kaj Fermi",
136137 "Reopen": "Remalfermi %{type}",
137138 "CommandAndReopen": "Komenti kaj Remalfermi"
schemas.mdView
@@ -1,0 +1,19 @@
1 +### Line comment
2 +
3 +```
4 +{
5 + "type": "line-comment",
6 + "text": string,
7 + "repo": MsgId,
8 + "repoBranch": MgsIds,
9 + "line": number,
10 +}
11 +```
12 +`repo`: id of `git-repo`
13 +`repoBranch`: id(s) of `git-update` messages that pushed the git commit, git trees, and git blobs needed to render the diff.
14 +`commitId`: commit that the comment is on
15 +`filePath`: path to the file that the comment is on
16 +`line`: line number of the file that the comment is on
17 +
18 +Replies to a line comment should be messages of type `post` with `root`
19 +linking to the `line-comment` message.

Built with git-ssb-web