versioning

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: UPL-1.0 Imports: 8 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-versioning

Reusable application version metadata and immutable Git tag allocation, using only the Go standard library.

go get github.com/marcuoli/go-versioning@v0.1.1
go install github.com/marcuoli/go-versioning/cmd/go-versioning@v0.1.1

Requires Go 1.27.3 or later. Repository operations also require Git.

Public package boundaries

Import Responsibility
github.com/marcuoli/go-versioning Application versions and runtime build metadata
github.com/marcuoli/go-versioning/gitversion Explicit Git inspection and tag allocation

The CLI is separate under cmd/go-versioning. See architecture for ownership and contributing for the development workflow.

Application versions

Identity Example
Release 1.2.3+20260102.42
Canonical application tag v1.2.3+20260102.42
Source build 1.2.3-snapshot+20260102.42
Unknown 0.0.0+unknown

The core is inherited until an explicit major, minor or patch bump. The date is the target commit's committer date in America/Sao_Paulo; the positive suffix is the merged pull-request number. Build metadata does not affect SemVer precedence. Order builds by Git ancestry and pull-request identity.

This library has its own conventional module releases, such as v0.1.0. Application tags with date/PR metadata are a separate versioning contract.

Embed application build metadata

package main

import (
    "fmt"
    "log"

    versioning "github.com/marcuoli/go-versioning"
)

var version, commit, buildTime string

