@hyperfrontend/versioning/git/models

models

Plain-data git models for commits, tags, and refs, with factories and small predicates.

GitCommit, GitRef, and GitTag are the structured shapes consumed by every other git module. Factories (createGitCommit, createGitRef, createLightweightTag, createAnnotatedTag) produce instances from raw fields. Predicates (isSameCommit, isMergeCommit, isRootCommit, isBranchRef, isTagRef, isRemoteRef, isHeadRef) and accessors (getShortHash, extractScope, extractType) cover the common questions consumers ask of these models without having to re-derive the answers from the raw git output.

API Reference§

ƒ Functions

§function

buildRefName(type: GitRefType, name: string, remote?: string): string

Builds a full reference name from type and name.
Parameters
NameTypeDescription
§type
GitRefTypeReference type
§name
stringReference name
§remote?
stringRemote name (for remote type)
Returns
string
Full reference name
Example
Build full reference names from type and name
buildRefName('branch', 'main') // 'refs/heads/main'
buildRefName('tag', 'v1.0.0') // 'refs/tags/v1.0.0'
buildRefName('remote', 'main', 'origin') // 'refs/remotes/origin/main'
§function

buildTagName(packageName: string, version: string, format: string): string

Builds a tag name from package name and version.
Parameters
NameTypeDescription
§packageName
stringPackage name (e.g., '@scope/package' or 'package')
§version
stringVersion string (e.g., '1.2.3')
§format
stringTag format template, uses ${package} and ${version} placeholders(default: '${package}@${version}')
Returns
string
Formatted tag name
Example
Build tag names with different formats
buildTagName('@scope/pkg', '1.2.3') // '@scope/pkg@1.2.3'
buildTagName('utils', '1.0.0', 'v${version}') // 'v1.0.0'
buildTagName('pkg', '2.0.0', '${package}-v${version}') // 'pkg-v2.0.0'
§function

compareRefsByName(a: GitRef, b: GitRef): number

Compares two references by name (alphabetically).
Parameters
NameTypeDescription
GitRefFirst reference
GitRefSecond reference
Returns
number
Comparison result (-1, 0, or 1)
Example

Sort references alphabetically by name

const refs = [refB, refA, refC]
refs.sort(compareRefsByName) // => [refA, refB, refC]
§function

compareTagsByVersion(a: GitTag, b: GitTag): number

Compares two tags by version (newest first). Useful for sorting tags.
Parameters
NameTypeDescription
GitTagFirst tag
GitTagSecond tag
Returns
number
Comparison result (-1, 0, or 1)
Example

Sort tags by version (newest first)

const tags = [tagV1, tagV2, tagV3]
tags.sort(compareTagsByVersion) // => [tagV3, tagV2, tagV1]
§function

createAnnotatedTag(options: CreateAnnotatedTagOptions): GitTag

Creates an annotated git tag.
Parameters
NameTypeDescription
§options
CreateAnnotatedTagOptionsTag creation options
Returns
GitTag
A new GitTag object
Example
Create an annotated git tag with metadata
const tag = createAnnotatedTag({
  name: 'v1.0.0',
  commitHash: 'abc123...',
  message: 'Release v1.0.0',
  taggerName: 'John Doe',
  taggerEmail: 'john@example.com',
  tagDate: '2026-03-12T10:00:00Z',
})
§function

createGitCommitModel(options: CreateGitCommitOptions): GitCommit

Creates a git commit model.
Parameters
NameTypeDescription
§options
CreateGitCommitOptionsCommit creation options
Returns
GitCommit
A new GitCommit object
Example
Create a git commit model
const commit = createGitCommit({
  hash: 'abc123...',
  authorName: 'John Doe',
  authorEmail: 'john@example.com',
  authorDate: '2026-03-12T10:00:00Z',
  subject: 'feat: add new feature',
})
§function

createGitRef(options: CreateGitRefOptions): GitRef

Creates a git reference from full name. Parses the reference type from the full name.
Parameters
NameTypeDescription
§options
CreateGitRefOptionsReference creation options
Returns
GitRef
A new GitRef object
Example
Create a git reference from full name
const ref = createGitRef({
  fullName: 'refs/heads/main',
  commitHash: 'abc123...',
})
§function

createLightweightTag(options: CreateLightweightTagOptions): GitTag

Creates a lightweight git tag.
Parameters
NameTypeDescription
§options
CreateLightweightTagOptionsTag creation options
Returns
GitTag
A new GitTag object
Example
Create a lightweight git tag
const tag = createLightweightTag({
  name: 'v1.0.0',
  commitHash: 'abc123...',
})
§function

extractPackageFromTag(tagName: string): string

