-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
75 lines (56 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const matchGroups = require('./lib/match-groups');
const parseStat = require('./lib/parse-stat');
const parseTags = require('./lib/parse-tags');
const parseBranches = require('./lib/parse-branches');
const Cursor = require('./lib/cursor');
const dayjs = require('dayjs');
module.exports = function(log) {
const commits = [];
const lines = log.split(/\r?\n/g);
const cursor = new Cursor(lines);
while (cursor.hasNext() && cursor.peek().length > 0) {
const { sha, decoration } = matchGroups(/commit\s(?<sha>[a-f0-9]*)(\s\((?<decoration>.*)\))?/i, cursor.next());
if (!sha) {
throw new Error(`Could not parse git log entry with no sha given at line ${cursor.index()}`);
}
const tags = parseTags(decoration);
const branches = parseBranches(decoration);
let author = cursor.next();
let merge = null;
if (author.indexOf('Merge') >= 0) {
merge = author;
author = cursor.next();
}
if (!author) {
throw new Error(`Could not parse git log entry with no author given at line ${cursor.index()}`);
}
const dateRaw = matchGroups(/Date:\s+\w+\s(?<date>.*)/, cursor.next()).date;
const date = dayjs(dateRaw, 'MMM D HH:mm:ss YYYY ZZ').toDate();
// skip newline
cursor.next();
const message = cursor
.nextWhile(line => line.length > 0)
.map(line => line.trim())
.reduce((accumulator, current, idx) => (idx === 0 ? current : accumulator + '\n' + current), '');
const stat = parseStat(cursor);
const commit = {
sha,
author: parseAuthor(author),
merge,
date,
message,
stat,
};
if (tags) {
commit.tags = tags;
}
if (branches) {
commit.branches = branches;
}
commits.push(commit);
}
return commits;
};
function parseAuthor(author) {
return matchGroups(/Author:\s(?<name>.*?)\s<(?<email>.*?)>/, author);
}