This repository was archived by the owner on May 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (70 loc) · 2.05 KB
/
Copy pathindex.js
File metadata and controls
81 lines (70 loc) · 2.05 KB
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
const path = require("path");
const colors = require("colors");
const {verifyCommit} = require("./verifyCommit");
const projectDirectory = __dirname.replace(/(\\|\/)node_modules(.+)$/, '');
const defaults = require('./defaults');
try {
packageJson = require(path.join(projectDirectory, 'package.json'));
} catch (e) {
console.log(defaults.errorWhileParsingPackageJson, e);
process.exit(1);
}
let config = packageJson['commit-naming-enforcement'] || {};
function getConfFromKey(key)
{
return (config[key] !== undefined) ? config[key] : defaults[key];
}
/* Module is disabled, we stop right there */
if( !getConfFromKey("enabled") )
{
process.exit(0);
}
const {exec} = require("child_process");
function execute(command, callback) {
exec(command, (error, stdout, stderr) => {
callback(stdout);
});
}
let firstCommitFound = false;
let lastCommitFound = false;
let lastCommit;
execute("git rev-list HEAD --max-count=1", (val) => {
lastCommit = val;
lastCommitFound = !lastCommitFound;
commitSHAFound();
});
let gitRefList;
let foundSHA;
execute("git rev-list HEAD --max-count=15 --pretty=%d", (val) => {
gitRefList = val;
foundSHA = (/commit ([^ ]+)\n.+[^ ]+\/HEAD/.exec(gitRefList) || [])[1];
firstCommitFound = !firstCommitFound;
commitSHAFound();
});
function commitSHAFound() {
if (firstCommitFound && lastCommitFound) {
execute(`git rev-list ${foundSHA}...${lastCommit}`, (commits) => {
checkOnCommits(commits.split("\n").filter(Boolean));
});
}
}
function checkOnCommits(commits) {
const max = commits.length;
let localCounter = 1;
commits.forEach((commitId) => {
execute(`git log --format=%B -n 1 ${commitId}`, (commitBody) => {
localCounter++;
validOrFail(verifyCommit(commitId, commitBody, getConfFromKey));
});
});
function validOrFail(returnedValue){
if(returnedValue === 1)
{
process.exit(1);
}
if(localCounter > max)
{
process.exit(0);
}
}
}