Skip to content

JS API

The package exports two classes — GitClient (a thin wrapper over the git CLI) and ConventionalGitClient (which adds conventional-commits helpers):

import { GitClient, ConventionalGitClient } from '@conventional-changelog/git-client'

See Introduction for a first example.

new GitClient(cwd: string)
MethodReturnsDescription
getRawCommits(params?)AsyncIterable<string>Raw commit messages. Accepts GitLogParams.
getTags(params?)AsyncIterable<string>All tags.
getLastTag(params?)Promise<string>The most recent tag.
getConfig(key)Promise<string>A git config value.
getCurrentBranch()Promise<string>The current branch name.
getDefaultBranch()Promise<string>The default branch name.
checkIgnore(file)Promise<boolean>Whether a file is ignored by .gitignore.
verify(rev, safe?)Promise<string>Verify that a revision exists.
MethodDescription
init()Initialize a repository.
add(files)Stage one file or an array of files.
commit(params)Commit staged changes. Accepts GitCommitParams.
tag(params)Create a tag on the current commit. Accepts GitTagParams.
push(branch, params?)Push to a remote. Accepts GitPushParams.
fetch(params?)Fetch from a remote.
setConfig(key, value)Set a git config value.
createBranch(branch) · deleteBranch(branch) · checkout(branch)Branch operations.

For anything not covered, exec(...args) runs an arbitrary git command and resolves to its output, and execStream(...args) returns it as a stream.

Extends GitClient — every method above is available — and adds conventional-commits helpers:

new ConventionalGitClient(cwd: string)
MethodReturnsDescription
getCommits(params?, parserOptions?)AsyncIterable<Commit>Parsed commits (reverts filtered out). Accepts GetCommitsParams and parser options.
getSemverTags(params?)AsyncIterable<string>Semver tags. Accepts GetSemverTagsParams.
getLastSemverTag(params?)Promise<string>The newest semver tag.
getVersionFromTags(params?)Promise<string | null>The current version (cleaned of its prefix), or null.

Used by getRawCommits (and, extended, by getCommits):

FieldTypeDescription
pathstring | string[]Only commits touching this path.
fromstringStart of the commit range (tag or SHA).
tostringEnd of the commit range.
formatstringCommit format string.
ignoreRegExpSkip commits matching this pattern.
sinceDate | stringOnly commits after this date.
reversebooleanReturn commits in reverse order.
mergesbooleanInclude merge commits.
firstParentbooleanFollow only the first parent of merges.

Everything in GitLogParams, plus:

FieldTypeDefaultDescription
filterRevertsbooleanFilter out reverted commits.

The range fields path, from, to, and since from GitLogParams, plus:

FieldTypeDescription
prefixstring | RegExpOnly tags with this prefix (e.g. packagePrefix('my-pkg')).
skipUnstablebooleanSkip unstable versions, e.g. x.x.x-rc.1.
cleanbooleanStrip the prefix and return a clean version.
TypeFields
GitCommitParamsmessage (required), files, verify, sign, allowEmpty.
GitTagParamsname (required), message, sign.
GitPushParamsverify, tags, followTags, force.
GitFetchParamsremote, branch, prune, unshallow, tags, all.