Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default async function defaultMain(args: Argv) {
});

if (args.clean) {
const dirty = await getCurrentGitStatus();
const dirty = await getCurrentGitStatus(cwd);
if (dirty) {
consola.error("Working directory is not clean.");
process.exit(1);
Expand All @@ -39,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.cwd);

// Parse commits as conventional commits
const commits = parseCommits(rawCommits, config)
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ export async function resolveChangelogConfig(
cwd: string
) {
if (!config.from) {
config.from = await getLastGitTag();
config.from = await getLastGitTag(cwd);
}

if (!config.to) {
config.to = await getCurrentGitRef();
config.to = await getCurrentGitRef(cwd);
}

if (config.output) {
Expand Down
33 changes: 20 additions & 13 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,48 @@ export interface GitCommit extends RawGitCommit {
isBreaking: boolean;
}

export async function getLastGitTag() {
export async function getLastGitTag(cwd?: string) {
try {
return execCommand("git describe --tags --abbrev=0")?.split("\n").at(-1);
return execCommand("git describe --tags --abbrev=0", cwd)
?.split("\n")
.at(-1);
} catch {
// Ignore
}
}

export function getCurrentGitBranch() {
return execCommand("git rev-parse --abbrev-ref HEAD");
export function getCurrentGitBranch(cwd?: string) {
return execCommand("git rev-parse --abbrev-ref HEAD", cwd);
}

export function getCurrentGitTag() {
return execCommand("git tag --points-at HEAD");
export function getCurrentGitTag(cwd?: string) {
return execCommand("git tag --points-at HEAD", cwd);
}

export function getCurrentGitRef() {
return getCurrentGitTag() || getCurrentGitBranch();
export function getCurrentGitRef(cwd?: string) {
return getCurrentGitTag(cwd) || getCurrentGitBranch(cwd);
}

export function getGitRemoteURL(cwd: string, remote = "origin") {
return execCommand(`git --work-tree="${cwd}" remote get-url "${remote}"`);
return execCommand(
`git --work-tree="${cwd}" remote get-url "${remote}"`,
cwd
);
}

export async function getCurrentGitStatus() {
return execCommand("git status --porcelain");
export async function getCurrentGitStatus(cwd?: string) {
return execCommand("git status --porcelain", cwd);
}

export async function getGitDiff(
from: string | undefined,
to = "HEAD"
to = "HEAD",
cwd?: 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`,
cwd
);
return r
.split("----\n")
Expand Down
2 changes: 1 addition & 1 deletion src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ export async function npmPublish(config: ChangelogConfig) {
args.push("--provenance");
}

return execCommand(`npm publish ${args.join(" ")}`);
return execCommand(`npm publish ${args.join(" ")}`, config.cwd);
}