forked from steveukx/git-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffSummary.js
92 lines (77 loc) · 1.93 KB
/
DiffSummary.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module.exports = DiffSummary;
/**
* The DiffSummary is returned as a response to getting `git().status()`
*
* @constructor
*/
function DiffSummary () {
this.files = [];
this.insertions = 0;
this.deletions = 0;
this.changed = 0;
}
/**
* Number of lines added
* @type {number}
*/
DiffSummary.prototype.insertions = 0;
/**
* Number of lines deleted
* @type {number}
*/
DiffSummary.prototype.deletions = 0;
/**
* Number of files changed
* @type {number}
*/
DiffSummary.prototype.changed = 0;
DiffSummary.parse = function (text) {
var line, handler;
var lines = text.trim().split('\n');
var status = new DiffSummary();
var summary = lines.pop();
if (summary) {
summary.trim().split(', ').forEach(function (text) {
var summary = /(\d+)\s([a-z]+)/.exec(text);
if (!summary) {
return;
}
if (/files?/.test(summary[2])) {
status.changed = parseInt(summary[1], 10);
}
else {
status[summary[2].replace(/s$/, '') + 's'] = parseInt(summary[1], 10);
}
});
}
while (line = lines.shift()) {
textFileChange(line, status.files) || binaryFileChange(line, status.files);
}
return status;
};
function textFileChange (line, files) {
line = line.trim().match(/^(.+)\s+\|\s+(\d+)(\s+[+\-]+)?$/);
if (line) {
var alterations = (line[3] || '').trim();
files.push({
file: line[1].trim(),
changes: parseInt(line[2], 10),
insertions: alterations.replace(/-/g, '').length,
deletions: alterations.replace(/\+/g, '').length,
binary: false
});
return true;
}
}
function binaryFileChange (line, files) {
line = line.match(/^(.+) \|\s+Bin ([0-9.]+) -> ([0-9.]+) ([a-z]+)$/);
if (line) {
files.push({
file: line[1].trim(),
before: +line[2],
after: +line[3],
binary: true
});
return true;
}
}