1 |
|
2 | module.exports = CommitSummary;
|
3 |
|
4 | function CommitSummary () {
|
5 | this.branch = '';
|
6 | this.commit = '';
|
7 | this.summary = {
|
8 | changes: 0,
|
9 | insertions: 0,
|
10 | deletions: 0
|
11 | };
|
12 | this.author = null;
|
13 | }
|
14 |
|
15 | var COMMIT_BRANCH_MESSAGE_REGEX = /\[([^\s]+) ([^\]]+)/;
|
16 | var COMMIT_AUTHOR_MESSAGE_REGEX = /\s*Author:\s(.+)/i;
|
17 |
|
18 | function setBranchFromCommit (commitSummary, commitData) {
|
19 | if (commitData) {
|
20 | commitSummary.branch = commitData[1];
|
21 | commitSummary.commit = commitData[2];
|
22 | }
|
23 | }
|
24 |
|
25 | function setSummaryFromCommit (commitSummary, commitData) {
|
26 | if (commitSummary.branch && commitData) {
|
27 | commitSummary.summary.changes = commitData[1] || 0;
|
28 | commitSummary.summary.insertions = commitData[2] || 0;
|
29 | commitSummary.summary.deletions = commitData[3] || 0;
|
30 | }
|
31 | }
|
32 |
|
33 | function setAuthorFromCommit (commitSummary, commitData) {
|
34 | var parts = commitData[1].split('<');
|
35 | var email = parts.pop();
|
36 |
|
37 | if (email.indexOf('@') <= 0) {
|
38 | return;
|
39 | }
|
40 |
|
41 | commitSummary.author = {
|
42 | email: email.substr(0, email.length - 1),
|
43 | name: parts.join('<').trim()
|
44 | };
|
45 | }
|
46 |
|
47 | CommitSummary.parse = function (commit) {
|
48 | var lines = commit.trim().split('\n');
|
49 | var commitSummary = new CommitSummary();
|
50 |
|
51 | setBranchFromCommit(commitSummary, COMMIT_BRANCH_MESSAGE_REGEX.exec(lines.shift()));
|
52 |
|
53 | if (COMMIT_AUTHOR_MESSAGE_REGEX.test(lines[0])) {
|
54 | setAuthorFromCommit(commitSummary, COMMIT_AUTHOR_MESSAGE_REGEX.exec(lines.shift()));
|
55 | }
|
56 |
|
57 | setSummaryFromCommit(commitSummary, /(\d+)[^,]*(?:,\s*(\d+)[^,]*)?(?:,\s*(\d+))?/g.exec(lines.shift()));
|
58 |
|
59 | return commitSummary;
|
60 | };
|