Skip to content

Commit

Permalink
chore: tidy the code
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Feb 20, 2023
1 parent 1254504 commit 805d93d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 94 deletions.
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ async function main() {
const rawCommits = await getGitDiff(config.from, config.to);

// Parse commits as conventional commits
const commits = parseCommits(rawCommits, config)
const filteredCommits = filterCommits(commits, config)
const commits = parseCommits(rawCommits, config);
const filteredCommits = filterCommits(commits, config);

// Bump version optionally
if (args.bump || args.release) {
Expand Down
58 changes: 34 additions & 24 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export interface GitCommit extends RawGitCommit {
}

export interface RevertPair {
shortRevertingHash: string
revertedHash: string
shortRevertingHash: string;
revertedHash: string;
}

export async function getLastGitTag() {
Expand Down Expand Up @@ -99,7 +99,7 @@ const ConventionalCommitRegex =
const CoAuthoredByRegex = /co-authored-by:\s*(?<name>.+)(<(?<email>.+)>)/gim;
const PullRequestRE = /\([ a-z]*(#\d+)\s*\)/gm;
const IssueRE = /(#\d+)/gm;
const RevertHashRE = /This reverts commit (?<hash>[a-f0-9]{40})./gm;
const RevertHashRE = /This reverts commit (?<hash>[\da-f]{40})./gm;

export function parseGitCommit(
commit: RawGitCommit,
Expand Down Expand Up @@ -134,10 +134,10 @@ export function parseGitCommit(
description = description.replace(PullRequestRE, "").trim();

// Extract the reverted hashes.
const revertedHashes = []
const matchedHashes = commit.body.matchAll(RevertHashRE)
const revertedHashes = [];
const matchedHashes = commit.body.matchAll(RevertHashRE);
for (const matchedHash of matchedHashes) {
revertedHashes.push(matchedHash.groups.hash)
revertedHashes.push(matchedHash.groups.hash);
}

Check warning on line 141 in src/git.ts

View check run for this annotation

Codecov / codecov/patch

src/git.ts#L140-L141

Added lines #L140 - L141 were not covered by tests

// Find all authors
Expand All @@ -161,46 +161,56 @@ export function parseGitCommit(
};
}

export function filterCommits (commits: GitCommit[], config: ChangelogConfig): GitCommit[] {
export function filterCommits(
commits: GitCommit[],
config: ChangelogConfig
): GitCommit[] {
const commitsWithNoDeps = commits.filter(
(c) =>
config.types[c.type] &&
!(c.type === "chore" && c.scope === "deps" && !c.isBreaking)
config.types[c.type] &&
!(c.type === "chore" && c.scope === "deps" && !c.isBreaking)
);

let resolvedCommits: GitCommit[] = []
let revertWatchList: RevertPair[] = []
let resolvedCommits: GitCommit[] = [];
let revertWatchList: RevertPair[] = [];
for (const commit of commitsWithNoDeps) {
// Include the reverted hashes in the watch list
if (commit.revertedHashes.length > 0) {
revertWatchList.push(...commit.revertedHashes.map(revertedHash => ({
revertedHash,
shortRevertingHash: commit.shortHash
} as RevertPair)))
revertWatchList.push(
...commit.revertedHashes.map(
(revertedHash) =>
({
revertedHash,
shortRevertingHash: commit.shortHash,
} as RevertPair)
)
);
}

// Find the commits which revert the current commit being evaluated
const shortRevertingHashes = revertWatchList.filter(
pair => pair.revertedHash.startsWith(commit.shortHash)
).map(pair => pair.shortRevertingHash)
const shortRevertingHashes = revertWatchList
.filter((pair) => pair.revertedHash.startsWith(commit.shortHash))
.map((pair) => pair.shortRevertingHash);

if (shortRevertingHashes.length > 0) {
// Remove commits that reverts this current commit
resolvedCommits = resolvedCommits.filter(
resolvedCommit => !shortRevertingHashes.includes(resolvedCommit.shortHash)
)
(resolvedCommit) =>
!shortRevertingHashes.includes(resolvedCommit.shortHash)
);

// Unwatch reverting hashes that has been resolved
revertWatchList = revertWatchList.filter(
watchedRevert => !shortRevertingHashes.includes(watchedRevert.shortRevertingHash)
)
(watchedRevert) =>
!shortRevertingHashes.includes(watchedRevert.shortRevertingHash)
);
} else {
// If the current commit is known not to have been reverted, put it to resolved commits.
resolvedCommits = [...resolvedCommits, commit]
resolvedCommits = [...resolvedCommits, commit];
}
}

return resolvedCommits
return resolvedCommits;
}

async function execCommand(cmd: string, args: string[]) {
Expand Down
132 changes: 64 additions & 68 deletions test/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from "vitest";
import { GitCommit } from '../src/git'
import type { ChangelogConfig } from '../src/config'
import { GitCommit } from "../src/git";
import type { ChangelogConfig } from "../src/config";
import {
generateMarkDown,
getGitDiff,
Expand Down Expand Up @@ -329,95 +329,91 @@ describe("git", () => {
`);
});

test("filterCommits should retain reverts from previous version", async () => {
test("filterCommits should retain reverts from previous version", () => {
const inputLog = [
{
type: 'example',
scope: '',
shortHash: 'a12345',
revertedHashes: ['b12345']
type: "example",
scope: "",
shortHash: "a12345",
revertedHashes: ["b12345"],
} as unknown as GitCommit,
{
type: 'example',
scope: '',
shortHash: 'c12345',
revertedHashes: ['d12345']
} as unknown as GitCommit
type: "example",
scope: "",
shortHash: "c12345",
revertedHashes: ["d12345"],
} as unknown as GitCommit,
];
const config: ChangelogConfig = {
types: {
example: { title: 'Example' }
example: { title: "Example" },
},
scopeMap: undefined,
github: '',
from: '',
to: '',
cwd: '',
output: ''
}
github: "",
from: "",
to: "",
cwd: "",
output: "",
};

const resolvedLog = filterCommits(inputLog, config)
expect(resolvedLog).toStrictEqual(
[
{
type: 'example',
scope: '',
shortHash: 'a12345',
revertedHashes: ['b12345']
} as unknown as GitCommit,
{
type: 'example',
scope: '',
shortHash: 'c12345',
revertedHashes: ['d12345']
} as unknown as GitCommit
]
);
const resolvedLog = filterCommits(inputLog, config);
expect(resolvedLog).toStrictEqual([
{
type: "example",
scope: "",
shortHash: "a12345",
revertedHashes: ["b12345"],
} as unknown as GitCommit,
{
type: "example",
scope: "",
shortHash: "c12345",
revertedHashes: ["d12345"],
} as unknown as GitCommit,
]);
});

test("filterCommits should remove reverts from upcoming version", async () => {
test("filterCommits should remove reverts from upcoming version", () => {
const inputLog = [
{
type: 'example',
scope: '',
shortHash: 'a12345',
revertedHashes: ['b12345']
type: "example",
scope: "",
shortHash: "a12345",
revertedHashes: ["b12345"],
} as unknown as GitCommit,
{
type: 'example',
scope: '',
shortHash: 'b12345',
revertedHashes: []
type: "example",
scope: "",
shortHash: "b12345",
revertedHashes: [],
} as unknown as GitCommit,
{
type: 'example',
scope: '',
shortHash: 'c12345',
revertedHashes: []
} as unknown as GitCommit
type: "example",
scope: "",
shortHash: "c12345",
revertedHashes: [],
} as unknown as GitCommit,
];
const config: ChangelogConfig = {
types: {
example: { title: 'Example' }
example: { title: "Example" },
},
scopeMap: undefined,
github: '',
from: '',
to: '',
cwd: '',
output: ''
}
github: "",
from: "",
to: "",
cwd: "",
output: "",
};

const resolvedLog = filterCommits(inputLog, config)
expect(resolvedLog).toStrictEqual(
[
{
type: 'example',
scope: '',
shortHash: 'c12345',
revertedHashes: []
} as unknown as GitCommit
]
);
const resolvedLog = filterCommits(inputLog, config);
expect(resolvedLog).toStrictEqual([
{
type: "example",
scope: "",
shortHash: "c12345",
revertedHashes: [],
} as unknown as GitCommit,
]);
});
});

0 comments on commit 805d93d

Please sign in to comment.