git ssb

0+

kyrias-defunct / git-ssb



forked from cel / git-ssb

Tree: b091444d9863bf469e062261c642accc00254dd1

Files: b091444d9863bf469e062261c642accc00254dd1 / bin.js

4814 bytesRaw
1#!/usr/bin/env node
2
3var path = require('path')
4var proc = require('child_process')
5var ssbRef = require('ssb-ref')
6
7var prog = 'git ssb'
8
9main()
10
11function main() {
12 switch (path.basename(process.argv[1])) {
13 case 'git-remote-ssb':
14 return require('git-remote-ssb/git-remote-ssb')
15 }
16
17 var appName = 'ssb_appname' in process.env ? process.env.ssb_appname :
18 proc.spawnSync('git', ['config', 'ssb.appname'],
19 {encoding: 'utf8'}).stdout.trim()
20 var config = require('ssb-config/inject')(appName)
21
22 var cmd = config._.shift()
23 if (config.help || config.h)
24 return help(cmd)
25 if (config.version)
26 return version()
27
28 switch (cmd) {
29 case 'create':
30 return createRepo(config, config._[0] || 'ssb')
31 case 'fork':
32 return forkRepo(config)
33 case 'web':
34 return require('git-ssb-web/server')
35 case 'help':
36 return help(config._[0])
37 case 'version':
38 return version()
39 case undefined:
40 return usage(0)
41 default:
42 err(1, 'No such command \'' + cmd + '\'')
43 }
44}
45
46function usage(code) {
47 out(
48 'Usage: git ssb [--version] [--help] [command]',
49 '',
50 'Commands:',
51 ' create Create a git repo on SSB',
52 ' fork Fork a git repo on SSB',
53 ' web Serve a web server for repos',
54 ' help Get help about a command')
55 process.exit(code)
56}
57
58function version() {
59 var pkg = require('./package')
60 console.log(pkg.name, pkg.version)
61}
62
63function help(cmd) {
64 switch (cmd) {
65 case 'help':
66 return out(
67 'Usage: ' + prog + ' help <command>',
68 '',
69 ' Get help about a git-ssb command',
70 '',
71 'Options:',
72 ' command Command to get help with')
73 case 'create':
74 return out(
75 'Usage: ' + prog + ' create [<remote_name>]',
76 '',
77 ' Create a new git-ssb repo and add it as a git remote',
78 '',
79 'Options:',
80 ' remote_name Name of the remote to add. default: \'ssb\'')
81 case 'fork':
82 return out(
83 'Usage: ' + prog + ' fork [<upstream>] <remote_name>',
84 '',
85 ' Create a new git-ssb repo as a fork of another repo',
86 ' and add it as a git remote',
87 '',
88 'Arguments:',
89 ' upstream id, url, or git remote name of the repo to fork.',
90 ' default: \'origin\' or \'ssb\'',
91 ' remote_name Name for the new remote')
92 case 'web':
93 return out(
94 'Usage: ' + prog + ' web [<host:port>] [<options>]',
95 '',
96 ' Host a git ssb web server',
97 '',
98 'Options:',
99 ' host Host to bind to. default: localhost',
100 ' port Port to bind to. default: 7718',
101 ' --public Make the instance read-only')
102 case undefined:
103 usage(0)
104 default:
105 err(1, 'No help for command \'' + cmd + '\'')
106 }
107}
108
109function out() {
110 console.log([].slice.call(arguments).join('\n'))
111}
112
113function err(code) {
114 var args = [].slice.call(arguments, 1)
115 console.error.apply(console, [prog + ':'].concat(args))
116 process.exit(code)
117}
118
119function getSbot(config, cb) {
120 var keys = require('ssb-keys')
121 .loadOrCreateSync(path.join(config.path, 'secret'))
122 require('ssb-client')(keys, config, cb)
123}
124
125function hasRemote(name) {
126 var child = proc.spawnSync('git', ['remote'], {encoding: 'utf8'})
127 var remotes = child.stdout.split(/\n/)
128 return !!~remotes.indexOf(name)
129}
130
131function getRemoteUrl(name) {
132 return proc.spawnSync('git', ['remote', 'get-url', name],
133 {encoding: 'utf8'}).stdout.trim()
134}
135
136function repoId(id) {
137 if (!id) return
138 id = String(id).replace(/^ssb:\/*/, '')
139 return ssbRef.isMsg(id) ? id : null
140}
141
142function createRepo(config, remoteName, upstream) {
143 if (hasRemote(remoteName))
144 err(1, 'Remote \'' + remoteName + '\' already exists')
145 getSbot(config, function (err, sbot) {
146 if (err) throw err
147 var ssbGit = require('ssb-git-repo')
148 ssbGit.createRepo(sbot, {upstream: upstream}, function (err, repo) {
149 if (err) throw err
150 var url = 'ssb://' + repo.id
151 console.log(url)
152 repo.close()
153 sbot.close()
154 proc.spawn('git', ['remote', 'add', remoteName, url], {stdio: 'inherit'})
155 })
156 })
157}
158
159function forkRepo(argv) {
160 var upstream, name
161 switch (argv._.length) {
162 case 1:
163 name = argv._[0]
164 upstream = repoId(getRemoteUrl('origin')) || repoId(getRemoteUrl('ssb'))
165 if (!upstream)
166 err(1, 'unable to find git-ssb upstream to fork')
167 break
168 case 2:
169 upstream = repoId(argv._[0]) || repoId(getRemoteUrl(argv._[0]))
170 name = argv._[1]
171 if (!upstream)
172 err(1, 'unable to find git-ssb upstream \'' + argv._[0] + '\'')
173 break
174 default:
175 return help('fork')
176 }
177
178 if (!name) err(1, 'missing remote name')
179
180 createRepo(argv, name, upstream)
181}
182

Built with git-ssb-web