From 2f16e880659d044a8e538f4cbe666435e8f545b6 Mon Sep 17 00:00:00 2001 From: Stephen Hebert Date: Wed, 2 Oct 2024 16:36:46 -0500 Subject: [PATCH] feat: add optional paths commit filter --- src/commands/default.ts | 3 ++- src/config.ts | 2 ++ src/git.ts | 6 ++++-- test/git.test.ts | 11 +++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/commands/default.ts b/src/commands/default.ts index 3af769b..1671c16 100644 --- a/src/commands/default.ts +++ b/src/commands/default.ts @@ -24,6 +24,7 @@ export default async function defaultMain(args: Argv) { from: args.from, to: args.to, output: args.output, + includePaths: args.paths?.split(' '), newVersion: typeof args.r === "string" ? args.r : undefined, }); @@ -38,7 +39,7 @@ export default async function defaultMain(args: Argv) { const logger = consola.create({ stdout: process.stderr }); logger.info(`Generating changelog for ${config.from || ""}...${config.to}`); - const rawCommits = await getGitDiff(config.from, config.to); + const rawCommits = await getGitDiff(config.from, config.to, config.includePaths); // Parse commits as conventional commits const commits = parseCommits(rawCommits, config) diff --git a/src/config.ts b/src/config.ts index 7b6359f..ee1be90 100644 --- a/src/config.ts +++ b/src/config.ts @@ -26,6 +26,7 @@ export interface ChangelogConfig { tagMessage?: string; tagBody?: string; }; + includePaths?: string[]; excludeAuthors: string[]; } @@ -71,6 +72,7 @@ const getDefaultConfig = () => tagMessage: "v{{newVersion}}", tagBody: "v{{newVersion}}", }, + includePaths: [], excludeAuthors: [], }; diff --git a/src/git.ts b/src/git.ts index ddef33a..1fcb32c 100644 --- a/src/git.ts +++ b/src/git.ts @@ -57,12 +57,14 @@ export async function getCurrentGitStatus() { export async function getGitDiff( from: string | undefined, - to = "HEAD" + to = "HEAD", + includePaths?: string[] ): Promise { // https://git-scm.com/docs/pretty-formats const r = execCommand( - `git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status` + `git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status${includePaths ? ` -- ${includePaths.join(" ")}` : ""}` ); + console.log(`git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status${includePaths ? ` -- ${includePaths.join(" ")}` : ""}`) return r .split("----\n") .splice(1) diff --git a/test/git.test.ts b/test/git.test.ts index ac1f1f8..9cfc8d2 100644 --- a/test/git.test.ts +++ b/test/git.test.ts @@ -21,6 +21,17 @@ describe("git", () => { ); }); + test("getGitDiff should accept paths parameter", async () => { + const COMMIT_INITIAL = "4554fc49265ac532b14c89cec15e7d21bb55d48b"; + const COMMIT_VER002 = "38d7ba15dccc3a44931bf8bf0abaa0d4d96603eb"; + expect((await getGitDiff(COMMIT_INITIAL, COMMIT_VER002, ['test/index.test.ts'])).length).toBe(1); + + const all = await getGitDiff(undefined); + expect((await getGitDiff(COMMIT_INITIAL, "HEAD")).length + 1).toBe( + all.length + ); + }); + test("parse commit with emoji", async () => { const rawCommitEmojiList = [ {