UNPKG

1.91 kBJavaScriptView Raw
1var la = require('lazy-ass')
2var check = require('check-more-types')
3var exec = require('./exec')
4
5// parses date, author, message
6function 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 // remove Date:
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/*
24parses git show --numstat output, which is something like this
25
26$ git show --numstat 46350c2
27commit 46350c2c9980551d338ce1ad0d8eff7bea9713ec
28Author: Gleb Bahmutov <gleb.bahmutov@gmail.com>
29Date: Mon Jan 13 19:04:06 2014 -0500
30
31 better gruntfile
32
331 1 .jshintrc
3454 25 Gruntfile.js
359 0 complexity.json
362 1 package.json
37*/
38function isValidLine (line) {
39 var rex = /^\s*\d+\s+\d+[\w\W]+$/
40 return line && rex.test(line)
41}
42la(isValidLine('1 1 time-method-call.js'))
43la(isValidLine(' 1 0 README.md'))
44
45function 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
72function 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
79module.exports = commitNumstat