git

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxCommitDepth = 10000

DefaultMaxCommitDepth is the maximum number of commits to walk when searching for tags

Variables

This section is empty.

Functions

func CreateTag

func CreateTag(tagName, message string) error

CreateTag creates a git tag with the specified name and message

func GetGitShortHash

func GetGitShortHash(hashLength int) (string, error)

GetGitShortHash returns the short hash of the current HEAD commit with specified length

func GetHashLength

func GetHashLength() int

GetHashLength returns the configured hash length from config file or environment variable Priority: 1) Config file, 2) VERSIONATOR_HASH_LENGTH env var, 3) Default (7)

func GetHashLengthFromConfig

func GetHashLengthFromConfig(configHashLength int) int

GetHashLengthFromConfig returns hash length from config, with fallback to environment/default

func IsGitRepository

func IsGitRepository() bool

IsGitRepository checks if we're in a git repository

func IsWorkingDirectoryClean

func IsWorkingDirectoryClean() (bool, error)

IsWorkingDirectoryClean checks if the git working directory is clean (no uncommitted changes)

func MakeTestBranch added in v0.1.0

func MakeTestBranch(name string, hash plumbing.Hash) plumbing.Reference

MakeTestBranch creates a branch reference for testing.

func MakeTestCommit added in v0.1.0

func MakeTestCommit(hash plumbing.Hash, message, authorName, authorEmail string, when time.Time) *object.Commit

MakeTestCommit creates a commit object for testing.

func MakeTestReference added in v0.1.0

func MakeTestReference(name plumbing.ReferenceName, hash plumbing.Hash) plumbing.Reference

MakeTestReference creates a reference for testing.

func MakeTestTag added in v0.1.0

func MakeTestTag(name string, hash plumbing.Hash) plumbing.Reference

MakeTestTag creates a tag reference for testing.

func TagExists

func TagExists(tagName string) (bool, error)

TagExists checks if a git tag with the specified name already exists

Types

type CommitInfo added in v0.1.0

type CommitInfo struct {
	Hash        plumbing.Hash
	AuthorName  string
	AuthorEmail string
	AuthorTime  time.Time
	Message     string
}

CommitInfo holds extracted information about a commit for testing purposes.

func ExtractCommitInfo added in v0.1.0

func ExtractCommitInfo(c *object.Commit) CommitInfo

ExtractCommitInfo extracts relevant fields from a commit object.

type GitVersionControlSystem

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

GitVersionControlSystem implements VersionControlSystem for Git

func NewGitVCS

func NewGitVCS(opener RepositoryOpener) *GitVersionControlSystem

NewGitVCS creates a new GitVersionControlSystem with a custom repository opener. Use this constructor for testing with mock repositories.

func NewGitVCSDefault added in v0.1.0

func NewGitVCSDefault() *GitVersionControlSystem

NewGitVCSDefault creates a new GitVersionControlSystem with the default go-git opener.

func (*GitVersionControlSystem) AmendCommit added in v0.1.0

func (g *GitVersionControlSystem) AmendCommit(files []string) error

AmendCommit stages the specified files and amends the last commit

func (*GitVersionControlSystem) BranchExists added in v0.0.12

func (g *GitVersionControlSystem) BranchExists(branchName string) (bool, error)

BranchExists checks if a branch with the specified name exists

func (*GitVersionControlSystem) CommitFiles added in v0.1.0

func (g *GitVersionControlSystem) CommitFiles(files []string, message string) error

CommitFiles stages and commits the specified files with the given message

func (*GitVersionControlSystem) CreateBranch added in v0.0.12

func (g *GitVersionControlSystem) CreateBranch(branchName string) error

CreateBranch creates a branch with the specified name from the current HEAD

func (*GitVersionControlSystem) CreateTag

func (g *GitVersionControlSystem) CreateTag(tagName, message string) error

CreateTag creates a git tag

func (*GitVersionControlSystem) GetBranchName

func (g *GitVersionControlSystem) GetBranchName() (string, error)

