UNPKG

2.89 kBJavaScriptView Raw
1var pull = require('pull-stream')
2var paramap = require('pull-paramap')
3var u = require('./util')
4var getAbout = require('ssb-avatar')
5
6exports.help = `
7Usage: git ssb [--no-forks] authors [<repo>]
8
9 List feeds who pushed to a repo, including those which pushed to
10 a fork if their commit was later merged into the upstream.
11
12 Output is space separated lines containing information about the
13 last update by each feed:
14
15 <last update id> <feed id> <feed name>
16
17Options:
18 -n --no-forks Only look at updates pushed directly to the repo,
19 not updates pushed to forks and then merged
20
21Arguments:
22 repo id, url, or git remote name of the base repo.
23 default: 'origin' or 'ssb'
24`
25
26function getRepoUpdates(sbot, repoMsg, includeMerged) {
27 // includeMerged: include updates pushed to downstream (fork) repos
28 // which are merged into the upstream
29
30 var commitsInUpstream = {}
31 function gotUpstreamCommit(commit) {
32 commitsInUpstream[commit.sha1] = true
33 }
34 function isCommitInUpstream(commit) {
35 return commit && commitsInUpstream[commit.sha1]
36 }
37
38 return pull(
39 includeMerged ? u.getForks(sbot, repoMsg) : pull.once(repoMsg),
40 pull.map(function (msg) {
41 return sbot.links({
42 dest: msg.key,
43 rel: 'repo',
44 values: true,
45 reverse: true,
46 })
47 }),
48 pull.flatten(),
49 pull.filter(function (msg) {
50 var c = msg.value.content
51 if (c.type !== 'git-update') return false
52 if (!includeMerged) return true
53 var commits = Array.isArray(c.commits) ? c.commits : []
54 // upstream messages come first
55 if (c.repo === repoMsg.key) {
56 // keep track of commits in upstream
57 commits.forEach(gotUpstreamCommit)
58 return true
59 } else {
60 // update to a fork. only include if it was later merged upstream.
61 return commits.some(isCommitInUpstream)
62 }
63 })
64 )
65}
66
67exports.fn = function (argv) {
68 var repoId = u.getRemote(argv._[0])
69 if (!repoId) throw 'unable to find git-ssb repo'
70 var noForks = argv.forks === false || argv.n === true
71
72 u.getSbot(argv, function (err, sbot) {
73 if (err) throw err
74 sbot.whoami(function (err, feed) {
75 if (err) throw err
76 sbot.get(repoId, function (err, value) {
77 if (err) throw err
78 next(sbot, feed.id, {key: repoId, value: value})
79 })
80 })
81 })
82
83 function next(sbot, myId, repoMsg) {
84 pull(
85 getRepoUpdates(sbot, repoMsg, !noForks),
86 pull.unique(function (msg) {
87 return msg.value.author
88 }),
89 paramap(function (msg, cb) {
90 getAbout(sbot, myId, msg.value.author, function (err, about) {
91 if (err) return cb(err)
92 cb(null, `${msg.key} ${msg.value.author} @${about.name}`)
93 })
94 }, 8),
95 pull.log(function (err) {
96 if (err) throw err
97 sbot.close()
98 })
99 )
100 }
101}