Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optional paths commit filter #233

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ChangelogConfig {
tagMessage?: string;
tagBody?: string;
};
includePaths?: string[];
excludeAuthors: string[];
}

Expand Down Expand Up @@ -71,6 +72,7 @@ const getDefaultConfig = () =>
tagMessage: "v{{newVersion}}",
tagBody: "v{{newVersion}}",
},
includePaths: [],
excludeAuthors: [],
};

Expand Down
6 changes: 4 additions & 2 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ export async function getCurrentGitStatus() {

export async function getGitDiff(
from: string | undefined,
to = "HEAD"
to = "HEAD",
includePaths?: string[]
): Promise<RawGitCommit[]> {
// 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)
Expand Down
11 changes: 11 additions & 0 deletions test/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down