git ssb

39+

cel / git-ssb



Tree: d4db2a8da13d8f78996e6913763056d5dd83a8a6

Files: d4db2a8da13d8f78996e6913763056d5dd83a8a6 / bin.js

8290 bytesRaw
1#!/bin/sh
2':' //; exec "$(command -v node || command -v nodejs)" "$0" "$@"
3// http://unix.stackexchange.com/questions/65235/universal-node-js-shebang
4// vi: ft=javascript
5
6var path = require('path')
7var proc = require('child_process')
8var u = require('./lib/util')
9
10var prog = 'git ssb'
11
12main()
13
14function main() {
15 switch (path.basename(process.argv[1])) {
16 case 'git-remote-ssb':
17 return require('git-remote-ssb/git-remote-ssb')
18 }
19
20 var appName = 'ssb_appname' in process.env ? process.env.ssb_appname :
21 proc.spawnSync('git', ['config', 'ssb.appname'],
22 {encoding: 'utf8'}).stdout.trim()
23 var config = require('ssb-config/inject')(appName)
24
25 var cmd = config._.shift()
26 if (config.help)
27 return help(cmd)
28 if (config.version)
29 return version()
30
31 switch (cmd) {
32 case 'create':
33 return createRepo(config, config._[0], config._[1])
34 case 'fork':
35 return forkRepo(config)
36 case 'forks':
37 return require('./lib/forks')(config)
38 case 'issues':
39 return require('./lib/issues')(config)
40 case 'prs':
41 case 'pull-requests':
42 return require('./lib/pull-requests')(config)
43 case 'name':
44 return nameRepo(config)
45 case 'pull-request':
46 return require('./lib/pull-request')(config)
47 case 'web':
48 return require('git-ssb-web/server')
49 case 'help':
50 return help(config._[0])
51 case 'version':
52 return version()
53 case undefined:
54 return usage(0)
55 default:
56 err(1, 'No such command \'' + cmd + '\'')
57 }
58}
59
60function usage(code) {
61 out(
62 'Usage: git ssb [--version] [--help] [command]',
63 '',
64 'Commands:',
65 ' create Create a git repo on SSB',
66 ' fork Fork a git repo on SSB',
67 ' forks List forks of a repo',
68 ' issues List issues for a repo',
69 ' prs List pull requests for a repo',
70 ' name Name a repo',
71 ' pull-request Create a pull-request',
72 ' web Serve a web server for repos',
73 ' help Get help about a command')
74 process.exit(code)
75}
76
77function version() {
78 var pkg = require('./package')
79 console.log(pkg.name, pkg.version)
80}
81
82function help(cmd) {
83 switch (cmd) {
84 case 'help':
85 return out(
86 'Usage: ' + prog + ' help <command>',
87 '',
88 ' Get help about a git-ssb command',
89 '',
90 'Options:',
91 ' command Command to get help with')
92 case 'create':
93 return out(
94 'Usage: ' + prog + ' create <remote> [<name>]',
95 '',
96 ' Create a new git-ssb repo and add it as a git remote',
97 '',
98 'Arguments:',
99 ' remote Name of the remote to add. e.g. \'origin\' or \'ssb\'',
100 ' name Name to give the repo, if any')
101 case 'fork':
102 return out(
103 'Usage: ' + prog + ' fork [<upstream>] <remote_name>',
104 '',
105 ' Create a new git-ssb repo as a fork of another repo',
106 ' and add it as a git remote',
107 '',
108 'Arguments:',
109 ' upstream id, url, or git remote name of the repo to fork.',
110 ' default: \'origin\' or \'ssb\'',
111 ' remote_name Name for the new remote')
112 case 'forks':
113 return out(
114 'Usage: ' + prog + ' forks [<repo>]',
115 '',
116 ' List repos that are forks of the given repo',
117 '',
118 'Arguments:',
119 ' repo id, url, or git remote name of the base repo.',
120 ' default: \'origin\' or \'ssb\'')
121 case 'issues':
122 return out(
123 'Usage: ' + prog + ' issues [--all|--open|--closed] [<repo>]',
124 '',
125 ' List issues about a repo',
126 '',
127 'Arguments:',
128 ' repo id, url, or git remote name of the repo.',
129 ' default: \'origin\' or \'ssb\'',
130 'Options:',
131 ' --open Show only open issues (default)',
132 ' --closed Show only closed issues',
133 ' --all Show issues of all state')
134 case 'pull-requests':
135 case 'prs':
136 return out(
137 'Usage: ' + prog + ' prs [--all|--open|--closed] [<repo>]',
138 '',
139 ' List pull requests for a repo',
140 '',
141 'Arguments:',
142 ' repo id, url, or git remote name of the base repo.',
143 ' default: \'origin\' or \'ssb\'',
144 'Options:',
145 ' --open Show only open pull requests (default)',
146 ' --closed Show only closed pull-requests',
147 ' --all Show pull requests of all state')
148 case 'name':
149 return out(
150 'Usage: ' + prog + ' name [<repo>] <name>',
151 '',
152 ' Publish a name for a git-ssb repo',
153 '',
154 'Arguments:',
155 ' repo id, url, or git remote name of the base repo.',
156 ' default: \'origin\' or \'ssb\'',
157 ' name the name to give the repo')
158 case 'pull-request':
159 return out(
160 'Usage: ' + prog + ' pull-request [-b <base>] [-h <head>],',
161 ' [-m <message> | -F <file>]',
162 '',
163 ' Create a pull request. This requests that changes from <head>',
164 ' be merged into <base>.',
165 '',
166 'Arguments:',
167 ' head the head repo/branch, in format "[<repo>:]<branch>"',
168 ' Defaults to \'origin\' or \'ssb\', and the current branch.',
169 ' base the base repo/branch, in format "[<repo>:]<branch>"',
170 ' where <repo> may be a repo id or git remote name.',
171 ' Defaults to the upstream of <head>, or <head>,',
172 ' and its default branch (usually \'master\')',
173 ' message the text for the pull-request message',
174 ' file name of file from which to read pull-request text')
175 case 'web':
176 return out(
177 'Usage: ' + prog + ' web [<host:port>] [<options>]',
178 '',
179 ' Host a git ssb web server',
180 '',
181 'Options:',
182 ' host Host to bind to. default: localhost',
183 ' port Port to bind to. default: 7718',
184 ' --public Make the instance read-only')
185 case undefined:
186 usage(0)
187 default:
188 err(1, 'No help for command \'' + cmd + '\'')
189 }
190}
191
192function out() {
193 console.log([].slice.call(arguments).join('\n'))
194}
195
196function err(code) {
197 var args = [].slice.call(arguments, 1)
198 console.error.apply(console, [prog + ':'].concat(args))
199 process.exit(code)
200}
201
202function hasRemote(name) {
203 var child = proc.spawnSync('git', ['remote'], {encoding: 'utf8'})
204 var remotes = child.stdout.split(/\n/)
205 return !!~remotes.indexOf(name)
206}
207
208function createRepo(config, remoteName, name, upstream) {
209 if (config._.length == 0) return help('create')
210 if (!remoteName) err(1, 'Missing remote name')
211 if (hasRemote(remoteName))
212 err(1, 'Remote \'' + remoteName + '\' already exists')
213 u.getSbot(config, function (err, sbot) {
214 if (err) throw err
215 var ssbGit = require('ssb-git-repo')
216 ssbGit.createRepo(sbot, {
217 upstream: upstream,
218 name: name
219 }, function (err, repo) {
220 if (err) throw err
221 var url = 'ssb://' + repo.id
222 console.log('Created repo:', url, name ? '(' + name + ')' : '')
223 proc.spawnSync('git', ['remote', 'add', remoteName, url])
224 console.log('Added remote:', remoteName)
225 repo.close()
226 sbot.close()
227 })
228 })
229}
230
231function forkRepo(argv) {
232 var repo
233 if (argv._.length == 1) repo = u.getDefaultRemote()
234 else if (argv._.length == 2) repo = u.getRemote(argv._.shift())
235 else return help('fork')
236 if (!repo) err(1, 'unable to find git-ssb upstream repo')
237 var name = argv._[0]
238 if (!name) err(1, 'missing remote name')
239
240 createRepo(argv, name, null, repo)
241}
242
243function nameRepo(argv) {
244 var repo
245 if (argv._.length == 1) repo = u.getDefaultRemote()
246 else if (argv._.length == 2) repo = u.getRemote(argv._.shift())
247 else return help('name')
248 if (!repo) err(1, 'unable to find git-ssb repo')
249 var name = argv._[0]
250 if (!name) err(1, 'missing name')
251
252 u.getSbot(argv, function (err, sbot) {
253 if (err) throw err
254 var schemas = require('ssb-msg-schemas')
255 sbot.publish(schemas.name(repo, name), function (err, msg) {
256 if (err) throw err
257 console.log(msg.key)
258 sbot.close()
259 })
260 })
261}
262

Built with git-ssb-web