Skip to content

Commit a841ae6

Browse files
committed
feat: add type and scope description to commit-msg-hook error log
1 parent ea6f08d commit a841ae6

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

git-conventional-commits.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ convention:
1313
- chore # Miscellaneous commits e.g. modifying `.gitignore`
1414
- merge
1515
- revert
16-
commitScopes: []
16+
commitScopes: [s]
1717
releaseTagGlobPattern: v[0-9]*.[0-9]*.[0-9]*
1818
changelog:
1919
commitTypes:

lib/commands/commandCommitMessageHook.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@ exports.handler = async function (argv) {
3939
!convention.commitTypes.includes(conventionalCommit.type)) {
4040
conventionError = true
4141
console.error(`[ERROR] Unexpected commit type: '${conventionalCommit.type}'`);
42-
console.error(` Permitted types : ${convention.commitTypes.map(e => `'${e}'`).join(', ')}`);
42+
const maxTypeLength = Math.max(...convention.commitTypes.map(s => s.length));
43+
console.error(` Permitted types:\n` +
44+
convention.commitTypes.map(e => ` - ${e.padEnd(maxTypeLength )} ${convention.commitTypeDescriptions?.[e] ?? ''}`).join('\n'));
4345
}
4446
if (conventionalCommit.scope &&
4547
convention.commitScopes && convention.commitScopes.length &&
4648
!convention.commitScopes.includes(conventionalCommit.scope)) {
4749
conventionError = true
4850
console.error(`[ERROR] Unexpected commit scope: '${conventionalCommit.scope}'`);
49-
console.error(` Permitted scopes: ${!convention.commitScopes || !convention.commitScopes.length ? '*' : convention.commitScopes.map(e => `'${e}'`).join(', ')}`);
51+
const maxScopeLength = Math.max(...convention.commitScopes.map(s => s.length));
52+
console.error(` Permitted scopes:\n` +
53+
convention.commitScopes.map(e => ` - ${e.padEnd(maxScopeLength )} ${convention.commitScopeDescriptions?.[e] ?? ''}`).join('\n'));
5054
}
5155

5256
if (conventionError) {

lib/commands/config.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function load(configFile) {
1717
}
1818

1919
const configText = fs.readFileSync(path.resolve(configFile)).toString();
20-
const configOverride = YAML.parse(configText);
21-
20+
const configOverrideDocument = YAML.parseDocument(configText);
21+
const configOverride =configOverrideDocument.toJSON();
2222
const config = defaultConfig();
2323

2424
if (configOverride.convention) {
@@ -29,8 +29,22 @@ function load(configFile) {
2929

3030
conventionConfig.msgRegex = RegExp(conventionOverride.commitMessageRegexPattern || conventionConfig.msgRegex);
3131
conventionConfig.commitTypes = conventionOverride.commitTypes || conventionConfig.commitTypes;
32+
conventionConfig.commitTypeDescriptions = configOverrideDocument
33+
.get('convention').get('commitTypes')
34+
.items.reduce((acc, item) => {
35+
acc[item.value] = [item.commentBefore?.trim(), item.comment?.trim()]
36+
.filter(Boolean).join('\n') || undefined;
37+
return acc;
38+
}, {});
3239
conventionConfig.featureCommitTypes = conventionOverride.featureCommitTypes || conventionConfig.featureCommitTypes;
3340
conventionConfig.commitScopes = conventionOverride.commitScopes || conventionConfig.commitScopes;
41+
conventionConfig.commitScopeDescriptions = configOverrideDocument
42+
.get('convention').get('commitScopes')
43+
.items.reduce((acc, item) => {
44+
acc[item.value] = [item.commentBefore?.trim(), item.comment?.trim()]
45+
.filter(Boolean).join('\n') || undefined;
46+
return acc;
47+
}, {});
3448

3549
// Legacy support convention.issueRegexPattern
3650
config.changelog.issueRegexPattern = conventionOverride.issueRegexPattern || config.changelog.issueRegexPattern;
@@ -80,8 +94,10 @@ function defaultConfig() {
8094
'chore',
8195
'merge',
8296
],
97+
commitTypeDescriptions: {},
8398
featureCommitTypes: ['feat'],
8499
commitScopes: null,
100+
commitScopeDescriptions: {},
85101
},
86102
changelog: {
87103
commitTypes: ['feat', 'fix', 'perf'],

lib/gitCommitConvention.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ module.exports = function (convention, commitAnchor = 'HEAD') {
115115
if (conventionalSubject.type &&
116116
convention.commitTypes && convention.commitTypes.length &&
117117
!convention.commitTypes.includes(conventionalSubject.type)) {
118-
console.warn(`[WARN] ${commit.hash ? `${commit.hash} - ` : ''}Unexpected commit type: '${conventionalSubject.type}'`);
118+
console.warn(`[WARN] ${commit.hash ? `${commit.hash.substring(0, 7)} - ` : ''}Unexpected commit type: '${conventionalSubject.type}'`);
119119
}
120120
if (conventionalSubject.scope &&
121121
convention.commitScopes && convention.commitScopes.length &&
122122
!convention.commitScopes.includes(conventionalSubject.scope)) {
123-
console.warn(`[WARN] ${commit.hash ? `${commit.hash} - ` : ''}Unexpected commit scope: '${conventionalSubject.scope}'`);
123+
console.warn(`[WARN] ${commit.hash ? `${commit.hash.substring(0, 7)} - ` : ''}Unexpected commit scope: '${conventionalSubject.scope}'`);
124124
}
125125

126126
return conventionalSubject;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "git-conventional-commits",
3-
"version": "2.7.2",
3+
"version": "2.8.0",
44
"description": "git conventional commits util",
55
"licence": "GPLv3",
66
"main": "cli.js",

0 commit comments

Comments
 (0)