Documentation
¶
Index ¶
- Variables
- func CleanBranch(branch string) string
- func LastName(s string) string
- func MaybeSync(w io.Writer)
- func NewErrGitExec(git string, args []string, err error, stderr string) error
- func NewErrUnexpectedRevParseOutput(expectedTags, gotLines int) error
- type DefaultGitter
- func (dg DefaultGitter) CheckGitRepo(dir string) (repo string, err error)
- func (dg DefaultGitter) CleanStatus(repo string, includeUntracked bool) (yes bool, err error)
- func (dg DefaultGitter) CreateTag(repo, tag string) (err error)
- func (dg DefaultGitter) DeleteRemoteTag(repo, tag string) (err error)
- func (dg DefaultGitter) DeleteTag(repo, tag string) (err error)
- func (dg DefaultGitter) Exec(args ...string) (output []byte, err error)
- func (dg DefaultGitter) FetchTags(repo string) (err error)
- func (dg DefaultGitter) GetBranch(repo string) (branch string, err error)
- func (dg DefaultGitter) GetBranchesFromTag(repo, tag string) (branches []string, err error)
- func (dg DefaultGitter) GetBuild(repo string) (buildnum string, err error)
- func (dg DefaultGitter) GetClosestTag(repo, commit string) (tag string, err error)
- func (dg DefaultGitter) GetCurrentTreeHash(repo string) (hash string, err error)
- func (dg DefaultGitter) GetHashes(repo, tag string) (commit, tree string, err error)
- func (dg DefaultGitter) GetHashesBatch(repo string, tags []string) (hashes []GitTag, err error)
- func (dg DefaultGitter) GetTags(repo string) (tags []string, err error)
- func (dg DefaultGitter) PushTag(repo, tag string) (err error)
- type Environment
- type GitSemVer
- func (vs *GitSemVer) Debug(f string, args ...any)
- func (vs *GitSemVer) GetBranch(repo string) (branchName string, err error)
- func (vs *GitSemVer) GetBuild(repo string) (build string, err error)
- func (vs *GitSemVer) GetTag(repo string) (tag string, match bool, err error)
- func (vs *GitSemVer) GetVersion(repo string) (vi VersionInfo, err error)
- func (vs *GitSemVer) IsEnvTrue(envvar string) (yes bool)
- func (vs *GitSemVer) IsReleaseBranch(branchName string) bool
- type GitTag
- type Gitter
- type OsEnvironment
- type VersionInfo
Constants ¶
This section is empty.
Variables ¶
var ErrGitExec = &errGitExec{}
var ErrNotDirectory = errors.New("not a directory")
var ErrUnexpectedRevParseOutput = &errUnexpectedRevParseOutput{}
ErrUnexpectedRevParseOutput classifies errors where rev-parse output line count does not match the requested tag/tree pairs.
Functions ¶
func CleanBranch ¶
Types ¶
type DefaultGitter ¶
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) 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 ¶
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 ¶
New returns a GitSemVer ready to examine the git repositories using the given Git binary.
func (*GitSemVer) GetBranch ¶
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 ¶
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 ¶
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 ¶
IsEnvTrue returns true if the given environment variable exists and is set to something that parses as true.
func (*GitSemVer) IsReleaseBranch ¶
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 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
type OsEnvironment ¶
type OsEnvironment struct{}
OsEnvironment calls the OS functions.
func (OsEnvironment) Getenv ¶
func (OsEnvironment) Getenv(key string) string
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"