commit

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package commit provides types and utilities for handling git commit messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body []byte

Body represents the body of a commit message.

It is a byte slice that may contain multiple lines of text.

func NewBody

func NewBody(bb []byte) Body

NewBody creates a new Body instance from the given byte slice.

  • bb: byte slice representing the commit body.

func (Body) Empty

func (b Body) Empty() bool

Empty checks if the Body is empty or contains only whitespace.

func (Body) String

func (b Body) String() string

String returns the string representation of the Body.

type Commit

type Commit struct {
	// contains filtered or unexported fields
}

Commit represents a git commit in the version control system.

func NewCommit

func NewCommit(
	hash Hash,
	msg Message,
	opts ...Option,
) Commit

NewCommit creates a new Commit instance.

  • hash: the commit hash.
  • msg: the commit message.
  • opts: functional options to configure the instance.

func (Commit) Hash

func (c Commit) Hash() Hash

Hash returns the commit hash.

func (Commit) Kind added in v0.1.4

func (c Commit) Kind() Kind

Kind returns the kind of the commit.

If no kind is provided in the constructor, it returns the default KindDefault.

func (Commit) Message

func (c Commit) Message() Message

Message returns the commit message associated with the commit.

func (Commit) Scope

func (c Commit) Scope() Scope

Scope returns the scope of the commit as defined in the subject of the commit message.

If no scope is defined, it returns an empty Scope.

Example:

"feat(ui): add new button" -> "ui"
"fix: resolve issue"       -> ""

func (Commit) String

func (c Commit) String() string

String returns the short string representation of the commit hash.

func (Commit) Type

func (c Commit) Type() Type

Type returns the type of the commit as defined in the subject of the commit message.

If no type is defined, it returns an empty Type.

Example:

"feat(ui): add new button" -> "feat"
"fix: resolve issue"       -> "fix"

type Description

type Description string

Description represents the description part of a commit subject.

func NewDescription

func NewDescription(value string) Description

NewDescription creates a new Description instance.

func ParseDescription

func ParseDescription(s string) Description

ParseDescription parses the description from a commit subject string.

The expected format has the description following a colon.

For example:

"feat(ui): add new button" -> description is "add new button"

func (Description) Empty

func (d Description) Empty() bool

Empty checks if the description is empty.

func (Description) String

func (d Description) String() string

String returns the string representation.

type Hash

type Hash string

Hash represents a commit hash in the version control system.

It uniquely identifies a commit, regardless of the underlying hash algorithm.

func NewHash

func NewHash(value string) Hash

NewHash creates a new Hash instance.

func (Hash) Empty

func (h Hash) Empty() bool

Empty checks if the commit hash is empty.

func (Hash) ShortString

func (h Hash) ShortString() string

ShortString returns a shortened version of the commit hash.

func (Hash) String

func (h Hash) String() string

String returns the string representation of the commit hash.

type Kind added in v0.1.4

type Kind uint8

Kind represents the kind of a commit.

It is used to split commits by their kind in the linting process.

const (
	// KindDefault represents default commit.
	KindDefault Kind = 0
	// KindMerge represents merge commit.
	KindMerge Kind = 1
	// KindRevert represents revert commit.
	KindRevert Kind = 2
)

func (Kind) Default added in v0.1.4

func (k Kind) Default() bool

Default defines whether the Kind is Default.

func (Kind) Merge added in v0.1.4

func (k Kind) Merge() bool

Merge defines whether the Kind is Merge.

func (Kind) Revert added in v0.1.4

func (k Kind) Revert() bool

Revert defines whether the Kind is Revert.

type Message

type Message struct {
	// contains filtered or unexported fields
}

Message represents a git commit message.

A Message is composed of a Subject and a Body, and optionally stores the raw bytes it was parsed from. Use NewMessage to build one from its parts, or ParseMessage to build one from a raw string or byte slice.

func NewMessage

func NewMessage(
	subject Subject,
	body Body,
	opts ...MessageOption,
) Message

NewMessage creates a new Message instance.

  • subject: the subject of the commit message.
  • body: the body of the commit message.
  • opts: optional configuration applied to the Message, such as WithRaw.

func ParseMessage

func ParseMessage[T []byte | string](v T) Message

ParseMessage parses a commit message from a byte slice or string.

It splits the input into subject and body at the first newline character. If there is a second newline immediately following the first (i.e., "subject\n\nbody"), that blank line is skipped so the body starts after it. If there is no newline, the entire input is treated as the subject, and the body is empty.

func (Message) Body

func (m Message) Body() Body

Body returns the body of the commit message.

func (Message) String

func (m Message) String() string

String returns the string representation of the Message.

When the Message stores raw bytes (for example one produced by ParseMessage, or by NewMessage with the WithRaw option), the raw bytes are returned as-is, preserving the original formatting of the commit message.

Otherwise the representation is rebuilt from the subject and body: if the body is empty, only the subject is returned; otherwise the subject and body are joined with two newlines in between.

func (Message) Subject

func (m Message) Subject() Subject

Subject returns the subject of the commit message.

func (Message) Ticket added in v0.1.1

func (m Message) Ticket(re *regexp.Regexp) ticket.Ticket

Ticket extracts a Ticket from the subject of the Message using the provided regular expression.

