UNPKG

2.23 kBJavaScriptView Raw
1var la = require('lazy-ass')
2var is = require('check-more-types')
3
4function 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 // should be id space log message
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
22function isNotMergeLine (s) {
23 return !/^Merge: /.test(s)
24}
25
26/*
27 parses single commit message (several lines), like this one
28
29 commit 7fbeb0ada137bc93493731df60bada794d95b13b
30 Author: Gleb Bahmutov <gleb.bahmutov@gmail.com>
31 Commit: Gleb Bahmutov <gleb.bahmutov@gmail.com>
32
33 chore(main): Main file with parsing of the message
34
35 This is the main logic
36
37If this is a merge commit, merge line is removed
38*/
39function 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
51var commitMessageSchema = {
52 id: is.unemptyString,
53 message: is.unemptyString,
54 body: is.maybe.array
55}
56var isCommitMessage = is.schema.bind(null, commitMessageSchema)
57
58function 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 parses git log generated using
78 git log --pretty=full
79
80 only looks at lines starting with "commit <SHA>" to separate the commits
81*/
82function parseCommitLog (data) {
83 la(is.string(data), 'expected string data', data)
84 // commit [SHA]
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
90module.exports = {
91 parseOneLineLog: parseOneLineLog,
92 parseCommitLog: parseCommitLog
93}