Core git operations for retrieving commit history, parsing conventional commits, and extracting repository information. These functions provide the foundation for changelog generation by interfacing directly with git repositories.
Retrieves raw git commits between two references using git log format.
/**
* Get git commits between two references
* @param from - Starting commit reference (tag, branch, hash). If undefined, gets all commits
* @param to - Ending commit reference. Defaults to "HEAD"
* @param cwd - Working directory path. Defaults to current directory
* @returns Array of raw git commits with basic information
*/
function getGitDiff(
from: string | undefined,
to?: string = "HEAD",
cwd?: string
): Promise<RawGitCommit[]>;Usage Examples:
import { getGitDiff } from "changelogen";
// Get commits since last tag
const commits = await getGitDiff("v1.0.0", "HEAD");
// Get all commits
const allCommits = await getGitDiff(undefined);
// Get commits in specific directory
const repoCommits = await getGitDiff("v1.0.0", "HEAD", "/path/to/repo");Parses raw git commits into conventional commit format with semantic information.
/**
* Parse raw commits into conventional commit format
* @param commits - Array of raw git commits
* @param config - Changelog configuration for parsing rules
* @returns Array of parsed conventional commits (filters out non-conventional commits)
*/
function parseCommits(
commits: RawGitCommit[],
config: ChangelogConfig
): GitCommit[];Usage Examples:
import { parseCommits, loadChangelogConfig, getGitDiff } from "changelogen";
const config = await loadChangelogConfig(process.cwd());
const rawCommits = await getGitDiff("v1.0.0", "HEAD");
// Parse commits with conventional commit format
const conventionalCommits = parseCommits(rawCommits, config);
// Filter by specific types
const featCommits = conventionalCommits.filter(c => c.type === 'feat');Parses a single raw git commit into conventional commit format.
/**
* Parse a single raw git commit into conventional commit format
* @param commit - Raw git commit
* @param config - Changelog configuration for parsing rules
* @returns Parsed conventional commit or null if not conventional
*/
function parseGitCommit(
commit: RawGitCommit,
config: ChangelogConfig
): GitCommit | null;Various functions for getting git repository state information.
/**
* Get the last git tag in the repository
* @param cwd - Working directory path
* @returns Last git tag or undefined if no tags exist
*/
function getLastGitTag(cwd?: string): Promise<string | undefined>;
/**
* Get current git branch name
* @param cwd - Working directory path
* @returns Current branch name
*/
function getCurrentGitBranch(cwd?: string): string;
/**
* Get current git tag (if HEAD points to a tag)
* @param cwd - Working directory path
* @returns Current tag name or empty string
*/
function getCurrentGitTag(cwd?: string): string;
/**
* Get current git reference (tag or branch)
* @param cwd - Working directory path
* @returns Current tag name or current branch name
*/
function getCurrentGitRef(cwd?: string): string;Usage Examples:
import {
getLastGitTag,
getCurrentGitBranch,
getCurrentGitRef
} from "changelogen";
// Get latest version tag
const lastTag = await getLastGitTag();
console.log(`Last release: ${lastTag}`);
// Get current branch
const branch = getCurrentGitBranch();
console.log(`Current branch: ${branch}`);
// Get current reference (tag or branch)
const ref = getCurrentGitRef();
console.log(`Current ref: ${ref}`);Functions for retrieving git repository metadata.
/**
* Get git remote URL for a repository
* @param cwd - Repository path
* @param remote - Remote name (defaults to "origin")
* @returns Remote URL string
*/
function getGitRemoteURL(cwd: string, remote?: string): string;
/**
* Get current git status in porcelain format
* @param cwd - Working directory path
* @returns Git status porcelain output
*/
function getCurrentGitStatus(cwd?: string): Promise<string>;Usage Examples:
import { getGitRemoteURL, getCurrentGitStatus } from "changelogen";
// Get origin URL
const originUrl = getGitRemoteURL(process.cwd());
console.log(`Origin: ${originUrl}`);
// Check if working directory is clean
const status = await getCurrentGitStatus();
const isClean = status === "";
console.log(`Working directory clean: ${isClean}`);interface GitCommitAuthor {
name: string;
email: string;
}
interface RawGitCommit {
message: string;
body: string;
shortHash: string;
author: GitCommitAuthor;
}
interface GitCommit extends RawGitCommit {
description: string;
type: string;
scope: string;
references: Reference[];
authors: GitCommitAuthor[];
isBreaking: boolean;
}
interface Reference {
type: "hash" | "issue" | "pull-request";
value: string;
}Changelogen follows the Conventional Commits specification:
type(scope)?: description! after type/scope or BREAKING CHANGE: in body#123) and PR (#456) referencesCo-authored-by: lines in commit body:emoji: text formatSupported commit types (configurable):
feat: New features (minor semver)fix: Bug fixes (patch semver)docs: Documentation changesstyle: Code style changesrefactor: Code refactoringtest: Test additions/changeschore: Maintenance tasks