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.
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 Message ¶
type Message struct {
// contains filtered or unexported fields
}
Message represents a git commit message.
func NewMessage ¶
NewMessage creates a new Message instance.
- subject: the subject of the commit message.
- body: the body of the commit message.
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.
If the body is empty, it returns only the subject. Otherwise, it concatenates the subject and body with two newlines in between.
func (Message) Ticket ¶ added in v1.0.0
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 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) 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
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.