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 ¶
NewBody creates a new Body instance from the given byte slice.
- bb: byte slice representing the commit body.
type Commit ¶
type Commit struct {
// contains filtered or unexported fields
}
Commit represents a git commit in the version control system.
func NewCommit ¶
NewCommit creates a new Commit instance.
- hash: the commit hash.
- msg: the commit message.
- opts: functional options to configure the instance.
func (Commit) Kind ¶ added in v0.1.4
Kind returns the kind of the commit.
If no kind is provided in the constructor, it returns the default KindDefault.
func (Commit) 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" -> ""
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 (Hash) ShortString ¶
ShortString returns a shortened version 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.
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 ¶
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) 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) Ticket ¶ added in v0.1.1
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.
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 ParseScope ¶
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"
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 ¶
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) 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
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 ParseType ¶
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 ""