forked from netease-lcap/CodeWaveSummerCompetition2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectCommitInfo.js
82 lines (78 loc) · 2.35 KB
/
collectCommitInfo.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
76
77
78
79
80
81
82
const { execCommand } = require('./execCommand');
const mergewith = require('lodash.mergewith');
const titleReg = /^(\S+?)(?:\((\S+)\))?\s*[\:\:](.*)/g;
const breakchangeReg = /^BREAKING CHANGE\s*[\:\:]\s*(\S.*)/g;
module.exports.collectCommitInfo = async (defaultLibraryName) => {
const commitsRaw = await execCommand(
`git log --format===='%n%H;%h;%an;%ae%n%B' main..HEAD`,
);
const tmpResult = {};
const revertCommitMap = {};
commitsRaw.split('===').forEach((commitStr) => {
if (!commitStr) return;
const lines = commitStr.split('\n');
const [hash, shotHash, authorName, authorEmail] = lines[1].split(';');
if (revertCommitMap[hash]) {
// current commit has been reverted
return;
}
lines.slice(2).forEach((_line) => {
const line = _line.trim();
const titleResult = titleReg.exec(line);
const breakChangeResult = breakchangeReg.exec(line);
let libraryName = defaultLibraryName;
if (titleResult) {
const type = getRealType(titleResult[1]);
if (titleResult[2]) libraryName = titleResult[2];
const subject = titleResult[3];
if (type === 'revert') {
revertCommitMap[libraryName] = true;
return;
}
if (type && libraryName) {
merge(tmpResult, {
[libraryName]: {
[type]: [
{
subject,
hash,
shotHash,
authorName,
authorEmail,
},
],
},
});
}
}
if (breakChangeResult) {
const breakingChange = breakChangeResult[1];
if (breakingChange || libraryName) {
merge(tmpResult, {
[libraryName]: {
breakingChange: [
{
subject: breakingChange,
authorName,
authorEmail,
hash,
shotHash,
},
],
},
});
}
}
});
});
return tmpResult;
};
const getRealType = (type) => {
if (/^fix$/i.test(type)) return 'fix';
if (/^feat$/i.test(type)) return 'feat';
if (/^revert$/i.test(type)) return 'revert';
};
const merge = (a, b) =>
mergewith(a, b, (source, target) => {
if (Array.isArray(source)) return source.concat(target);
});