git

package
v3.0.2 Latest Latest
Warning

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

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

Documentation

Overview

Package git provides Git command execution utilities.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoStagedFiles is returned when there are no staged files.
	ErrNoStagedFiles = errors.New("there is no file to commit, please execute the `git add` command to add the commit file")

	// ErrNotGitRepo is returned when the current directory is not a git repository.
	ErrNotGitRepo = errors.New("not a git repository")
)

Common errors.

View Source
var (
	ErrLuckyPrefixEmpty    = errors.New("invalid lucky commit prefix: cannot be empty")
	ErrLuckyPrefixTooLong  = fmt.Errorf("invalid lucky commit prefix: maximum length is %d characters", consts.LuckyCommitMaxLen)
	ErrLuckyPrefixInvalid  = errors.New("invalid lucky commit prefix: must contain only hex characters [0-9a-f]")
	ErrLuckyCommitNotFound = fmt.Errorf("%s not found in PATH, install it from: %s", consts.LuckyCommitBinary, consts.LuckyCommitURL)
)

Lucky commit errors.

Functions

func Author

func Author() (name, email string)

Author returns the git user name and email. Returns empty strings if not configured.

func CheckLuckyCommit

func CheckLuckyCommit() error

CheckLuckyCommit checks if lucky_commit executable exists in PATH. Returns nil if found, error with download instructions if not.

func Commit

func Commit(msg CommitMessage) error

Commit creates a new commit with the given message.

func CreateBranch

func CreateBranch(name string) (string, error)

CreateBranch creates a new branch with the given name.

func CreateSOB

func CreateSOB() string

CreateSOB creates a Signed-off-by line.

func CreateTypedBranch

func CreateTypedBranch(commitType, name string) (string, error)

CreateTypedBranch creates a new branch with type prefix (e.g., feat/name).

func CurrentBranch

func CurrentBranch() (string, error)

CurrentBranch returns the current branch name.

func GetHeadHash

func GetHeadHash() (string, error)

GetHeadHash returns the current HEAD commit hash.

func GetLuckyPrefix

func GetLuckyPrefix() string

GetLuckyPrefix returns the lucky commit prefix from gitconfig. Returns empty string if not set.

func GetPreviousCommits

func GetPreviousCommits(n int) ([]string, error)

GetPreviousCommits returns the last n commit messages. If on a feature branch, returns commits since diverging from main.

func GetStagedDiff

func GetStagedDiff(context int) (string, error)

GetStagedDiff returns the staged diff with specified context lines.

func HasStagedFiles

func HasStagedFiles() error

HasStagedFiles checks if there are any staged files.

func LuckyCommitCmd

func LuckyCommitCmd(prefix string) *exec.Cmd

LuckyCommitCmd creates an exec.Cmd for running lucky_commit with the given prefix.

func ParseBranchType added in v3.0.2

func ParseBranchType(branch string) string

ParseBranchType extracts the commit type from a branch name. Supports formats: type/name, type-name, type_name Returns empty string if no match found.

func Push

func Push() (string, error)

Push pushes the current branch to origin.

func RepoCheck

func RepoCheck() error

RepoCheck checks if the current directory is a git repository.

func Run

func Run(args ...string) (string, error)

Run executes a git command with the given arguments.

func ValidateLuckyPrefix

func ValidateLuckyPrefix(prefix string) (string, error)

ValidateLuckyPrefix validates and normalizes the prefix. Returns lowercase prefix or error if invalid.

Types

type CommitMessage

type CommitMessage struct {
	Type    string
	Scope   string
	Subject string
	Body    string
	Footer  string
	SOB     string // Signed-off-by line
}

CommitMessage represents a structured commit message.

func (CommitMessage) String

func (m CommitMessage) String() string

String formats the commit message.

type FileDiff

type FileDiff struct {
	Path string
	Diff string
}

FileDiff represents a single file's diff.

func SplitDiffByFile

func SplitDiffByFile(diff string) []FileDiff

SplitDiffByFile splits a unified diff into per-file chunks.

Jump to

Keyboard shortcuts

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