git

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package git provides the git abstraction layer for semantic version calculation. It defines concrete entity types (Commit, Branch, Tag), a Repository interface, and higher-level domain queries via RepositoryStore.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractVersionFromBranch

func ExtractVersionFromBranch(branchName, tagPrefix string) (string, bool)

ExtractVersionFromBranch attempts to extract a semantic version string from a branch name. It splits the branch name on '/' and '-', strips tag prefix, and looks for a segment that starts with a digit pattern. Segments like "JIRA-123" are not matched because the pre-dash portion is non-numeric.

Types

type Branch

type Branch struct {
	Name           ReferenceName
	Tip            *Commit
	IsRemote       bool
	IsDetachedHead bool
}

Branch represents a git branch.

func (Branch) FriendlyName

func (b Branch) FriendlyName() string

FriendlyName returns the friendly name of the branch.

type BranchCommit

type BranchCommit struct {
	Branch Branch
	Commit Commit
}

BranchCommit represents a branch and the commit where it was branched from.

type Commit

type Commit struct {
	Sha     string
	Parents []string // parent SHAs; len > 1 means merge commit
	When    time.Time
	Message string
}

Commit represents a git commit.

func (Commit) IsEmpty

func (c Commit) IsEmpty() bool

IsEmpty returns true if the commit has no SHA (zero value).

func (Commit) IsMerge

func (c Commit) IsMerge() bool

IsMerge returns true if the commit has more than one parent.

func (Commit) ShortSha

func (c Commit) ShortSha() string

ShortSha returns the first 7 characters of the SHA.

type GoGitRepository

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

GoGitRepository implements Repository using go-git.

func Open

func Open(path string) (*GoGitRepository, error)

Open opens a git repository at the given path.

func (*GoGitRepository) BranchCommits

func (r *GoGitRepository) BranchCommits(branch Branch, _ ...PathFilter) ([]Commit, error)

func (*GoGitRepository) Branches

func (r *GoGitRepository) Branches(_ ...PathFilter) ([]Branch, error)

func (*GoGitRepository) BranchesContainingCommit

func (r *GoGitRepository) BranchesContainingCommit(sha string) ([]Branch, error)

func (*GoGitRepository) CommitFromSha

func (r *GoGitRepository) CommitFromSha(sha string) (Commit, error)

func (*GoGitRepository) CommitLog

func (r *GoGitRepository) CommitLog(from, to string, _ ...PathFilter) ([]Commit, error)

func (*GoGitRepository) CommitsPriorTo

func (r *GoGitRepository) CommitsPriorTo(olderThan time.Time, branch Branch) ([]Commit, error)

func (*GoGitRepository) FindMergeBase

func (r *GoGitRepository) FindMergeBase(sha1, sha2 string) (string, error)

func (*GoGitRepository) Head

func (r *GoGitRepository) Head() (Branch, error)

func (*GoGitRepository) IsHeadDetached

func (r *GoGitRepository) IsHeadDetached() bool

func (*GoGitRepository) MainlineCommitLog

func (r *GoGitRepository) MainlineCommitLog(from, to string, _ ...PathFilter) ([]Commit, error)

func (*GoGitRepository) NumberOfUncommittedChanges

func (r *GoGitRepository) NumberOfUncommittedChanges() (int, error)

func (*GoGitRepository) Path

func (r *GoGitRepository) Path() string

func (*GoGitRepository) PeelTagToCommit

func (r *GoGitRepository) PeelTagToCommit(tag Tag) (string, error)

func (*GoGitRepository) Tags

func (r *GoGitRepository) Tags(_ ...PathFilter) ([]Tag, error)

func (*GoGitRepository) WorkingDirectory

func (r *GoGitRepository) WorkingDirectory() string

type MergeMessage

type MergeMessage struct {
	FormatName          string
	MergedBranch        string
	TargetBranch        string
	PullRequestNumber   int
	IsMergedPullRequest bool
}

MergeMessage represents a parsed merge commit or squash merge message.

func ParseMergeMessage

func ParseMergeMessage(message string, customFormats map[string]string) MergeMessage

ParseMergeMessage parses a commit message against all known merge formats. Custom formats are tried first, then defaults, then squash formats. Returns a zero MergeMessage if no format matches.

