From 0cac22a7e0e3cc41eae8196869db45e48135835e Mon Sep 17 00:00:00 2001 From: Olivers Vitins Date: Sat, 4 May 2024 23:01:10 +0300 Subject: [PATCH] perf(changelog): improve generation time by ~2800 times (#23) --- src/commands/changelog.ts | 67 +++++++++++---------------------------- 1 file changed, 19 insertions(+), 48 deletions(-) diff --git a/src/commands/changelog.ts b/src/commands/changelog.ts index c9c8669..e769436 100644 --- a/src/commands/changelog.ts +++ b/src/commands/changelog.ts @@ -51,63 +51,34 @@ const command = new Command("changelog") for (const commit of commits) { const tag = - sort((await simpleGit().tags([`--contains=${commit.hash}`])).all)[0]! || - unreleased; + (commit.refs.match(/tag: (\S+)(?:,|$)/)?.[1] ?? lastTag) || unreleased; - const currentCommitStack = parsedCommitStacks.find( + let currentCommitStackIndex = parsedCommitStacks.findIndex( (commitStack) => commitStack.version === tag, - ) || { - version: tag || unreleased, - breaking: [], - features: [], - fixes: [], - miscellaneous: [], - }; + ); - if (lastTag !== tag) { - parsedCommitStacks = [currentCommitStack, ...parsedCommitStacks]; + if (currentCommitStackIndex === -1) { + parsedCommitStacks.push({ + version: tag || unreleased, + breaking: [], + features: [], + fixes: [], + miscellaneous: [], + }); + + currentCommitStackIndex = parsedCommitStacks.findIndex( + (commitStack) => commitStack.version === tag, + ); } if (commit.message.includes("!:")) { - parsedCommitStacks = [ - { - ...currentCommitStack, - breaking: [...currentCommitStack.breaking, commit], - }, - ...parsedCommitStacks.filter( - (commitStack) => commitStack.version !== tag, - ), - ]; + parsedCommitStacks[currentCommitStackIndex].breaking.push(commit); } else if (commit.message.startsWith("feat")) { - parsedCommitStacks = [ - { - ...currentCommitStack, - features: [...currentCommitStack.features, commit], - }, - ...parsedCommitStacks.filter( - (commitStack) => commitStack.version !== tag, - ), - ]; + parsedCommitStacks[currentCommitStackIndex].features.push(commit); } else if (commit.message.startsWith("fix")) { - parsedCommitStacks = [ - { - ...currentCommitStack, - fixes: [...currentCommitStack.fixes, commit], - }, - ...parsedCommitStacks.filter( - (commitStack) => commitStack.version !== tag, - ), - ]; + parsedCommitStacks[currentCommitStackIndex].fixes.push(commit); } else { - parsedCommitStacks = [ - { - ...currentCommitStack, - miscellaneous: [...currentCommitStack.miscellaneous, commit], - }, - ...parsedCommitStacks.filter( - (commitStack) => commitStack.version !== tag, - ), - ]; + parsedCommitStacks[currentCommitStackIndex].miscellaneous.push(commit); } lastTag = tag;