UNPKG

1.9 kBJavaScriptView Raw
1'use strict';
2module.exports = MergeSummary;
3
4var PullSummary = require('./PullSummary');
5
6function MergeConflict (reason, file) {
7 this.reason = reason;
8 this.file = file;
9}
10
11MergeConflict.prototype.toString = function () {
12 return this.file + ':' + this.reason;
13};
14
15function MergeSummary () {
16 PullSummary.call(this);
17
18 this.conflicts = [];
19 this.merges = [];
20}
21
22MergeSummary.prototype = Object.create(PullSummary.prototype);
23
24MergeSummary.prototype.result = 'success';
25
26MergeSummary.prototype.toString = function () {
27 if (this.conflicts.length) {
28 return 'CONFLICTS: ' + this.conflicts.join(', ');
29 }
30 return 'OK';
31};
32
33Object.defineProperty(MergeSummary.prototype, 'failed', {
34 get: function () {
35 return this.conflicts.length > 0;
36 }
37});
38
39MergeSummary.parsers = [
40 {
41 test: /^Auto-merging\s+(.+)$/,
42 handle: function (result, mergeSummary) {
43 mergeSummary.merges.push(result[1]);
44 }
45 },
46 {
47 test: /^CONFLICT\s+\((.+)\).+ in (.+)$/,
48 handle: function (result, mergeSummary) {
49 mergeSummary.conflicts.push(new MergeConflict(result[1], result[2]));
50 }
51 },
52 {
53 test: /^Automatic merge failed;\s+(.+)$/,
54 handle: function (result, mergeSummary) {
55 mergeSummary.reason = result[1];
56 }
57 }
58];
59
60MergeSummary.parse = function (output) {
61 let mergeSummary = new MergeSummary();
62
63 output.trim().split('\n').forEach(function (line) {
64 for (var i = 0, iMax = MergeSummary.parsers.length; i < iMax; i++) {
65 let parser = MergeSummary.parsers[i];
66
67 var result = parser.test.exec(line);
68 if (result) {
69 parser.handle(result, mergeSummary);
70 break;
71 }
72 }
73 });
74
75 let pullSummary = PullSummary.parse(output);
76 if (pullSummary.summary.changes) {
77 Object.assign(mergeSummary, pullSummary);
78 }
79
80 return mergeSummary;
81};