git ssb

39+

cel / git-ssb



Tree: d4db2a8da13d8f78996e6913763056d5dd83a8a6

Files: d4db2a8da13d8f78996e6913763056d5dd83a8a6 / lib / util.js

3106 bytesRaw
1var path = require('path')
2var ssbRef = require('ssb-ref')
3var proc = require('child_process')
4var u = exports
5
6u.gitSync = function () {
7 var args = [].concat.apply([], arguments)
8 return proc.spawnSync('git', args, {encoding: 'utf8'}).stdout.trim()
9}
10
11u.getRemoteUrl = function (name) {
12 if (!name) return
13 return u.gitSync('ls-remote', '--get-url', name)
14}
15
16u.getCurrentBranch = function () {
17 return u.gitSync('symbolic-ref', '--short', 'HEAD')
18}
19
20u.getRemote = function (name) {
21 return u.repoId(name) || u.repoId(u.getRemoteUrl(name))
22}
23
24u.getDefaultRemote = function (name) {
25 return u.repoId(u.getRemoteUrl('origin'))
26 || u.repoId(u.getRemoteUrl('ssb'))
27}
28
29u.repoId = function (id) {
30 if (!id) return
31 id = String(id).replace(/^ssb:\/*/, '')
32 return ssbRef.isMsg(id) ? id : null
33}
34
35u.getSbot = function (config, cb) {
36 var keys = require('ssb-keys')
37 .loadOrCreateSync(path.join(config.path, 'secret'))
38 require('ssb-client')(keys, config, cb)
39}
40
41u.gitDir = function () {
42 return u.gitSync('rev-parse', '-q', '--git-dir')
43}
44
45u.editor = function (name, defaultText, useText, cb) {
46 var fs = require('fs')
47 var filename = path.resolve(u.gitDir(), name + '_EDITMSG')
48
49 // prepare the file for editing
50 var separator = '=== This line and anything below will be removed ==='
51 var text = fs.existsSync(filename) ?
52 fs.readFileSync(filename, 'utf8').split(separator)[0].trim() : ''
53 text += '\n\n' + separator + '\n' + defaultText
54 fs.writeFileSync(filename, text)
55
56 // edit the file
57 var editor = u.gitSync('var', 'GIT_EDITOR')
58 var args = [filename]
59 // give vim special formats, like hub(1) does
60 if (/[ng]?vim?$/.test(editor))
61 args.unshift('-c', '"set ft=markdown tw=0 wrap lbr"')
62 var child = proc.spawn(editor, args, {shell: true, stdio: 'inherit'})
63 child.on('exit', function (code) {
64 if (code) return cb('error using text editor')
65 fs.readFile(filename, 'utf8', function (err, text) {
66 if (err) return cb(err)
67 // use the data
68 useText(text.split(separator)[0].trim(), function (err) {
69 if (err) return cb(err)
70 // delete the file after succesful read
71 fs.unlink(filename, cb)
72 })
73 })
74 })
75}
76
77// Get a name for a thing from multiple fallback sources
78u.getName = function (sbot, sources, dest, cb) {
79 var pull = require('pull-stream')
80 var cat = require('pull-cat')
81 var name
82 pull(
83 cat(sources.map(function (source) {
84 return sbot.links({
85 source: source, dest: dest, rel: 'about',
86 values: true, keys: false, meta: false,
87 reverse: true
88 })
89 })),
90 pull.drain(function (value) {
91 name = value && value.content && value.content.name
92 if (name) return false
93 }, function (err) {
94 cb(err === true ? null : err, name)
95 })
96 )
97}
98
99u.formatTitle = function (str, len) {
100 str = String(str).replace(/[\n\r\t ]+/g, ' ')
101 if (str.length > len) str = str.substr(0, len) + '…'
102 return str
103}
104
105u.issueStateBool = function (argv) {
106 return argv.all || (argv.open && argv.closed) ? null :
107 argv.open ? true :
108 argv.closed ? false :
109 true
110}
111

Built with git-ssb-web