Skip to content

Commit

Permalink
Add --prerelease-suffix flag
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Aug 16, 2020
1 parent 93b03a3 commit a309fe2
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WORKDIR /build
COPY ./ /build/
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build .

FROM scratch
FROM alpine
COPY --from=builder /build/git-describe-semver /bin/git-describe-semver
WORKDIR /workdir
ENTRYPOINT ["/bin/git-describe-semver"]
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ none | fail | `v0.0.0-dev.23.gabc1234`

* Flag `--fallback v0.0.0`: Fallback to given tag name if no tag is available
* Flag `--drop-prefix`: Drop any present prefix (like `v`) from the output
* Flag `--prerelease-suffix`: Adds a dash-separated suffix to the prerelease part

### Binary

```bash
cd my-git-directory
wget -q https://github.com/choffmeister/git-describe-semver/releases/download/v0.2.0/git-describe-semver-linux-amd64
wget -q https://github.com/choffmeister/git-describe-semver/releases/download/v0.2.1/git-describe-semver-linux-amd64
chmod +x git-describe-semver-linux-amd64
./git-describe-semver-linux-amd64 --fallback v0.0.0
./git-describe-semver-linux-amd64
```

### Docker

```bash
cd my-git-directory
docker pull choffmeister/git-describe-semver:v0.2.0
docker run --rm -v $PWD:/workdir choffmeister/git-describe-semver:v0.2.0 --fallback v0.0.0
docker pull choffmeister/git-describe-semver:v0.2.1
docker run --rm -v $PWD:/workdir choffmeister/git-describe-semver:v0.2.1
```
67 changes: 35 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,49 @@ import (
type GenerateVersionOptions struct {
FallbackTagName string
DropTagNamePrefix bool
PrereleaseSuffix string
}

// GenerateVersion ...
func GenerateVersion(tagName string, counter int, headHash string, opts GenerateVersionOptions) (*string, error) {
devPreRelease := []string{"dev", strconv.Itoa(counter), "g" + (headHash)[0:7]}
devPrerelease := []string{"dev", strconv.Itoa(counter), "g" + (headHash)[0:7]}
if opts.PrereleaseSuffix != "" {
devPrerelease[len(devPrerelease) - 1] = devPrerelease[len(devPrerelease) - 1] + "-" + opts.PrereleaseSuffix
}
version := &SemVer{}
if tagName == "" {
version := SemVerParse(opts.FallbackTagName)
version = SemVerParse(opts.FallbackTagName)
if version == nil {
return nil, fmt.Errorf("unable to parse fallback tag")
}
version.PreRelease = devPreRelease
if opts.DropTagNamePrefix {
version.Prefix = ""
version.Prerelease = devPrerelease
} else {
version = SemVerParse(tagName)
if version == nil {
return nil, fmt.Errorf("unable to parse tag")
}
result := version.String()
return &result, nil
}
version := SemVerParse(tagName)
if version == nil {
return nil, fmt.Errorf("unable to parse tag")
}
if counter == 0 {
result := version.String()
return &result, nil
}
if len(version.PreRelease) > 0 {
version = &SemVer{
Prefix: version.Prefix,
Major: version.Major,
Minor: version.Minor,
Patch: version.Patch,
PreRelease: append(version.PreRelease, devPreRelease...),
BuildMetadata: append([]string{}, version.BuildMetadata...),
if counter == 0 {
result := version.String()
return &result, nil
}
} else {
version = &SemVer{
Prefix: version.Prefix,
Major: version.Major,
Minor: version.Minor,
Patch: version.Patch + 1,
PreRelease: devPreRelease,
BuildMetadata: append([]string{}, version.BuildMetadata...),
if len(version.Prerelease) > 0 {
version = &SemVer{
Prefix: version.Prefix,
Major: version.Major,
Minor: version.Minor,
Patch: version.Patch,
Prerelease: append(version.Prerelease, devPrerelease...),
BuildMetadata: append([]string{}, version.BuildMetadata...),
}
} else {
version = &SemVer{
Prefix: version.Prefix,
Major: version.Major,
Minor: version.Minor,
Patch: version.Patch + 1,
Prerelease: devPrerelease,
BuildMetadata: append([]string{}, version.BuildMetadata...),
}
}
}
if opts.DropTagNamePrefix {
Expand Down Expand Up @@ -85,6 +86,7 @@ func Run(dir string, opts GenerateVersionOptions) (*string, error) {
func main() {
fallback := flag.String("fallback", "", "The first version to fallback to should there be no tag")
dropPrefix := flag.Bool("drop-prefix", false, "Drop prefix from output")
prereleaseSuffix := flag.String("prerelease-suffix", "", "Suffix to add to prereleases")
flag.Parse()

dir, err := os.Getwd()
Expand All @@ -94,6 +96,7 @@ func main() {
opts := GenerateVersionOptions{
FallbackTagName: *fallback,
DropTagNamePrefix: *dropPrefix,
PrereleaseSuffix: *prereleaseSuffix,
}
result, err := Run(dir, opts)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func TestGenerateVersion(t *testing.T) {
actual, err := GenerateVersion(inputTagName, inputCounter, inputHeadHash, inputOpts)
if assert.NoError(err) {
assert.Equal(expected, *actual)

}
}

Expand All @@ -29,6 +28,10 @@ func TestGenerateVersion(t *testing.T) {
test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "0.0.0"}, "0.0.0-dev.1.gabc1234")
test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0"}, "v0.0.0-dev.1.gabc1234")
test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", DropTagNamePrefix: true}, "0.0.0-dev.1.gabc1234")

test("v0.0.0", 0, "abc1234", GenerateVersionOptions{PrereleaseSuffix: "SNAPSHOT"}, "v0.0.0")
test("v0.0.0", 1, "abc1234", GenerateVersionOptions{PrereleaseSuffix: "SNAPSHOT"}, "v0.0.1-dev.1.gabc1234-SNAPSHOT")

_, err := GenerateVersion("", 1, "abc1234", GenerateVersionOptions{})
assert.Error(err)
}
Expand Down
12 changes: 6 additions & 6 deletions semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type SemVer struct {
Major int
Minor int
Patch int
PreRelease []string
Prerelease []string
BuildMetadata []string
}

Expand All @@ -24,15 +24,15 @@ func (v SemVer) Equal(v2 SemVer) bool {
v.Major == v2.Major &&
v.Minor == v2.Minor &&
v.Patch == v2.Patch &&
equalStringSlice(v.PreRelease, v2.PreRelease) &&
equalStringSlice(v.Prerelease, v2.Prerelease) &&
equalStringSlice(v.BuildMetadata, v2.BuildMetadata)
}

// String ...
func (v SemVer) String() string {
str := fmt.Sprintf("%s%d.%d.%d", v.Prefix, v.Major, v.Minor, v.Patch)
if len(v.PreRelease) > 0 {
str = str + "-" + strings.Join(v.PreRelease, ".")
if len(v.Prerelease) > 0 {
str = str + "-" + strings.Join(v.Prerelease, ".")
}
if len(v.BuildMetadata) > 0 {
str = str + "+" + strings.Join(v.BuildMetadata, ".")
Expand All @@ -53,15 +53,15 @@ func SemVerParse(str string) *SemVer {
major, _ := strconv.Atoi(match[2])
minor, _ := strconv.Atoi(match[3])
patch, _ := strconv.Atoi(match[4])
preRelease := stringToSlice(match[5], ".")
prerelease := stringToSlice(match[5], ".")
buildMetadata := stringToSlice(match[6], ".")

return &SemVer{
Prefix: prefix,
Major: major,
Minor: minor,
Patch: patch,
PreRelease: preRelease,
Prerelease: prerelease,
BuildMetadata: buildMetadata,
}
}
Expand Down
20 changes: 10 additions & 10 deletions semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func TestSemVerString(t *testing.T) {
test(SemVer{}, "0.0.0")
test(SemVer{Prefix: "v"}, "v0.0.0")
test(SemVer{Major: 1, Minor: 2, Patch: 3}, "1.2.3")
test(SemVer{PreRelease: []string{"rc", "1"}}, "0.0.0-rc.1")
test(SemVer{PreRelease: []string{"alpha-version", "1"}}, "0.0.0-alpha-version.1")
test(SemVer{Prerelease: []string{"rc", "1"}}, "0.0.0-rc.1")
test(SemVer{Prerelease: []string{"alpha-version", "1"}}, "0.0.0-alpha-version.1")
test(SemVer{BuildMetadata: []string{"foo", "bar"}}, "0.0.0+foo.bar")
test(SemVer{Prefix: "v", Major: 1, Minor: 2, Patch: 3, PreRelease: []string{"rc", "1"}, BuildMetadata: []string{"foo", "bar"}}, "v1.2.3-rc.1+foo.bar")
test(SemVer{Prefix: "v", Major: 1, Minor: 2, Patch: 3, Prerelease: []string{"rc", "1"}, BuildMetadata: []string{"foo", "bar"}}, "v1.2.3-rc.1+foo.bar")
}

func TestSemVerParse(t *testing.T) {
Expand All @@ -32,10 +32,10 @@ func TestSemVerParse(t *testing.T) {
test("0.0.0", &SemVer{})
test("v0.0.0", &SemVer{Prefix: "v"})
test("1.2.3", &SemVer{Major: 1, Minor: 2, Patch: 3})
test("0.0.0-rc.1", &SemVer{PreRelease: []string{"rc", "1"}})
test("0.0.0-alpha-version.1", &SemVer{PreRelease: []string{"alpha-version", "1"}})
test("0.0.0-rc.1", &SemVer{Prerelease: []string{"rc", "1"}})
test("0.0.0-alpha-version.1", &SemVer{Prerelease: []string{"alpha-version", "1"}})
test("0.0.0+foo.bar", &SemVer{BuildMetadata: []string{"foo", "bar"}})
test("v1.2.3-rc.1+foo.bar", &SemVer{Prefix: "v", Major: 1, Minor: 2, Patch: 3, PreRelease: []string{"rc", "1"}, BuildMetadata: []string{"foo", "bar"}})
test("v1.2.3-rc.1+foo.bar", &SemVer{Prefix: "v", Major: 1, Minor: 2, Patch: 3, Prerelease: []string{"rc", "1"}, BuildMetadata: []string{"foo", "bar"}})
test("invalid", nil)
}

Expand All @@ -50,10 +50,10 @@ func TestSemVerEqual(t *testing.T) {
test(SemVer{Major: 1}, SemVer{Major: 2}, false)
test(SemVer{Minor: 1}, SemVer{Minor: 2}, false)
test(SemVer{Patch: 1}, SemVer{Patch: 2}, false)
test(SemVer{PreRelease: []string{"foo"}}, SemVer{PreRelease: []string{"foo"}}, true)
test(SemVer{PreRelease: []string{"foo"}}, SemVer{PreRelease: []string{"bar"}}, false)
test(SemVer{PreRelease: []string{"foo"}}, SemVer{}, false)
test(SemVer{}, SemVer{PreRelease: []string{"bar"}}, false)
test(SemVer{Prerelease: []string{"foo"}}, SemVer{Prerelease: []string{"foo"}}, true)
test(SemVer{Prerelease: []string{"foo"}}, SemVer{Prerelease: []string{"bar"}}, false)
test(SemVer{Prerelease: []string{"foo"}}, SemVer{}, false)
test(SemVer{}, SemVer{Prerelease: []string{"bar"}}, false)
test(SemVer{BuildMetadata: []string{"foo"}}, SemVer{BuildMetadata: []string{"foo"}}, true)
test(SemVer{BuildMetadata: []string{"foo"}}, SemVer{BuildMetadata: []string{"bar"}}, false)
test(SemVer{BuildMetadata: []string{"foo"}}, SemVer{}, false)
Expand Down

0 comments on commit a309fe2

Please sign in to comment.