UNPKG

1.11 kBJavaScriptView Raw
1var spawn = require('child_process').spawn
2var check = require('check-more-types')
3
4module.exports = {
5 getFileRevision: getFileRevision,
6 getRepoRevision: getRepoRevision
7}
8
9// commit can be SHA-1 or symbolic master~3 type
10// filename should be relative to repo root
11function 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 // console.log('getting file revision', args);
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 // console.log(data);
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
40function getRepoRevision (filename, cb) {
41 getFileRevision('master', filename, cb)
42}