var fs = require('fs') var proc = require('child_process') var pull = require('pull-stream') var toPull = require('stream-to-pull-stream') var paramap = require('pull-paramap') var multicb = require('multicb') var u = require('./util') var ssbRef = require('ssb-ref') var packidx = require('pull-git-packidx-parser') exports.help = ` Usage: git ssb find-object [--repo ] Find a message that introduced the given git object id. On success, output the message id. Arguments: sha1 git object id to search for Options: --repo id, url or remote name of git-ssb repo to search within. default: ssb or origin ` function getBlob(sbot, id, cb) { sbot.blobs.size(id, function (err, size) { if (err) gotBlob(err) else if (size == null) sbot.blobs.want(id, gotBlob) else gotBlob(null, true) function gotBlob(err, got) { if (!err && !got) err = '???' if (err) return cb(new Error('failed to get blob ' + id + ': ' + (err.stack||err))) cb(null, sbot.blobs.get(id)) } }) } exports.fn = function (argv) { if (argv._.length !== 1) return u.help('find-object') var repoId = argv.repo && u.getRemote(argv.repo) var objId = argv._[0] if (!/^[0-9a-f]{40}$/.test(objId)) throw 'invalid git object id: ' + objId var objIdBuf = Buffer.from(objId, 'hex') u.getSbot(argv, function (err, sbot) { if (err) throw err pull( u.gitUpdates(sbot, repoId), pull.map(function (msg) { var c = msg && msg.value && msg.value.content return c && Array.isArray(c.indexes) ? c.indexes.map(function (idx) { if (idx != null && typeof idx === 'object') idx = idx.link if (typeof idx !== 'string') return return {msg: msg, idxId: idx} }).filter(Boolean) : [] }), pull.flatten(), paramap(function (info, cb) { // console.error('getting blob', info.idxId) getBlob(sbot, info.idxId, function (err, readBlob) { if (err) throw 'failed to get index blob ' + info.idxId + ': ' + (err.stack || err) // console.error('parsing packidx', info.idxId) pull(readBlob, packidx(function (err, idx) { if (err) throw 'failed to parse index blob ' + info.idxId + ': ' + (err.stack || err) cb(null, {msg: info.msg, idx: idx}) })) }) }, 8), pull.filter(function (info) { var offset = info.idx.find(objIdBuf) return offset != null }), pull.drain(function (info) { console.log(info.msg.key) }, function (err) { if (err) throw err sbot.close() }) ) }) }