1 |
|
2 | module.exports = BranchSummary;
|
3 |
|
4 | function BranchSummary () {
|
5 | this.detached = false;
|
6 | this.current = '';
|
7 | this.all = [];
|
8 | this.branches = {};
|
9 | }
|
10 |
|
11 | BranchSummary.prototype.push = function (current, detached, name, commit, label) {
|
12 | if (current) {
|
13 | this.detached = detached;
|
14 | this.current = name;
|
15 | }
|
16 | this.all.push(name);
|
17 | this.branches[name] = {
|
18 | current: current,
|
19 | name: name,
|
20 | commit: commit,
|
21 | label: label
|
22 | };
|
23 | };
|
24 |
|
25 | BranchSummary.detachedRegex = /^(\*?\s+)\((?:HEAD )?detached (?:from|at) (\S+)\)\s+([a-z0-9]+)\s(.*)$/;
|
26 | BranchSummary.branchRegex = /^(\*?\s+)(\S+)\s+([a-z0-9]+)\s(.*)$/;
|
27 |
|
28 | BranchSummary.parse = function (commit) {
|
29 | var branchSummary = new BranchSummary();
|
30 |
|
31 | commit.split('\n')
|
32 | .forEach(function (line) {
|
33 | var detached = true;
|
34 | var branch = BranchSummary.detachedRegex.exec(line);
|
35 | if (!branch) {
|
36 | detached = false;
|
37 | branch = BranchSummary.branchRegex.exec(line);
|
38 | }
|
39 |
|
40 | if (branch) {
|
41 | branchSummary.push(
|
42 | branch[1].charAt(0) === '*',
|
43 | detached,
|
44 | branch[2],
|
45 | branch[3],
|
46 | branch[4]
|
47 | );
|
48 | }
|
49 | });
|
50 |
|
51 | return branchSummary;
|
52 | };
|