GetBranchName returns the current branch name

func (*GitVersionControlSystem) GetCommitAuthor

func (g *GitVersionControlSystem) GetCommitAuthor() (string, error)

GetCommitAuthor returns the name of the current commit's author

func (*GitVersionControlSystem) GetCommitAuthorEmail

func (g *GitVersionControlSystem) GetCommitAuthorEmail() (string, error)

GetCommitAuthorEmail returns the email of the current commit's author

func (*GitVersionControlSystem) GetCommitDate

func (g *GitVersionControlSystem) GetCommitDate() (time.Time, error)

GetCommitDate returns the date of the current commit

func (*GitVersionControlSystem) GetCommitMessagesSinceTag added in v0.1.0

func (g *GitVersionControlSystem) GetCommitMessagesSinceTag() ([]string, error)

GetCommitMessagesSinceTag returns all commit messages since the most recent tag Returns commit messages newest first, empty slice if on tagged commit or no tags

func (*GitVersionControlSystem) GetCommitsSinceTag

func (g *GitVersionControlSystem) GetCommitsSinceTag() (int, error)

GetCommitsSinceTag returns the number of commits since the most recent tag Returns 0 if on a tagged commit, -1 if no tags exist

func (*GitVersionControlSystem) GetDirtyFiles added in v0.1.0

func (g *GitVersionControlSystem) GetDirtyFiles() ([]string, error)

GetDirtyFiles returns the list of files with uncommitted changes. Uses git CLI which natively respects .gitignore and skips ignored directories during the walk, avoiding performance issues with large ignored trees (e.g., .cargo-container/) and permission errors on unreadable files.

func (*GitVersionControlSystem) GetHooksPath added in v0.1.0

func (g *GitVersionControlSystem) GetHooksPath() (string, error)

GetHooksPath returns the path to the git hooks directory

func (*GitVersionControlSystem) GetLastTag

func (g *GitVersionControlSystem) GetLastTag() (string, error)

GetLastTag returns the most recent semver tag Returns empty string if no tags exist

func (*GitVersionControlSystem) GetLastTagCommit

func (g *GitVersionControlSystem) GetLastTagCommit() (string, error)

GetLastTagCommit returns the SHA of the commit the last tag points to

func (*GitVersionControlSystem) GetRepositoryRoot

func (g *GitVersionControlSystem) GetRepositoryRoot() (string, error)

GetRepositoryRoot returns the root directory of the git repository

func (*GitVersionControlSystem) GetTemplateVariables

func (g *GitVersionControlSystem) GetTemplateVariables(context map[string]string) map[string]string

GetTemplateVariables returns git-specific template variables

func (*GitVersionControlSystem) GetUncommittedChanges

func (g *GitVersionControlSystem) GetUncommittedChanges() (int, error)

GetUncommittedChanges returns the count of uncommitted changes (staged + unstaged + untracked) Files matching gitignore patterns are excluded from the count

func (*GitVersionControlSystem) GetVCSIdentifier

func (g *GitVersionControlSystem) GetVCSIdentifier(length int) (string, error)

GetVCSIdentifier returns a short hash of the current commit

func (*GitVersionControlSystem) IsRepository

func (g *GitVersionControlSystem) IsRepository() bool

IsRepository checks if we're in a git repository

func (*GitVersionControlSystem) IsWorkingDirectoryClean

func (g *GitVersionControlSystem) IsWorkingDirectoryClean() (bool, error)

IsWorkingDirectoryClean checks if there are no uncommitted changes Files matching gitignore patterns are not considered when checking for cleanliness

func (*GitVersionControlSystem) Name

func (g *GitVersionControlSystem) Name() string

Name returns "git"

func (*GitVersionControlSystem) PushBranch added in v0.1.0

func (g *GitVersionControlSystem) PushBranch(branchName string) error

PushBranch pushes a branch to the remote repository

func (*GitVersionControlSystem) PushTag added in v0.1.0

func (g *GitVersionControlSystem) PushTag(tagName string) error

