git ssb

39+

cel / git-ssb



Tree: bd722e72406db3a793dbeada2e62de0bbaa26c22

Files: bd722e72406db3a793dbeada2e62de0bbaa26c22 / bin.js

6728 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] || 'ssb')
34 case 'fork':
35 return forkRepo(config)
36 case 'forks':
37 return require('./lib/forks')(config)
38 case 'name':
39 return nameRepo(config)
40 case 'pull-request':
41 return require('./lib/pull-request')(config)
42 case 'web':
43 return require('git-ssb-web/server')
44 case 'help':
45 return help(config._[0])
46 case 'version':
47 return version()
48 case undefined:
49 return usage(0)
50 default:
51 err(1, 'No such command \'' + cmd + '\'')
52 }
53}
54
55function usage(code) {
56 out(
57 'Usage: git ssb [--version] [--help] [command]',
58 '',
59 'Commands:',
60 ' create Create a git repo on SSB',
61 ' fork Fork a git repo on SSB',
62 ' forks List forks of a repo',
63 ' name Name a repo',
64 ' pull-request Create a pull-request',
65 ' web Serve a web server for repos',
66 ' help Get help about a command')
67 process.exit(code)
68}
69
70function version() {
71 var pkg = require('./package')
72 console.log(pkg.name, pkg.version)
73}
74
75function help(cmd) {
76 switch (cmd) {
77 case 'help':
78 return out(
79 'Usage: ' + prog + ' help <command>',
80 '',
81 ' Get help about a git-ssb command',
82 '',
83 'Options:',
84 ' command Command to get help with')
85 case 'create':
86 return out(
87 'Usage: ' + prog + ' create [<remote_name>]',
88 '',
89 ' Create a new git-ssb repo and add it as a git remote',
90 '',
91 'Options:',
92 ' remote_name Name of the remote to add. default: \'ssb\'')
93 case 'fork':
94 return out(
95 'Usage: ' + prog + ' fork [<upstream>] <remote_name>',
96 '',
97 ' Create a new git-ssb repo as a fork of another repo',
98 ' and add it as a git remote',
99 '',
100 'Arguments:',
101 ' upstream id, url, or git remote name of the repo to fork.',
102 ' default: \'origin\' or \'ssb\'',
103 ' remote_name Name for the new remote')
104 case 'forks':
105 return out(
106 'Usage: ' + prog + ' forks [<repo>]',
107 '',
108 ' List repos that are forks of the given repo',
109 '',
110 'Arguments:',
111 ' repo id, url, or git remote name of the base repo.',
112 ' default: \'origin\' or \'ssb\'')
113 case 'name':
114 return out(
115 'Usage: ' + prog + ' name [<repo>] <name>',
116 '',
117 ' Publish a name for a git-ssb repo',
118 '',
119 'Arguments:',
120 ' repo id, url, or git remote name of the base repo.',
121 ' default: \'origin\' or \'ssb\'',
122 ' name the name to give the repo')
123 case 'pull-request':
124 return out(
125 'Usage: ' + prog + ' pull-request [-b <base>] [-h <head>],',
126 ' [-m <message> | -F <file>]',
127 '',
128 ' Create a pull request. This requests that changes from <head>',
129 ' be merged into <base>.',
130 '',
131 'Arguments:',
132 ' head the head repo/branch, in format "[<repo>:]<branch>"',
133 ' Defaults to \'origin\' or \'ssb\', and the current branch.',
134 ' base the base repo/branch, in format "[<repo>:]<branch>"',
135 ' where <repo> may be a repo id or git remote name.',
136 ' Defaults to the upstream of <head>, or <head>,',
137 ' and its default branch (usually \'master\')',
138 ' message the text for the pull-request message',
139 ' file name of file from which to read pull-request text')
140 case 'web':
141 return out(
142 'Usage: ' + prog + ' web [<host:port>] [<options>]',
143 '',
144 ' Host a git ssb web server',
145 '',
146 'Options:',
147 ' host Host to bind to. default: localhost',
148 ' port Port to bind to. default: 7718',
149 ' --public Make the instance read-only')
150 case undefined:
151 usage(0)
152 default:
153 err(1, 'No help for command \'' + cmd + '\'')
154 }
155}
156
157function out() {
158 console.log([].slice.call(arguments).join('\n'))
159}
160
161function err(code) {
162 var args = [].slice.call(arguments, 1)
163 console.error.apply(console, [prog + ':'].concat(args))
164 process.exit(code)
165}
166
167function hasRemote(name) {
168 var child = proc.spawnSync('git', ['remote'], {encoding: 'utf8'})
169 var remotes = child.stdout.split(/\n/)
170 return !!~remotes.indexOf(name)
171}
172
173function createRepo(config, remoteName, upstream) {
174 if (hasRemote(remoteName))
175 err(1, 'Remote \'' + remoteName + '\' already exists')
176 u.getSbot(config, function (err, sbot) {
177 if (err) throw err
178 var ssbGit = require('ssb-git-repo')
179 ssbGit.createRepo(sbot, {upstream: upstream}, function (err, repo) {
180 if (err) throw err
181 var url = 'ssb://' + repo.id
182 console.log(url)
183 repo.close()
184 sbot.close()
185 proc.spawn('git', ['remote', 'add', remoteName, url], {stdio: 'inherit'})
186 })
187 })
188}
189
190function forkRepo(argv) {
191 var repo
192 if (argv._.length == 1) repo = u.getDefaultRemote()
193 else if (argv._.length == 2) repo = u.getRemote(argv._.shift())
194 else return help('fork')
195 if (!repo) err(1, 'unable to find git-ssb upstream repo')
196 var name = argv._[0]
197 if (!name) err(1, 'missing remote name')
198
199 createRepo(argv, name, repo)
200}
201
202function nameRepo(argv) {
203 var repo
204 if (argv._.length == 1) repo = u.getDefaultRemote()
205 else if (argv._.length == 2) repo = u.getRemote(argv._.shift())
206 else return help('name')
207 if (!repo) err(1, 'unable to find git-ssb repo')
208 var name = argv._[0]
209 if (!name) err(1, 'missing name')
210
211 u.getSbot(argv, function (err, sbot) {
212 if (err) throw err
213 var schemas = require('ssb-msg-schemas')
214 sbot.publish(schemas.name(repo, name), function (err, msg) {
215 if (err) throw err
216 console.log(msg.key)
217 sbot.close()
218 })
219 })
220}
221

Built with git-ssb-web