Extracts package name from tag name. Handles formats like: @scope/package@1.2.3, package@1.2.3, package-v1.2.3 Uses character-by-character parsing (no regex).
Parameters
NameTypeDescription
§tagName
stringTag name to parse
Returns
string
The extracted package name or undefined if not found
Example
Extract package name from tag
extractPackageFromTag('@scope/pkg@1.2.3') // '@scope/pkg'
extractPackageFromTag('lib-utils@1.2.3') // 'lib-utils'
extractPackageFromTag('v1.2.3') // undefined
§function

extractScope(subject: string): string

Extracts the scope from a commit subject if it follows conventional commit format. Uses character-by-character parsing (no regex).
Parameters
NameTypeDescription
§subject
stringCommit subject line
Returns
string
Scope string or undefined if no scope found
Example
Extract scope from conventional commit
extractScope('feat(lib-versioning): add git support') // 'lib-versioning'
extractScope('fix: resolve issue') // undefined
§function

extractType(subject: string): string

Extracts the type from a commit subject if it follows conventional commit format. Uses character-by-character parsing (no regex).
Parameters
NameTypeDescription
§subject
stringCommit subject line
Returns
string
Type string or undefined if no valid type found
Example
Extract type from conventional commit
extractType('feat(lib-versioning): add git support') // 'feat'
extractType('fix: resolve issue') // 'fix'
extractType('random message') // undefined
§function

extractVersionFromTag(tagName: string): string

Extracts version from tag name. Handles common formats: v1.2.3, @scope/package@1.2.3, package@1.2.3 Uses character-by-character parsing (no regex).
Parameters
NameTypeDescription
§tagName
stringTag name to parse
Returns
string
The extracted version string or undefined if no version found
Example
Extract version from various tag formats
extractVersionFromTag('v1.2.3') // '1.2.3'
extractVersionFromTag('@scope/pkg@1.2.3') // '1.2.3'
extractVersionFromTag('release-1.2.3') // '1.2.3'
§function

filterRefsByRemote(refs: unknown, remote: string): unknown

Filters references by remote.
Parameters
NameTypeDescription
§refs
unknownReferences to filter
§remote
stringRemote name to filter by
Returns
unknown
Filtered references
Example

Filter references by remote

const originRefs = filterRefsByRemote(remoteRefs, 'origin')
const upstreamRefs = filterRefsByRemote(remoteRefs, 'upstream')
§function

filterRefsByType(refs: unknown, type: GitRefType): unknown

Filters references by type.
Parameters
NameTypeDescription
§refs
unknownReferences to filter
§type
GitRefTypeType to filter by
Returns
unknown
Filtered references
Example

Filter references by type

const branches = filterRefsByType(allRefs, 'branch')
const tags = filterRefsByType(allRefs, 'tag')
§function

getRemote(ref: GitRef): string

Gets the tracking remote for a reference.
Parameters
NameTypeDescription
§ref
GitRefReference to check
Returns
string
Remote name or undefined
Example

Get the remote for a reference

getRemote({ type: 'remote', name: 'main', remote: 'origin' }) // => 'origin'
getRemote({ type: 'branch', name: 'main' }) // => undefined
§function

getShortHash(hash: string): string

Gets a short hash (7 characters) from a full commit hash.
Parameters
NameTypeDescription
§hash
stringFull commit hash
Returns
string
Short hash (7 characters)
Example

Get short hash from full commit hash

getShortHash('abc123def456789') // => 'abc123d'
§function

isAnnotatedTag(tag: GitTag): boolean

Checks if a tag is annotated.
Parameters
NameTypeDescription
§tag
GitTagTag to check
Returns
boolean
True if tag is annotated
Example

Check if a tag is annotated

const tag = createAnnotatedTag({ name: 'v1.0.0', commitHash: 'abc123', message: 'Release' })
isAnnotatedTag(tag) // => true
§function

isBranchRef(ref: GitRef): boolean

Checks if a reference is a branch.
Parameters
NameTypeDescription
§ref
GitRefReference to check
Returns
boolean
True if reference is a branch
Example

Check if reference is a branch

isBranchRef({ type: 'branch', name: 'main' }) // => true
§function

isHeadRef(ref: GitRef): boolean

Checks if a reference points to the current HEAD.
Parameters
NameTypeDescription
§ref
GitRefReference to check
Returns
boolean
True if reference is HEAD
Example

Check if reference points to HEAD

isHeadRef({ type: 'head', name: 'HEAD' }) // => true
isHeadRef({ type: 'branch', name: 'main', isHead: true }) // => true
§function

isLightweightTag(tag: GitTag): boolean

Checks if a tag is lightweight.
Parameters
NameTypeDescription
§tag
GitTagTag to check
Returns
boolean
True if tag is lightweight
Example

Check if a tag is lightweight

const tag = createLightweightTag({ name: 'v1.0.0', commitHash: 'abc123' })
isLightweightTag(tag) // => true
§function

isMergeCommit(commit: GitCommit): boolean

Checks if a commit is a merge commit (has multiple parents).
Parameters
NameTypeDescription
§commit
GitCommitCommit to check
Returns
boolean
True if commit has more than one parent
Example

