UNPKG

2.99 kBJavaScriptView Raw
1
2module.exports = PullSummary;
3
4/**
5 * The PullSummary is returned as a response to getting `git().pull()`
6 *
7 * @constructor
8 */
9function PullSummary () {
10 this.files = [];
11 this.insertions = {};
12 this.deletions = {};
13
14 this.summary = {
15 changes: 0,
16 insertions: 0,
17 deletions: 0
18 };
19
20 this.created = [];
21 this.deleted = [];
22}
23
24/**
25 * Array of files that were created
26 * @type {string[]}
27 */
28PullSummary.prototype.created = null;
29
30/**
31 * Array of files that were deleted
32 * @type {string[]}
33 */
34PullSummary.prototype.deleted = null;
35
36/**
37 * The array of file paths/names that have been modified in any part of the pulled content
38 * @type {string[]}
39 */
40PullSummary.prototype.files = null;
41
42/**
43 * A map of file path to number to show the number of insertions per file.
44 * @type {Object}
45 */
46PullSummary.prototype.insertions = null;
47
48/**
49 * A map of file path to number to show the number of deletions per file.
50 * @type {Object}
51 */
52PullSummary.prototype.deletions = null;
53
54/**
55 * Overall summary of changes/insertions/deletions and the number associated with each
56 * across all content that was pulled.
57 * @type {Object}
58 */
59PullSummary.prototype.summary = null;
60
61PullSummary.FILE_UPDATE_REGEX = /^\s*(.+?)\s+\|\s+\d+\s*(\+*)(-*)/;
62PullSummary.SUMMARY_REGEX = /(\d+)\D+((\d+)\D+\(\+\))?(\D+(\d+)\D+\(-\))?/;
63PullSummary.ACTION_REGEX = /(create|delete) mode \d+ (.+)/;
64
65PullSummary.parse = function (text) {
66 var pullSummary = new PullSummary;
67 var lines = text.split('\n');
68
69 while (lines.length) {
70 var line = lines.shift().trim();
71 if (!line) {
72 continue;
73 }
74
75 update(pullSummary, line) || summary(pullSummary, line) || action(pullSummary, line);
76 }
77
78 return pullSummary;
79};
80
81function update (pullSummary, line) {
82
83 var update = PullSummary.FILE_UPDATE_REGEX.exec(line);
84 if (!update) {
85 return false;
86 }
87
88 pullSummary.files.push(update[1]);
89
90 var insertions = update[2].length;
91 if (insertions) {
92 pullSummary.insertions[update[1]] = insertions;
93 }
94
95 var deletions = update[3].length;
96 if (deletions) {
97 pullSummary.deletions[update[1]] = deletions;
98 }
99
100 return true;
101}
102
103function summary (pullSummary, line) {
104 if (!pullSummary.files.length) {
105 return false;
106 }
107
108 var update = PullSummary.SUMMARY_REGEX.exec(line);
109 if (!update || (update[3] === undefined && update[5] === undefined)) {
110 return false;
111 }
112
113 pullSummary.summary.changes = +update[1] || 0;
114 pullSummary.summary.insertions = +update[3] || 0;
115 pullSummary.summary.deletions = +update[5] || 0;
116
117 return true;
118}
119
120function action (pullSummary, line) {
121
122 var match = PullSummary.ACTION_REGEX.exec(line);
123 if (!match) {
124 return false;
125 }
126
127 var file = match[2];
128
129 if (pullSummary.files.indexOf(file) < 0) {
130 pullSummary.files.push(file);
131 }
132
133 var container = (match[1] === 'create') ? pullSummary.created : pullSummary.deleted;
134 container.push(file);
135
136 return true;
137}