UNPKG

1.66 kBJavaScriptView Raw
1
2module.exports = DiffSummary;
3
4/**
5 * The DiffSummary is returned as a response to getting `git().status()`
6 *
7 * @constructor
8 */
9function DiffSummary () {
10 this.files = [];
11 this.insertions = 0;
12 this.deletions = 0;
13}
14
15/**
16 * Number of lines added
17 * @type {number}
18 */
19DiffSummary.prototype.insertions = 0;
20
21/**
22 * Number of lines deleted
23 * @type {number}
24 */
25DiffSummary.prototype.deletions = 0;
26
27DiffSummary.parse = function (text) {
28 var line, handler;
29
30 var lines = text.trim().split('\n');
31 var status = new DiffSummary();
32
33 var summary = lines.pop();
34 if (summary) {
35 summary.trim().split(', ').slice(1).forEach(function (text) {
36 var summary = /(\d+)\s([a-z]+)/.exec(text);
37 if (summary) {
38 status[summary[2].replace(/s$/, '') + 's'] = parseInt(summary[1], 10);
39 }
40 });
41 }
42
43 while (line = lines.shift()) {
44 textFileChange(line, status.files) || binaryFileChange(line, status.files);
45 }
46
47 return status;
48};
49
50function textFileChange (line, files) {
51 line = line.trim().match(/^(.+)\s+\|\s+(\d+)\s+([+\-]+)$/);
52 if (line) {
53 files.push({
54 file: line[1].trim(),
55 changes: parseInt(line[2], 10),
56 insertions: line[3].replace(/\-/g, '').length,
57 deletions: line[3].replace(/\+/g, '').length,
58 binary: false
59 });
60
61 return true;
62 }
63}
64
65function binaryFileChange (line, files) {
66 line = line.match(/^(.+) \| Bin ([0-9.]+) -> ([0-9.]+) ([a-z]+)$/);
67 if (line) {
68 files.push({
69 file: line[1].trim(),
70 before: +line[2],
71 after: +line[3],
72 binary: true
73 });
74 return true;
75 }
76}