PushTag pushes a tag to the remote repository

func (*GitVersionControlSystem) TagExists

func (g *GitVersionControlSystem) TagExists(tagName string) (bool, error)

TagExists checks if a tag exists

func (*GitVersionControlSystem) Types

Types returns the set of plugin types this VCS implements

type GoGitRepository added in v0.1.0

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

GoGitRepository wraps go-git's *git.Repository to implement the Repository interface.

func (*GoGitRepository) Branches added in v0.1.0

func (r *GoGitRepository) Branches() (storer.ReferenceIter, error)

func (*GoGitRepository) CommitObject added in v0.1.0

func (r *GoGitRepository) CommitObject(h plumbing.Hash) (*object.Commit, error)

func (*GoGitRepository) CreateTag added in v0.1.0

func (r *GoGitRepository) CreateTag(name string, hash plumbing.Hash, opts *git.CreateTagOptions) (*plumbing.Reference, error)

func (*GoGitRepository) Head added in v0.1.0

func (r *GoGitRepository) Head() (*plumbing.Reference, error)

func (*GoGitRepository) Log added in v0.1.0

func (*GoGitRepository) SetReference added in v0.1.0

func (r *GoGitRepository) SetReference(ref *plumbing.Reference) error

func (*GoGitRepository) TagObject added in v0.1.0

func (r *GoGitRepository) TagObject(h plumbing.Hash) (*object.Tag, error)

func (*GoGitRepository) Tags added in v0.1.0

func (*GoGitRepository) Worktree added in v0.1.0

func (r *GoGitRepository) Worktree() (Worktree, error)

type GoGitWorktree added in v0.1.0

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

GoGitWorktree wraps go-git's *git.Worktree to implement the Worktree interface.

func (*GoGitWorktree) Add added in v0.1.0

func (w *GoGitWorktree) Add(path string) (plumbing.Hash, error)

func (*GoGitWorktree) Commit added in v0.1.0

func (w *GoGitWorktree) Commit(msg string, opts *git.CommitOptions) (plumbing.Hash, error)

func (*GoGitWorktree) Status added in v0.1.0

func (w *GoGitWorktree) Status() (git.Status, error)

type MockRepository added in v0.1.0

type MockRepository struct {
	// Head configuration
	HeadRef *plumbing.Reference
	HeadErr error

	// CommitObject configuration
	Commits   map[plumbing.Hash]*object.Commit
	CommitErr error

	// CreateTag configuration
	CreateTagRef *plumbing.Reference
	CreateTagErr error

	// Tags configuration
	TagRefs []plumbing.Reference
	TagsErr error

	// Branches configuration
	BranchRefs  []plumbing.Reference
	BranchesErr error

	// TagObject configuration
	TagObjects map[plumbing.Hash]*object.Tag
	TagObjErr  error

	// Log configuration
	LogCommits []*object.Commit
	LogErr     error

	// SetReference configuration
	SetRefErr    error
	SetRefCalled bool
	SetRefArg    *plumbing.Reference

	// Worktree configuration
	MockWorktree *MockWorktree
	WorktreeErr  error
}

MockRepository is a test double for the Repository interface. It allows configuring return values for each method.

func NewMockRepository added in v0.1.0

func NewMockRepository() *MockRepository

NewMockRepository creates a new MockRepository with sensible defaults.

func (*MockRepository) Branches added in v0.1.0

func (m *MockRepository) Branches() (storer.ReferenceIter, error)

func (*MockRepository) CommitObject added in v0.1.0

func (m *MockRepository) CommitObject(h plumbing.Hash) (*object.Commit, error)

func (*MockRepository) CreateTag added in v0.1.0

func (m *MockRepository) CreateTag(name string, hash plumbing.Hash, opts *git.CreateTagOptions) (*plumbing.Reference, error)

func (*MockRepository) Head added in v0.1.0

func (m *MockRepository) Head() (*plumbing.Reference, error)

func (*MockRepository) Log added in v0.1.0

