Skip to content

Commit

Permalink
fix: matched extractPattern groups are no longer removed from the for…
Browse files Browse the repository at this point in the history
…matted commit message (#87)
  • Loading branch information
brajkowski authored Sep 29, 2022
1 parent 3ab5df9 commit a26c153
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
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

0 comments on commit a26c153

Please sign in to comment.