From b0980846f7abad1938fd8af8bc57cd99dd4b89d1 Mon Sep 17 00:00:00 2001
From: samydoesit <15785987+samydoesit@users.noreply.github.com>
Date: Wed, 13 Nov 2024 00:26:09 +0100
Subject: [PATCH] feat(git): add merge filter options
---
README.md | 2 ++
src/commands/default.ts | 10 +++++++++-
src/git.ts | 13 +++++++++++--
3 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index fb9ac5e..235f460 100644
--- a/README.md
+++ b/README.md
@@ -42,6 +42,8 @@ npx changelogen@latest [...args] [--dir
]
- `--output`: Changelog file name to create or update. Defaults to `CHANGELOG.md` and resolved relative to dir. Use `--no-output` to write to console only.
- `--bump`: Determine semver change and update version in `package.json`.
- `--release`. Bumps version in `package.json` and creates commit and git tags using local `git`. You can disable commit using `--no-commit` and tag using `--no-tag`. You can enable the automatic push of the new tag and release commit to your git repository by adding `--push`.
+- `--onlyMerges`: Use merge commits in changelog.
+- `--noMerges`: Exclude merge commits in changelog.
- `--publish`. Publishes package as a new version on `npm`. You will need to set authorisation tokens separately via `.npmrc` or environment variables.
- `--publishTag` Use custom npm tag for publishing (Default is `latest`)
- `--nameSuffix`: Adds suffix to package name (Example: `--nameSuffix canary` renames `foo` to `foo-canary`)
diff --git a/src/commands/default.ts b/src/commands/default.ts
index 3af769b..3c1bd5e 100644
--- a/src/commands/default.ts
+++ b/src/commands/default.ts
@@ -38,7 +38,15 @@ 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);
+ let onlyMerges: boolean;
+ if (args.onlyMerges) {
+ onlyMerges = true;
+ }
+ if (args.noMerges) {
+ onlyMerges = false;
+ }
+
+ const rawCommits = await getGitDiff(config.from, config.to, onlyMerges);
// Parse commits as conventional commits
const commits = parseCommits(rawCommits, config)
diff --git a/src/git.ts b/src/git.ts
index ddef33a..9fd416d 100644
--- a/src/git.ts
+++ b/src/git.ts
@@ -57,11 +57,20 @@ export async function getCurrentGitStatus() {
export async function getGitDiff(
from: string | undefined,
- to = "HEAD"
+ to = "HEAD",
+ onlyMerges?: boolean
): Promise {
+ let mergeFilter = "";
+ if (onlyMerges === true) {
+ mergeFilter = "--merges";
+ }
+ if (onlyMerges === false) {
+ mergeFilter = "--no-merges";
+ }
+
// 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 ${mergeFilter}`
);
return r
.split("----\n")