func (*MockRepository) SetReference added in v0.1.0

func (m *MockRepository) SetReference(ref *plumbing.Reference) error

func (*MockRepository) TagObject added in v0.1.0

func (m *MockRepository) TagObject(h plumbing.Hash) (*object.Tag, error)

func (*MockRepository) Tags added in v0.1.0

func (*MockRepository) Worktree added in v0.1.0

func (m *MockRepository) Worktree() (Worktree, error)

type MockWorktree added in v0.1.0

type MockWorktree struct {
	StatusResult git.Status
	StatusErr    error

	AddHash  plumbing.Hash
	AddErr   error
	AddCalls []string

	CommitHash  plumbing.Hash
	CommitErr   error
	CommitCalls []struct {
		Msg  string
		Opts *git.CommitOptions
	}
}

MockWorktree is a test double for the Worktree interface.

func NewMockWorktree added in v0.1.0

func NewMockWorktree() *MockWorktree

NewMockWorktree creates a new MockWorktree with sensible defaults.

func (*MockWorktree) Add added in v0.1.0

func (m *MockWorktree) Add(path string) (plumbing.Hash, error)

func (*MockWorktree) Commit added in v0.1.0

func (m *MockWorktree) Commit(msg string, opts *git.CommitOptions) (plumbing.Hash, error)

func (*MockWorktree) Status added in v0.1.0

func (m *MockWorktree) Status() (git.Status, error)

type Repository added in v0.1.0

type Repository interface {
	// Head returns the HEAD reference
	Head() (*plumbing.Reference, error)

	// CommitObject returns a commit object by hash
	CommitObject(h plumbing.Hash) (*object.Commit, error)

	// CreateTag creates an annotated tag
	CreateTag(name string, hash plumbing.Hash, opts *git.CreateTagOptions) (*plumbing.Reference, error)

	// Tags returns an iterator over all tags
	Tags() (storer.ReferenceIter, error)

	// Branches returns an iterator over all branches
	Branches() (storer.ReferenceIter, error)

	// TagObject returns a tag object by hash (for annotated tags)
	TagObject(h plumbing.Hash) (*object.Tag, error)

	// Log returns a commit log iterator
	Log(opts *git.LogOptions) (object.CommitIter, error)

	// SetReference stores a reference (used for branch creation)
	SetReference(ref *plumbing.Reference) error

	// Worktree returns the repository worktree
	Worktree() (Worktree, error)
}

Repository abstracts git repository operations for testability. This interface wraps go-git's *git.Repository to enable mocking in tests.

func DefaultRepositoryOpener added in v0.1.0

func DefaultRepositoryOpener(path string) (Repository, error)

DefaultRepositoryOpener opens a git repository using go-git's PlainOpen.

type RepositoryOpener added in v0.1.0

type RepositoryOpener func(path string) (Repository, error)

RepositoryOpener is a function that opens a git repository at the given path.

func ErrorRepositoryOpener added in v0.1.0

func ErrorRepositoryOpener(err error) RepositoryOpener

ErrorRepositoryOpener returns a RepositoryOpener that always returns an error.

func MockRepositoryOpener added in v0.1.0

func MockRepositoryOpener(mock Repository) RepositoryOpener

MockRepositoryOpener returns a RepositoryOpener that returns the given mock.

type TagInfo

type TagInfo struct {
	CommitsSinceTag   int    // -1 if no tags exist
	LastTagName       string // empty if no tags
	LastTagCommitHash string // empty if no tags
}

TagInfo holds pre-computed tag-related information from a single walk

type Worktree added in v0.1.0

type Worktree interface {
	// Status returns the working tree status
	Status() (git.Status, error)

	// Add stages a file
	Add(path string) (plumbing.Hash, error)

	// Commit creates a commit with the staged changes
	Commit(msg string, opts *git.CommitOptions) (plumbing.Hash, error)
}

Worktree abstracts git worktree operations for testability.

Jump to

Keyboard shortcuts

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