1 | const spawn = require('child_process').spawn
|
2 | const Promise = require('bluebird')
|
3 | const fs = Promise.promisifyAll(require('fs'))
|
4 | const tmp = Promise.promisifyAll(require('temp').track())
|
5 | const gh = require('github-url-to-object')
|
6 |
|
7 | function runGit () {
|
8 | const git = spawn('git', Array.from(arguments))
|
9 |
|
10 | return new Promise((resolve, reject) => {
|
11 | git.on('exit', reject)
|
12 | git.stdout.on('data', (data) => resolve(data.toString().trim()))
|
13 | })
|
14 | }
|
15 |
|
16 | function* getRef (branch) {
|
17 | return runGit('rev-parse', branch || 'HEAD')
|
18 | }
|
19 |
|
20 | function* getBranch (symbolicRef) {
|
21 | return runGit('symbolic-ref', '--short', symbolicRef)
|
22 | }
|
23 |
|
24 | function* getCommitTitle (ref) {
|
25 | return runGit('log', ref || '', '-1', '--pretty=format:%s')
|
26 | }
|
27 |
|
28 | function* createArchive (ref) {
|
29 | const tar = spawn('git', ['archive', '--format', 'tar.gz', ref])
|
30 | const file = yield tmp.openAsync({ suffix: '.tar.gz' })
|
31 | const write = tar.stdout.pipe(fs.createWriteStream(file.path))
|
32 |
|
33 | return new Promise((resolve, reject) => {
|
34 | write.on('close', () => resolve(file.path))
|
35 | write.on('error', reject)
|
36 | })
|
37 | }
|
38 |
|
39 | function* githubRepository () {
|
40 | const remote = yield runGit('remote', 'get-url', 'origin')
|
41 | const repository = gh(remote)
|
42 |
|
43 | if (repository === null) {
|
44 | throw new Error('Not a GitHub repository')
|
45 | }
|
46 |
|
47 | return repository
|
48 | }
|
49 |
|
50 | function* readCommit (commit) {
|
51 | const branch = yield getBranch('HEAD')
|
52 | const ref = yield getRef(commit)
|
53 | const message = yield getCommitTitle(ref)
|
54 |
|
55 | return Promise.resolve({
|
56 | branch: branch,
|
57 | ref: ref,
|
58 | message: message
|
59 | })
|
60 | }
|
61 |
|
62 | module.exports = {
|
63 | createArchive,
|
64 | githubRepository,
|
65 | readCommit
|
66 | }
|