1 | var spawn = require('child_process').spawn
|
2 | var check = require('check-more-types')
|
3 |
|
4 | module.exports = {
|
5 | getFileRevision: getFileRevision,
|
6 | getRepoRevision: getRepoRevision
|
7 | }
|
8 |
|
9 |
|
10 |
|
11 | function getFileRevision (commit, filename, cb) {
|
12 | check.verify.string(commit, 'missing commit code')
|
13 | check.verify.string(filename, 'missing filename')
|
14 | check.verify.fn(cb, 'expected function callback')
|
15 |
|
16 | filename = filename.replace(/\\/g, '/')
|
17 |
|
18 | var args = ['show', commit + ':' + filename]
|
19 |
|
20 | var git = spawn('git', args)
|
21 |
|
22 | var contents = ''
|
23 | git.stdout.setEncoding('utf-8')
|
24 | git.stdout.on('data', function (data) {
|
25 | data.trim()
|
26 |
|
27 | contents += data
|
28 | })
|
29 |
|
30 | git.stderr.setEncoding('utf-8')
|
31 | git.stderr.on('data', function (data) {
|
32 | throw new Error('Could not get file\n' + filename + '\n' + data)
|
33 | })
|
34 |
|
35 | git.on('exit', function () {
|
36 | cb(contents)
|
37 | })
|
38 | }
|
39 |
|
40 | function getRepoRevision (filename, cb) {
|
41 | getFileRevision('master', filename, cb)
|
42 | }
|