The regular expression should contain a capturing group that matches the ticket name. Only the subject is considered for ticket extraction; the body is never searched.

The subject is read from the raw bytes stored on the Message (the part before the first newline) rather than from the parsed Subject. This preserves a leading ticket prefix that ParseSubject strips, so a subject such as "TASK-1234 fix(ui): add button" still yields "TASK-1234" even though the parsed Subject no longer contains it.

Ticket returns an empty Ticket when the Message has no raw bytes or when no ticket is found in the subject. In particular, a Message built with NewMessage without the WithRaw option always yields an empty Ticket. Use ParseMessage, which records the raw bytes, to ensure extraction works as expected.

When the raw bytes contain no newline, the entire raw buffer is treated as the subject, mirroring ParseMessage's handling of single-line messages, so a message such as "TASK-1234 fix(ui): add button" still yields "TASK-1234".

Panics if re is nil.

Example usage:

re := regexp.MustCompile(`^((?:TASK|PROJ|BUG)-\d+)`)
t := ParseMessage("TASK-1234 add new feature\n\nDetails here.").Ticket(re)
fmt.Println(t.Name()) // Output: TASK-1234

type MessageOption added in v0.1.3

type MessageOption func(*Message)

MessageOption configures a Message at construction time.

It is passed as an optional argument to NewMessage and ParseMessage to set fields that are not part of the required subject and body, such as the raw bytes the message was parsed from.

func WithRaw added in v0.1.3

func WithRaw(raw []byte) MessageOption

WithRaw returns a MessageOption that sets the raw bytes of a Message.

The raw value preserves the original bytes the message was parsed from so that methods such as Ticket can inspect the unmodified input.

type Option added in v0.1.4

type Option func(*Commit)

Option configures a Commit instance during creation in the constructor.

func WithKind added in v0.1.4

func WithKind(k Kind) Option

WithKind returns an Option that sets the kind of the commit on its creation.

type Scope

type Scope string

Scope represents the scope of a commit.

It is typically used to specify the area of the codebase that the commit affects. For example, "(core)", "(ui)", etc.

func NewScope

func NewScope(value string) Scope

NewScope creates a new Scope instance.

func ParseScope

func ParseScope(s string) Scope

ParseScope parses the scope from a commit subject string.

The expected format has the scope enclosed in parentheses.

For example:

"feat(ui): add new button" -> scope is "ui"

func (Scope) Empty

func (s Scope) Empty() bool

Empty checks if the scope is empty.

func (Scope) String

func (s Scope) String() string

String returns the string representation of the Scope.

type Subject

type Subject struct {
	// contains filtered or unexported fields
}

Subject represents the subject of a commit message.

func NewSubject

func NewSubject(
	t Type,
	s Scope,
	d Description,
) Subject

NewSubject creates a new Subject instance.

  • t: type of the commit.
  • s: scope of the commit.
  • d: description of the commit.

func ParseSubject

func ParseSubject[T string | []byte](v T) Subject

ParseSubject parses a commit subject string into a Subject instance.

The expected formats of the input string are:

"type: description"
"type(scope): description"
"description"

Examples:

"feat: add new feature"
"fix(ui): resolve button issue"
"update documentation"

func (Subject) Description added in v0.1.1

func (s Subject) Description() Description

Description returns the description of the subject.

func (Subject) Scope added in v0.1.1

func (s Subject) Scope() Scope

Scope returns the scope of the subject.

func (Subject) String

func (s Subject) String() string

String returns the string representation of the Subject.

The format is:

"type(scope): description"
"type: description"
"description"

depending on the presence of type and scope.

Examples:

"feat(ui): add new feature"
"fix: resolve button issue"
"update documentation"

Note: If both type and scope are empty, only the description is returned.

func (Subject) Ticket added in v0.1.1

func (s Subject) Ticket(re *regexp.Regexp) ticket.Ticket

Ticket extracts a Ticket from the Subject using the provided regular expression.

The regular expression should contain a capturing group that matches the ticket name. If the subject is empty or does not match the regular expression, an empty Ticket is returned.

Example usage:

re := regexp.MustCompile(`^((?:TASK|PROJ|BUG)-\d+)`)
t := ParseSubject("TASK-1234 add new feature").Ticket(re)
fmt.Println(t.Name()) // Output: TASK-1234

func (Subject) Type added in v0.1.1

func (s Subject) Type() Type

Type returns the type of the subject.

type Type

type Type string

Type represents the type of a commit subject.

It is used to categorize the commit, such as "feat", "fix", etc.

func NewType

func NewType(value string) Type

NewType creates a new Type instance.

func ParseType

func ParseType(s string) Type

ParseType parses the type from a commit subject string.

The expected format has the type at the beginning of the subject, optionally followed by a scope in parentheses and a colon. A leading ticket prefix (such as "WS-1234") is stripped automatically.

For example:

"feat(ui): add new button"        -> type is "feat"
"[WS-1234] feat(ui): some feature"  -> type is "feat"
"fix: resolve issue"              -> type is "fix"
"feat"                           -> type is ""

func (Type) Empty

func (t Type) Empty() bool

Empty checks if the type is empty.

func (Type) String

func (t Type) String() string

String returns the string representation of the Type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL