1 | var la = require('lazy-ass')
|
2 | var is = require('check-more-types')
|
3 |
|
4 | function parseOneLineLog (data) {
|
5 | la(is.string(data), 'expected string data', data)
|
6 |
|
7 | var lines = data.split('\n')
|
8 | lines = lines.filter(function (line) {
|
9 | return !!line
|
10 | })
|
11 | var splitLines = lines.map(function (line) {
|
12 |
|
13 | var firstSpace = line.indexOf(' ')
|
14 | return {
|
15 | id: line.substr(0, firstSpace),
|
16 | message: line.substr(firstSpace).trim()
|
17 | }
|
18 | })
|
19 | return splitLines
|
20 | }
|
21 |
|
22 | function isNotMergeLine (s) {
|
23 | return !/^Merge: /.test(s)
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function parseCommit (oneCommit) {
|
40 | la(is.unemptyString(oneCommit), 'expected commit', oneCommit)
|
41 | var lines = oneCommit.split('\n')
|
42 | lines = lines.filter(isNotMergeLine)
|
43 |
|
44 | return {
|
45 | id: lines[0].substr(7).trim(),
|
46 | message: lines[4],
|
47 | body: lines.slice(6)
|
48 | }
|
49 | }
|
50 |
|
51 | var commitMessageSchema = {
|
52 | id: is.unemptyString,
|
53 | message: is.unemptyString,
|
54 | body: is.maybe.array
|
55 | }
|
56 | var isCommitMessage = is.schema.bind(null, commitMessageSchema)
|
57 |
|
58 | function trim (parsedInfo) {
|
59 | la(isCommitMessage(parsedInfo), 'invalid commit info', parsedInfo)
|
60 |
|
61 | parsedInfo.message = parsedInfo.message.trim()
|
62 | la(
|
63 | is.array(parsedInfo.body),
|
64 | 'expected list of lines in the body',
|
65 | parsedInfo
|
66 | )
|
67 | parsedInfo.body = parsedInfo.body
|
68 | .map(function (line) {
|
69 | return line.trim()
|
70 | })
|
71 | .join('\n')
|
72 | .trim()
|
73 | return parsedInfo
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | function parseCommitLog (data) {
|
83 | la(is.string(data), 'expected string data', data)
|
84 |
|
85 | var commits = data.split(/\n(?=commit [0-9a-f]{40})\n?/g)
|
86 |
|
87 | return commits.filter(is.unemptyString).map(parseCommit).map(trim)
|
88 | }
|
89 |
|
90 | module.exports = {
|
91 | parseOneLineLog: parseOneLineLog,
|
92 | parseCommitLog: parseCommitLog
|
93 | }
|