commit

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 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,
) Commit

NewCommit creates a new Commit instance.

  • hash: the commit hash.
  • msg: the commit message.

func (Commit) Hash

func (c Commit) Hash() Hash

Hash returns the commit hash.

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 Message

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

Message represents a git commit message.

func NewMessage

func NewMessage(
	subject Subject,
	body Body,
) Message

NewMessage creates a new Message instance.

  • subject: the subject of the commit message.
  • body: the body of the commit message.

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.

If the body is empty, it returns only the subject. Otherwise, it concatenates the subject and body 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 v1.0.0

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

Ticket extracts a Ticket from the Message's 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.

If there is no ticket found in the subject, it will not attempt to search the body for a ticket. Only the subject is considered for ticket extraction.

Example usage:

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

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) 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 v1.0.0

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

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.

For example:

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

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