gitsemver

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrGitExec = &errGitExec{}
View Source
var ErrNotDirectory = errors.New("not a directory")
View Source
var ErrUnexpectedRevParseOutput = &errUnexpectedRevParseOutput{}

ErrUnexpectedRevParseOutput classifies errors where rev-parse output line count does not match the requested tag/tree pairs.

Functions

func CleanBranch

func CleanBranch(branch string) string

func LastName

func LastName(s string) string

func MaybeSync

func MaybeSync(w io.Writer)

func NewErrGitExec

func NewErrGitExec(git string, args []string, err error, stderr string) error

func NewErrUnexpectedRevParseOutput

func NewErrUnexpectedRevParseOutput(expectedTags, gotLines int) error

Types

type DefaultGitter

type DefaultGitter struct {
	Git      string
	DebugOut io.Writer
}

func (DefaultGitter) CheckGitRepo

func (dg DefaultGitter) CheckGitRepo(dir string) (repo string, err error)

CheckGitRepo checks that the given directory is part of a git repository, meaning that it or one of its parent directories has a valid '.git' marker. If it is, it returns the absolute path of the git repo and a nil error.

func (DefaultGitter) CleanStatus

func (dg DefaultGitter) CleanStatus(repo string, includeUntracked bool) (yes bool, err error)

func (DefaultGitter) CreateTag

func (dg DefaultGitter) CreateTag(repo, tag string) (err error)

func (DefaultGitter) DeleteRemoteTag

func (dg DefaultGitter) DeleteRemoteTag(repo, tag string) (err error)

func (DefaultGitter) DeleteTag

func (dg DefaultGitter) DeleteTag(repo, tag string) (err error)

func (DefaultGitter) Exec

func (dg DefaultGitter) Exec(args ...string) (output []byte, err error)

func (DefaultGitter) FetchTags

func (dg DefaultGitter) FetchTags(repo string) (err error)

func (DefaultGitter) GetBranch

func (dg DefaultGitter) GetBranch(repo string) (branch string, err error)

func (DefaultGitter) GetBranchesFromTag

func (dg DefaultGitter) GetBranchesFromTag(repo, tag string) (branches []string, err error)

func (DefaultGitter) GetBuild

func (dg DefaultGitter) GetBuild(repo string) (buildnum string, err error)

func (DefaultGitter) GetClosestTag

func (dg DefaultGitter) GetClosestTag(repo, commit string) (tag string, err error)

GetClosestTag returns the closest semver tag for the given commit hash.

func (DefaultGitter) GetCurrentTreeHash

func (dg DefaultGitter) GetCurrentTreeHash(repo string) (hash string, err error)

GetCurrentTreeHash returns the current tree hash.

func (DefaultGitter) GetHashes

func (dg DefaultGitter) GetHashes(repo, tag string) (commit, tree string, err error)

GetHashes returns the commit and tree hashes for the given tag.

func (DefaultGitter) GetHashesBatch

func (dg DefaultGitter) GetHashesBatch(repo string, tags []string) (hashes []GitTag, err error)

GetHashesBatch returns commit/tree hashes for many tags using chunked rev-parse calls to reduce process startup overhead.

func (DefaultGitter) GetTags

func (dg DefaultGitter) GetTags(repo string) (tags []string, err error)

GetTags returns all tags, sorted by version descending. The latest tag is the first in the list.

func (DefaultGitter) PushTag

func (dg DefaultGitter) PushTag(repo, tag string) (err error)

type Environment

type Environment interface {
	Getenv(string) string
	LookupEnv(string) (string, bool)
}

Environment allows us to mock the OS environment

type GitSemVer

type GitSemVer struct {
	Git      Gitter      // Git
	Env      Environment // environment
	DebugOut io.Writer   // if nit nil, write debug output here
	// contains filtered or unexported fields
}

GitSemVer holds git metadata used while computing a version.

A GitSemVer instance is intended for one repository per run. Reusing a single instance across multiple repositories is unsupported and can produce incorrect results because internal tag metadata is cached. Reusing a single instance across repeated GetVersion calls after repository changes is also unsupported for the same reason.

func New

func New(gitBin string, debugOut io.Writer) (vs *GitSemVer, err error)

New returns a GitSemVer ready to examine the git repositories using the given Git binary.

func (*GitSemVer) Debug

func (vs *GitSemVer) Debug(f string, args ...any)

Debug writes debugging output to DebugOut if it's not nil.

func (*GitSemVer) GetBranch

func (vs *GitSemVer) GetBranch(repo string) (branchName string, err error)

GetBranch returns the current branch as a string suitable for inclusion in the semver text as well as the actual branch name in the build system or Git. If no branch name can be found (for example, in detached HEAD state), then an empty string is returned.

func (*GitSemVer) GetBuild

func (vs *GitSemVer) GetBuild(repo string) (build string, err error)

GetBuild returns the build counter. This is taken from the CI system if available, otherwise the Git commit count is used. Returns an empty string if no reasonable build counter can be found.

