git ssb

39+

cel / git-ssb



Commit c51a7c32602d5b5501312e9ad0344b4a93fdd8f8

Factor all commands out of bin.js

except version
cel committed on 11/10/2016, 4:10:08 AM
Parent: 23a4bef6309656f08ae027f6ff5a79a2fcc6bf77

Files changed

bin.jschanged
lib/create.jsadded
lib/fork.jsadded
lib/help.jsadded
lib/name.jsadded
bin.jsView
@@ -4,12 +4,9 @@
44 // vi: ft=javascript
55
66 var path = require('path')
77 var proc = require('child_process')
8-var u = require('./lib/util')
98
10-var prog = 'git ssb'
11-
129 main()
1310
1411 function main() {
1512 switch (path.basename(process.argv[1])) {
@@ -23,239 +20,41 @@
2320 var config = require('ssb-config/inject')(appName)
2421
2522 var cmd = config._.shift()
2623 if (config.help)
27- return help(cmd)
24 + return require('./lib/help')(cmd)
2825 if (config.version)
2926 return version()
3027
3128 switch (cmd) {
3229 case 'create':
33- return createRepo(config, config._[0], config._[1])
30 + return require('./lib/create')(config, config._[0], config._[1])
3431 case 'fork':
35- return forkRepo(config)
32 + return require('./lib/fork')(config)
3633 case 'forks':
3734 return require('./lib/forks')(config)
3835 case 'issues':
3936 return require('./lib/issues')(config)
4037 case 'prs':
4138 case 'pull-requests':
4239 return require('./lib/pull-requests')(config)
4340 case 'name':
44- return nameRepo(config)
41 + return require('./lib/name')(config)
4542 case 'pull-request':
4643 return require('./lib/pull-request')(config)
4744 case 'web':
4845 return require('git-ssb-web/server')
4946 case 'help':
50- return help(config._[0])
47 + return require('./lib/help')(config._[0])
5148 case 'version':
5249 return version()
5350 case undefined:
54- return usage(0)
51 + return require('./lib/help')()
5552 default:
56- err(1, 'No such command \'' + cmd + '\'')
53 + throw 'No such command \'' + cmd + '\''
5754 }
5855 }
5956
60-function 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-
7757 function version() {
7858 var pkg = require('./package')
7959 console.log(pkg.name, pkg.version)
8060 }
81-
82-function 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-
192-function out() {
193- console.log([].slice.call(arguments).join('\n'))
194-}
195-
196-function err(code) {
197- var args = [].slice.call(arguments, 1)
198- console.error.apply(console, [prog + ':'].concat(args))
199- process.exit(code)
200-}
201-
202-function 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-
208-function 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-
231-function 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-
243-function 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-}
lib/create.jsView
@@ -1,0 +1,30 @@
1 +var u = require('./util')
2 +
3 +function hasRemote(name) {
4 + var child = proc.spawnSync('git', ['remote'], {encoding: 'utf8'})
5 + var remotes = child.stdout.split(/\n/)
6 + return !!~remotes.indexOf(name)
7 +}
8 +
9 +module.exports = function (config, remoteName, name, upstream) {
10 + if (config._.length == 0) return require('./help')('create')
11 + if (!remoteName) throw 'Missing remote name'
12 + if (hasRemote(remoteName))
13 + throw 'Remote \'' + remoteName + '\' already exists'
14 + u.getSbot(config, function (err, sbot) {
15 + if (err) throw err
16 + require('ssb-git-repo').createRepo(sbot, {
17 + upstream: upstream,
18 + name: name
19 + }, function (err, repo) {
20 + if (err) throw err
21 + var url = 'ssb://' + repo.id
22 + console.log('Created repo:', url, name ? '(' + name + ')' : '')
23 + proc.spawnSync('git', ['remote', 'add', remoteName, url])
24 + console.log('Added remote:', remoteName)
25 + repo.close()
26 + sbot.close()
27 + })
28 + })
29 +}
30 +
lib/fork.jsView
@@ -1,0 +1,12 @@
1 +var u = require('./util')
2 +
3 +module.exports = function (argv) {
4 + var repo
5 + if (argv._.length == 1) repo = u.getDefaultRemote()
6 + else if (argv._.length == 2) repo = u.getRemote(argv._.shift())
7 + else return require('./help')('fork')
8 + if (!repo) throw 'unable to find git-ssb upstream repo'
9 + var name = argv._[0]
10 + if (!name) throw 'missing remote name'
11 + require('./create')(argv, name, null, repo)
12 +}
lib/help.jsView
@@ -1,0 +1,133 @@
1 +var prog = 'git ssb'
2 +
3 +module.exports = function (cmd) {
4 + switch (cmd) {
5 + case 'help':
6 + return out(
7 + 'Usage: ' + prog + ' help <command>',
8 + '',
9 + ' Get help about a git-ssb command',
10 + '',
11 + 'Options:',
12 + ' command Command to get help with')
13 + case 'create':
14 + return out(
15 + 'Usage: ' + prog + ' create <remote> [<name>]',
16 + '',
17 + ' Create a new git-ssb repo and add it as a git remote',
18 + '',
19 + 'Arguments:',
20 + ' remote Name of the remote to add. e.g. \'origin\' or \'ssb\'',
21 + ' name Name to give the repo, if any')
22 + case 'fork':
23 + return out(
24 + 'Usage: ' + prog + ' fork [<upstream>] <remote_name>',
25 + '',
26 + ' Create a new git-ssb repo as a fork of another repo',
27 + ' and add it as a git remote',
28 + '',
29 + 'Arguments:',
30 + ' upstream id, url, or git remote name of the repo to fork.',
31 + ' default: \'origin\' or \'ssb\'',
32 + ' remote_name Name for the new remote')
33 + case 'forks':
34 + return out(
35 + 'Usage: ' + prog + ' forks [<repo>]',
36 + '',
37 + ' List repos that are forks of the given repo',
38 + '',
39 + 'Arguments:',
40 + ' repo id, url, or git remote name of the base repo.',
41 + ' default: \'origin\' or \'ssb\'')
42 + case 'issues':
43 + return out(
44 + 'Usage: ' + prog + ' issues [--all|--open|--closed] [<repo>]',
45 + '',
46 + ' List issues about a repo',
47 + '',
48 + 'Arguments:',
49 + ' repo id, url, or git remote name of the repo.',
50 + ' default: \'origin\' or \'ssb\'',
51 + 'Options:',
52 + ' --open Show only open issues (default)',
53 + ' --closed Show only closed issues',
54 + ' --all Show issues of all state')
55 + case 'pull-requests':
56 + case 'prs':
57 + return out(
58 + 'Usage: ' + prog + ' prs [--all|--open|--closed] [<repo>]',
59 + '',
60 + ' List pull requests for a repo',
61 + '',
62 + 'Arguments:',
63 + ' repo id, url, or git remote name of the base repo.',
64 + ' default: \'origin\' or \'ssb\'',
65 + 'Options:',
66 + ' --open Show only open pull requests (default)',
67 + ' --closed Show only closed pull-requests',
68 + ' --all Show pull requests of all state')
69 + case 'name':
70 + return out(
71 + 'Usage: ' + prog + ' name [<repo>] <name>',
72 + '',
73 + ' Publish a name for a git-ssb repo',
74 + '',
75 + 'Arguments:',
76 + ' repo id, url, or git remote name of the base repo.',
77 + ' default: \'origin\' or \'ssb\'',
78 + ' name the name to give the repo')
79 + case 'pull-request':
80 + return out(
81 + 'Usage: ' + prog + ' pull-request [-b <base>] [-h <head>],',
82 + ' [-m <message> | -F <file>]',
83 + '',
84 + ' Create a pull request. This requests that changes from <head>',
85 + ' be merged into <base>.',
86 + '',
87 + 'Arguments:',
88 + ' head the head repo/branch, in format "[<repo>:]<branch>"',
89 + ' Defaults to \'origin\' or \'ssb\', and the current branch.',
90 + ' base the base repo/branch, in format "[<repo>:]<branch>"',
91 + ' where <repo> may be a repo id or git remote name.',
92 + ' Defaults to the upstream of <head>, or <head>,',
93 + ' and its default branch (usually \'master\')',
94 + ' message the text for the pull-request message',
95 + ' file name of file from which to read pull-request text')
96 + case 'web':
97 + return out(
98 + 'Usage: ' + prog + ' web [<host:port>] [<options>]',
99 + '',
100 + ' Host a git ssb web server',
101 + '',
102 + 'Options:',
103 + ' host Host to bind to. default: localhost',
104 + ' port Port to bind to. default: 7718',
105 + ' --public Make the instance read-only')
106 + case '':
107 + case undefined:
108 + usage(0)
109 + default:
110 + throw 'No help for command \'' + cmd + '\''
111 + }
112 +}
113 +
114 +function usage(code) {
115 + out(
116 + 'Usage: git ssb [--version] [--help] [command]',
117 + '',
118 + 'Commands:',
119 + ' create Create a git repo on SSB',
120 + ' fork Fork a git repo on SSB',
121 + ' forks List forks of a repo',
122 + ' issues List issues for a repo',
123 + ' prs List pull requests for a repo',
124 + ' name Name a repo',
125 + ' pull-request Create a pull-request',
126 + ' web Serve a web server for repos',
127 + ' help Get help about a command')
128 + process.exit(code)
129 +}
130 +
131 +function out() {
132 + console.log([].slice.call(arguments).join('\n'))
133 +}
lib/name.jsView
@@ -1,0 +1,22 @@
1 +var u = require('./util')
2 +
3 +module.exports = function (argv) {
4 + var repo
5 + if (argv._.length == 1) repo = u.getDefaultRemote()
6 + else if (argv._.length == 2) repo = u.getRemote(argv._.shift())
7 + else return require('./help')('name')
8 + if (!repo) throw 'unable to find git-ssb repo'
9 + var name = argv._[0]
10 + if (!name) throw 'missing name'
11 +
12 + u.getSbot(argv, function (err, sbot) {
13 + if (err) throw err
14 + var schemas = require('ssb-msg-schemas')
15 + sbot.publish(schemas.name(repo, name), function (err, msg) {
16 + if (err) throw err
17 + console.log(msg.key)
18 + sbot.close()
19 + })
20 + })
21 +}
22 +

Built with git-ssb-web