UNPKG

2.3 kBJavaScriptView Raw
1var pull = require('pull-stream')
2var paramap = require('pull-paramap')
3var u = require('./util')
4var getAbout = require('ssb-avatar')
5
6function getRepoUpdates(sbot, repoMsg, includeMerged) {
7 // includeMerged: include updates pushed to downstream (fork) repos
8 // which are merged into the upstream
9
10 var commitsInUpstream = {}
11 function gotUpstreamCommit(commit) {
12 commitsInUpstream[commit.sha1] = true
13 }
14 function isCommitInUpstream(commit) {
15 return commit && commitsInUpstream[commit.sha1]
16 }
17
18 return pull(
19 includeMerged ? u.getForks(sbot, repoMsg) : pull.once(repoMsg),
20 pull.map(function (msg) {
21 return sbot.links({
22 dest: msg.key,
23 rel: 'repo',
24 values: true,
25 reverse: true,
26 })
27 }),
28 pull.flatten(),
29 pull.filter(function (msg) {
30 var c = msg.value.content
31 if (c.type !== 'git-update') return false
32 if (!includeMerged) return true
33 var commits = Array.isArray(c.commits) ? c.commits : []
34 // upstream messages come first
35 if (c.repo === repoMsg.key) {
36 // keep track of commits in upstream
37 commits.forEach(gotUpstreamCommit)
38 return true
39 } else {
40 // update to a fork. only include if it was later merged upstream.
41 return commits.some(isCommitInUpstream)
42 }
43 })
44 )
45}
46
47module.exports = function (argv) {
48 var repoId = u.getRemote(argv._[0])
49 if (!repoId) throw 'unable to find git-ssb repo'
50 var noForks = argv.forks === false || argv.n === true
51
52 u.getSbot(argv, function (err, sbot) {
53 if (err) throw err
54 sbot.whoami(function (err, feed) {
55 if (err) throw err
56 sbot.get(repoId, function (err, value) {
57 if (err) throw err
58 next(sbot, feed.id, {key: repoId, value: value})
59 })
60 })
61 })
62
63 function next(sbot, myId, repoMsg) {
64 pull(
65 getRepoUpdates(sbot, repoMsg, !noForks),
66 pull.unique(function (msg) {
67 return msg.value.author
68 }),
69 paramap(function (msg, cb) {
70 getAbout(sbot, myId, msg.value.author, function (err, about) {
71 if (err) return cb(err)
72 cb(null, `${msg.key} ${msg.value.author} @${about.name}`)
73 })
74 }, 8),
75 pull.log(function (err) {
76 if (err) throw err
77 sbot.close()
78 })
79 )
80 }
81}