Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 14 additions & 29 deletions src/commit-msg-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,21 @@ function dedupeCurrentCommitMsg(
commitMsgFormat: string,
branchMatches: RegExpMatchArray
): string {
const nonTokenChars = commitMsgFormat
// Find all formatting tokens.
.match(/(%m|%b[0-9]+)(\s\|\s(lower|upper))?/g)

// Remove all formatting tokens -- what remains are the non formatting token characters.
?.reduce(
(msgFormat, formatToken) => msgFormat.replace(formatToken, ""),
commitMsgFormat
);

let currentCommitMsgDeduped = branchMatches.reduce(
// Remove any matched branch details.
(msg, branchDetail) =>
msg.replace(new RegExp(branchDetail, RegExpFlag.IGNORE_CASE), ""),
currentCommitMsg
const appliedFormatting = branchMatches.reduce(
(msg, branchDetail, index) =>
msg
.replace(`%b${index} | upper`, branchDetail.toUpperCase())
.replace(`%b${index} | lower`, branchDetail.toLowerCase())
.replace(`%b${index}`, branchDetail),
commitMsgFormat
.replace("%m | upper", "")
.replace("%m | lower", "")
.replace("%m", "")
);
return currentCommitMsg.replace(
new RegExp(escapeStringRegexp(appliedFormatting), RegExpFlag.IGNORE_CASE),
""
);

if (currentCommitMsgDeduped === currentCommitMsg) {
// The current commit message is not formatted if the message is unchanged after the previous step.
return currentCommitMsg;
}

if (nonTokenChars) {
// Remove any non formatting token characters.
currentCommitMsgDeduped = currentCommitMsgDeduped.replace(
new RegExp(escapeStringRegexp(nonTokenChars), RegExpFlag.IGNORE_CASE),
""
);
}
return currentCommitMsgDeduped;
}

let branchName: string;
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/commit-msg-hook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ describe("commit-msg-hook", () => {
commitMsgFormat: "%b0 match %m",
},
},
{
branch: "sc-123456",
originalMessage: "scaled up 2022-09-28",
expectedMessage: "sc-123456 scaled up 2022-09-28",
config: {
extractPattern: "(sc)(-)?([0-9]+)",
extractPatternMatchCase: false,
commitMsgFormat: "%b1 | lower-%b3 %m",
},
},
];
test.each(configCases)(
"should update the commit message per the rc file",
Expand Down