From dd3e70672100b33e373e0fa69e56767b600bb6ec Mon Sep 17 00:00:00 2001 From: cel Date: Sun, 28 May 2017 18:00:41 -1000 Subject: factor out getting git object links --- lib/serve.js | 76 +++++++++++++++++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 42 deletions(-) (limited to 'lib/serve.js') diff --git a/lib/serve.js b/lib/serve.js index 64aa82b..9d907e0 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -1406,33 +1406,11 @@ Serve.prototype.gitCommit = function (rev) { ]) : '', commit.parents.length ? ph('div', ['parents: ', pull( pull.values(commit.parents), - paramap(function (id, cb) { - self.app.git.getObjectMsg({ - obj: id, - headMsgId: obj.msg.key, - }, function (err, msg) { - var path = '/git/commit/' + id - + '?msg=' + encodeURIComponent(msg.key) - cb(null, [ph('code', ph('a', { - href: self.app.render.toUrl(path) - }, id.substr(0, 8))), ' ']) - }) - }, 4) + self.gitObjectLinks(obj.msg.key, 'commit') )]) : '', commit.tree ? ph('div', ['tree: ', pull( pull.once(commit.tree), - pull.asyncMap(function (id, cb) { - self.app.git.getObjectMsg({ - obj: id, - headMsgId: obj.msg.key, - }, function (err, msg) { - var path = '/git/tree/' + id - + '?msg=' + encodeURIComponent(msg.key) - cb(null, [ph('code', ph('a', { - href: self.app.render.toUrl(path) - }, id.substr(0, 8))), ' ']) - }) - }) + self.gitObjectLinks(obj.msg.key, 'tree') )]) : '', h('pre', self.app.render.linkify(commit.body)).outerHTML, ] @@ -1501,19 +1479,7 @@ Serve.prototype.gitTag = function (rev) { tag.type, ' ', pull( pull.once(tag.object), - pull.asyncMap(function (id, cb) { - self.app.git.getObjectMsg({ - obj: id, - type: tag.type, - headMsgId: obj.msg.key, - }, function (err, msg) { - var path = '/git/' + tag.type + '/' + id - + '?msg=' + encodeURIComponent(msg.key) - cb(null, [ph('code', ph('a', { - href: self.app.render.toUrl(path) - }, id.substr(0, 8))), ' ']) - }) - }) + self.gitObjectLinks(obj.msg.key, tag.type) ), ' ', ph('code', u.escapeHTML(tag.tag)), h('pre', self.app.render.linkify(tag.body)).outerHTML, @@ -1581,21 +1547,26 @@ Serve.prototype.gitTree = function (rev) { pull.map(function (item) { var type = item.mode === 0040000 ? 'tree' : item.mode === 0160000 ? 'commit' : 'blob' + if (!item.msg) return ph('tr', [ + ph('td', + u.escapeHTML(item.name) + (type === 'tree' ? '/' : '')), + ph('td', 'missing') + ]) var path = '/git/' + type + '/' + item.hash + '?msg=' + encodeURIComponent(item.msg.key) var fileDate = new Date(item.msg.value.timestamp) return ph('tr', [ ph('td', ph('a', {href: self.app.render.toUrl(path)}, - item.name + (type === 'tree' ? '/' : ''))), - item.msg ? ph('td', - self.phIdLink(item.msg.value.author)) : '', - item.msg ? ph('td', + u.escapeHTML(item.name) + (type === 'tree' ? '/' : ''))), + ph('td', + self.phIdLink(item.msg.value.author)), + ph('td', ph('a', { href: self.app.render.toUrl(item.msg.key), title: fileDate.toLocaleString(), }, htime(fileDate)) - ) : '', + ), ]) }) ) @@ -1666,6 +1637,27 @@ Serve.prototype.gitBlob = function (rev) { }) } +Serve.prototype.gitObjectLinks = function (headMsgId, type) { + var self = this + return paramap(function (id, cb) { + self.app.git.getObjectMsg({ + obj: id, + headMsgId: headMsgId, + type: type, + }, function (err, msg) { + if (err && err.name === 'ObjectNotFoundError') + return cb(null, [ + ph('code', u.escapeHTML(id.substr(0, 8))), '(missing)']) + if (err) return cb(err) + var path = '/git/' + type + '/' + id + + '?msg=' + encodeURIComponent(msg.key) + cb(null, [ph('code', ph('a', { + href: self.app.render.toUrl(path) + }, u.escapeHTML(id.substr(0, 8)))), ' ']) + }) + }, 8) +} + // wrap a binary source and render it or turn into an embed Serve.prototype.wrapBinary = function (opts) { var self = this -- cgit v1.2.3