92 lines · 1.9 KB · JavaScript
RawDownload
 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;   }}