forked from steveukx/git-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPullSummary.js
137 lines (106 loc) · 2.92 KB
/
PullSummary.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module.exports = PullSummary;
/**
* The PullSummary is returned as a response to getting `git().pull()`
*
* @constructor
*/
function PullSummary () {
this.files = [];
this.insertions = {};
this.deletions = {};
this.summary = {
changes: 0,
insertions: 0,
deletions: 0
};
this.created = [];
this.deleted = [];
}
/**
* Array of files that were created
* @type {string[]}
*/
PullSummary.prototype.created = null;
/**
* Array of files that were deleted
* @type {string[]}
*/
PullSummary.prototype.deleted = null;
/**
* The array of file paths/names that have been modified in any part of the pulled content
* @type {string[]}
*/
PullSummary.prototype.files = null;
/**
* A map of file path to number to show the number of insertions per file.
* @type {Object}
*/
PullSummary.prototype.insertions = null;
/**
* A map of file path to number to show the number of deletions per file.
* @type {Object}
*/
PullSummary.prototype.deletions = null;
/**
* Overall summary of changes/insertions/deletions and the number associated with each
* across all content that was pulled.
* @type {Object}
*/
PullSummary.prototype.summary = null;
PullSummary.FILE_UPDATE_REGEX = /^\s*(.+?)\s+\|\s+\d+\s*(\+*)(-*)/;
PullSummary.SUMMARY_REGEX = /(\d+)\D+((\d+)\D+\(\+\))?(\D+(\d+)\D+\(-\))?/;
PullSummary.ACTION_REGEX = /(create|delete) mode \d+ (.+)/;
PullSummary.parse = function (text) {
var pullSummary = new PullSummary;
var lines = text.split('\n');
while (lines.length) {
var line = lines.shift().trim();
if (!line) {
continue;
}
update(pullSummary, line) || summary(pullSummary, line) || action(pullSummary, line);
}
return pullSummary;
};
function update (pullSummary, line) {
var update = PullSummary.FILE_UPDATE_REGEX.exec(line);
if (!update) {
return false;
}
pullSummary.files.push(update[1]);
var insertions = update[2].length;
if (insertions) {
pullSummary.insertions[update[1]] = insertions;
}
var deletions = update[3].length;
if (deletions) {
pullSummary.deletions[update[1]] = deletions;
}
return true;
}
function summary (pullSummary, line) {
if (!pullSummary.files.length) {
return false;
}
var update = PullSummary.SUMMARY_REGEX.exec(line);
if (!update || (update[3] === undefined && update[5] === undefined)) {
return false;
}
pullSummary.summary.changes = +update[1] || 0;
pullSummary.summary.insertions = +update[3] || 0;
pullSummary.summary.deletions = +update[5] || 0;
return true;
}
function action (pullSummary, line) {
var match = PullSummary.ACTION_REGEX.exec(line);
if (!match) {
return false;
}
var file = match[2];
if (pullSummary.files.indexOf(file) < 0) {
pullSummary.files.push(file);
}
var container = (match[1] === 'create') ? pullSummary.created : pullSummary.deleted;
container.push(file);
return true;
}