git ssb

39+

cel / git-ssb



Tree: 403bed97ce3c8f93b87fa33b014b7f20e5e5670d

Files: 403bed97ce3c8f93b87fa33b014b7f20e5e5670d / lib / find-object.js

2659 bytesRaw
1var fs = require('fs')
2var proc = require('child_process')
3var pull = require('pull-stream')
4var toPull = require('stream-to-pull-stream')
5var paramap = require('pull-paramap')
6var multicb = require('multicb')
7var u = require('./util')
8var ssbRef = require('ssb-ref')
9var packidx = require('pull-git-packidx-parser')
10
11exports.help = `
12Usage: git ssb find-object [--repo <id>] <sha1>
13
14 Find a message that introduced the given git object id.
15 On success, output the message id.
16
17Arguments:
18 sha1 git object id to search for
19
20Options:
21 --repo <id> id, url or remote name of git-ssb repo to search within.
22 default: ssb or origin
23`
24
25function getBlob(sbot, id, cb) {
26 sbot.blobs.size(id, function (err, size) {
27 if (err) gotBlob(err)
28 else if (size == null) sbot.blobs.want(id, gotBlob)
29 else gotBlob(null, true)
30 function gotBlob(err, got) {
31 if (!err && !got) err = '???'
32 if (err) return cb(new Error('failed to get blob ' + id + ': ' + (err.stack||err)))
33 cb(null, sbot.blobs.get(id))
34 }
35 })
36}
37
38exports.fn = function (argv) {
39 if (argv._.length !== 1) return u.help('find-object')
40
41 var repoId = argv.repo && u.getRemote(argv.repo)
42 var objId = argv._[0]
43 if (!/^[0-9a-f]{40}$/.test(objId)) throw 'invalid git object id: ' + objId
44 var objIdBuf = Buffer.from(objId, 'hex')
45
46 u.getSbot(argv, function (err, sbot) {
47 if (err) throw err
48 pull(
49 u.gitUpdates(sbot, repoId),
50 pull.map(function (msg) {
51 var c = msg && msg.value && msg.value.content
52 return c && Array.isArray(c.indexes) ? c.indexes.map(function (idx) {
53 if (idx != null && typeof idx === 'object') idx = idx.link
54 if (typeof idx !== 'string') return
55 return {msg: msg, idxId: idx}
56 }).filter(Boolean) : []
57 }),
58 pull.flatten(),
59 paramap(function (info, cb) {
60 // console.error('getting blob', info.idxId)
61 getBlob(sbot, info.idxId, function (err, readBlob) {
62 if (err) throw 'failed to get index blob ' + info.idxId + ': ' + (err.stack || err)
63 // console.error('parsing packidx', info.idxId)
64 pull(readBlob, packidx(function (err, idx) {
65 if (err) throw 'failed to parse index blob ' + info.idxId + ': ' + (err.stack || err)
66 cb(null, {msg: info.msg, idx: idx})
67 }))
68 })
69 }, 8),
70 pull.filter(function (info) {
71 var offset = info.idx.find(objIdBuf)
72 return offset != null
73 }),
74 pull.drain(function (info) {
75 console.log(info.msg.key)
76 }, function (err) {
77 if (err) throw err
78 sbot.close()
79 })
80 )
81 })
82}
83

Built with git-ssb-web