git ssb

39+

cel / git-ssb



Tree: 0fc3cbb4068db6a6e9533948747d90e5e9a8549e

Files: 0fc3cbb4068db6a6e9533948747d90e5e9a8549e / bin.js

7030 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 '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 'Arguments:',
92 ' remote Name of the remote to add. e.g. \'origin\' or \'ssb\'',
93 ' name Name to give the repo, if any')
94 case 'fork':
95 return out(
96 'Usage: ' + prog + ' fork [<upstream>] <remote_name>',
97 '',
98 ' Create a new git-ssb repo as a fork of another repo',
99 ' and add it as a git remote',
100 '',
101 'Arguments:',
102 ' upstream id, url, or git remote name of the repo to fork.',
103 ' default: \'origin\' or \'ssb\'',
104 ' remote_name Name for the new remote')
105 case 'forks':
106 return out(
107 'Usage: ' + prog + ' forks [<repo>]',
108 '',
109 ' List repos that are forks of the given repo',
110 '',
111 'Arguments:',
112 ' repo id, url, or git remote name of the base repo.',
113 ' default: \'origin\' or \'ssb\'')
114 case 'name':
115 return out(
116 'Usage: ' + prog + ' name [<repo>] <name>',
117 '',
118 ' Publish a name for a git-ssb repo',
119 '',
120 'Arguments:',
121 ' repo id, url, or git remote name of the base repo.',
122 ' default: \'origin\' or \'ssb\'',
123 ' name the name to give the repo')
124 case 'pull-request':
125 return out(
126 'Usage: ' + prog + ' pull-request [-b <base>] [-h <head>],',
127 ' [-m <message> | -F <file>]',
128 '',
129 ' Create a pull request. This requests that changes from <head>',
130 ' be merged into <base>.',
131 '',
132 'Arguments:',
133 ' head the head repo/branch, in format "[<repo>:]<branch>"',
134 ' Defaults to \'origin\' or \'ssb\', and the current branch.',
135 ' base the base repo/branch, in format "[<repo>:]<branch>"',
136 ' where <repo> may be a repo id or git remote name.',
137 ' Defaults to the upstream of <head>, or <head>,',
138 ' and its default branch (usually \'master\')',
139 ' message the text for the pull-request message',
140 ' file name of file from which to read pull-request text')
141 case 'web':
142 return out(
143 'Usage: ' + prog + ' web [<host:port>] [<options>]',
144 '',
145 ' Host a git ssb web server',
146 '',
147 'Options:',
148 ' host Host to bind to. default: localhost',
149 ' port Port to bind to. default: 7718',
150 ' --public Make the instance read-only')
151 case undefined:
152 usage(0)
153 default:
154 err(1, 'No help for command \'' + cmd + '\'')
155 }
156}
157
158function out() {
159 console.log([].slice.call(arguments).join('\n'))
160}
161
162function err(code) {
163 var args = [].slice.call(arguments, 1)
164 console.error.apply(console, [prog + ':'].concat(args))
165 process.exit(code)
166}
167
168function hasRemote(name) {
169 var child = proc.spawnSync('git', ['remote'], {encoding: 'utf8'})
170 var remotes = child.stdout.split(/\n/)
171 return !!~remotes.indexOf(name)
172}
173
174function createRepo(config, remoteName, name, upstream) {
175 if (config._.length == 0) return help('create')
176 if (!remoteName) err(1, 'Missing remote name')
177 if (hasRemote(remoteName))
178 err(1, 'Remote \'' + remoteName + '\' already exists')
179 u.getSbot(config, function (err, sbot) {
180 if (err) throw err
181 var ssbGit = require('ssb-git-repo')
182 ssbGit.createRepo(sbot, {
183 upstream: upstream,
184 name: name
185 }, function (err, repo) {
186 if (err) throw err
187 var url = 'ssb://' + repo.id
188 console.log('Created repo:', url, name ? '(' + name + ')' : '')
189 console.log('Added remote:', remoteName)
190 repo.close()
191 sbot.close()
192 proc.spawn('git', ['remote', 'add', remoteName, url], {stdio: 'inherit'})
193 })
194 })
195}
196
197function forkRepo(argv) {
198 var repo
199 if (argv._.length == 1) repo = u.getDefaultRemote()
200 else if (argv._.length == 2) repo = u.getRemote(argv._.shift())
201 else return help('fork')
202 if (!repo) err(1, 'unable to find git-ssb upstream repo')
203 var name = argv._[0]
204 if (!name) err(1, 'missing remote name')
205
206 createRepo(argv, name, null, repo)
207}
208
209function nameRepo(argv) {
210 var repo
211 if (argv._.length == 1) repo = u.getDefaultRemote()
212 else if (argv._.length == 2) repo = u.getRemote(argv._.shift())
213 else return help('name')
214 if (!repo) err(1, 'unable to find git-ssb repo')
215 var name = argv._[0]
216 if (!name) err(1, 'missing name')
217
218 u.getSbot(argv, function (err, sbot) {
219 if (err) throw err
220 var schemas = require('ssb-msg-schemas')
221 sbot.publish(schemas.name(repo, name), function (err, msg) {
222 if (err) throw err
223 console.log(msg.key)
224 sbot.close()
225 })
226 })
227}
228

Built with git-ssb-web