Skip to content

Commit 7c8f470

Browse files
committed
🔧 update: enhance commit message validation for breaking changes
1 parent 8bb8382 commit 7c8f470

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

.husky/validate-commit-msg.mjs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,30 @@ const firstLine = raw.replace(/\r/g, "").split("\n")[0].trim();
2020
if (/^Merge /.test(firstLine) || /^Revert /.test(firstLine)) process.exit(0);
2121

2222
// Clean Commit convention pattern
23-
// Format: <emoji> <type>[(<scope>)]: <description>
23+
// Format: <emoji> <type>[!][(<scope>)]: <description>
2424
const pattern =
25-
/^(📦|🔧|🗑\uFE0F?|🔒|\uFE0F?||🧪|📖|🚀) (new|update|remove|security|setup|chore|test|docs|release)( \([a-z0-9][a-z0-9-]*\))?: .{1,72}$/u;
25+
/^(📦|🔧|🗑\uFE0F?|🔒|\uFE0F?||🧪|📖|🚀) (new|update|remove|security|setup|chore|test|docs|release)(!?)( \([a-z0-9][a-z0-9-]*\))?: .{1,72}$/u;
26+
27+
// Only new, update, remove, security may use the breaking change marker
28+
const breakingMatch = firstLine.match(pattern);
29+
if (breakingMatch) {
30+
const type = breakingMatch[2];
31+
const bang = breakingMatch[3];
32+
if (bang === '!' && !['new', 'update', 'remove', 'security'].includes(type)) {
33+
console.error('');
34+
console.error('✖ Breaking change marker (!) is only allowed for: new, update, remove, security');
35+
console.error('');
36+
process.exit(1);
37+
}
38+
}
2639

2740
if (!pattern.test(firstLine)) {
2841
console.error("");
2942
console.error("✖ Invalid commit message format.");
3043
console.error("");
31-
console.error(" Expected: <emoji> <type>[(<scope>)]: <description>");
44+
console.error(" Expected: <emoji> <type>[!][(<scope>)]: <description>");
45+
console.error("");
46+
console.error(" Use ! after type for breaking changes (new, update, remove, security only)");
3247
console.error("");
3348
console.error(" Types and emojis:");
3449
console.error(" 📦 new – new features, files, or capabilities");
@@ -45,6 +60,8 @@ if (!pattern.test(firstLine)) {
4560
console.error(" 📦 new: user authentication system");
4661
console.error(" 🔧 update (api): improve error handling");
4762
console.error(" ⚙️ setup (ci): configure github actions workflow");
63+
console.error(" 📦 new!: completely redesign authentication system");
64+
console.error(" 🔧 update! (api): change response format for all endpoints");
4865
console.error("");
4966
console.error(" Reference: https://github.com/wgtechlabs/clean-commit");
5067
console.error("");

0 commit comments

Comments
 (0)