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