History

Update Git documentation (2.30.0)

Simon Legner committed 5 years ago

Commit 3600a9f1 parent 8f17cfa41 files · +4398 −1Browse files

Showing the changes to node_modules/simple-git/src/responses/StatusSummary.js (1 of 41 files). Show all

Unified · Split

node_modules/simple-git/src/responses/StatusSummary.js+188−0
@@ -0,0 +1,188 @@
1
2
3
4var FileStatusSummary = require('./FileStatusSummary');
5
6module.exports = StatusSummary;
7
8/**
9 * The StatusSummary is returned as a response to getting `git().status()`
10 *
11 * @constructor
12 */
13function StatusSummary () {
14 this.not_added = [];
15 this.conflicted = [];
16 this.created = [];
17 this.deleted = [];
18 this.modified = [];
19 this.renamed = [];
20 this.files = [];
21 this.staged = [];
22}
23
24
25/**
26 * Number of commits ahead of the tracked branch
27 * @type {number}
28 */
29StatusSummary.prototype.ahead = 0;
30
31/**
32 * Number of commits behind the tracked branch
33 * @type {number}
34 */
35StatusSummary.prototype.behind = 0;
36
37/**
38 * Name of the current branch
39 * @type {null}
40 */
41StatusSummary.prototype.current = null;
42
43/**
44 * Name of the branch being tracked
45 * @type {string}
46 */
47StatusSummary.prototype.tracking = null;
48
49/**
50 * All files represented as an array of objects containing the `path` and status in `index` and
51 * in the `working_dir`.
52 *
53 * @type {Array}
54 */
55StatusSummary.prototype.files = null;
56
57/**
58 * Gets whether this StatusSummary represents a clean working branch.
59 *
60 * @return {boolean}
61 */
62StatusSummary.prototype.isClean = function () {
63 return 0 === Object.keys(this).filter(function (name) {
64 return Array.isArray(this[name]) && this[name].length;
65 }, this).length;
66};
67
68StatusSummary.parsers = {
69 '##': function (line, status) {
70 var aheadReg = /ahead (\d+)/;
71 var behindReg = /behind (\d+)/;
72 var currentReg = /^(.+?(?=(?:\.{3}|\s|$)))/;
73 var trackingReg = /\.{3}(\S*)/;
74 var regexResult;
75
76 regexResult = aheadReg.exec(line);
77 status.ahead = regexResult && +regexResult[1] || 0;
78
79 regexResult = behindReg.exec(line);
80 status.behind = regexResult && +regexResult[1] || 0;
81
82 regexResult = currentReg.exec(line);
83 status.current = regexResult && regexResult[1];
84
85 regexResult = trackingReg.exec(line);
86 status.tracking = regexResult && regexResult[1];
87 },
88
89 '??': function (line, status) {
90 status.not_added.push(line);
91 },
92
93 A: function (line, status) {
94 status.created.push(line);
95 },
96
97 AM: function (line, status) {
98 status.created.push(line);
99 },
100
101 D: function (line, status) {
102 status.deleted.push(line);
103 },
104
105 M: function (line, status, indexState) {
106 status.modified.push(line);
107
108 if (indexState === 'M') {
109 status.staged.push(line);
110 }
111 },
112
113 R: function (line, status) {
114 var detail = /^(.+) -> (.+)$/.exec(line) || [null, line, line];
115
116 status.renamed.push({
117 from: detail[1],
118 to: detail[2]
119 });
120 },
121
122 UU: function (line, status) {
123 status.conflicted.push(line);
124 }
125};
126
127StatusSummary.parsers.MM = StatusSummary.parsers.M;
128
129/* Map all unmerged status code combinations to UU to mark as conflicted */
130StatusSummary.parsers.AA = StatusSummary.parsers.UU;
131StatusSummary.parsers.UD = StatusSummary.parsers.UU;
132StatusSummary.parsers.DU = StatusSummary.parsers.UU;
133StatusSummary.parsers.DD = StatusSummary.parsers.UU;
134StatusSummary.parsers.AU = StatusSummary.parsers.UU;
135StatusSummary.parsers.UA = StatusSummary.parsers.UU;
136
137StatusSummary.parse = function (text) {
138 var file;
139 var lines = text.trim().split('\n');
140 var status = new StatusSummary();
141
142 for (var i = 0, l = lines.length; i < l; i++) {
143 file = splitLine(lines[i]);
144
145 if (!file) {
146 continue;
147 }
148
149 if (file.handler) {
150 file.handler(file.path, status, file.index, file.workingDir);
151 }
152
153 if (file.code !== '##') {
154 status.files.push(new FileStatusSummary(file.path, file.index, file.workingDir));
155 }
156 }
157
158 return status;
159};
160
161
162function splitLine (lineStr) {
163 var line = lineStr.trim().match(/(..?)(\s+)(.*)/);
164 if (!line || !line[1].trim()) {
165 line = lineStr.trim().match(/(..?)\s+(.*)/);
166 }
167
168 if (!line) {
169 return;
170 }
171
172 var code = line[1];
173 if (line[2].length > 1) {
174 code += ' ';
175 }
176 if (code.length === 1 && line[2].length === 1) {
177 code = ' ' + code;
178 }
179
180 return {
181 raw: code,
182 code: code.trim(),
183 index: code.charAt(0),
184 workingDir: code.charAt(1),
185 handler: StatusSummary.parsers[code.trim()],
186 path: line[3]
187 };
188}