func (*GitSemVer) GetTag

func (vs *GitSemVer) GetTag(repo string) (tag string, match bool, err error)

GetTag returns the semver git version tag matching the current tree, or the closest semver tag if none match exactly. It also returns a bool that is true if the tree hashes match and there are no uncommitted changes.

func (*GitSemVer) GetVersion

func (vs *GitSemVer) GetVersion(repo string) (vi VersionInfo, err error)

GetVersion returns a VersionInfo for the source code in the Git repository. A GitSemVer instance should be treated as single-snapshot state: if the repo changes, create a new GitSemVer before calling GetVersion again.

func (*GitSemVer) IsEnvTrue

func (vs *GitSemVer) IsEnvTrue(envvar string) (yes bool)

IsEnvTrue returns true if the given environment variable exists and is set to something that parses as true.

func (*GitSemVer) IsReleaseBranch

func (vs *GitSemVer) IsReleaseBranch(branchName string) bool

IsReleaseBranch returns true if the given branch name should be allowed to use 'release mode', where the version string doesn't contains build information suffix.

type GitTag

type GitTag struct {
	Tag    string
	Commit string
	Tree   string
}

type Gitter

type Gitter interface {
	Exec(args ...string) (output []byte, err error)
	// CheckGitRepo checks that the given directory is part of a git repository.
	CheckGitRepo(dir string) (repo string, err error)
	// GetTags returns all tags, sorted by version descending.
	GetTags(repo string) (tags []string, err error)
	// GetCurrentTreeHash returns the current tree hash.
	GetCurrentTreeHash(repo string) (string, error)
	// GetHashes returns the commit and tree hashes for the given tag.
	GetHashes(repo, tag string) (commit string, tree string, err error)
	// GetHashesBatch returns commit/tree hashes for many tags.
	GetHashesBatch(repo string, tags []string) (hashes []GitTag, err error)
	// GetClosestTag returns the closest semver tag for the given commit hash.
	GetClosestTag(repo, commit string) (tag string, err error)
	// GetBranch returns the current branch in the repository or an empty string.
	GetBranch(repo string) (branch string, err error)
	// GetBranchesFromTag returns the non-HEAD branches in the repository that have the tag, otherwise an empty string.
	GetBranchesFromTag(repo, tag string) (branches []string, err error)
	// GetBuild returns the number of commits in the currently checked out branch as a string, or an empty string
	GetBuild(repo string) (string, error)
	// FetchTags calls "git fetch --tags". Uses the "--unshallow" option if needed.
	FetchTags(repo string) error
	// CreateTag creates a new lightweight tag. Does nothing if tag is empty.
	CreateTag(repo, tag string) error
	// DeleteTag deletes the given tag. Does nothing if tag is empty.
	DeleteTag(repo, tag string) (err error)
	// PushTag pushes the given tag to the origin. Does nothing if tag is empty.
	PushTag(repo, tag string) (err error)
	// DeleteRemoteTag deletes the given tag from origin. Does nothing if tag is empty.
	DeleteRemoteTag(repo, tag string) (err error)
	// CleanStatus returns true if there are no uncommitted changes in the repo.
	// If includeUntracked is false, untracked files do not affect cleanliness.
	CleanStatus(repo string, includeUntracked bool) (yes bool, err error)
}

Gitter is an interface exposing the required Git functionality

func NewDefaultGitter

func NewDefaultGitter(gitBin string, debugOut io.Writer) (gitter Gitter, err error)

type OsEnvironment

type OsEnvironment struct{}

OsEnvironment calls the OS functions.

func (OsEnvironment) Getenv

func (OsEnvironment) Getenv(key string) string

func (OsEnvironment) LookupEnv

func (OsEnvironment) LookupEnv(key string) (string, bool)

type VersionInfo

type VersionInfo struct {
	Tag       string   // git tag, e.g. "v1.2.3"
	Branch    string   // git branch, e.g. "Special--Branch"
	Build     string   // git or CI build number, e.g. "456"
	SameTree  bool     // true if tree hash is identical
	IsRelease bool     // true if the branch is a release branch
	Tags      []GitTag // all tags and their tree hashes
}

func (*VersionInfo) GoPackage

func (vi *VersionInfo) GoPackage(repo, pkgName string) (retv string, err error)

GoPackage returns a small piece of Go code defining global variables named "PkgName" and "PkgVersion" with the given pkgName in all lower case and the contents of Version. If the pkgName isn't a valid Go identifier, an error is returned.

func (*VersionInfo) HasTag

func (vi *VersionInfo) HasTag(tag string) bool

func (*VersionInfo) IncPatch

func (vi *VersionInfo) IncPatch() string

IncPatch increments the patch level of the version, returning the new tag.

func (*VersionInfo) Version

func (vi *VersionInfo) Version() (version string)

Version returns the composite version, e.g. "v1.2.3-mybranch.456"

Jump to

Keyboard shortcuts

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