gitsemver

package
v1.12.1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2025 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func CleanBranch added in v1.1.0

func CleanBranch(branch string) string

func LastName

func LastName(s string) string

func MaybeSync added in v1.9.13

func MaybeSync(w io.Writer)

func NewErrGitExec added in v1.9.0

func NewErrGitExec(git string, args []string, err error, stderr string) 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 it's parent directories has a '.git' subdirectory. If it is, it returns the absolute path of the git repo and a nil error.

func (DefaultGitter) CleanStatus added in v1.3.21

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

func (DefaultGitter) CreateTag added in v1.1.1

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

func (DefaultGitter) DeleteTag added in v1.1.1

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

func (DefaultGitter) Exec added in v1.9.0

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 added in v1.4.33

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) 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 added in v1.1.1

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
}

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 added in v1.3.21

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.

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 added in v1.4.33

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)
	// 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"
	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)
	// CleanStatus returns true if there are no uncommitted changes in the repo
	CleanStatus(repo string) (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 added in v1.4.33

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

func (*VersionInfo) IncPatch added in v1.0.3

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