From 328b6ffd8dc1a0cb66111a0ae8b346c5c2aad8f5 Mon Sep 17 00:00:00 2001 From: cel Date: Fri, 30 Nov 2018 14:53:52 -1000 Subject: Add git object page Allows linking to a git object without knowing its type. --- lib/serve.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) (limited to 'lib/serve.js') diff --git a/lib/serve.js b/lib/serve.js index db9be20..a28f758 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -1863,6 +1863,7 @@ Serve.prototype.wrapUserFeed = function (isScrolled, id) { Serve.prototype.git = function (url) { var m = /^\/?([^\/]*)\/?(.*)?$/.exec(url) switch (m[1]) { + case 'object': return this.gitObject(m[2]) case 'commit': return this.gitCommit(m[2]) case 'tag': return this.gitTag(m[2]) case 'tree': return this.gitTree(m[2]) @@ -1884,7 +1885,7 @@ Serve.prototype.gitRaw = function (rev) { } if (!u.isRef(self.query.msg)) return pull( ph('div.error', 'missing message id'), - self.wrapPage('git tree ' + rev), + self.wrapPage('git object ' + rev), self.respondSink(400) ) @@ -1927,6 +1928,63 @@ Serve.prototype.gitAuthorLink = function (author) { } } +Serve.prototype.gitObject = function (rev) { + var self = this + if (!/[0-9a-f]{24}/.test(rev)) { + return pull( + ph('div.error', 'rev is not a git object id'), + self.wrapPage('git'), + self.respondSink(400) + ) + } + if (!u.isRef(self.query.msg)) return pull( + ph('div.error', 'missing message id'), + self.wrapPage('git object ' + rev), + self.respondSink(400) + ) + + if (self.query.search) { + return self.app.git.getObjectMsg({ + obj: rev, + headMsgId: self.query.msg, + }, function (err, msg) { + if (err && err.name === 'BlobNotFoundError') + return self.askWantBlobs(err.links) + if (err) return pull( + pull.once(u.renderError(err).outerHTML), + self.wrapPage('git object ' + rev), + self.respondSink(400) + ) + var path = '/git/object/' + rev + + '?msg=' + encodeURIComponent(msg.key) + return self.redirect(self.app.render.toUrl(path)) + }) + } + + self.app.git.openObject({ + obj: rev, + msg: self.query.msg, + }, function (err, obj) { + if (err && err.name === 'BlobNotFoundError') + return self.askWantBlobs(err.links) + if (err) return pull( + pull.once(u.renderError(err).outerHTML), + self.wrapPage('git object ' + rev), + self.respondSink(400) + ) + self.app.git.statObject(obj, function (err, stat) { + if (err) return pull( + pull.once(u.renderError(err).outerHTML), + self.wrapPage('git object ' + rev), + self.respondSink(400) + ) + var path = '/git/' + stat.type + '/' + rev + + '?msg=' + encodeURIComponent(self.query.msg) + return self.redirect(self.app.render.toUrl(path)) + }) + }) +} + Serve.prototype.gitCommit = function (rev) { var self = this if (!/[0-9a-f]{24}/.test(rev)) { -- cgit v1.2.3