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 ¶
- func ExtractVersionFromBranch(branchName, tagPrefix string) (string, bool)
- type Branch
- type BranchCommit
- type Commit
- type GoGitRepository
- func (r *GoGitRepository) BranchCommits(branch Branch, _ ...PathFilter) ([]Commit, error)
- func (r *GoGitRepository) Branches(_ ...PathFilter) ([]Branch, error)
- func (r *GoGitRepository) BranchesContainingCommit(sha string) ([]Branch, error)
- func (r *GoGitRepository) CommitFromSha(sha string) (Commit, error)
- func (r *GoGitRepository) CommitLog(from, to string, _ ...PathFilter) ([]Commit, error)
- func (r *GoGitRepository) CommitsPriorTo(olderThan time.Time, branch Branch) ([]Commit, error)
- func (r *GoGitRepository) FindMergeBase(sha1, sha2 string) (string, error)
- func (r *GoGitRepository) Head() (Branch, error)
- func (r *GoGitRepository) IsHeadDetached() bool
- func (r *GoGitRepository) MainlineCommitLog(from, to string, _ ...PathFilter) ([]Commit, error)
- func (r *GoGitRepository) NumberOfUncommittedChanges() (int, error)
- func (r *GoGitRepository) Path() string
- func (r *GoGitRepository) PeelTagToCommit(tag Tag) (string, error)
- func (r *GoGitRepository) Tags(_ ...PathFilter) ([]Tag, error)
- func (r *GoGitRepository) WorkingDirectory() string
- type MergeMessage
- type MergeMessageFormat
- type MockRepository
- func (m *MockRepository) BranchCommits(branch Branch, filters ...PathFilter) ([]Commit, error)
- func (m *MockRepository) Branches(filters ...PathFilter) ([]Branch, error)
- func (m *MockRepository) BranchesContainingCommit(sha string) ([]Branch, error)
- func (m *MockRepository) CommitFromSha(sha string) (Commit, error)
- func (m *MockRepository) CommitLog(from, to string, filters ...PathFilter) ([]Commit, error)
- func (m *MockRepository) CommitsPriorTo(olderThan time.Time, branch Branch) ([]Commit, error)
- func (m *MockRepository) FindMergeBase(sha1, sha2 string) (string, error)
- func (m *MockRepository) Head() (Branch, error)
- func (m *MockRepository) IsHeadDetached() bool
- func (m *MockRepository) MainlineCommitLog(from, to string, filters ...PathFilter) ([]Commit, error)
- func (m *MockRepository) NumberOfUncommittedChanges() (int, error)
- func (m *MockRepository) Path() string
- func (m *MockRepository) PeelTagToCommit(tag Tag) (string, error)
- func (m *MockRepository) Tags(filters ...PathFilter) ([]Tag, error)
- func (m *MockRepository) WorkingDirectory() string
- type ObjectID
- type PathFilter
- type ReferenceName
- type Repository
- type RepositoryStore
- func (s *RepositoryStore) FindCommitBranchWasBranchedFrom(branch Branch, cfg *config.Config, excludedBranches ...Branch) (BranchCommit, error)
- func (s *RepositoryStore) FindMainBranch(cfg *config.Config) (Branch, bool, error)
- func (s *RepositoryStore) FindMergeBase(branch1, branch2 Branch) (Commit, bool, error)
- func (s *RepositoryStore) FindMergeBaseFromCommits(commit1, commit2 Commit) (Commit, bool, error)
- func (s *RepositoryStore) GetBaseVersionSource(tip Commit) (Commit, error)
- func (s *RepositoryStore) GetBranchesContainingCommit(commit Commit) ([]Branch, error)
- func (s *RepositoryStore) GetBranchesForCommit(commit Commit) ([]Branch, error)
- func (s *RepositoryStore) GetCommitLog(from, to Commit) ([]Commit, error)
- func (s *RepositoryStore) GetCurrentCommit(branch Branch, commitID string) (Commit, error)
- func (s *RepositoryStore) GetCurrentCommitTaggedVersion(commit Commit, tagPrefix string) (semver.SemanticVersion, bool, error)
- func (s *RepositoryStore) GetMainlineCommitLog(from, to Commit) ([]Commit, error)
- func (s *RepositoryStore) GetMergeBaseCommits(mergedHead, mergeBase Commit) ([]Commit, error)
- func (s *RepositoryStore) GetNumberOfUncommittedChanges() (int, error)
- func (s *RepositoryStore) GetReleaseBranches(releaseBranchConfig map[string]*config.BranchConfig) ([]Branch, error)
- func (s *RepositoryStore) GetTargetBranch(targetBranchName string) (Branch, error)
- func (s *RepositoryStore) GetValidVersionTags(tagPrefix string, olderThan *time.Time, filters ...PathFilter) ([]VersionTag, error)
- func (s *RepositoryStore) GetVersionTagsOnBranch(branch Branch, tagPrefix string, filters ...PathFilter) ([]semver.SemanticVersion, error)
- func (s *RepositoryStore) IsCommitOnBranch(commit Commit, branch Branch) (bool, error)
- type Tag
- type VersionTag
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractVersionFromBranch ¶
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 ¶
FriendlyName returns the friendly name of the branch.
type BranchCommit ¶
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.
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 (*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 ¶
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 (*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.
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 ¶
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.