Check if commit is a merge commit

if (isMergeCommit(commit)) {
  console.log('Commit has parents:', commit.parents)
}
§function

isRemoteRef(ref: GitRef): boolean

Checks if a reference is a remote tracking branch.
Parameters
NameTypeDescription
§ref
GitRefReference to check
Returns
boolean
True if reference is a remote
Example

Check if reference is a remote tracking branch

isRemoteRef({ type: 'remote', name: 'main', remote: 'origin' }) // => true
§function

isRootCommit(commit: GitCommit): boolean

Checks if a commit is a root commit (has no parents).
Parameters
NameTypeDescription
§commit
GitCommitCommit to check
Returns
boolean
True if commit has no parents
Example

Check if commit is the initial commit

if (isRootCommit(commit)) {
  console.log('This is the initial commit')
}
§function

isSameCommit(a: GitCommit, b: GitCommit): boolean

Checks if two commits are the same based on their hash.
Parameters
NameTypeDescription
GitCommitFirst commit
GitCommitSecond commit
Returns
boolean
True if commits have the same hash
Example

Compare two commits by hash

isSameCommit(commitA, commitB) // => true if commitA.hash === commitB.hash
§function

isTagRef(ref: GitRef): boolean

Checks if a reference is a tag.
Parameters
NameTypeDescription
§ref
GitRefReference to check
Returns
boolean
True if reference is a tag
Example

Check if reference is a tag

isTagRef({ type: 'tag', name: 'v1.0.0' }) // => true

◈ Interfaces

§interface

CreateAnnotatedTagOptions

Options for creating an annotated tag.
Properties
§readonly name:stringTag name
§readonly commitHash:stringTarget commit hash
§readonly message:stringTag message
§readonly taggerName:stringTagger name
§readonly taggerEmail:stringTagger email
§readonly tagDate:stringTag date (ISO 8601 format)
§interface

CreateGitCommitOptions

Options for creating a git commit model.
Properties
§readonly hash:stringFull commit hash
§readonly authorName:stringCommit author name
§readonly authorEmail:stringCommit author email
§readonly authorDate:stringAuthor date (ISO 8601 format)
§readonly subject:stringCommit subject (first line of message)
§readonly committerName?:stringCommitter name (defaults to author)
§readonly committerEmail?:stringCommitter email (defaults to author)
§readonly commitDate?:stringCommit date (defaults to author date)
§readonly body?:stringFull commit body (defaults to empty string)
§readonly parents?:unknownParent commit hashes (defaults to empty array)
§readonly refs?:unknownAssociated refs (defaults to empty array)
§interface

CreateGitRefOptions

Options for creating a git reference.
Properties
§readonly fullName:stringFull reference name
§readonly commitHash:stringTarget commit hash
§readonly isHead?:booleanWhether this is the current HEAD
§interface

CreateLightweightTagOptions

Options for creating a lightweight tag.
Properties
§readonly name:stringTag name
§readonly commitHash:stringTarget commit hash
§interface

GitCommit

A git commit with its metadata.
Properties
§readonly hash:stringFull commit hash (40 characters)
§readonly shortHash:stringAbbreviated commit hash (typically 7 characters)
§readonly authorName:stringCommit author name
§readonly authorEmail:stringCommit author email
§readonly authorDate:stringAuthor date (ISO 8601 format)
§readonly committerName:stringCommitter name
§readonly committerEmail:stringCommitter email
§readonly commitDate:stringCommit date (ISO 8601 format)
§readonly subject:stringCommit subject (first line of message)
§readonly body:stringFull commit body (excludes subject)
§readonly message:stringFull commit message (subject + body)
§readonly parents:unknownParent commit hashes
§readonly refs:unknownAssociated refs (branches, tags, etc.)
§interface

GitRef

A git reference.
Properties
§readonly fullName:stringFull reference name (e.g., refs/heads/main)
§readonly name:stringShort reference name (e.g., main)
§readonly type:GitRefTypeReference type
§readonly commitHash:stringTarget commit hash
§readonly remote?:stringRemote name for remote refs
§readonly isHead?:booleanWhether this is the current HEAD
§interface

GitTag

A git tag with its metadata.
Properties
§readonly name:stringTag name
§readonly commitHash:stringTarget commit hash
§readonly type:GitTagTypeTag type
§readonly message?:stringTag message (for annotated tags)
§readonly taggerName?:stringTagger name (for annotated tags)
§readonly taggerEmail?:stringTagger email (for annotated tags)
§readonly tagDate?:stringTag date (ISO 8601 format, for annotated tags)

◆ Types

§type

GitRefType

Type of git reference.
type GitRefType = "branch" | "tag" | "remote" | "head" | "stash"
§type

GitTagType

Represents either a lightweight or annotated git tag.
type GitTagType = "lightweight" | "annotated"