/
index.js
74 lines (61 loc) · 1.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var spawn = require('child_process').spawn
, pipeline = require('stream-combiner')
, map = require('map-stream')
, split = require('split')
, path = require('path')
var defaultFormat = {
sha: '%H'
, author: {
name: '%an'
, email: '%ae'
, date: '%ad'
}
, committer: {
name: '%cn'
, email: '%ce'
, date: '%cd'
}
, body: '%B'
}
// get all format params - even nested ones
function pluck (obj) {
var params = []
Object.keys(obj).forEach(function(key) {
var args = 'string' === typeof obj[key] ? [obj[key]] : pluck(obj[key])
params.push.apply(params, args)
})
return params
};
// Dirty hack, but it works a charm.
module.exports = function(dir, options) {
dir = path.resolve(dir || process.cwd)
var extras = []
, options = options || {}
, fieldDelimiter = '$@$@$@$@$'
, lineDelimiter = '@$!$@$!$!'
, format = options.format || defaultFormat
if (options.since) extras = extras.concat(['--since', options.since / 1000])
if (options.until) extras = extras.concat(['--until', options.until / 1000])
var params = pluck(format)
var proc = spawn('git', ['log', '--format=' + params.join(fieldDelimiter) + lineDelimiter].concat(extras), { cwd: dir })
var splitter = split(lineDelimiter)
, mapper = map(function write(entry, next) {
entry = entry.split(fieldDelimiter)
if (!entry[0] || entry[0] === '\n') return next()
var last = entry.length - 1
entry[0] = entry[0].replace(/^\n/, '') // Remove leading newline
entry[last] = entry[last].replace(/\n$/, '') // Remove trailing newline
var index = 0
var commit = assign(format)
// assign entries to their respective properties
function assign(obj) {
var commit = {}
Object.keys(obj).forEach(function(key) {
commit[key] = ('string' === typeof obj[key]) ? entry[index++] : assign(obj[key])
})
return commit
}
return next(null, commit)
})
return pipeline(proc.stdout, splitter, mapper)
};