func (MergeMessage) IsEmpty

func (m MergeMessage) IsEmpty() bool

IsEmpty returns true if the merge message did not match any format.

type MergeMessageFormat

type MergeMessageFormat struct {
	Name    string
	Pattern *regexp.Regexp
}

MergeMessageFormat defines a named regex pattern for merge messages.

func DefaultMergeMessageFormats

func DefaultMergeMessageFormats() []MergeMessageFormat

DefaultMergeMessageFormats returns the 6 built-in merge message formats.

func SquashMergeMessageFormats

func SquashMergeMessageFormats() []MergeMessageFormat

SquashMergeMessageFormats returns the squash merge message formats.

type MockRepository

type MockRepository struct {
	PathFunc                       func() string
	WorkingDirectoryFunc           func() string
	IsHeadDetachedFunc             func() bool
	HeadFunc                       func() (Branch, error)
	BranchesFunc                   func(...PathFilter) ([]Branch, error)
	TagsFunc                       func(...PathFilter) ([]Tag, error)
	CommitFromShaFunc              func(string) (Commit, error)
	CommitLogFunc                  func(string, string, ...PathFilter) ([]Commit, error)
	MainlineCommitLogFunc          func(string, string, ...PathFilter) ([]Commit, error)
	BranchCommitsFunc              func(Branch, ...PathFilter) ([]Commit, error)
	CommitsPriorToFunc             func(time.Time, Branch) ([]Commit, error)
	FindMergeBaseFunc              func(string, string) (string, error)
	BranchesContainingCommitFunc   func(string) ([]Branch, error)
	NumberOfUncommittedChangesFunc func() (int, error)
	PeelTagToCommitFunc            func(Tag) (string, error)
}

MockRepository is a configurable mock implementation of Repository for testing. Each method is backed by a function field. If the function field is nil, the method returns sensible zero values.

func (*MockRepository) BranchCommits

func (m *MockRepository) BranchCommits(branch Branch, filters ...PathFilter) ([]Commit, error)

func (*MockRepository) Branches

func (m *MockRepository) Branches(filters ...PathFilter) ([]Branch, error)

func (*MockRepository) BranchesContainingCommit

func (m *MockRepository) BranchesContainingCommit(sha string) ([]Branch, error)

func (*MockRepository) CommitFromSha

func (m *MockRepository) CommitFromSha(sha string) (Commit, error)

func (*MockRepository) CommitLog

func (m *MockRepository) CommitLog(from, to string, filters ...PathFilter) ([]Commit, error)

func (*MockRepository) CommitsPriorTo

func (m *MockRepository) CommitsPriorTo(olderThan time.Time, branch Branch) ([]Commit, error)

func (*MockRepository) FindMergeBase

func (m *MockRepository) FindMergeBase(sha1, sha2 string) (string, error)

func (*MockRepository) Head

func (m *MockRepository) Head() (Branch, error)

func (*MockRepository) IsHeadDetached

func (m *MockRepository) IsHeadDetached() bool

func (*MockRepository) MainlineCommitLog

func (m *MockRepository) MainlineCommitLog(from, to string, filters ...PathFilter) ([]Commit, error)

func (*MockRepository) NumberOfUncommittedChanges

func (m *MockRepository) NumberOfUncommittedChanges() (int, error)

func (*MockRepository) Path

func (m *MockRepository) Path() string

func (*MockRepository) PeelTagToCommit

func (m *MockRepository) PeelTagToCommit(tag Tag) (string, error)

func (*MockRepository) Tags

func (m *MockRepository) Tags(filters ...PathFilter) ([]Tag, error)

func (*MockRepository) WorkingDirectory

func (m *MockRepository) WorkingDirectory() string

type ObjectID

type ObjectID struct {
	Sha string
}

ObjectID represents a git object identifier.

func (ObjectID) ShortSha

func (id ObjectID) ShortSha(n int) string

ShortSha returns the first n characters of the SHA.

func (ObjectID) String

func (id ObjectID) String() string

String returns the full SHA.

type PathFilter

type PathFilter string

PathFilter constrains git queries to files matching a path pattern. Used for monorepo support. An empty PathFilter means no filtering.

type ReferenceName

