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
37 changes: 19 additions & 18 deletions src/commit-msg-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import escapeStringRegexp from "escape-string-regexp";
import { readFileSync, writeFileSync } from "fs";
import { exit } from "process";
import { getConfig } from "./config";
import { activeBranchName, GitError } from "./git-interop";
import { GitError, activeBranchName } from "./git-interop";

enum RegExpFlag {
IGNORE_CASE = "i",
}

/**
* Prevent messages that are already in the proper format
* Prevent commit subjects that are already in the proper format
* from being formatted again which would duplicate any matched
* branch details and non formatting token characters in the final commit message.
* branch details and non formatting token characters in the final commit subject.
*
* This allows amending a commit message to work as expected.
*
* @param currentCommitMsg
* @param currentCommitSubject
* @param commitMsgFormat
* @param branchMatches
* @returns The current commit message removed of any formatting that may have already been applied.
* @returns The current commit subject removed of any formatting that may have already been applied.
*/
function dedupeCurrentCommitMsg(
currentCommitMsg: string,
function dedupeCurrentCommitSubject(
currentCommitSubject: string,
commitMsgFormat: string,
branchMatches: RegExpMatchArray
): string {
Expand All @@ -37,18 +37,18 @@ function dedupeCurrentCommitMsg(
.replace("%m | lower", "")
.replace("%m", "")
);
return currentCommitMsg.replace(
return currentCommitSubject.replace(
new RegExp(escapeStringRegexp(appliedFormatting), RegExpFlag.IGNORE_CASE),
""
);
}

let branchName: string;
const commitMsgFilePath = process.argv[process.argv.length - 1];
const currentCommitMsg = readFileSync(commitMsgFilePath)
const commitMsgFileLines = readFileSync(commitMsgFilePath)
.toString()
.replace(/^#.*(\r\n|\n|\r)?/gm, "")
.trimEnd();
.split(/\r?\n/);
const currentCommitSubject = commitMsgFileLines[0];
const hookConfig = getConfig();

if (hookConfig === undefined) {
Expand All @@ -75,13 +75,13 @@ if (!branchMatches) {
exit(0);
}

const currentCommitMsgDeduped = dedupeCurrentCommitMsg(
currentCommitMsg,
const currentCommitSubjectDeduped = dedupeCurrentCommitSubject(
currentCommitSubject,
hookConfig.commitMsgFormat,
branchMatches
);

const newCommitMsg = branchMatches
const newCommitSubject = branchMatches
.reduce(
(msg, branchDetail, index) =>
msg
Expand All @@ -90,8 +90,9 @@ const newCommitMsg = branchMatches
.replace(`%b${index}`, branchDetail),
hookConfig.commitMsgFormat
)
.replace("%m | upper", currentCommitMsgDeduped.toUpperCase())
.replace("%m | lower", currentCommitMsgDeduped.toLowerCase())
.replace("%m", currentCommitMsgDeduped);
.replace("%m | upper", currentCommitSubjectDeduped.toUpperCase())
.replace("%m | lower", currentCommitSubjectDeduped.toLowerCase())
.replace("%m", currentCommitSubjectDeduped);
commitMsgFileLines[0] = newCommitSubject;

writeFileSync(commitMsgFilePath, newCommitMsg);
writeFileSync(commitMsgFilePath, commitMsgFileLines.join("\n"));
16 changes: 0 additions & 16 deletions test/e2e/commit-msg-hook.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { readFileSync } from "fs";
import path from "path";
import { Config } from "../../src/config";
import { createRcFile, deleteRcFile } from "../test-util/config";
import {
Expand All @@ -12,10 +10,6 @@ import {
import { installHook } from "./install";

describe("commit-msg-hook", () => {
const multilineCommitMsgWithComments = readFileSync(
path.join(__dirname, "fixtures", "multiline-commit-msg-with-comments.txt")
).toString();

beforeEach(() => {
createRepo();
installHook();
Expand Down Expand Up @@ -110,16 +104,6 @@ describe("commit-msg-hook", () => {
commitMsgFormat: "%b1 | upper %b2 %b3 | lower",
},
},
{
branch: "branch",
originalMessage: multilineCommitMsgWithComments,
expectedMessage: "my message (branch)",
config: {
extractPattern: ".*",
extractPatternMatchCase: false,
commitMsgFormat: "%m (%b0)",
},
},
{
branch: "branch",
originalMessage: "my message",
Expand Down
5 changes: 0 additions & 5 deletions test/e2e/fixtures/multiline-commit-msg-with-comments.txt

This file was deleted.