1 | var la = require('lazy-ass')
|
2 | var check = require('check-more-types')
|
3 | var exec = require('./exec')
|
4 |
|
5 |
|
6 | function commitMeta (lines) {
|
7 | la(lines.length > 3, 'expected more lines', lines)
|
8 |
|
9 | var commit = lines[0].split(' ')[1].trim()
|
10 | var author = lines[1].split(':')[1].trim()
|
11 |
|
12 | var date = lines[2].substr(5).trim()
|
13 | var message = lines[4].trim()
|
14 |
|
15 | return {
|
16 | commit: commit,
|
17 | author: author,
|
18 | date: date,
|
19 | message: message
|
20 | }
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function isValidLine (line) {
|
39 | var rex = /^\s*\d+\s+\d+[\w\W]+$/
|
40 | return line && rex.test(line)
|
41 | }
|
42 | la(isValidLine('1 1 time-method-call.js'))
|
43 | la(isValidLine(' 1 0 README.md'))
|
44 |
|
45 | function parseNumstat (stdout) {
|
46 | la(check.unemptyString(stdout), 'missing numstat output', stdout)
|
47 | var lines = stdout.split('\n')
|
48 | la(lines.length > 3, 'expected more lines', stdout)
|
49 |
|
50 | var info = commitMeta(lines)
|
51 |
|
52 | var k = 6
|
53 | var fileChanges = {}
|
54 | for (; k < lines.length; k += 1) {
|
55 | if (isValidLine(lines[k])) {
|
56 | var parts = lines[k].split('\t')
|
57 | var added = Number(parts[0].trim())
|
58 | var deleted = Number(parts[1].trim())
|
59 | var filename = parts[2].trim()
|
60 | fileChanges[filename] = {
|
61 | filename: filename,
|
62 | added: added,
|
63 | deleted: deleted
|
64 | }
|
65 | }
|
66 | }
|
67 |
|
68 | info.changes = fileChanges
|
69 | return info
|
70 | }
|
71 |
|
72 | function commitNumstat (hash) {
|
73 | la(check.unemptyString(hash), 'missing commit hash', hash)
|
74 |
|
75 | var cmd = 'git show --numstat ' + hash
|
76 | return exec(cmd).then(parseNumstat)
|
77 | }
|
78 |
|
79 | module.exports = commitNumstat
|