func main() {
    info, err := versioning.Current(versioning.Info{
        Version: version, Commit: commit, BuildTime: buildTime,
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(info.Version)
}

A release build supplies the full application version, full commit SHA and an RFC3339 timestamp using -ldflags "-X main.version=... -X main.commit=... -X main.buildTime=...". Complete injected metadata wins. Partial metadata can use the main executable's embedded VCS information when the commit identities agree. Missing versions remain explicitly unknown. No timestamp is fabricated.

The runtime package never executes Git or discovers a repository from the current directory. Source tools can opt in with gitversion.NewContext(ctx, repositoryDirectory) and Repository.Inspect(ctx); that explicit repository produces a snapshot.

CLI

go-versioning inspect --repo . --json
go-versioning next --repo . --branch main --commit FULL_COMMIT_SHA --pr 42 --json
go-versioning tag --repo . --branch main --commit FULL_COMMIT_SHA --pr 42
go-versioning tag --repo . --branch main --commit FULL_COMMIT_SHA --pr 42 --push --remote origin

next is a read-only local plan. tag creates a local lightweight tag; --push --remote NAME explicitly enables publication to an existing named Git remote. The tool never receives a token or credential-bearing URL. Use --bump major|minor|patch to advance the core. For a repository with no application version history, supply --initial-core 1.0.0 without a bump. Use each subcommand's --help for flags.

Allocation requires complete history without shallow boundaries or active grafts and an explicit target branch, full commit SHA and positive pull-request number. Canonical tags on first-parent history take precedence; legacy build/ tags are read-only fallback. Multiple/conflicting application tags fail. The tool never moves or deletes an existing tag. Successful reruns require the exact same tag and commit.

Push mode requires one identical fetch/push destination for the named remote. It verifies the remote application tags and selected predecessor before creating a tag. Missing or stale local tags require an ordinary fetch outside the tool followed by a new plan. A failed push preserves the local tag and reports its state, allowing an explicit retry.

JSON success output goes to stdout. Errors go to stderr; --json makes them JSON objects with an error field and, where applicable, partial operation state. Exit codes: 0 success, 1 operation failure, 2 invalid usage. Git subprocesses have finite deadlines and honor cancellation.

Integration and development

See the consumer example, workflow examples and Gitea-to-GitHub copying. Application presentation, localization, packaging and release notes remain with the consuming application.

bash scripts/validate.sh
bash scripts/validate.sh --race

On Windows use ./scripts/validate.ps1 and optionally -Race with an existing C compiler. Development scripts validate the effective Go 1.27.3 toolchain. The default VS Code build task creates a local snapshot and does not publish.

Licensed under UPL 1.0.

Documentation

Overview

Package versioning implements immutable application versions and build metadata.

Application versions have the form MAJOR.MINOR.PATCH+YYYYMMDD.PR. Source snapshots insert -snapshot before the plus sign. These application identities are distinct from the ordinary SemVer tags used to release this Go module. Next selects the calendar date of the final commit in America/Sao_Paulo.

Applications can inject Info fields through their own linker variables and pass them to Current. Runtime discovery reads only the main executable's Go build information; it never inspects a working directory or a dependency's repository. Repository inspection and tag allocation belong to gitversion.

Index

Constants

View Source
const UnknownVersion = "0.0.0+unknown"

UnknownVersion identifies a build with no known application version.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bump

type Bump string

Bump selects the numeric component to advance before allocating a version.

const (
	// BumpInherit keeps the previous core. The empty Bump has the same meaning.
	BumpInherit Bump = "inherit"
	// BumpMajor increments Major and resets Minor and Patch.
	BumpMajor Bump = "major"
	// BumpMinor increments Minor and resets Patch.
	BumpMinor Bump = "minor"
	// BumpPatch increments Patch.
	BumpPatch Bump = "patch"
)

type Core

type Core struct {
	Major uint64
	Minor uint64
	Patch uint64
}

Core is the numeric SemVer core shared by successive application builds.

func ParseCore

func ParseCore(s string) (Core, error)

ParseCore parses three unsigned decimal components without leading zeroes. Components larger than uint64 are rejected.

func (Core) Bump

func (c Core) Bump(b Bump) (Core, error)

Bump advances the selected component, resets lower components, and rejects unsupported bump values or overflow. Inherit and the empty value keep c.

func (Core) String

func (c Core) String() string

String returns the core in canonical decimal form.

type Info

type Info struct {
	Version   string `json:"version"`
	Commit    string `json:"commit"`
	BuildTime string `json:"build_time"`
}

Info describes an application build. Empty Commit and BuildTime mean unknown; Commit, when present, is a full 40- or 64-digit hexadecimal identity, and BuildTime, when present, is RFC3339. Version uses UnknownVersion when unknown.

func Current

func Current(injected Info) (Info, error)

Current resolves injection against the main executable's build information. Complete valid injection bypasses runtime build-information discovery.

func ReadBuildInfo

func ReadBuildInfo() Info

ReadBuildInfo reads metadata from the main executable's embedded Go build information. It never uses dependency module versions or probes a repository. Ordinary module versions and unavailable application versions become unknown. The VCS revision and time, when present, are returned for Resolve to validate.

func Resolve

func Resolve(injected, embedded Info) (Info, error)

Resolve validates and combines injected and embedded application metadata. Complete injection wins without inspecting embedded fields. Partial injection takes precedence field by field, but conflicting full commit identities are rejected. Ordinary embedded Go module versions are treated as absent. An explicit injected UnknownVersion is preserved rather than filled in.

type Version

type Version struct {
	Core     Core
	Date     time.Time
	PR       uint64
	Snapshot bool
}

Version identifies one application build. Date is a calendar date, PR is a positive pull request number, and Snapshot distinguishes untagged source builds. Values returned by Parse and Next satisfy these constraints.

func Next

func Next(previous Core, commitTime time.Time, pr uint64, bump Bump) (Version, error)

Next allocates a release identity using commitTime's calendar date in America/Sao_Paulo. The time must be present, PR positive, and bump valid. This pure calculation does not inspect Git or create a tag.

func Parse

func Parse(s string) (Version, error)

Parse parses an application release or snapshot version without a Git tag prefix. It rejects unknown versions, invalid calendar dates, and overflow.

func ParseTag

func ParseTag(s string) (Version, bool, error)

ParseTag parses a canonical v-prefixed application release tag or a legacy build/ tag. The bool reports a legacy tag, including when its suffix is invalid. Snapshot tags and ordinary module release tags are rejected.

func (Version) String

func (v Version) String() string

String formats the value as an application version. It does not validate a manually constructed Version; Tag validates before producing a release tag.

func (Version) Tag

func (v Version) Tag() (string, error)

Tag returns the canonical immutable application release tag. Snapshots and invalid manually constructed values cannot become release tags.

Directories

Path Synopsis
cmd
go-versioning command
examples
app command
The app example reads metadata from its own binary without running Git.
The app example reads metadata from its own binary without running Git.
Package gitversion inspects explicit Git repositories and allocates immutable application tags.
Package gitversion inspects explicit Git repositories and allocates immutable application tags.

Jump to

Keyboard shortcuts

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