Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Github-style vX.Y.Z semver #23

Merged
merged 1 commit into from
Sep 4, 2023
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ Use `--newline` flag if you prefer to see the newline.
release next --type hotfix
0.8.1

### Github-style semver `vx.y.z`

The tool also supports [Github-style semver](https://semver.org/#is-v123-a-semantic-version):

release show -v v0.1.0
release to -v v0.2.0

## Development

### Run
Expand Down
15 changes: 13 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,24 @@ const (
// Version transforms 0.1.0 to [0.1.0].
// Returns error when given input is not following SemVar.
func Version(in string) (string, error) {
githubStyleSemver := false

if len(in) > 0 && in[0] == 'v' {
in = in[1:]
githubStyleSemver = true
}

v, err := semver.Make(in)

if err != nil {
return "", errors.New("given version is not compatible with Semantic Versioning")
}

return fmt.Sprintf("[%s]", v.String()), nil
if githubStyleSemver == true {
return fmt.Sprintf("[v%s]", v.String()), nil
} else {
return fmt.Sprintf("[%s]", v.String()), nil
}
}

// To returns document replaced with given version.
Expand Down Expand Up @@ -135,7 +146,7 @@ func Show(doc string, ver string) ([]string, error) {
// This operation simply matches to the first h2 header.
func Latest(doc string) (string, error) {

re := regexp.MustCompile(`## \[(\d*\.\d*\.\d*)\]`)
re := regexp.MustCompile(`## \[([v]?\d*\.\d*\.\d*)\]`)

for _, line := range strings.Split(doc, "\n") {
got := re.FindStringSubmatch(line)
Expand Down
1 change: 1 addition & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestOpenRepository(t *testing.T) {
"0.1.0",
"0.1.0-beta",
"100.200.300",
"v1.2.3",
}

for _, p := range pats {
Expand Down
6 changes: 6 additions & 0 deletions parser/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ func (c SemanticVersion) Increment(in VersionType) SemanticVersion {
func CastVersion(name, val string) (int, error) {
const failcode = -1

if name == "major" {
if val[0] == 'v' {
val = val[1:]
}
}

i, err := strconv.Atoi(val)

if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion parser/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var (
"1.2.3a",
"1 2 3",
"v100",
"v1.2.3",
"v1.20",
"1.20.a",
"🍎",
Expand Down Expand Up @@ -50,6 +49,10 @@ func TestNewSemanticVersion(t *testing.T) {
expected: parser.SemanticVersion{Major: 123, Minor: 456, Patch: 789},
sample: "123.456.789",
},
{
expected: parser.SemanticVersion{Major: 1, Minor: 2, Patch: 3},
sample: "v1.2.3",
},
}

for _, p := range pats {
Expand Down
Loading