type ReferenceName struct {
	Canonical     string // e.g., "refs/heads/main"
	Friendly      string // e.g., "main"
	WithoutRemote string // e.g., "main" (strips "origin/" from remote refs)
}

ReferenceName represents a git reference with canonical and friendly forms.

func NewBranchReferenceName

func NewBranchReferenceName(name string) ReferenceName

NewBranchReferenceName creates a ReferenceName for a local branch.

func NewReferenceName

func NewReferenceName(canonical string) ReferenceName

NewReferenceName creates a ReferenceName from a canonical ref path.

func (ReferenceName) IsBranch

func (r ReferenceName) IsBranch() bool

IsBranch returns true if this reference is a local branch.

func (ReferenceName) IsRemoteBranch

func (r ReferenceName) IsRemoteBranch() bool

IsRemoteBranch returns true if this reference is a remote tracking branch.

func (ReferenceName) IsTag

func (r ReferenceName) IsTag() bool

IsTag returns true if this reference is a tag.

type Repository

type Repository interface {
	// Path returns the path to the .git directory.
	Path() string

	// WorkingDirectory returns the path to the working directory.
	WorkingDirectory() string

	// IsHeadDetached returns true if HEAD is not pointing to a branch.
	IsHeadDetached() bool

	// Head returns the current HEAD branch.
	Head() (Branch, error)

	// Branches returns all branches in the repository.
	Branches(filters ...PathFilter) ([]Branch, error)

	// Tags returns all tags in the repository.
	Tags(filters ...PathFilter) ([]Tag, error)

	// CommitFromSha returns the commit with the given SHA.
	CommitFromSha(sha string) (Commit, error)

	// CommitLog returns commits reachable from 'to' but not from 'from',
	// in reverse chronological order. If from is empty, all ancestors of
	// 'to' are returned.
	CommitLog(from, to string, filters ...PathFilter) ([]Commit, error)

	// MainlineCommitLog returns first-parent-only commits reachable from
	// 'to' but not from 'from'. Used for mainline mode calculations.
	MainlineCommitLog(from, to string, filters ...PathFilter) ([]Commit, error)

	// BranchCommits returns commits on a specific branch in reverse
	// chronological order.
	BranchCommits(branch Branch, filters ...PathFilter) ([]Commit, error)

	// CommitsPriorTo returns branch commits whose date is older than the
	// given time.
	CommitsPriorTo(olderThan time.Time, branch Branch) ([]Commit, error)

	// FindMergeBase returns the best common ancestor of two commits.
	// Returns an empty string if no merge base exists.
	FindMergeBase(sha1, sha2 string) (string, error)

	// BranchesContainingCommit returns all branches that contain the
	// given commit SHA.
	BranchesContainingCommit(sha string) ([]Branch, error)

	// NumberOfUncommittedChanges returns the count of uncommitted changes
	// in the working directory.
	NumberOfUncommittedChanges() (int, error)

	// PeelTagToCommit resolves a tag to its target commit SHA.
	// For lightweight tags, returns the target directly.
	// For annotated tags, peels through to the commit.
	PeelTagToCommit(tag Tag) (string, error)
}

Repository provides low-level git operations. This is the key abstraction point for testing and backend swapping. All methods that traverse commits or list refs accept optional PathFilter parameters for monorepo support.

type RepositoryStore

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

RepositoryStore provides higher-level domain queries built on top of a Repository. It uses config and semver packages to interpret git data in the context of semantic versioning.

func NewRepositoryStore

func NewRepositoryStore(repo Repository) *RepositoryStore

NewRepositoryStore creates a new RepositoryStore wrapping the given Repository.

func (*RepositoryStore) FindCommitBranchWasBranchedFrom

func (s *RepositoryStore) FindCommitBranchWasBranchedFrom(branch Branch, cfg *config.Config, excludedBranches ...Branch) (BranchCommit, error)

FindCommitBranchWasBranchedFrom finds where a branch was forked from a source branch. It examines source branches defined in config and returns the branch and commit of the closest fork point.

func (*RepositoryStore) FindMainBranch

func (s *RepositoryStore) FindMainBranch(cfg *config.Config) (Branch, bool, error)

FindMainBranch returns the branch matching the main branch regex from config.

func (*RepositoryStore) FindMergeBase

