Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/markdown-fail-fast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ jobs:
echo "Running in pull_request context"
echo "Base SHA: $BASE_SHA"
echo "Head SHA: $HEAD_SHA"
CHANGED=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" | grep '\.md$' || true)
CHANGED=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" | grep -v '^NOTICES' | grep '\.md$' || true)
elif [[ "$EVENT_NAME" == "push" ]]; then
echo "Running in push context"
CHANGED=$(git diff --name-only HEAD~1 HEAD | grep '\.md$' || true)
CHANGED=$(git diff --name-only HEAD~1 HEAD | grep -v '^NOTICES' | grep '\.md$' || true)
else
echo "Unsupported event type: $EVENT_NAME"
exit 1
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ MARKDOWNIMAGE := $(shell awk '$$4=="markdown" {print $$2}' $(DEPENDENCIES_DOCKER
.PHONY: lint-markdown
lint-markdown:
@echo "### Linting markdown"
@docker run --rm -v "$(CURDIR):/workdir" $(MARKDOWNIMAGE) "**/*.md"
@docker run --rm -v "$(CURDIR):/workdir" $(MARKDOWNIMAGE) "{*.md,!(NOTICES)/**/*.md}"

.PHONY: lint-markdown-fix
lint-markdown-fix:
@echo "### Formatting markdown"
@docker run --rm -v "$(CURDIR):/workdir" $(MARKDOWNIMAGE) --fix "**/*.md"
@docker run --rm -v "$(CURDIR):/workdir" $(MARKDOWNIMAGE) --fix "{*.md,!(NOTICES)/**/*.md}"

.PHONY: update-offsets
update-offsets: $(GO_OFFSETS_TRACKER)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: go-tests

on: [push]
on:
pull_request:
branches: [ main ]

env:
TEST_RESULTS: /tmp/test-results
Expand All @@ -11,16 +13,16 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ 1.15.3, 1.19 ]
go-version: ['stable', 'oldstable']

steps:
- name: Setup go
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Create test directory
run: |
Expand All @@ -30,7 +32,7 @@ jobs:
run: go mod download

- name: Cache / restore go modules
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
with:
path: |
~/go/pkg/mod
Expand All @@ -50,7 +52,7 @@ jobs:
fi

- name: Run golangci-lint
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9
uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20

# Install gotestsum with go get for 1.15.3; otherwise default to go install
- name: Install gotestsum
Expand All @@ -71,13 +73,13 @@ jobs:

# Save coverage report parts
- name: Upload and save artifacts
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f
with:
name: Test Results-${{matrix.go-version}}
path: ${{ env.TEST_RESULTS }}

- name: Upload coverage report
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f
with:
path: coverage.out
name: Coverage-report-${{matrix.go-version}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
# 1.9.0 (Mar 30, 2026)

ENHANCEMENTS:

Support parsing versions with custom prefixes via opt-in option in https://github.com/hashicorp/go-version/pull/79

INTERNAL:

- Bump the github-actions-backward-compatible group across 1 directory with 2 updates in https://github.com/hashicorp/go-version/pull/179
- Bump the github-actions-breaking group with 4 updates in https://github.com/hashicorp/go-version/pull/180
- Bump the github-actions-backward-compatible group with 3 updates in https://github.com/hashicorp/go-version/pull/182
- Update GitHub Actions to trigger on pull requests and update go version in https://github.com/hashicorp/go-version/pull/185
- Bump actions/upload-artifact from 6.0.0 to 7.0.0 in the github-actions-breaking group across 1 directory in https://github.com/hashicorp/go-version/pull/183
- Bump the github-actions-backward-compatible group across 1 directory with 2 updates in https://github.com/hashicorp/go-version/pull/186

# 1.8.0 (Nov 28, 2025)

ENHANCEMENTS:

- Add benchmark test for version.String() in https://github.com/hashicorp/go-version/pull/159
- Bytes implementation in https://github.com/hashicorp/go-version/pull/161

INTERNAL:

- Add CODEOWNERS file in .github/CODEOWNERS in https://github.com/hashicorp/go-version/pull/145
- Linting in https://github.com/hashicorp/go-version/pull/151
- Correct typos in comments in https://github.com/hashicorp/go-version/pull/134
- Migrate GitHub Actions updates from TSCCR to Dependabot in https://github.com/hashicorp/go-version/pull/155
- Bump the github-actions-backward-compatible group with 2 updates in https://github.com/hashicorp/go-version/pull/157
- Update doc reference in README in https://github.com/hashicorp/go-version/pull/135
- Bump the github-actions-breaking group with 3 updates in https://github.com/hashicorp/go-version/pull/156
- [Compliance] - PR Template Changes Required in https://github.com/hashicorp/go-version/pull/158
- Bump actions/cache from 4.2.3 to 4.2.4 in the github-actions-backward-compatible group in https://github.com/hashicorp/go-version/pull/167
- Bump actions/checkout from 4.2.2 to 5.0.0 in the github-actions-breaking group in https://github.com/hashicorp/go-version/pull/166
- Bump the github-actions-breaking group across 1 directory with 2 updates in https://github.com/hashicorp/go-version/pull/171
- [IND-4226] [COMPLIANCE] Update Copyright Headers in https://github.com/hashicorp/go-version/pull/172
- drop init() in https://github.com/hashicorp/go-version/pull/175

# 1.7.0 (May 24, 2024)

ENHANCEMENTS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ if v1.LessThan(v2) {
}
```

#### Version Parsing and Comparison with Prefixes

The library also supports parsing versions with a custom prefix.
Using the `WithPrefix` option, you can specify a prefix to strip
before parsing the version.

Use `WithPrefix` when your input strings carry a known release prefix such as
`deployment-`, `controller-`, etc.

After parsing, the prefix is not part of the canonical version value. This
means the regular comparison methods such as `Compare`, `LessThan`, `Equal`,
and `GreaterThan` compare only the stripped version. If you compare versions
from different prefixes with these methods, the prefixes are ignored. If you
need to reject cross-prefix comparisons, inspect the parsed prefixes before
comparing the versions.

```go
v1, _ := version.NewVersion("deployment-v1.2.3-beta+metadata", version.WithPrefix("deployment-"))
v2, _ := version.NewVersion("deployment-v1.2.4", version.WithPrefix("deployment-"))

if v1.LessThan(v2) {
fmt.Printf("%s (%s) is less than %s (%s)\n", v1, v1.Original(), v2, v2.Original())
// Outputs: 1.2.3-beta+metadata (deployment-v1.2.3-beta+metadata) is less than 1.2.4 (deployment-v1.2.4)
}
```

#### Version Constraints

```go
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,60 @@ const (
`?`
)

// Optional options for NewVersion function.
type options struct {
// If set, this prefix will be trimmed from the version string before parsing.
prefix string
}

// Option is a functional option for NewVersion.
type Option func(*options)

// WithPrefix is a functional option that sets a prefix to be removed from the
// version string before parsing.
func WithPrefix(prefix string) Option {
return func(o *options) {
o.prefix = prefix
}
}

// Version represents a single version.
type Version struct {
metadata string
pre string
segments []int64
si int
original string
prefix string
}

// NewVersion parses the given version and returns a new
// Version.
func NewVersion(v string) (*Version, error) {
return newVersion(v, getVersionRegexp())
// NewVersion parses the given version and returns a new Version.
//
// Optional parsing behavior can be enabled with Option values such as
// WithPrefix, which validates and strips an expected prefix before parsing.
func NewVersion(v string, opts ...Option) (*Version, error) {
options := &options{}
for _, opt := range opts {
if opt != nil {
opt(options)
}
}

vToParse := v
if options.prefix != "" {
if !strings.HasPrefix(v, options.prefix) {
return nil, fmt.Errorf("version %q does not have prefix %q", v, options.prefix)
}
vToParse = strings.TrimPrefix(v, options.prefix)
}

ver, err := newVersion(vToParse, getVersionRegexp())
if err != nil {
return nil, err
}
ver.prefix = options.prefix
ver.original = v
return ver, nil
}

// NewSemver parses the given version and returns a new
Expand Down Expand Up @@ -424,6 +465,11 @@ func (v *Version) Original() string {
return v.original
}

// Prefix returns the explicit prefix used with WithPrefix, if any.
func (v *Version) Prefix() string {
return v.prefix
}

// UnmarshalText implements encoding.TextUnmarshaler interface.
func (v *Version) UnmarshalText(b []byte) error {
temp, err := NewVersion(string(b))
Expand Down
Loading
Loading