func (s *RepositoryStore) FindMergeBase(branch1, branch2 Branch) (Commit, bool, error)

FindMergeBase returns the merge base commit of two branches.

func (*RepositoryStore) FindMergeBaseFromCommits

func (s *RepositoryStore) FindMergeBaseFromCommits(commit1, commit2 Commit) (Commit, bool, error)

FindMergeBaseFromCommits returns the merge base of two commits.

func (*RepositoryStore) GetBaseVersionSource

func (s *RepositoryStore) GetBaseVersionSource(tip Commit) (Commit, error)

GetBaseVersionSource returns the root commit (first commit) reachable from tip.

func (*RepositoryStore) GetBranchesContainingCommit

func (s *RepositoryStore) GetBranchesContainingCommit(commit Commit) ([]Branch, error)

GetBranchesContainingCommit returns branches that contain the given commit.

func (*RepositoryStore) GetBranchesForCommit

func (s *RepositoryStore) GetBranchesForCommit(commit Commit) ([]Branch, error)

GetBranchesForCommit returns non-remote branches whose tip is the given commit.

func (*RepositoryStore) GetCommitLog

func (s *RepositoryStore) GetCommitLog(from, to Commit) ([]Commit, error)

GetCommitLog returns commits between from and to.

func (*RepositoryStore) GetCurrentCommit

func (s *RepositoryStore) GetCurrentCommit(branch Branch, commitID string) (Commit, error)

GetCurrentCommit returns the commit from a SHA or the branch tip.

func (*RepositoryStore) GetCurrentCommitTaggedVersion

func (s *RepositoryStore) GetCurrentCommitTaggedVersion(commit Commit, tagPrefix string) (semver.SemanticVersion, bool, error)

GetCurrentCommitTaggedVersion returns the highest semantic version tag on the given commit. Returns false if the commit has no version tag.

func (*RepositoryStore) GetMainlineCommitLog

func (s *RepositoryStore) GetMainlineCommitLog(from, to Commit) ([]Commit, error)

GetMainlineCommitLog returns first-parent-only commits between from and to.

func (*RepositoryStore) GetMergeBaseCommits

func (s *RepositoryStore) GetMergeBaseCommits(mergedHead, mergeBase Commit) ([]Commit, error)

GetMergeBaseCommits returns commits reachable from mergedHead but not from mergeBase.

func (*RepositoryStore) GetNumberOfUncommittedChanges

func (s *RepositoryStore) GetNumberOfUncommittedChanges() (int, error)

GetNumberOfUncommittedChanges returns the number of uncommitted changes.

func (*RepositoryStore) GetReleaseBranches

func (s *RepositoryStore) GetReleaseBranches(releaseBranchConfig map[string]*config.BranchConfig) ([]Branch, error)

GetReleaseBranches returns all branches matching any release branch config regex.

func (*RepositoryStore) GetTargetBranch

func (s *RepositoryStore) GetTargetBranch(targetBranchName string) (Branch, error)

GetTargetBranch resolves the target branch from a name or HEAD.

func (*RepositoryStore) GetValidVersionTags

func (s *RepositoryStore) GetValidVersionTags(tagPrefix string, olderThan *time.Time, filters ...PathFilter) ([]VersionTag, error)

GetValidVersionTags returns all tags that parse as semantic versions, optionally filtered to tags on commits older than the given time.

func (*RepositoryStore) GetVersionTagsOnBranch

func (s *RepositoryStore) GetVersionTagsOnBranch(branch Branch, tagPrefix string, filters ...PathFilter) ([]semver.SemanticVersion, error)

GetVersionTagsOnBranch returns semantic versions from tags on the given branch. Results are sorted by version descending (highest first).

func (*RepositoryStore) IsCommitOnBranch

func (s *RepositoryStore) IsCommitOnBranch(commit Commit, branch Branch) (bool, error)

IsCommitOnBranch checks if a commit is reachable from the branch tip.

type Tag

type Tag struct {
	Name      ReferenceName
	TargetSha string // SHA of the commit this tag points to
}

Tag represents a git tag.

type VersionTag

type VersionTag struct {
	Tag     Tag
	Version semver.SemanticVersion
	Commit  Commit
}

VersionTag holds a tag, its parsed semantic version, and the target commit.

Jump to

Keyboard shortcuts

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